mysql的主备库复制_【MySQL】MySQL一主二从复制环境切换主从库

假设有一个一主二从的环境,当主库M出现故障时,需要将其中一个从库S1切换为主库,同时将S2指向新的主库S1,如果可能,需要将故障的主库M修复并重置为新的从库。

搭建一主二从复制环境可参考:MySQL搭建主从复制环境。

下面将演示一主二从复制环境主从库的切换,具体如下:

1、环境信息;

Mater:192.168.1.110

Slave1:192.168.1.111

Slave2:192.168.1.112

2、查看主备库状态;

Master库:

mysql> show processlist;

+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+

| 2 | root | localhost | NULL | Query | 0 | starting | show processlist |

| 3 | repl | 192.168.1.112:49819 | NULL | Binlog Dump | 207 | Master has sent all binlog to slave; waiting for more updates | NULL |

| 4 | repl | 192.168.1.111:53017 | NULL | Binlog Dump | 165 | Master has sent all binlog to slave; waiting for more updates | NULL |

+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+

3 rows in set (0.00 sec)

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select *from t_repl;

+------+-------+---------------------+

| id | name | cdate |

+------+-------+---------------------+

| 1 | Alen | 2018-03-04 17:56:57 |

| 2 | Repl | 2018-03-04 20:10:45 |

| 3 | USA | 2018-03-04 22:19:48 |

| 4 | China | 2018-03-04 22:19:48 |

| 5 | Japan | 2018-03-04 22:23:28 |

| 6 | UK | 2018-03-04 22:23:28 |

+------+-------+---------------------+

6 rows in set (0.00 sec)

mysql> insert into t_repl(id,name) values(7,'Jacky'),(8,'Tom');

Query OK, 2 rows affected (0.03 sec)

Records: 2 Duplicates: 0 Warnings: 0

mysql>

Slave1库:

mysql> show processlist;

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

| 1 | system user | | NULL | Connect | 299 | Waiting for master to send event | NULL |

| 2 | system user | | NULL | Connect | 173 | Slave has read all relay log; waiting for more updates | NULL |

| 4 | root | localhost | NULL | Query | 0 | starting | show processlist |

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

3 rows in set (0.00 sec)

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select *from t_repl;

+------+-------+---------------------+

| id | name | cdate |

+------+-------+---------------------+

| 1 | Alen | 2018-03-04 17:56:57 |

| 2 | Repl | 2018-03-04 20:10:45 |

| 3 | USA | 2018-03-04 22:19:48 |

| 4 | China | 2018-03-04 22:19:48 |

| 5 | Japan | 2018-03-04 22:23:28 |

| 6 | UK | 2018-03-04 22:23:28 |

| 7 | Jacky | 2018-03-05 18:55:32 |

| 8 | Tom | 2018-03-05 18:55:32 |

+------+-------+---------------------+

8 rows in set (0.00 sec)

mysql>

Slave2库:

mysql> show processlist;

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

| 1 | system user | | NULL | Connect | 356 | Waiting for master to send event | NULL |

| 2 | system user | | NULL | Connect | 291 | Slave has read all relay log; waiting for more updates | NULL |

| 4 | root | localhost | test | Query | 0 | starting | show processlist |

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

3 rows in set (0.00 sec)

mysql> select *from t_repl;

+------+-------+---------------------+

| id | name | cdate |

+------+-------+---------------------+

| 1 | Alen | 2018-03-04 17:56:57 |

| 2 | Repl | 2018-03-04 20:10:45 |

| 3 | USA | 2018-03-04 22:19:48 |

| 4 | China | 2018-03-04 22:19:48 |

| 5 | Japan | 2018-03-04 22:23:28 |

| 6 | UK | 2018-03-04 22:23:28 |

| 7 | Jacky | 2018-03-05 18:55:32 |

| 8 | Tom | 2018-03-05 18:55:32 |

+------+-------+---------------------+

8 rows in set (0.00 sec)

mysql>

3、模拟主库Master宕机;

