mysql主从切换
2011-07-28 17:09
测试环境:
Type           OS               Mysql
Master       rhel3.5           5.1.22-rc-log
Slave1       rhel3.5           5.1.22-rc-log
Slave2       rhel4.4           5.1.22-rc-log
切换测试过程是:1) Master down
               2) Slave1 切换成新的Master
               3) Slave2 更换Master配置为原Slave2
详细过程如下:
1、从slave(slave1)群众中选定一个slave,准备钱换成master;
2、检查slave1的复制状态:
mysql> SHOW PROCESSLIST ;
+—-+————-+———–+——+———+————+———————————————————————–+——————+
| Id | User        | Host      | db   | Command | Time       | State                                                                 | Info             |
+—-+————-+———–+——+———+————+———————————————————————–+——————+
|  1 | system user |           | NULL | Connect |      54733 | Waiting for master to send event                                      | NULL             |
|  2 | system user |           | NULL | Connect | 4294965772 |
Has read all relay log; waiting for the slave I/O thread to update it | NULL             |
|  8 | root        | localhost | NULL | Query   |          0 | NULL                                                                  | SHOW PROCESSLIST |
+—-+————-+———–+——+———+————+———————————————————————–+——————+
这里主要是检测Slave1是否已经应用完从Master读取过来的在relay log中的操作,
如果未应用完不能stop slave,否则数据肯定会有丢失
3、停止slave1的slave进程,并reset称master:
mysql> STOP SLAVE ;
Query OK, 0 rows affected (0.00 sec)
mysql> RESET MASTER;
Query OK, 0 rows affected, 8 warnings (0.02 sec)
4、将slave 群中的其他slave(slave2)的Master切换成新的master(由原slave1reset成的):
    1) 停slave进程:
      mysql> stop slave;
      Query OK, 0 rows affected (0.00 sec)
    2) 更换master:
      mysql> CHANGE MASTER TO
          ->   MASTER_HOST=’10.0.65.106′,
          ->     MASTER_USER=’repl’,
          ->     MASTER_PASSWORD=’slavepass’
          -> ;
      Query OK, 0 rows affected (0.00 sec)
    3) 开启slave:
      mysql> start slave;
      Query OK, 0 rows affected (0.00 sec)
5、检查slave的状态:
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.65.106
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 106
               Relay_Log_File: oindeve-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             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: 106
              Relay_Log_Space: 408
              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:
1 row in set (0.00 sec)
6、最后变换实际数据监测新的复制是否能成功进行:
    1) 在新的Master创建测试表:
      mysql> create table t3 as select * from test;
      Query OK, 100003 rows affected (0.11 sec)
      Records: 100003  Duplicates: 0  Warnings: 0
      mysql> show tables;
      +—————-+
      | Tables_in_test |
      +—————-+
      | t1             |
      | t2             |
      | t3             |
      | test           |
      +—————-+
      4 rows in set (0.00 sec)
      mysql> select count(*) from t3;
      +———-+
      | count(*) |
      +———-+
      |   100003 |
      +———-+
      1 row in set (0.00 sec)
    2) 在slave2上面看是否已经复制过去:
      mysql> show tables;
      +—————-+
      | Tables_in_test |
      +—————-+
      | t1             |
      | t2             |
      | t3             |
      | test           |
      +—————-+
      4 rows in set (0.00 sec)
      mysql> select count(*) from t3;
      +———-+
      | count(*) |
      +———-+
      |   100003 |
      +———-+
      1 row in set (0.00 sec)
        
7、到此确认复制正常,master切换完成。
 
在这里要注意 log-slave-update 这个选项,通过show variables; 查看,要确保它 是off,以免 把 修改 主从的时候 写到记录中去 (my.cnf)。