当主服务器M因为意外情况宕机,需要将其中的一个从数据库服务器切换成主数据库服务器,同时修改其他的从数据库的配置,使其指向新的主数据库。
下面详细介绍切换主从的操作步骤。
1.首先要确保所有的从数据库都已经执行了relay log中的全部更新,在所有的从库上,执行stop slave io_thread,停止I/O线程,然后检查show processlist的输出,直到看到状态是Slave has read all relay log; waiting for the slave I/O thread to update it,表示更新都执行完毕.
从库操作:
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.06 sec)
mysql> show processlist\G
*************************** 1. row ***************************
Id: 10
User: system user
Host:
db: NULL
Command: Connect
Time: 66746
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 2. row ***************************
Id: 16
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
2 rows in set (0.00 sec)
2.在从库slave1上,执行stop slave停止从服务,然后执行reset master以重置成主数据库,并且进行授权账号,让其他从库有权限进行连接(确保设置了log-bin参数)
slave1(从库1操作):
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> reset master;
Query OK, 0 rows affected (0.06 sec)
mysql> grant replication slave on *.* to 'repluser'@'192.168.20.%' identified by 'repluser';
Query OK, 0 rows affected (0.00 sec)
3.在其他从库上,执行stop slave停止从服务,然后执行change master to master_host='slave1' 以重新设置主数据库,然后再执行start slave启动复制:
其他从库操作:
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> change master to master_host='192.168.20.11';
Query OK, 0 rows affected (0.06 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
4.查看从库2复制状态是否正常:
复制代码
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.20.11
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000003
Read_Master_Log_Pos: 120
Relay_Log_File: binlog.000008
Relay_Log_Pos: 280
Relay_Master_Log_File: binlog.000003
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: 120
Relay_Log_Space: 604
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: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
Master_Info_File: /u02/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)
查看原来的从库slave1,现在的主库的show processlist情况:
mysql> show processlist \G
*************************** 1. row ***************************
Id: 7
User: root
Host: localhost
db: NULL
Command: Sleep
Time: 1163
State:
Info: NULL
*************************** 2. row ***************************
Id: 8
User: repluser
Host: 192.168.20.12:15432
db: NULL
Command: Binlog Dump
Time: 99
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 3. row ***************************
Id: 9
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
3 rows in set (0.00 sec)
mysql>
5.删除新的主库slave1服务器上的master.info和relay-log.info文件,否则下次重启时还会按照从库启动。我们也可以设置该参数:
skip_slave_start
总结:
上面的测试步骤中S1默认都是打开log-bin选项的,这样重置成主数据库后可以将二进制日志记录下来,并传送到其他从库,这是提升为主库必须的。
下面详细介绍切换主从的操作步骤。
1.首先要确保所有的从数据库都已经执行了relay log中的全部更新,在所有的从库上,执行stop slave io_thread,停止I/O线程,然后检查show processlist的输出,直到看到状态是Slave has read all relay log; waiting for the slave I/O thread to update it,表示更新都执行完毕.
从库操作:
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.06 sec)
mysql> show processlist\G
*************************** 1. row ***************************
Id: 10
User: system user
Host:
db: NULL
Command: Connect
Time: 66746
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 2. row ***************************
Id: 16
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
2 rows in set (0.00 sec)
2.在从库slave1上,执行stop slave停止从服务,然后执行reset master以重置成主数据库,并且进行授权账号,让其他从库有权限进行连接(确保设置了log-bin参数)
slave1(从库1操作):
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> reset master;
Query OK, 0 rows affected (0.06 sec)
mysql> grant replication slave on *.* to 'repluser'@'192.168.20.%' identified by 'repluser';
Query OK, 0 rows affected (0.00 sec)
3.在其他从库上,执行stop slave停止从服务,然后执行change master to master_host='slave1' 以重新设置主数据库,然后再执行start slave启动复制:
其他从库操作:
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> change master to master_host='192.168.20.11';
Query OK, 0 rows affected (0.06 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
4.查看从库2复制状态是否正常:
复制代码
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.20.11
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000003
Read_Master_Log_Pos: 120
Relay_Log_File: binlog.000008
Relay_Log_Pos: 280
Relay_Master_Log_File: binlog.000003
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: 120
Relay_Log_Space: 604
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: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
Master_Info_File: /u02/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)
查看原来的从库slave1,现在的主库的show processlist情况:
mysql> show processlist \G
*************************** 1. row ***************************
Id: 7
User: root
Host: localhost
db: NULL
Command: Sleep
Time: 1163
State:
Info: NULL
*************************** 2. row ***************************
Id: 8
User: repluser
Host: 192.168.20.12:15432
db: NULL
Command: Binlog Dump
Time: 99
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 3. row ***************************
Id: 9
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
3 rows in set (0.00 sec)
mysql>
5.删除新的主库slave1服务器上的master.info和relay-log.info文件,否则下次重启时还会按照从库启动。我们也可以设置该参数:
skip_slave_start
总结:
上面的测试步骤中S1默认都是打开log-bin选项的,这样重置成主数据库后可以将二进制日志记录下来,并传送到其他从库,这是提升为主库必须的。