实验机器:三台虚拟机
MASTER:192.168.254.253
SLAVE:192.168.254.250/247
主机master DOWN机
192.168.250.250 现象:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 192.168.254.253
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 188
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 334
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
步骤一、登陆从库192.168.254.250
mysql> show processlist \G;
*************************** 1. row ***************************
Id: 2
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 8
User: system user
Host:
db: NULL
Command: Connect
Time: 7671
State: Waiting for master to send event //IO进程
Info: NULL
*************************** 3. row ***************************
Id: 9
User: system user
Host:
db: NULL
Command: Connect
Time: 7334
State: Slave has read all relay log; waiting for the slave I/O thread to update //SQL进程
it
Info: NULL
3 rows in set (0.00 sec)
登陆从库192.168.254.247
mysql> show processlist \G;
*************************** 1. row ***************************
Id: 2
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 5
User: system user
Host:
db: NULL
Command: Connect
Time: 7697
State: Waiting for master to send event Info: NULL
*************************** 3. row ***************************
Id: 6
User: system user
Host:
db: NULL
Command: Connect
Time: 7397
State: Slave has read all relay log; waiting for the slave I/O thread to update
it
Info: NULL
步骤二:登陆从库192.168.254.250/247分别查看:
cat /application/mysql-5.5.32/data/master.info
cat /application/mysql-5.5.32/data/master.info
确保IO线程和SQL线程更新完毕,看看2个从库哪个最快(数值最大).
mysql-bin.000003
188
192.168.254.253
rep
12345qwert
3306
60
0
mysql-bin.000003
188
192.168.254.253
rep
12345qwert
3306
60
0
步骤三:选个POS最大的为主库 例如192.168.254.250 (本实验中POS点都是188)
或者利用半同步功能,直接选择做了实时同步的这个从库。
授权同步用户和主库一样。
1. 确保所有relay log全部更新完毕。
在从库上执行stop slave io_thread;show processlist;
直到看到Has read all relay log;表示从库更新都执行完毕;
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.254.253
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 188
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 334
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: Yes
mysql> show processlist \G;
*************************** 1. row ***************************
Id: 2
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 9
User: system user
Host:
db: NULL
Command: Connect
Time: 7980
State: Slave has read all relay log; waiting for the slave I/O thread to update
it
Info: NULL
2.开启二进制日志
vi /etc/my.cnf
开启:
log-bin = /application/mysql-5.5.32/data/mysql-bin
//如果存在log-slave-updates ,read-only等一定要注释掉。
service mysqld restart
3.提升从库为主库,登陆从库:
mysql -uroot -p12345qwert
stop slave;
reset master;
quit;
4.进入到数据库数据目录,删除master.info relay-log.info
cd /application/mysql-5.5.32/data
rm -rf master.info relay-log.info
5.如果主库服务器没有宕机,需要去主库拉取binlog补全提升主库的从库。
6.其它从库操作:
已检查(同步user rep均存在)
登陆从库
stop slave
change master to master_host='192.168.254.250'; //如果不同步,就指定位置点。
start slave;
show slave status \G;
-----------------------------主库宕机切换成功
7.修改程序配置文件从主数据库35的指向32.
平时访问数据库用域名,则直接可以修改hosts解析。
8.修理损坏的主库,完成后作为从库使用,或者切换。
更多恢复信息,参考mysql手册 第六章的FAQ。
-------------------------------------------
假如:我们有计划切换,如何做?
1.主库锁表
2.登陆所有的库查看同步状态,是否完成。
其他与前面一样。