MYSQL 主从切换

Posted feiyun8616的作坊 (半个程序员and dba)

tags:

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

https://blog.csdn.net/chengxuyuanyonghu/article/details/50297321

####sample 1 测试通过 (5.6 + GTID 方式同步)

 

1> 正常切换

1)从服务器检查SHOW PROCESSLIST语句的输出,直到你看到Has read all relaylogwaiting for the slave I/O thread to update it

从服务器:
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| 3 | system user | | NULL | Connect | 2316 | Waiting for master to send event | NULL |
| 4 | system user | | NULL | Connect | 2178 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 12 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

 

2)确保从服务器已经处理了日志中的所有语句。
mysql> STOP SLAVE IO_THREAD

当从服务器都执行完这些,它们可以被重新配置为一个新的设置。

3)在被提升为主服务器的从服务器上,发出 STOP SLAVE和RESET MASTER和RESET SLAVE操作。

mysql> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> RESET MASTER;
Query OK, 0 rows affected (0.01 sec)

mysql> RESET SLAVE;
Query OK, 0 rows affected (0.01 sec)


4)然后重启mysql服务。

mysqladmin -uroot -p shutdown -S /var/lib/mysql/mysql_3307.sock

mysqld_safe --defaults-file=/mysqldb/data_3307/my.cnf &

5)在备用服务器(新的主服务器)创建用户grant replication slave on *.* [email protected]‘IP of A‘ identified by ‘replpwd‘

grant allprivileges on *.* to ‘repdcs‘@‘IP‘ identified by replpwd;

6) 在主服务器上RESET MASTER。然后CHANGE MASTER TOMASTER_HOST=‘原从服务器IP‘,MASTER_USER=‘用户名‘,MASTER_PASSWORD=‘密码‘ ,master_log_file=‘master-bin.000015‘ ;

mysql --host=* --port=3307 -urep -p*

change master to
master_host=‘*‘,
master_user=‘rep‘,
master_password=‘123456‘,
master_port=3307,
master_auto_position = 1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)


start SLAVE;
查看状态 show slave status\G;

7)
新主库 :
Show master status;


mysql> Show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 151 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

do some dml

mysql> Show master status;
+------------------+----------+--------------+------------------+----------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000001 | 411 | | | a7a643b1-5a42-11e8-aa18-005056a436cc:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)


如果还不行再次重启主库,并在备库上执行CHANGE MASTER TOMASTER_HOST=‘192.168.0.110‘,MASTER_USER=‘repdcs‘,MASTER_PASSWORD=‘111111‘,master_log_file=‘mysqlbin.000002‘;

8)修改应用的连接地址到新的主库

 

切换完成。

 







































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

mysql主从切换步骤

mysql主从同步宕机切换问题

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

keepalived vrrp 主从切换

mysqlrouter能否判断主从切换

MySQL之主从切换