mysql> shutdown;

Query OK, 0 rows affected (0.01 sec)

mysql> system service mysql.server status;

ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

mysql>

4、确保从库都执行了relay log的全部更新,在每个从库上执行stop slave io_thread,然后检查show processlist的输出,状态是Slave has read all relay log; waiting for more updates,表示更新都执行完毕;

mysql> stop slave io_thread;

Query OK, 0 rows affected (0.01 sec)

mysql> show processlist;

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

| 2 | system user | | NULL | Connect | 7500 | Slave has read all relay log; waiting for more updates | NULL |

| 4 | root | localhost | test | Query | 0 | starting | show processlist |

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

2 rows in set (0.00 sec)

5、在从库Slave1上,执行stop slave来停止从服务,然后执行reset master重置成主库;

mysql> show master status;

Empty set (0.00 sec)

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

mysql> reset master;

Query OK, 0 rows affected (0.00 sec)

6、检查从库Slave1的log-bin是否打开,没打开则打开;

7、删除从库Slave1上的master.info和relay-log.info,否则下次重启则按照从库启动;

8、在Slave2上,执行stop slave停止从库服务,然后执行change master to重新指向主库slave1,再执行start slave启动从库;

mysql> stop slave;

Query OK, 0 rows affected (0.01 sec)

mysql> change master to

-> master_host='192.168.1.111';

Query OK, 0 rows affected (0.02 sec)

mysql> start slave;

Query OK, 0 rows affected (0.02 sec)

mysql>

9、检查Slave2的状态,发现已经指向新的主库Slave1;

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.111

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql111-bin.000002

Read_Master_Log_Pos: 154

Relay_Log_File: mysql-relay-bin.000003

Relay_Log_Pos: 373

Relay_Master_Log_File: mysql111-bin.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: 154

Relay_Log_Space: 799

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: 111

Master_UUID: c8368e4a-1fa4-11e8-aa25-000c299f40a9

Master_Info_File: /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/data/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:

Executed_Gtid_Set:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.00 sec)

ERROR:

No query specified

10、将应用程序指向新的主库Slave1,这样Slave1的所有更新写入到Slave1的Binlog中,从而同步到新的从库Slave2中;

新的主库Slave1:

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select *from t_repl;

+------+-------+---------------------+

| id | name | cdate |

+------+-------+---------------------+

| 1 | Alen | 2018-03-04 17:56:57 |

| 2 | Repl | 2018-03-04 20:10:45 |

| 3 | USA | 2018-03-04 22:19:48 |

| 4 | China | 2018-03-04 22:19:48 |

| 5 | Japan | 2018-03-04 22:23:28 |

| 6 | UK | 2018-03-04 22:23:28 |

| 7 | Jacky | 2018-03-05 18:55:32 |

| 8 | Tom | 2018-03-05 18:55:32 |

+------+-------+---------------------+

8 rows in set (0.00 sec)

mysql> insert into t_repl(id,name) values(9,'Slave1-->Master');

Query OK, 1 row affected (0.01 sec)

mysql>

新的从库Slave2:

mysql> use test ;

Database changed

mysql> select *from t_repl;

+------+-----------------+---------------------+

| id | name | cdate |

+------+-----------------+---------------------+

| 1 | Alen | 2018-03-04 17:56:57 |

| 2 | Repl | 2018-03-04 20:10:45 |

| 3 | USA | 2018-03-04 22:19:48 |

| 4 | China | 2018-03-04 22:19:48 |

| 5 | Japan | 2018-03-04 22:23:28 |

| 6 | UK | 2018-03-04 22:23:28 |

| 7 | Jacky | 2018-03-05 18:55:32 |

| 8 | Tom | 2018-03-05 18:55:32 |

| 9 | Slave1-->Master | 2018-03-05 21:28:54 |

+------+-----------------+---------------------+

9 rows in set (0.00 sec)

11、最后,如果主库Master修复,则将其重新配置成Slave1的从库;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值