一 、MySQL升级
1、官方升级策略
- 注意
升级过程中必须使用具有管理权限的MySQL帐户来执行SQL语句。
1.升级方法
-
逻辑升级:
- 涉及使用 mysqldump从旧的MySQL版本导出现有数据 ,安装新的MySQL版本,将转储文件加载到新的MySQL版本以及运行
mysql_upgrade
。
- 涉及使用 mysqldump从旧的MySQL版本导出现有数据 ,安装新的MySQL版本,将转储文件加载到新的MySQL版本以及运行
2.升级线路
仅支持
MySQL(GA)版本
之间升级。支持从MySQL 5.6升级到5.7。在升级到下一个版本之前,建议升级到
最新版本
。例如,在升级到MySQL 5.7之前升级到最新的MySQL 5.6版本。不支持跨版本的升级
。例如,不支持从MySQL 5.5直接升级到5.7。支持发行系列中的升级。例如,支持从MySQL 5.7.x升级到5.7.y。
也支持跨小版本
。例如,支持从MySQL 5.7.x升级到5.7.z。
3.准备工作
-
升级之前,请查看以下信息并执行建议的步骤:
在升级之前,通过创建当前数据库和日志文件的备份来保护您的数据,备份应包含
mysql系统数据库
,其中包含MySQL系统表
.查看发行说明,其中提供了有关MySQL 5.7中
新增功能
的信息,或与早期MySQL版本中的功能不同。其中一些更改可能导致不兼容。有关在MySQL 5.7中
删除的MySQL服务器功能
的说明,请参阅 MySQL 5.7中删除的功能。如果您使用其中任何一项功能,升级需要对这些功能进行更改。有关在MySQL 5.7中
添加,弃用或删除的MySQL服务器变量和选项的列表
,如果您使用这些项目中的任何一项,则升级需要进行配置更改。如果您使用复制,请查看升级相关复制设置。
如果您使用XA事务InnoDB,请XA RECOVER在升级之前运行以检查未提交的XA事务。如果返回结果,则通过发出XA COMMIT或 XA ROLLBACK声明来提交或回滚XA事务。
如果你的MySQL安装包含
本地升级后要进行很长时间数据转换
,那你需要创建一个测试环境来评估
,涉及到什么可能需要的转换和工作数据库实例来执行它们,制作包含完整副本的MySQL实例的副本mysql数据库以及所有其他没有数据的数据库。在此虚拟实例上运行升级过程以查看可能需要执行的操作,以便您可以更好地评估在原始数据库实例上执行实际数据转换时涉及的工作
。建议您在安装或升级到新的MySQL版本时重建和重新安装
MySQL语言界面
。这适用于MySQL接口,例如PHP mysql扩展,Perl DBD::mysql模块和Python MySQLdb模块。
4.逻辑升级
- 先检查准备工作,然后在继续
注意:
如果最初升级是通过安装多个RPM软件包生成的安装,请升级所有软件包,而不仅仅升级一些软件包。例如:如果您以前安装了服务器和客户端的RPM包,则不要仅升级服务器RPM包。对于某些Linux平台,从RPM包安装的MySQL包括用于支持管理MySQL服务器启动和关闭的systemd。
- 执行逻辑升级:
1)从以前的MySQL版本导出现有数据:
mysqldump -u root -p
--add-drop-table --routines --events
--all-databases --force > data-for-upgrade.sql
注意
如果您的数据库包含
函数
,请在mysqldump中使用--routines
和--events
选项 (如上所示)。--all-databases
选项包括转储中的所有数据库,包括保存系统表的mysql
数据库。
重要
如果您的表包含
生成列
,请使用MySQL 5.7.9
或更高版本
提供的mysqldump来创建备份文件。早期版本mysqldump有bug。可以使用INFORMATION_SCHEMA.COLUMNS
表来查那些表有生成列
。
2)关闭旧的MySQL服务器。例如:
mysqladmin -u root -p shutdown
3)安装MySQL 5.7,请参见安装MySQL。
4)初始化新的数据目录。例如:
mysqld --initialize --datadir=/path/to/5.7-datadir
将'root'@'localhost'
显示的临时密码
复制到屏幕或写入错误日志中以备将来使用。
5)使用新的数据目录启动MySQL 5.7服务器。例如:
mysqld_safe --user=mysql --datadir=/path/to/5.7-datadir
6)重置root密码:
shell> mysql -u root -p
Enter password: **** <- enter temporary root password
mysql> ALTER USER USER() IDENTIFIED BY 'your new password';
7)将之前创建的备份文件导入新的MySQL服务器中。例如:
mysql -u root -p --force < data-for-upgrade.sql
注意
如果您的备份文件包含
系统表
, 则不建议在启用GTID的服务器(gtid_mode=ON
)上导入备份文件。 mysqldump为使用非事务性
MyISAM存储引擎的系统表发出DML指令,并且在启用GTID时不允许使用此组合。另外注意
将带有GTID的备份文件导入到另一台启用了GTID的服务器中会导致生成
不同的事务标识符
。
8)运行mysql_upgrade。例如:
mysql_upgrade -u root -p
mysql_upgrade
检查所有数据库中的所有表
与当前版本的MySQL是否兼容。mysql_upgrade
还升级mysql系统数据库
,以便您可以利用新的权限或功能。
注意:mysql_upgrade不会升级帮助表的内容。
9)关闭并重新启动MySQL服务器以确保对系统表所做的任何更改都会生效。例如:
mysqladmin -u root -p shutdown
mysqld_safe --user=mysql --datadir=/path/to/5.7-datadir
2、升级说明
- 1、mysql5.5 升级到 5.7 流程(待定):
- 升级流程:前端业务停服,同时进行备份主库和从库数据;先升级主库(
主库权限+主库备份
),然后升级从库(从库权限+主库新备份
),重建主从同步,前端业务开服 - 由于本次升级是跨版本升级(5.5直接升级到5.7),有可能存在版本引擎底层不兼容问题,请现场同事多测试。
- 升级前需要确认导出是否有字符集问题,要进行数据转换或者DDL操作,请在升级之前做好。
- 升级过程耗时:导出时间+scp/mv时间+安装时间+导入时间+配置以及处理问题时间,由于各地环境环境不一样,请评估耗时时间。最好脚本化。
- 升级流程:前端业务停服,同时进行备份主库和从库数据;先升级主库(
-
2、升级环境
名称 升级前 升级后 备注 OS 版本 RHEL 7.3 x86_64 RHEL 7.3 x86_64 MySQL 版本 5.5.33-log 5.7.18-log Linux Generic Binaries MySQL 安装目录 /usr/local/mysql5.5 /usr/local/mysql5.7.18 MySQL Basedir /usr/local/mysql /usr/local/mysql 通过链接的方式 MySQL Datadir /r2/mysqldata /r2/mysqldata MySQL Port 3306 3306 my.cnf 路径 /etc/my.cnf /etc/my.cnf -
3、安装包下载地址
- MD5: ebc8cbdaa9c356255ef82bd989b07cfb
-
官方下载地址:
MySQL Community Server 5.7.18 Linux Generic x86-64bit -
sohu下载地址:
MySQL Community Server 5.7.18 Linux Generic x86-64bit
3、停止应用服务和 MySQL 5.5 实例并备份数据库
-
1、停止应用服务
- 前端业务停止服务,确认没有MySQL线程连接(show processlist查看)
-
2、数据库备份说明
- 2.1 、分别导出主库和从库用户权限
- 2.2 、通过
mysqldump
对主库和从库进行备份,备份时数据库需要正常运行。 - 2.3 、备份命令 :mysqldump -uroot -p123456 --add-drop-table --single-transaction --master-data=2 -E -R --flush-logs -B XXX XXXX >/r2/bak_sql/all_20171201.sql
-
3、备份数据库并关闭MySQL 5.5实例,主库和从库都需要操作步骤(3.1~3.4)
-
3.1、导出权限并备份数据库
- 注意:MySQL 5.7之后导权限分为两个语句(SHOW GRANTS和SHOW CREATE USER),脚本里设置帐号信息,执行exp_grants.sh会导出两个文件(users.sql,grants.sql)
[root@localhost-m(252) /usr/local/src]# sh exp_grants.sh 2>/dev/null
-
3.2、mysqldump备份数据库
[root@localhost-m(252) /usr/local/src]# mysqldump -uroot -pxxxxx --add-drop-table --single-transaction --master-data=2 -E -R --ignore-table=mysql.* -B XXX YYY ZZZ >/r2/bak_sql/all_20171201.sql [root@localhost-m(252) /usr/local/src]# cd /r2/bak_sql/ [root@localhost-m(252) /r2/bak_sql]# ls -lsh total 102M 102M -rw-r--r-- 1 root root 102M 12月 1 17:16 all_20171201.sql
-
3.3、关闭 MySQL 5.5 实例
[root@localhost-m(252) /r2/bak_sql]# /etc/init.d/mysqld stop Shutting down MySQL.. SUCCESS! [root@localhost-m(252) /r2/bak_sql]# ps -ef |grep mysqld root 105281 82231 0 18:19 pts/4 00:00:00 grep --color=auto mysqld
-
3.4、通过物理备份数据库(
升级失败回退时使用
)-- 在实例关闭之后,通过 mv datadir 目录的方式来备份。 [root@localhost-m(252) /r2/bak_sql]# cd /r2/ [root@localhost-m(252) /r2]# mv mysqldata mysqldatabak
4、升级MySQL版本
-
-
1、安装 MySQL 5.7
[root@localhost-m(252) /usr/local/src]# pwd /usr/local/src [root@localhost-m(252) /usr/local/src]# ll |grep mysql -rw-r--r-- 1 root root 654430368 12月 1 16:05 mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz drwxr-xr-x 14 mysql mysql 4096 7月 31 17:03 zabbix-3.2.7 [root@localhost-m(252) /usr/local/src]# mv /usr/local/mysql /usr/local/mysql5.5 --备份安装目录 /usr/local/mysql(根据实际情况) [root@localhost-m(252) /usr/local/src]# md5sum mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz --校验tar包MD5与官网MD5对比是否正确 ebc8cbdaa9c356255ef82bd989b07cfb mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz [root@localhost-m(252) /usr/local/src]# tar zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ mysql-5.7.18-linux-glibc2.5-x86_64/bin/myisam_ftdump mysql-5.7.18-linux-glibc2.5-x86_64/bin/myisamchk ......省略中...... mysql-5.7.18-linux-glibc2.5-x86_64/docs/INFO_SRC [root@localhost-m(252) /usr/local]# ll |grep mysql drwxr-xr-x. 13 root root 4096 3月 9 2016 mysql5.5 drwxr-xr-x 9 root root 4096 12月 4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64 [root@localhost-m(252) /usr/local]# ln -s mysql-5.7.18-linux-glibc2.5-x86_64 mysql [root@localhost-m(252) /usr/local]# ll |grep mysql lrwxrwxrwx 1 root root 34 12月 4 09:20 mysql -> mysql-5.7.18-linux-glibc2.5-x86_64 drwxr-xr-x. 13 root root 4096 3月 9 2016 mysql5.5 drwxr-xr-x 9 root root 4096 12月 4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64
-
2、修改相关目录的权限
[root@localhost-m(252) /usr/local]# chown -R mysql.mysql mysql [root@localhost-m(252) /usr/local]# chown -R mysql.mysql mysql-5.7.18-linux-glibc2.5-x86_64
3、检查改环境变量
- 方法一
- 在/etc/profile 中的添加"export PATH=$PATH:/usr/local/mysql/bin/",使用 source /etc/profile 刷新配置
[root@localhost-m(252) /r2/mysqldata]# which mysql /usr/local/mysql/bin/mysql
-
方法二
- 编辑/etc/profile.d/mysql.sh,添加“export PATH=$PATH:/usr/local/mysql/bin/” ,使用 source /etc/profile 刷新配置
[root@localhost-m(252) /r2/mysqldata]#cat /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin/
5、修改 my.cnf 配置文件并启动 MySQL 5.7
-
1、修改 my.cnf 配置文件
- 1.1、备份 my.cnf 配置文件
[root@localhost-m(252) /usr/local]# cp /etc/my.cnf /r2/mysqldatabak/my_5.5.33.cnf --备份配置文件
- 1.2、可以参考
template_update-my.cnf
模板修改my.cnf
配置文件
-
2、配置MySQL目录和权限
- 2.1、配置MySQL目录和权限
[root@localhost-m(252) /usr/local]# mkdir -p /r2/mysqldata [root@localhost-m(252) /usr/local]# cd /r2/mysqldata [root@localhost-m(252) /r2/mysqldata]# mkdir -p /r2/undolog [root@localhost-m(252) /r2/mysqldata]# chown -R mysql.mysql /r2/undolog [root@localhost-m(252) /r2/mysqldata]# chown -R mysql.mysql /r2/mysqldata [root@localhost-m(252) /r2/mysqldata]# ll /r2 |grep undolog drwxr-xr-x 2 mysql mysql 4096 12月 4 10:57 undolog
-
3、初始化MySQL和启动
- 3.1、初始化MySQL5.7,获取随机密码
[root@localhost-m(252) /r2/mysqldata]# /usr/local/mysql/bin/mysqld --initialize --user=mysql [root@localhost-m(252) /r2/mysqldata]# grep password /r2/mysqldata/error.log --获取随机密码 2017-12-04T11:00:12.112903+08:00 1 [Note] A temporary password is generated for root@localhost: ;YV-le1Y5UYa
- 3.2添加MySQL服务到启动项,并启动mysql5.7
[root@localhost-m(252) /r2/mysqldata]# cat > /usr/lib/systemd/system/mysql.service <EOF [Unit] Description=Mysql After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking #PIDFile=/r2/mysqldata/mysqldb.pid ExecStart=/usr/local/mysql/support-files/mysql.server start ExecReload=/usr/local/mysql/support-files/mysql.server restart ExecStop=/usr/local/mysql/support-files/mysql.server stop LimitNOFILE = 65535 PrivateTmp=false [Install] WantedBy=multi-user.target EOF [root@localhost-m(252) /r2/mysqldata]# systemctl daemon-reload [root@localhost-m(252) /r2/mysqldata]# systemctl start mysql --确认是否启动成功 [root@localhost-m(252) /r2/mysqldata]# systemctl status mysql ● mysql.service - Mysql Loaded: loaded (/usr/lib/systemd/system/mysql.service; disabled; vendor preset: disabled) Active: active (running) since 一 2017-12-04 11:32:29 CST; 6s ago Process: 65996 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS) Main PID: 66004 (mysqld_safe) CGroup: /system.slice/mysql.service ├─66004 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid └─66894 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/error.lo... 12月 04 11:32:26 localhost.localdomain systemd[1]: Starting Mysql... 12月 04 11:32:29 localhost.localdomain mysql.server[65996]: Starting MySQL... SUCCESS! 12月 04 11:32:29 localhost.localdomain systemd[1]: Started Mysql. [root@localhost-m(252) /r2/mysqldata]# ps -ef | grep mysqld | grep -v grep root 66004 1 0 11:32 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid mysql 66894 66004 1 11:32 ? 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/error.log --open-files-limit=1024000 --pid-file=/r2/mysqldata/localhost.localdomain.pid --socket=/r2/mysqldata/mysql.sock --port=3306
如果是系统是CentOS6/RHEL6,配置MySQL启动项如下:
shell> cd /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64/support-files/ shell> cp mysql.server /etc/init.d/mysqld shell> chmod +x /etc/init.d/mysqld shell> chkconfig --add mysqld shell> chkconfig --list mysqld shell> service mysqld start shell> service mysqld status
-
4、添加MySQL自启动服务
[root@localhost-m(252) /r2/mysqldata]# systemctl enable mysql Created symlink from /etc/systemd/system/multi-user.target.wants/mysql.service to /usr/lib/systemd/system/mysql.service. [root@localhost-m(252) /r2/mysqldata]# systemctl list-unit-files |grep mysql mysql.service enabled [root@localhost-m(252) /r2/mysqldata]#
6、导入备份的权限和数据
- 1、导入权限sql
--直接导入会报错,需要先修改root@localhost密码,然后再导入
[root@localhost-m(252) /r2/mysqldata]# mysql -uroot -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 784
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@ipanel 11:45: [(none)]> source /usr/local/src/grants.sql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. --提示用alter user更改密码
root@ipanel 11:47: [(none)]> alter user root@localhost identified by '123456'; --更改随机密码
Query OK, 0 rows affected (0.00 sec)
root@ipanel 11:48: [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
root@ipanel 11:48: [(none)]> source /usr/local/src/grants.sql --导入原来的账号密码,其中包括原来root账号密码
Query OK, 0 rows affected, 2 warnings (0.00 sec)
············省略输出············
Query OK, 0 rows affected, 1 warning (0.00 sec)
root@ipanel 11:48: [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
root@ipanel 11:49: [(none)]> select user,host,authentication_string from mysql.user;
+-----------+-----------+-------------------------------------------+
| user | host | authentication_string |
+-----------+-----------+-------------------------------------------+
| root | localhost | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
············省略输出············
| operator | localhost | **xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+-----------+-----------+-------------------------------------------+
9 rows in set (0.00 sec)
- 2、导入主库备份文件,运行 mysql_upgrade(mysql_upgrade检查所有数据库中的所有表与当前版本的MySQL是否兼容)
[root@localhost-m(252) /r2/mysqldata]# mysql -uroot -pxxxxx </r2/bak_sql/all_20171201.sql
[root@localhost-m(252) /r2/mysqldata]# mysql_upgrade -uroot -piforgot --force >/tmp/mysql_uprade.log
mysql_upgrade: [Warning] Using a password on the command line interface can be insecure.
[root@localhost-m(252) /r2/mysqldata]# more /tmp/mysql_uprade.log
Checking if update is needed.
Checking server version.
Running queries to upgrade MySQL server.
Checking system database.
mysql.columns_priv OK
mysql.db OK
mysql.engine_cost OK
mysql.event OK
mysql.func OK
mysql.general_log OK
mysql.gtid_executed OK
mysql.help_category OK
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.innodb_index_stats OK
mysql.innodb_table_stats OK
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.proxies_priv OK
mysql.server_cost OK
mysql.servers OK
mysql.slave_master_info OK
mysql.slave_relay_log_info OK
mysql.slave_worker_info OK
mysql.slow_log OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK
mysql.time_zone_name OK
mysql.time_zone_transition OK
mysql.time_zone_transition_type OK
mysql.user OK
The sys schema is already up to date (version 1.5.1).
Checking databases.
sys.sys_config OK
test.bool_test OK
......省略中......
Upgrade process completed successfully. --成功完成升级过程
Checking if update is needed.
- 3、确认升级是否成功
- 检查error,是否有报错
- 重启数据库验证是否有无报错
- 验证账号是否可用(业务部门确认)
[root@localhost-m(252) /r2/mysqldata]# cat /r2/mysqldata/error.log |grep error
[root@localhost-m(252) /r2/mysqldata]# systemctl stop mysql
[root@localhost-m(252) /r2/mysqldata]# systemctl status mysql
● mysql.service - Mysql
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 一 2017-12-04 14:58:57 CST; 10s ago
Process: 80083 ExecStop=/usr/local/mysql/support-files/mysql.server stop (code=exited, status=0/SUCCESS)
Main PID: 66004 (code=exited, status=0/SUCCESS)
12月 04 11:32:26 localhost.localdomain systemd[1]: Starting Mysql...
12月 04 11:32:29 localhost.localdomain mysql.server[65996]: Starting MySQL... SUCCESS!
12月 04 11:32:29 localhost.localdomain systemd[1]: Started Mysql.
12月 04 14:58:44 localhost.localdomain systemd[1]: Stopping Mysql...
12月 04 14:58:57 localhost.localdomain mysql.server[80083]: Shutting down MySQL............. SUCCESS!
12月 04 14:58:57 localhost.localdomain systemd[1]: Stopped Mysql.
[root@localhost-m(252) /r2/mysqldata]# systemctl start mysql
[root@localhost-m(252) /r2/mysqldata]# systemctl status mysql
● mysql.service - Mysql
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2017-12-04 14:59:18 CST; 1s ago
Process: 80083 ExecStop=/usr/local/mysql/support-files/mysql.server stop (code=exited, status=0/SUCCESS)
Process: 80207 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)
Main PID: 80215 (mysqld_safe)
CGroup: /system.slice/mysql.service
├─80215 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid
└─81105 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/error.lo...
12月 04 14:59:15 localhost.localdomain systemd[1]: Starting Mysql...
12月 04 14:59:18 localhost.localdomain mysql.server[80207]: Starting MySQL... SUCCESS!
12月 04 14:59:18 localhost.localdomain systemd[1]: Started Mysql.
- 4、升级完成,清除权限文件后,请跳过步骤II(回滚),直接进入步骤III(配置主从)
[root@localhost-m(252) /r2/mysqldata]# rm /usr/local/src/grants.sql
rm: remove regular file ‘/usr/local/src/grants.sql’? Y
二、升级失败回滚
1、升级回退到MySQL5.5
-
1、关闭数据库
[root@localhost-m(252) /r2/mysqldata]# systemctl stop mysql [root@localhost-m(252) /r2/mysqldata]# ps -ef |grep mysqld root 85807 64413 0 16:13 pts/0 00:00:00 grep --color=auto mysqld
-
2、恢复5.5.33数据目录
[root@localhost-m(252) /r2]# pwd /r2 [root@localhost-m(252) /r2]# mv mysqldata mysqldata57bak [root@localhost-m(252) /r2]# mv mysqldatabak mysqldata
-
2、恢复5.5.33配置文件
[root@localhost-m(252) /r2]# cp /etc/my.cnf mysqldata57bak/my.cnf [root@localhost-m(252) /r2]# cp mysqldata/my.cnf /etc/my.cnf cp: overwrite ‘/etc/my.cnf’? y
-
3、恢复mysql5.5.33 软件
- 如过之前是yum安装或者源码安装
[root@localhost-m(252) /r2]# cd /usr/local/ [root@localhost-m(252) /usr/local]# ll |grep mysql lrwxrwxrwx 1 mysql mysql 34 12月 4 09:20 mysql -> mysql-5.7.18-linux-glibc2.5-x86_64 --mysql链接5.7 drwxr-xr-x. 13 root root 4096 3月 9 2016 mysql5.5 drwxr-xr-x 9 mysql mysql 4096 12月 4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64 -- 删除5.7 mysql链接 [root@localhost-m(252) /usr/local]# rm mysql rm: remove symbolic link ‘mysql’? y [root@localhost-m(252) /usr/local]# ll |grep mysql drwxr-xr-x. 13 root root 4096 3月 9 2016 mysql5.5 drwxr-xr-x 9 mysql mysql 4096 12月 4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64 --重建5.5.33 链接 [root@localhost-m(252) /usr/local]# ln -s mysql5.5 mysql [root@localhost-m(252) /usr/local]# ll |grep mysql lrwxrwxrwx 1 root root 8 12月 4 16:19 mysql -> mysql5.5 --mysql链接到5.5 drwxr-xr-x. 13 root root 4096 3月 9 2016 mysql5.5 drwxr-xr-x 9 mysql mysql 4096 12月 4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64 --重新授权软件目录权限 [root@localhost-m(252) /usr/local]# chown -R mysql.mysql /usr/local/mysql [root@localhost-m(252) /usr/local]# chown -R mysql.mysql /usr/local/mysql5.5 [root@localhost-m(252) /usr/local]# ll |grep mysql lrwxrwxrwx 1 mysql mysql 8 12月 4 16:19 mysql -> mysql5.5 drwxr-xr-x. 13 mysql mysql 4096 3月 9 2016 mysql5.5 drwxr-xr-x 9 mysql mysql 4096 12月 4 09:18 mysql-5.7.18-linux-glibc2.5-x86_64 --重新授权数据目录权限 [root@localhost-m(252) /usr/local]# chown -R mysql.mysql /r2/mysqldata [root@localhost-m(252) /usr/local]# ll /r2/ |grep mysqldata drwxr-xr-x 64 mysql mysql 4096 12月 4 16:28 mysqldata drwxr-xr-x 53 mysql mysql 12288 12月 4 16:16 mysqldata57bak
-
4、启动mysql5.5实例
[root@localhost-m(252) /usr/local]# systemctl start mysql [root@localhost-m(252) /usr/local]# systemctl status mysql ● mysql.service - Mysql Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled) Active: active (running) since 一 2017-12-04 16:28:15 CST; 5s ago Process: 89752 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS) Main PID: 89764 (mysqld_safe) CGroup: /system.slice/mysql.service ├─89764 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid └─90233 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/localhos... 12月 04 16:28:13 localhost.localdomain systemd[1]: Starting Mysql... 12月 04 16:28:15 localhost.localdomain mysql.server[89752]: Starting MySQL.. SUCCESS! 12月 04 16:28:15 localhost.localdomain systemd[1]: Started Mysql. [root@localhost-m(252) /usr/local]# ps -ef |grep mysqld |grep -v grep root 89764 1 0 16:28 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/r2/mysqldata --pid-file=/r2/mysqldata/localhost.localdomain.pid mysql 90233 89764 0 16:28 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/r2/mysqldata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/r2/mysqldata/localhost.localdomain.err --pid-file=/r2/mysqldata/localhost.localdomain.pid --socket=/r2/mysqldata/mysql.sock --port=3306
-
5、登录验证mysql5.5实例
[root@localhost-m(252) /usr/local]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.33-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
三、配置主从复制
1、配置基于GTID的并行复制
例如:主库(192.68.48.168)和从库(192.168.24.180)
-
1、从库(192.168.24.180)
- 从库
my.cnf
添加并行复制
参数,修改server-id=2
### 开启并行复制(从库) #### slave-parallel-type=LOGICAL_CLOCK #基于组提交的并行复制方式 slave-parallel-workers=24 #并行的SQL线程数量(根据CPU核数) master-info_repository=TABLE #master信息以表的形式保存 relay_log_info_repository=TABLE #slave信息以表的形式保存 relay_log_recovery=ON #relay_log自我修复
- 从库
- 2、建立主从复制
- 2.1.从原主库复制备份文件,从库导入原主库备份文件
[root@proxy r2]# cd bak_sql/ [root@proxy bak_sql]# scp 192.168.48.168:/r2/bak_sql/all_20171201.sql ./
- 2.2.建立GTID主从复制
[root@proxy ~]# mysql -uroot -p
(root@localhost) 19:15:50 [(none)]> source /r2/bak_sql/all_20171201.sql
(root@localhost) 20:01:14 [(none)]> change master to
-> master_host='192.168.48.168',
-> master_user='repl',
-> master_password='repl',
-> master_auto_position = 1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
(root@localhost) 20:04:49 [(none)]> start slave;
Query OK, 0 rows affected (0.09 sec)
(root@localhost) 20:04:55 [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Queueing master event to the relay log
Master_Host: 192.168.48.168
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 139295376
Relay_Log_File: proxy-relay-bin.000002
Relay_Log_Pos: 81035047
Relay_Master_Log_File: binlog.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 81034840
Relay_Log_Space: 139295790
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 10279
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 3eae5854-d89f-11e7-ab20-246e960a8d84
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Waiting for dependent transaction to commit
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-2476
Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-1408,
9dc847d8-bf72-11e7-9ec4-000c2998e4f1:1-2845713
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.01 sec)
ERROR:
No query specified
- 2.3 检查多线程复制(24个)
--slave启动了24个线程(Waiting for an event from Coordinator)
(root@localhost) 15:34:15 [(none)]> show processlist;
+----+-------------+-----------+------+---------+---------+--------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+---------+--------------------------------------------------------+------------------+
| 4 | system user | | NULL | Connect | 1366166 | Waiting for master to send event | NULL |
| 5 | system user | | NULL | Connect | 1364215 | Slave has read all relay log; waiting for more updates | NULL |
| 6 | system user | | NULL | Connect | 843 | Waiting for an event from Coordinator | NULL |
| 7 | system user | | NULL | Connect | 12366 | Waiting for an event from Coordinator | NULL |
| 8 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 9 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 10 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 11 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 12 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 13 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 14 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 15 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 16 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 17 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 18 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 19 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 20 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 21 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 22 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 23 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 24 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 25 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 26 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 27 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 28 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 29 | system user | | NULL | Connect | 1366166 | Waiting for an event from Coordinator | NULL |
| 49 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+-------------+-----------+------+---------+---------+--------------------------------------------------------+------------------+
27 rows in set (0.00 sec)
- 2.4、验证主从复制(增删改查)
--主库
mysql> create database tttt;
Query OK, 1 row affected (0.00 sec)
mysql> create table tttt.aa(a int);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into tttt.aa values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tttt.aa values(2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from tttt.aa;
+------+
| a |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql> update tttt.aa set a=10 where a =1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> delete from tttt.aa where a =2;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tttt.aa;
+------+
| a |
+------+
| 10 |
+------+
1 row in set (0.00 sec)
--从库
(root@localhost) 15:39:28 [(none)]> select * from tttt.aa;
+------+
| a |
+------+
| 10 |
+------+
1 row in set (0.00 sec)
- 2.5、 drop 测试库
mysql> drop database tttt;
Query OK, 1 row affected (0.06 sec)
四、确认服务正常
1、业务开启,观察数据,确认从库是否正常接收到数据
- 主库
(root@localhost) 10:04:08 [(none)]> show master status \G;
*************************** 1. row ***************************
File: binlog.000004
Position: 38701778
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7887
1 row in set (0.00 sec)
ERROR:
No query specified
(root@localhost) 10:04:08 [(none)]> show master status \G;
*************************** 1. row ***************************
File: binlog.000004
Position: 41768570 --当数据写入binlog pos点一直在变
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7890 --GTID值也在变化
1 row in set (0.00 sec)
ERROR:
No query specified
(root@localhost) 10:04:21 [(none)]> show master status \G;
*************************** 1. row ***************************
File: binlog.000004
Position: 43813112
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7892
1 row in set (0.00 sec)
- 从库
(root@localhost) 12:10:37 [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.48.168
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 105168419
Relay_Log_File: proxy-relay-bin.000004
Relay_Log_Pos: 105168472
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 105168265
Relay_Log_Space: 105170645
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 3eae5854-d89f-11e7-ab20-246e960a8d84
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7952
Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7952,
9dc847d8-bf72-11e7-9ec4-000c2998e4f1:1-2845713
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
(root@localhost) 12:10:48 [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.48.168
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 112328518
Relay_Log_File: proxy-relay-bin.000004
Relay_Log_Pos: 111305709
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 111305502 --执行接受主库pos点
Relay_Log_Space: 112330744
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 3eae5854-d89f-11e7-ab20-246e960a8d84
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Waiting for dependent transaction to commit
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7959 --接受主库GTID值也在变化
Executed_Gtid_Set: 3eae5854-d89f-11e7-ab20-246e960a8d84:1-7958,
9dc847d8-bf72-11e7-9ec4-000c2998e4f1:1-2845713
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
(root@localhost) 12:10:50 [(none)]>
2、备份从库
- 主从正常,执行备份
- 备份命令 : mysqldump -uroot -p --single-transaction --master-data=2 -E -R --flush-logs -B xxx yyy zzz >/r2/bak_sql/all_XXXXXXX.sql
[root@proxy bak_sql]# mysqldump -uroot -p --single-transaction --master-data=2 -E -R --flush-logs -A >/r2/bak_sql/all_`date +"%Y%m%d"`.sql
Enter password:
[root@proxy bak_sql]# ll
total 269124
-rw-r--r-- 1 root root 275582147 Dec 5 12:38 all_20171205.sql
五、清理升级过程产生的数据
建议保留一周,确认无异常,再删除
清理备份文件夹/r2/mysqldatabak
清理软件文件夹/usr/local/mysql5.5
清理权限文件/usr/local/src/grants.sql
六、升级过程有可能遇见的坑
如果发生问题,例如新的mysqld服务器未启动,用
mysqld --print-defaults
来验证是否存在旧的my.cnf文件,有得话会输出配置信息。mysql5.5中mysql.user的password字段改为5.7的authentication_string字段
mysql5.7版本SQL_MODE模式是严格模式
mysqldump导出的时候如果选错字符集,将导致升级后数据库出现乱码
mysql升级最好是不备份mysql库,用重新创建帐号和授权的方式升级
mysql文件夹权限不足
-
mysql环境变量没配置好