下载地址:https://downloads.mysql.com/archives/community/
步骤一、解压
# cd /opt
# tar -xf mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz
# mv mysql-8.0.22-linux-glibc2.12-x86_64 mysql8
步骤二、创建用户
# groupadd mysql
# useradd -r -g mysql mysql
步骤三、创建mysql数据目录data
# mkdir -p /data/mysql8_data
# chmod -R 755 /data/mysql8_data/
# chown mysql:mysql -R /data/mysql8_data
步骤四、编写配置文件
# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql8_data/
socket=/data/mysql8_data/mysql.sock
symbolic-links=0
[client]
socket=/data/mysql8_data/mysql.sock #指定mysql.sock,不然有可能出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)报错
[mysqld_safe]
log-error=/data/mysql8_data/mysql.log
pid-file=/data/mysql8_data/mysql.pid
步骤五、初始化mysql
# cd /opt/mysql8/bin
# ./mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql8/ --datadir=/data/mysql8_data/ --user=mysql --initialize
初始密码可以查看mysql.log
步骤六、启动mysql
# cd /opt/mysql8
# cp -a ./support-files/mysql.server /etc/init.d/mysql
# chown 777 /etc/my.cnf
# chmod +x /etc/init.d/mysqld
# mkdir /var/lib/mysql
# chown -R mysql:mysql /var/lib/mysql/
# service mysql start
/etc/init.d/mysql: line 259: cd: /usr/local/mysql: No such file or directory
Starting MySQLCouldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)[FAILED]
此时会报错,是因为启动命令路径不一样导致
解决方法:
# vim support-files/mysql.server
basedir=/opt/mysql8
# cp -a ./support-files/mysql.server /etc/init.d/mysql
# chmod +x /etc/init.d/mysql
# chkconfig --add mysql
# chkconfig --list mysql
# service mysql start
Starting MySQL..[ OK ]
步骤七、登录mysql进行密码更新
# mysql -uroot -p
> ALTER USER 'root'@'localhost' IDENTIFIED with mysql_native_password BY '你的新密码';
> flush privileges; #刷新权限
> use mysql;
> select user,host,plugin,authentication_string from user;
> CREATE user 'root'@'%'; #创建用户任意远程访问
> alter user 'root'@'%' identified with mysql_native_password by 'root'; #修改密码
> grant all privileges on *.* to "root"@"%"; #给用户授权
> flush privileges; #刷新权限