MySQL 5.6主从(MHA)升级MySQL 5.7

Master-Slave或者MHA架构,先升级slave,再升级master。

MHA架构需要先把db3上的mha manager先停止,升级完成后再启动。

1. 备份数据库(master上执行)

执行如下命令备份数据库:

innobackupex --defaults-file=/etc/my.cnf --host=localhost --user=backupuser --password=backupuser /opt/xxx/backup/mysql

或者执行脚本备份:

/opt/xxx/mysql/scripts/mysqlback_temp.sh

2. 升级slave

1) 停止MySQL 5.6

/opt/xxx/init.d/mysql stop

2) 备份MySQL相关目录

把所有MySQL相关目录mv到其他路径或者重命名:

mv /opt/xxx/db/mysql /opt/xxxxx/db/mysql_bak`date +%Y%m%d`
mv /opt/xxx/conf/mysql /opt/xxxx/conf/mysql_bak`date +%Y%m%d`
mv /opt/xx/logs/mysql /opt/bxxxxx/logs/mysql_bak`date +%Y%m%d`
mv /opt/xeix/run/mysql /opt/bxxxix/run/mysql_bak`date +%Y%m%d`
mv /opt/xx/scripts/mysql /opt/xxx/scripts/mysql_bak`date +%Y%m%d`

3) 安装MySQL 5.7 bin包

安装完后,会自动创建上一步中所述的目录,并且MySQL 5.7会自动启动。
先把MySQL 5.7停止:

/opt/xix/init.d/mysql stop

4) 还原数据目录

mv /opt/x/db/mysql /opt/xxxx/db/mysql_new`date +%Y%m%d`
mv /opt/xx/db/mysql_bak`date +%Y%m%d` /opt/xxxx/db/mysql

5) 修改my.cnf

软链接my.cnf文件:

ln -s /opt/xx/conf/mysql/my-slave.cnf /etc/my.cnf

比较新老配置文件中innodb_buffer_pool_size这个参数,使新旧一致,旧配置文件路径为:/opt/blueix/conf/mysql_bak`date +%Y%m%d`/my-master.cnf

slave添加参数禁止slave随MySQL启动:

skip-slave-start = 1

另外,如果是mha架构,第三台还需要把sever_id改为sever_id=3

6) 执行mysql_upgrade

/opt/xxx/init.d/mysql start
mysql_upgrade -uroot -p

7) 重启MySQL 5.7

/opt/x/init.d/mysql restart

8) 删除my.cnf中的skip-slave-start参数

9) 修改/etc/crontab文件

主从架构的slave做如下更改:
修改这行:

0 1 * * * root /opt/x/mysql/scripts/mysqlbackup.sh >> /opt/xix/lxxogs/mysql/mysqlback.log 2>&1

改为:

0 1 * * * root /opt/x/mysql/scripts/mysqlbackup_slave.sh >> /opt/xxx/logs/mysql/mysqlback.log 2>&1

MHA的db02删除下面两行:

10 0 * * * root /opt/x/mysql/scripts/mysqlflush_logs.sh > /dev/null

0 1 * * * root /opt/xx/mysql/scripts/mysqlbackup.sh >> /opt/xix/lxxogs/mysql/mysqlback.log 2>&1

MHA的db03删除这行:

10 0 * * * root /opt/xix/mysql/scripts/mysqlflush_logs.sh > /dev/null

修改这行:

0 1 * * * root /opt/xx/mysql/scripts/mysqlbackup.sh >> /opt/xxxix/logs/mysql/mysqlback.log 2>&1

改为:

0 1 * * * root /opt/x/mysql/scripts/mysqlbackup_slave.sh >> /opt/xxxix/logs/mysql/mysqlback.log 2>&1

3. 升级master

大体步骤同升级slave,但有两点不同:

1) my.cnf文件

软链接my.cnf文件:

ln -s /opt/xx/conf/mysql/my-master.cnf /etc/my.cnf

不需要加skip-slave-start参数

2) 修改/etc/crontab文件

删除下面这行:

0 1 * * * root /opt/xix/mysql/scripts/mysqlbackup.sh >> /opt/xxxix/logs/mysql/mysqlback.log 2>&1

4. 恢复主从

slave上执行start slave,观察复制状态是否正常。

5. 更改timestamp类型字段默认值(master上执行)

把数据库里表结构中timestamp类型的默认值由'0000-00-00 00:00:00'改成CURRENT_TIMESTAMP

mysql -uroot -p -e "
select replace(alter_sql,',',', modify ')
from(select concat('alter table ',TABLE_SCHEMA,'.',TABLE_NAME,' modify ',
group_concat(COLUMN_NAME,' timestamp default CURRENT_TIMESTAMP'),';') alter_sql
from information_schema.COLUMNS
where COLUMN_DEFAULT = '0000-00-00 00:00:00'
and TABLE_SCHEMA not in('performance_schema','mysql','information_schema','sys')
group by TABLE_SCHEMA,TABLE_NAME) t into outfile '/tmp/alter.sql'"

mysql -uroot -p < /tmp/alter.sql

可用如下SQL查询是否还有需要修改的字段:

select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE
from information_schema.COLUMNS
where COLUMN_DEFAULT = '0000-00-00 00:00:00'
and TABLE_SCHEMA not in('performance_schema','mysql','information_schema','sys');

6. 更改某些字段的NULL属性(master上执行)

mysql -uroot -p -e "
use gudong;
alter table account_t modify column current_cycle_end timestamp NULL DEFAULT CURRENT_TIMESTAMP,modify column current_cycle_start timestamp NULL DEFAULT CURRENT_TIMESTAMP;
alter table location_share_session_t modify column end_time timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '结束时间';
alter table gudong.favorite_t modify content longtext CHARACTER SET utf8mb4;
use gudongweb;
alter table bind_pic_t modify column create_time timestamp NULL DEFAULT CURRENT_TIMESTAMP;"

注意:针对MHA是使用keepalived的,在前两台(主和候选主)上执行:

cp /opt/xxx/conf/mysql_bak`date +%Y%m%d`/keepalived_mha.cnf  /opt/xeixxx/conf/mysql/

cp /opt/xxxx/mysql/mysql56_files/scripts/keepalive_mha.sh  /opt/bxxxeix/mysql/scripts

7. 其他(master上执行)

如果MySQL中有短信模块的库,执行如下命令:

use sms_provider;
alter table sms_record_t modify column recieve_time timestamp NULL DEFAULT CURRENT_TIMESTAMP

alter table sms_record_t modify column send_time timestamp NULL DEFAULT CURRENT_TIMESTAMP

如果有通讯录同步程序的环境,还需执行如下命令:

use xxg;

update docking_org_info_config_t set struct_fileds = 'name,structId,structParentId,action,level,category,longCode,longPath,status,seq';
update docking_org_info_config_t set member_fileds = 'name,memberId,structId,action,mobile,email,position,job,business,telephone,fax,address,longCode,longPath,status,seq,role,register,serialNumber,contactEx';

注意,sql为update语句,没有通讯录同步的项目执行会报错。

你可能感兴趣的:(MySql)