1. 环境检测

检查系统中事发后存在 mariadb 的 rpm 包

  1. $ rpm -qa | grep mariadb
  2. mariadb-libs-5.5.60-1.el7_5.x86_64

如果存在的话,就卸载掉:

  1. $ rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64

如果卸载不掉的话,报以下错误,执行 yum remove mysql-libs

  1. [root downloads]# rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64
  2. error: Failed dependencies:
  3. libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64
  4. libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64

2. 下载 MySQL8

下载链接:https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar

安装包下载完毕后,查看安装包md5值

  1. $ md5sum mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar


解压压缩包:

  1. $ tar -xvf mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar
  2. $ ll
  3. mysql-community-client-8.0.25-1.el7.x86_64.rpm
  4. mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
  5. mysql-community-common-8.0.25-1.el7.x86_64.rpm
  6. mysql-community-devel-8.0.25-1.el7.x86_64.rpm
  7. mysql-community-embedded-compat-8.0.25-1.el7.x86_64.rpm
  8. mysql-community-libs-8.0.25-1.el7.x86_64.rpm
  9. mysql-community-libs-compat-8.0.25-1.el7.x86_64.rpm
  10. mysql-community-server-8.0.25-1.el7.x86_64.rpm
  11. mysql-community-test-8.0.25-1.el7.x86_64.rpm

3. 安装MySQL

安装顺序请按照以下顺序进行安装:

  1. $ rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm
  2. $ rpm -ivh mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
  3. $ rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
  4. $ rpm -ivh mysql-community-libs-compat-8.0.25-1.el7.x86_64.rpm
  5. $ rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm
  6. $ rpm -ivh mysql-community-devel-8.0.25-1.el7.x86_64.rpm
  7. $ rpm -ivh mysql-community-embedded-compat-8.0.25-1.el7.x86_64.rpm
  8. $ rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm
  9. $ rpm -ivh mysql-community-test-8.0.25-1.el7.x86_64.rpm

检测一下是否安装成功:

  1. $ rpm -qa | grep mysql
  2. mysql-community-libs-compat-8.0.25-1.el7.x86_64
  3. mysql-community-server-8.0.25-1.el7.x86_64
  4. mysql-community-common-8.0.25-1.el7.x86_64
  5. mysql-community-libs-8.0.25-1.el7.x86_64
  6. mysql-community-client-8.0.25-1.el7.x86_64
  7. mysql-community-embedded-compat-8.0.25-1.el7.x86_64
  8. mysql-community-client-plugins-8.0.25-1.el7.x86_64
  9. mysql-community-devel-8.0.25-1.el7.x86_64

4. 修改配置文件

备份 mysql 的配置文件:

  1. $ cp /etc/my.cnf /etc/my.cnf.bak

创建 mysql 安装路径,并修改权限:

  1. $ mkdir /home/mysql8
  2. $ chown -R mysql:mysql /home/mysql8

修改 /etc/my.cnf 文件:

  1. # For advice on how to change settings please see
  2. # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
  3. [mysqld]
  4. #
  5. # Remove leading # and set to the amount of RAM for the most important data
  6. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  7. # innodb_buffer_pool_size = 128M
  8. #
  9. # Remove the leading "# " to disable binary logging
  10. # Binary logging captures changes between backups and is enabled by
  11. # default. It's default setting is log_bin=binlog
  12. # disable_log_bin
  13. #
  14. # Remove leading # to set options mainly useful for reporting servers.
  15. # The server defaults are faster for transactions and fast SELECTs.
  16. # Adjust sizes as needed, experiment to find the optimal values.
  17. # join_buffer_size = 128M
  18. # sort_buffer_size = 2M
  19. # read_rnd_buffer_size = 2M
  20. #
  21. # Remove leading # to revert to previous value for default_authentication_plugin,
  22. # this will increase compatibility with older clients. For background, see:
  23. # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
  24. # default-authentication-plugin=mysql_native_password
  25. port=3306
  26. basedir=/home/mysql8
  27. datadir=/home/mysql8/data
  28. socket=/home/mysql8/mysql.sock
  29. log-error=/home/mysql8/mysqld.log
  30. pid-file=/var/run/mysqld/mysqld.pid
  31. lower-case-table-names=1

5. MySQL初始化

  1. $ mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize

如果出现以下报错:

  1. [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock

执行以下操作:

  1. $ ln -s /home/mysql8/mysql.sock /var/lib/mysql/mysql.sock

6. 启动&停止&查看MySQL状态

  1. $ systemctl status mysqld.service
  2. $ systemctl start mysqld.service
  3. $ systemctl stop mysqld.service

7. 更改root密码

  1. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root1234';
  2. mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root1234';
  3. mysql> CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root1234';
  4. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
  5. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
  6. mysql> FLUSH PRIVILEGES;

8. 查看MySQL配置是否更改

  1. mysql> SHOW GLOBAL VARIABLES LIKE '%basedir%';
  2. +---------------+---------------+
  3. | Variable_name | Value |
  4. +---------------+---------------+
  5. | basedir | /home/mysql8/ |
  6. +---------------+---------------+
  7. 1 row in set (0.00 sec)

9. 参考文档