mysql主从--主库故障,从库切主库

主库故障,从库切主库的几种情况:
1、完全同步的状态,直接将完全同步的从库切成主库
2、不同步状态,a、服务器宕机,直接将从库切成主库,可能存在数据不一致;b、数据库宕机,把从库没有应用完的主库binlog,传至从库中应用后再切

但过程大概一致,略有不同。以完全同步的情况为例:环境:一主两从,在同一服务器上,多个实例(多实例的安装,有时间补充上)

主库端口:3306

从库端口:3307、3308

1)模拟主库故障(默认端口,并已添加mysqld服务至系统)

service mysqld stop

2)查看从库状态

mysql -S /home/mysql/run/mysql3307.sock -uroot -p123456
show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Reconnecting after a failed master event read
                  Master_Host: 192.168.7.221
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000044
          Read_Master_Log_Pos: 120
               Relay_Log_File: mysqld-relay-bin.000090
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000044
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
                Last_IO_Errno: 2003
                Last_IO_Error: error reconnecting to master '[email protected]:3306' - retry-time: 60  retries: 1

show processlist;
+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time   | State                                                                       | Info             |
+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
|  1 | system user |           | NULL | Connect | 262412 | Reconnecting after a failed master event read                               | NULL             |
|  2 | system user |           | NULL | Connect |  62439 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             
3)两个从库都处于同步状态,将其中一个从库切为主库,这里将端口3307从库切为主库
stop slave;

 4) 修改my.cnf 
  
vi /home/mysql/3307/my.cnf
log-bin=mysql-bin
log-bin-index=binlogs.index
注释掉
relay-log=mysqld-relay-bin
relay-log-index=mysqld-relay-bin.index
说明:如果之前开启过binlog,又关闭了,而且日志也清除了,则之后重启报错
mysqld: File './mysql-bin.000004' not found (Errcode: 2 - No such file or directory)
2015-08-13 10:39:09 32790 [ERROR] Failed to open log (file './mysql-bin.000004', errno 2)
2015-08-13 10:39:09 32790 [ERROR] Could not open log file
这里删除旧的binlogs.index即可解决:rm -f /home/mysql/3307/binlogs.index

5)重启3307从库

mysqladmin shutdown -S /home/mysql/run/mysql3307.sock -uroot -p123456
mysqld --defaults-file=/home/mysql/3307/my.cnf --user=mysql &
6)创建复制用户(最好与原主库的用户密码一致)
grant replication slave on *.* to 'slave'@'192.168.7.221' IDENTIFIED BY 'mysql';
7)如果有其他从库,处理一下端口3308从库
stop slave;
change master to master_port= 3307;
start slave;

说明:a、change master中其它信息都没有变动,只改一下端口即可

            b、由于3307binlog刚打开,所以从mysql-bin.000001开始记录,此时不用担心,其它从库change master不用添加master_log_file及master_log_pos,其它从库会自动识别,如下状态

8)查看端口3308从库状态
mysql -S /home/mysql/run/mysql3308.sock -uroot -p123456
show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.7.221
                  Master_User: slave
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 331
               Relay_Log_File: mysqld3308-relay-bin.000002
                Relay_Log_Pos: 494
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
 
 

你可能感兴趣的:(Mysql)