MySQL主从切换

网友投稿 933 2022-10-12

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

MySQL主从切换

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 status\G;*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.17.0.2Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000012Read_Master_Log_Pos: 195Relay_Log_File: relaylog.000018Relay_Log_Pos: 409Relay_Master_Log_File: mysql-bin.000012Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 195Relay_Log_Space: 672Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 23306Master_UUID: 8876d804-9218-11e8-8eaf-0242ac110002Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set: 8876d804-9218-11e8-8eaf-0242ac110002:1-224227Executed_Gtid_Set: 8876d804-9218-11e8-8eaf-0242ac110002:1-224227,febb1cb0-922f-11e8-ba72-0242ac110003:1-2Auto_Position: 1Replicate_Rewrite_DB:Channel_Name: mysq57-3306Master_TLS_Version:Master_public_key_path:Get_master_public_key: 01 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 TOMASTER_HOST=‘oracle2.example.com’,MASTER_USER=‘repl’,MASTER_PASSWORD=‘password’,MASTER_PORT=3306,MASTER_AUTO_POSITION=1;

未开启GTID模式

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

上一篇:Linux定时执行任务Crontab
下一篇:简单聊聊Kubernetes的定向调度
相关文章

 发表评论

暂时没有评论,来抢沙发吧~