1. 环境检测
检查系统中事发后存在 mariadb 的 rpm 包
$ rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
如果存在的话,就卸载掉:
$ rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64
如果卸载不掉的话,报以下错误,执行 yum remove mysql-libs
:
[root downloads]# rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64
error: Failed dependencies:
libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64
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值
$ md5sum mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar
解压压缩包:
$ tar -xvf mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar
$ ll
mysql-community-client-8.0.25-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
mysql-community-common-8.0.25-1.el7.x86_64.rpm
mysql-community-devel-8.0.25-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.25-1.el7.x86_64.rpm
mysql-community-libs-8.0.25-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.25-1.el7.x86_64.rpm
mysql-community-server-8.0.25-1.el7.x86_64.rpm
mysql-community-test-8.0.25-1.el7.x86_64.rpm
3. 安装MySQL
安装顺序请按照以下顺序进行安装:
$ rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-libs-compat-8.0.25-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-devel-8.0.25-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-embedded-compat-8.0.25-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-test-8.0.25-1.el7.x86_64.rpm
检测一下是否安装成功:
$ rpm -qa | grep mysql
mysql-community-libs-compat-8.0.25-1.el7.x86_64
mysql-community-server-8.0.25-1.el7.x86_64
mysql-community-common-8.0.25-1.el7.x86_64
mysql-community-libs-8.0.25-1.el7.x86_64
mysql-community-client-8.0.25-1.el7.x86_64
mysql-community-embedded-compat-8.0.25-1.el7.x86_64
mysql-community-client-plugins-8.0.25-1.el7.x86_64
mysql-community-devel-8.0.25-1.el7.x86_64
4. 修改配置文件
备份 mysql 的配置文件:
$ cp /etc/my.cnf /etc/my.cnf.bak
创建 mysql 安装路径,并修改权限:
$ mkdir /home/mysql8
$ chown -R mysql:mysql /home/mysql8
修改 /etc/my.cnf 文件:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
port=3306
basedir=/home/mysql8
datadir=/home/mysql8/data
socket=/home/mysql8/mysql.sock
log-error=/home/mysql8/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
lower-case-table-names=1
5. MySQL初始化
$ mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize
如果出现以下报错:
[2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock
执行以下操作:
$ ln -s /home/mysql8/mysql.sock /var/lib/mysql/mysql.sock
6. 启动&停止&查看MySQL状态
$ systemctl status mysqld.service
$ systemctl start mysqld.service
$ systemctl stop mysqld.service
7. 更改root密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root1234';
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root1234';
mysql> CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root1234';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
8. 查看MySQL配置是否更改
mysql> SHOW GLOBAL VARIABLES LIKE '%basedir%';
+---------------+---------------+
| Variable_name | Value |
+---------------+---------------+
| basedir | /home/mysql8/ |
+---------------+---------------+
1 row in set (0.00 sec)