MySQL主从切换

Posted elontian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL主从切换相关的知识,希望对你有一定的参考价值。


主备切换:
1、主库停止应用,确认主库不再有数据生成
将主库改为read_only模式

mysql> set global super_read_only=on;
Query OK, 0 rows affected (0.00 sec)

mysql> set global read_only=on;
Query OK, 0 rows affected (0.00 sec)

2、查看主备库数据是否一致

主库:
mysql> show master status;
+------------------+----------+--------------+------------------+-----------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-----------------------------------------------+
| mysql-bin.000012 | 195 | | | 8876d804-9218-11e8-8eaf-0242ac110002:1-224227 |
+------------------+----------+--------------+------------------+-----------------------------------------------+
1 row in set (0.00 sec)
mysql> select @@server_uuid;
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| 8876d804-9218-11e8-8eaf-0242ac110002 |
+--------------------------------------+
1 row in set (0.00 sec)


备库
mysql> show slave statusG;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 195
Relay_Log_File: relaylog.000018
Relay_Log_Pos: 409
Relay_Master_Log_File: mysql-bin.000012
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: 195
Relay_Log_Space: 672
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: 23306
Master_UUID: 8876d804-9218-11e8-8eaf-0242ac110002
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: 8876d804-9218-11e8-8eaf-0242ac110002:1-224227
Executed_Gtid_Set: 8876d804-9218-11e8-8eaf-0242ac110002:1-224227,
febb1cb0-922f-11e8-ba72-0242ac110003:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: mysq57-3306
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
1 row in set (0.00 sec)

ERROR:
No query specified

#Retrieved_Gtid_Set ,Executed_Gtid_Set这2个值是否和主库的Executed_Gtid_Set 值相等,如果相等,则代表一致


3、备库:停止io_thread和sql_thread
mysql> stop slave;
Query OK, 0 rows affected (0.11 sec)

4、备库:记录binlog的POS
mysql> show master status;
+------------------+-----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+-----------+--------------+------------------+-------------------+
| master2-bin.001 | 162644437 | | | |
+------------------+-----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> flush logs;
mysql> show master status;
+------------------+-----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+-----------+--------------+------------------+-------------------+
| master2-bin.002 | 163266389 | | | |
+------------------+-----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

5、将备库只读模式关闭

mysql> show global variables like ‘%read_only%‘;
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| super_read_only | ON |
| transaction_read_only | OFF |
+-----------------------+-------+
4 rows in set (0.01 sec)

mysql> set global super_read_only=off;
Query OK, 0 rows affected (0.00 sec)

mysql> set global read_only=off;
Query OK, 0 rows affected (0.00 sec)

mysql> show global variables like ‘%read_only%‘;
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | OFF |
| super_read_only | OFF |
| transaction_read_only | OFF |
+-----------------------+-------+
4 rows in set (0.00 sec)

6、新备库执行change master to语句,指向新主库


CHANGE MASTER 分2种情况,一种是开启了GTID模式,一种是未开启GTID模式

GTID模式:

CHANGE MASTER TO
MASTER_HOST=‘oracle2.example.com‘,
MASTER_USER=‘repl‘,
MASTER_PASSWORD=‘password‘,
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1;


未开启GTID模式

CHANGE MASTER TO
MASTER_HOST=‘oracle2.example.com‘,
MASTER_USER=‘repl‘,
MASTER_PASSWORD=‘password‘,
MASTER_PORT=3306,
MASTER_LOG_FILE=‘master2-bin.002‘,
MASTER_LOG_POS=163266389;










































































































































以上是关于MySQL主从切换的主要内容,如果未能解决你的问题,请参考以下文章

mysql主从切换步骤

mysql主从同步宕机切换问题

mysql主从切换维护时的几点注意

keepalived vrrp 主从切换

mysqlrouter能否判断主从切换

MySQL之主从切换