Linux(Centos7)升级MySQL 5.7到8.0.31

95

一、下载MySQL安装包

下载地址:https://downloads.mysql.com/archives/community/

二、备份

mkdir /home/mysqlback
mysqldump -u root -p --all-databases > /home/mysqlback/mysqldback.sql

三、卸载MySQL

注:卸载之前一定要先备份好数据库

1.检查当前系统是否安装过MySQL

rpm -qa | grep mysql
[root@VM-0-6-centos mysqlback]# rpm -qa |grep mysql
mysql-community-client-5.7.41-1.el7.x86_64
mysql-community-libs-5.7.41-1.el7.x86_64
mysql-community-server-5.7.41-1.el7.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-common-5.7.41-1.el7.x86_64

2.停止MySQL服务

systemctl stop mysqld

3.删除相关应用

rpm -e --nodeps mysql-community-client-5.7.41-1.el7.x86_64
rpm -e --nodeps mysql-community-libs-5.7.41-1.el7.x86_64
rpm -e --nodeps mysql-community-server-5.7.41-1.el7.x86_64
rpm -e --nodeps mysql80-community-release-el7-3.noarch
rpm -e --nodeps mysql-community-common-5.7.41-1.el7.x86_64

4.查看文件名中含有MySQL的文件

find / -name mysql
whereis mysql

若出现结果文件,则需根据rm -rf 路径删除相关文件

5.删除其他有关MySQL服务

#删除老版本mysql的所有文件和库
rm -rf /var/lib/mysql
 
#mysql的配置文件
rm -rf /etc/my.cnf
  
#如果不删除这个文件,则重新安装的Mysql将无法设置password
rm -rf /var/log/mysqld.log

四、解压MySQL安装包并安装

1.解压

mv mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar /usr/local/
tar -xvf mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar

解压完后:

[root@VM-0-6-centos local]# tar -xvf mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar 
mysql-community-client-8.0.31-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm
mysql-community-common-8.0.31-1.el7.x86_64.rpm
mysql-community-debuginfo-8.0.31-1.el7.x86_64.rpm
mysql-community-devel-8.0.31-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.31-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm
mysql-community-libs-8.0.31-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.31-1.el7.x86_64.rpm
mysql-community-server-8.0.31-1.el7.x86_64.rpm
mysql-community-server-debug-8.0.31-1.el7.x86_64.rpm
mysql-community-test-8.0.31-1.el7.x86_64.rpm

2.安装

MySQL各安装包之间有依赖关系,所以安装顺序必须按照下列来

rpm -ivh mysql-community-common-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.31-1.el7.x86_64.rpm 
rpm -ivh mysql-community-client-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.31-1.el7.x86_64.rpm

以上为必须要安装的包,其余可选。

五、修改配置文件(/etc/my.cnf)(必做,否则一旦启动则有些配置不能再修改了)

# 设置导入sql时文件大小限制 否则有些表导入不进去
innodb_log_file_size=1024M
innodb_strict_mode=0
# 设置大小写不敏感
lower-case-table-names=1

完整的my.cnf如下:

[mysqld]
port = 3306
##密码过期时间,为0则为永不过期
#default_password_lifetime=0
#设置导入SQL时文件大小,否则有些达标导不进去
innodb_log_file_size=1024M
innodb_strict_mode=0
## 配置表明不区分大小写
lower_case_table_names=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
​
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
​

六、初始化MySQL

mysqld --initialize --console

添加MySQL用户及用户组权限

chown -R mysql:mysql /var/lib/mysql/

启动MySQL

systemctl start mysqld

也可设置MySQL自启动

设置开机自启动:systemctl  enable mysqld
禁用开机自启动:systemctl disable mysqld

查看MySQL启动时的临时密码:

cat /var/log/mysqld.log | grep localhost
#或者
cat /var/log/mysqld.log |  grep password

启动MySQL后,使用临时密码登录MySQL数据库

mysql -u root -p

登录MySQL后,必须先修改用户密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

开启远程访问

#选择数据库
use mysql;
#查看用户和可以访问数据库的ip地址
select host,user from user;
#修改权限,允许所有ip均可访问
update user set host = '%' where user ='root';
#刷新权限
FLUSH PRIVILEGES;

七、导入备份数据

source /home/mysqlback/mysqldback.sql