解压到当前目录:
tar -xzvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
安装其它环境:
yum -y install gcc gcc-c++ openssl openssl-devel libaio libaio-devel ncurses ncurses-deve
yum -y install numactl
建立mysql的安装目录:
mkdir /usr/local/mysql
复制相关文件并打开安装目录:
cp -r mysql-5.7.23-linux-glibc2.12-x86_64/* /usr/local/mysql/
cd /usr/local/mysql
建立数据和日志目录:
mkdir data
mkdir log
制作配置文件:
vi /etc/my.cnf
如果是分两行显示,第二行要注释掉
配置文件需要的如下,如果配置文件存在key,只修改value就好了:
user=root
port=3306
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/log/mysql-err.log
default_authentication_plugin=mysql_native_password
socket=/tmp/mysql.sock
创建mysql用户:
useradd mysql
创建mysql目录:
mkdir /var/lib/mysql
mysql目录改为mysql读写权限:
chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /usr/local/mysql
初始化数据库目录:
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
注意,此时 root 账号会生成临时密码,要记下来,如下:
[Note] A temporary password is generated for root@localhost: .t*l<f7Oq4AI
添加环境目录:
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
复制 mysql.server 并输入命令编辑 mysqld 文件:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
vi /etc/init.d/mysqld
编辑 mysqld 文件,找到这两个配置项,改成当前配置:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
现在就可以对mysql命令进行启动、关闭、重启、查看状态:
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart
/etc/init.d/mysqld status
登录MySQL,输入密码,即刚才说的临时密码:
mysql -u root -p
修改root密码:
alter user 'root'@'localhost' identified by '123456';
修改host:
update mysql.user set host = '%' where user = 'root';
退出MySQL:
quit;
然后重启MySQL。
我这里用的是阿里云的云服务器 ECS,所以得去阿里云开放3306端口。
最后使用 Navicat 连接数据库即可。