MySQL 5.6 的MASTER– SLAVE 主从实例配置及切换 (二)
6. 参考
a) MySQL的master和slave的切换
http://lyhm4.blog.163.com/blog/static/98776700201386111831351/
b) Mysql测试一:MysqlSlave群切换Master
http://www.68idc.cn/help/mysqldata/mysql/20150215220740.html
摘要
master—— MySQL-A,以下简称A slave—— MySQL-B,以下简称B 准备工作: 1. 检查B是否与A同步。 2.检查B中用户权限是否与A配置一致。 3.修改应用系统数据库读写连接地址为B。 4.B:参数read-only=0,使其可读写。 5.如果B没有配置log_slave_updates请添加该配置。 6.B:showmaster status 取当前binlog位置。 线上切换: 1.A:执行setglobal read_only=1 使其进入只读状态 2.A:执行showmaster status,检查并确认已经进入只读状态 3.轮流重启应用服务器 善后工作: 1.检查B的工作状态是否正常 2.B:执行stopslave停止与A的同步,执行resetslave解除与A的slave关系 3.A:执行changemaster与B建立slave关系 |
7. MASTER “停”
7.I 查询Master状态
执行如下命令,
mysql>show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
|Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 195 | | 3306 | 213 |33f11615-0933-11e7-942f-0e3aa1fb7108 |
+-----------+------+------+-----------+--------------------------------------+
1row in set (0.00 sec)
7.II 检查SLAVE的复制状态
在SLAVE上执行,如果有多个SLAVE,选择一个,执行结果如:
mysql>show processlist \G;
***************************1. row ***************************
Id:1
User:system user
Host:
db:NULL
Command:Connect
Time:4182
State:Slave has read all relay log; waitingfor the slave I/O thread to update it
Info:NULL
***************************2. row ***************************
Id:2
User:system user
…..................
如上,已经应用完从Master读取过来的在relaylog中的操作
7.III 设置MASTER为只读状态,(相当于暂停 服务)
在MASTER上,命令执行如下:
mysql>show variables like 'read_only';
+---------------+-------+
|Variable_name | Value |
+---------------+-------+
|read_only | OFF |
+---------------+-------+
1row in set (0.00 sec)
mysql>
mysql>
mysql>SET GLOBAL read_only=1;
QueryOK, 0 rows affected (0.00 sec)
mysql>show variables like 'read_only';
+---------------+-------+
|Variable_name | Value |
+---------------+-------+
|read_only | ON |
+---------------+-------+
1row in set (0.00 sec)
mysql>
8. 激活SLAVE
8.I 撤销 只读状态
mysql>SET GLOBAL read_only=OFF;
8.II 停止IO_THREAD 线程
mysql>STOP SLAVE IO_THREAD;
这个时候,再查询processlist的时候, 相关的connection就停了。
确保状态为:hasread all relay log
8.III从库变主库
mysql>stop slave;
QueryOK, 0 rows affected (0.01 sec)
mysql>reset master;
QueryOK, 0 rows affected (0.01 sec)
mysql>reset slave all; #命令会删除从库的replication参数,之后showslave status 的信息返回为空
QueryOK, 0 rows affected (0.65 sec)
mysql>show binary logs;
+------------------+-----------+
|Log_name | File_size |
+------------------+-----------+
|mysql-bin.000001 | 120 |
+------------------+-----------+
1row in set (0.00 sec)
mysql>
检查一下服务状态
mysql>show master status;
+------------------+----------+--------------+----------------------------------+-------------------+
|File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
|mysql-bin.000001 | 120 | |information_schema,cluster,mysql | |
+------------------+----------+--------------+----------------------------------+-------------------+
1row in set (0.00 sec)
mysql>show slave status;
Emptyset (0.00 sec)
mysql>
这个时候,两边的数据库操作,已经不能同步了。
9. 修改其他SLAVE的“群主”
本部分,没有测试,(原因是没有配置其他SLAVE) a) 停slave进程: |
10. 原来MASTER重新加入
模拟 MASTER机器及服务,修复后,重新加入群
mysql>RESET MASTER;
QueryOK, 0 rows affected (0.00 sec)
mysql>show master status;
+------------------+----------+--------------+----------------------------------+-------------------+
|File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
|mysql-bin.000001 | 120 | userdb |information_schema,cluster,mysql | |
+------------------+----------+--------------+----------------------------------+-------------------+
1row in set (0.00 sec)
mysql>show slave status;
Emptyset (0.00 sec)
mysql>RESET SLAVE;
QueryOK, 0 rows affected (0.00 sec)
mysql>show slave status;
Emptyset (0.00 sec)
mysql>CHANGE MASTER TO
->MASTER_HOST='10.0.0.195',
->MASTER_USER='root',
->MASTER_PASSWORD='mima',
->MASTER_LOG_FILE='mysql-bin.000001',
->MASTER_LOG_POS=352;
QueryOK, 0 rows affected, 2 warnings (0.03 sec)
mysql>START SLAVE;
QueryOK, 0 rows affected (0.00 sec)
mysql>show slave status;
+----------------------+-------------+
......
说明:
配置完成后,不能同步数据,需要进一步修改:
a) 两个my.cnf文件,
log-bin=mysql-bin
server-id=213
binlog-ignore-db=information_schema binlog-ignore-db=cluster binlog-ignore-db=mysql
# 指定需要同步的数据库 binlog-do-db=userdb
replicate-do-db=userdb
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60 |
b) 权限操作
在新的MASTER上的mysql中操作
GRANTFILE ON *.* TO 'root'@'10.0.0.213' IDENTIFIED BY 'mima';
GRANTREPLICATION SLAVE ON *.* TO 'root'@'10.0.0.213' IDENTIFIED BY'mima';
FLUSHPRIVILEGES;
c) RESTART mysqld 服务
service mysqld restart
d)可以观察到,在设置的Position之后的数据同步了。
新MASTER |
新SLAVE |
mysql> select * fromtest1; +------+-----------+ | id | name | +------+-----------+ | 1 | unknown | | 100 | no name | | 150 | has name | +------+-----------+ 3 rows in set (0.00 sec) |
mysql> select * fromtest1; +------+-----------+ | id | name | +------+-----------+ | 1 | unknown | | 10 | nothing | | 150 | has name | +------+-----------+ 3 rows in set (0.00 sec) |
e)二进制log 在 目录 /var/lib/mysql 下。
ls /var/lib/mysql/mysql-bin.*