1.清理环境
①卸载已安装的mysql
$ rpm -qa | grep mysql
# 结果如下
mysql-community-common-8.0.18-1.el7.x86_64
mysql-community-client-8.0.18-1.el7.x86_64
mysql-community-libs-8.0.18-1.el7.x86_64
# 使用以下命令依次删除上面的程序
$ yum remove mysql-xxx-xxx-
②删除mysql的配置文件
$ find / -name mysql|xargs rm -fr
③删除MariaDB的文件
$ rpm -qa | grep mariadb
$ rpm -e mariadb-xxx --nodeps
2.下载mysql8.0包
下载地址
3.安装mysql8.0
①创建目录
$ mkdir -p /usr/local/mysql && cd /usr/local/mysql
$ rz mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar.tar
$ tar xf mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar.tar
[root@sonar /usr/local/mysql]# ll
total 1.3G
mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar.tar
mysql-community-client-8.0.18-1.el7.x86_64.rpm
mysql-community-common-8.0.18-1.el7.x86_64.rpm
mysql-community-devel-8.0.18-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.18-1.el7.x86_64.rpm
mysql-community-libs-8.0.18-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.18-1.el7.x86_64.rpm
mysql-community-server-8.0.18-1.el7.x86_64.rpm
mysql-community-test-8.0.18-1.el7.x86_64.rpm
②安装
$ rpm -ivh mysql-community-common-8.0.18-1.el7.x86_64.rpm --nodeps --force
$ rpm -ivh mysql-community-libs-8.0.18-1.el7.x86_64.rpm --nodeps --force
$ rpm -ivh mysql-community-client-8.0.18-1.el7.x86_64.rpm --nodeps --force
$ rpm -ivh mysql-community-server-8.0.18-1.el7.x86_64.rpm --nodeps --force
$ yum install libaio -y
# 检查
[root@sonar /usr/local/mysql]# rpm -qa | grep mysql
mysql-community-common-8.0.18-1.el7.x86_64
mysql-community-libs-8.0.18-1.el7.x86_64
mysql-community-server-8.0.18-1.el7.x86_64
mysql-community-client-8.0.18-1.el7.x86_64
③初始化
$ mysqld --initialize
$ chown mysql:mysql /var/lib/mysql -R
$ systemctl start mysqld.service
$ systemctl enable mysqld
④启动报错
[root@sonar /usr/local/mysql]# systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
# 查看日志
[root@sonar /var/lib]# cat /var/log/mysqld.log|grep 'initialize'
2020-04-16T16:10:57.488085Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
# 查看配置文件
[root@sonar /var/lib]# cat /etc/my.cnf|grep "datadir"
datadir=/var/lib/mysql
# 解决
将这个mysql目录备份后重启即可,由于这个mysql目录含有证书认证,改名备份后启动不再进行认证。
$ cd /var/lib/ && mv mysql mysql.bak
$ systemctl start mysqld
$ systemctl enable mysqld
$ systemctl status mysqld
$ netstat -anlptu|grep 3306 # 监听33060和3306证明启动正常
⑤修改密码
[root@sonar ~]# cat /var/log/mysqld.log | grep password
2020-04-16T16:13:23.042762Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: MbIq4xlV6-3*
# 使用刚才的密码登录,复制粘贴即可
$ mysql -uroot -p
# 密码规则:至少大写+小写+符号+数字
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Oldboy@123';
create user 'root'@'%' identified with mysql_native_password by 'Oldboy@123';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
转载:https://www.pingface.com/archives/mysql80