MySQL主从切换
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySQL主从切换,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6272字,纯文字阅读大概需要9分钟。
内容图文
主备切换:
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 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主从切换
标签:set 只读 info main thread retrieve inf mysql table
本文系统来源:https://www.cnblogs.com/elontian/p/10019461.html
内容总结
以上是互联网集市为您收集整理的MySQL主从切换全部内容,希望文章能够帮你解决MySQL主从切换所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。