MySQL主从切换

1、查看从库数据库状态:
mysql> show processlist;
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
| Id | User        | Host            | db     | Command | Time  | State                                                                       | Info             |
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
|  2 | root        | 127.0.0.1:59012 | sakila | Query   |     0 | NULL                                                                        | show processlist |
|  3 | system user |                 | NULL   | Connect | 17618 | Waiting for master to send event                                            | NULL             |
|  4 | system user |                 | NULL   | Connect | 17305 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)


mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.33.143
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql.000003
          Read_Master_Log_Pos: 3296928
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 3296845
        Relay_Master_Log_File: mysql.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: test
           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: 3296928
              Relay_Log_Space: 3297001
              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: 1
1 row in set (0.00 sec)


mysql> show processlist;
+----+--------+---------------------+--------+-------------+-------+-----------------------------------------------------------------------+------------------+
| Id | User   | Host                | db     | Command     | Time  | State                                                                 | Info             |
+----+--------+---------------------+--------+-------------+-------+-----------------------------------------------------------------------+------------------+
|  1 | root   | 127.0.0.1:33623     | sakila | Query       |     0 | NULL                                                                  | show processlist |
|  2 | master | 172.16.33.144:48988 | NULL   | Binlog Dump | 17455 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL             |
+----+--------+---------------------+--------+-------------+-------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)


mysql> show master status;
+--------------+----------+--------------+------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000003 |  3296928 |              |                  |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)


2、修改配置文件:
主库:read_only = 1
从库:#read_only = 1


3、操作从库:
mysql> show processlist;
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
| Id | User        | Host            | db     | Command | Time  | State                                                                       | Info             |
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
|  2 | root        | 127.0.0.1:59012 | sakila | Query   |     0 | NULL                                                                        | show processlist |
|  3 | system user |                 | NULL   | Connect | 17770 | Waiting for master to send event                                            | NULL             |
|  4 | system user |                 | NULL   | Connect | 17457 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)


mysql> stop slave io_thread;
Query OK, 0 rows affected (0.00 sec)


mysql> show processlist;    
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
| Id | User        | Host            | db     | Command | Time  | State                                                                       | Info             |
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
|  2 | root        | 127.0.0.1:59012 | sakila | Query   |     0 | NULL                                                                        | show processlist |
|  4 | system user |                 | NULL   | Connect | 17526 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |
+----+-------------+-----------------+--------+---------+-------+-----------------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)


确认从库的状态为:Slave has read all relay log


mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.16.33.143
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql.000003
          Read_Master_Log_Pos: 3296928
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 3296845
        Relay_Master_Log_File: mysql.000003
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: test
           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: 3296928
              Relay_Log_Space: 3297001
              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: NULL
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: 1
1 row in set (0.00 sec)


4、查看主库状态:
mysql> show master status;
+--------------+----------+--------------+------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000003 |  3296928 |              |                  |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)


5、主从切换,从库变主库:
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> reset master;
Query OK, 0 rows affected (0.02 sec)


mysql> reset slave;
Query OK, 0 rows affected (0.00 sec)


mysql> show master status;
+--------------+----------+--------------+------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000001 |      107 |              |                  |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)


6、主从切换,主库变从库:
mysql> reset master;
Query OK, 0 rows affected (0.02 sec)


mysql> reset slave;
Query OK, 0 rows affected (0.00 sec)


mysql> change master to master_host='172.16.33.144',master_port=3306,master_user='master',master_password='123456',master_log_file=' mysql-bin.000001',master_log_pos=107;
Query OK, 0 rows affected (0.04 sec)


7、重启主从:
[root@yoon ~]# service mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS! 


8、查看主从是否正常:
主库:
show processlist;
show master status;


从库:
show processlist;
start slave;
show slave status\G
MySQL主从切换是指将原本的MySQL主服务器切换为从服务器,同时将原本的从服务器切换为新的主服务器。这种切换可以用于实现高可用性和故障恢复。 下面是一个示例的MySQL主从切换过程: 1. 停止应用程序与MySQL数据库的连接,确保所有数据传输已完成并且没有未完成的事务。 2. 在原主服务器上执行FLUSH TABLES WITH READ LOCK语句,以保证在备份期间没有写操作。 3. 使用mysqldump或其他备份工具对原主服务器进行全量备份。可以使用以下命令执行备份: ``` mysqldump -u <username> -p<password> --all-databases --master-data > backup.sql ``` 4. 将备份文件传输到新主服务器上。 5. 在新主服务器上还原备份文件。可以使用以下命令执行还原: ``` mysql -u <username> -p<password> < backup.sql ``` 6. 在新主服务器上打开binlog功能,并获取新的binlog文件名和位置。 7. 在原从服务器上执行CHANGE MASTER TO语句,将新主服务器配置为新的主服务器。可以使用以下命令执行配置: ``` CHANGE MASTER TO MASTER_HOST='<new_master_host>', MASTER_USER='<replication_user>', MASTER_PASSWORD='<replication_password>', MASTER_LOG_FILE='<new_master_log_file>', MASTER_LOG_POS=<new_master_log_pos>; ``` 8. 在原从服务器上启动复制过程: ``` START SLAVE; ``` 9. 等待一段时间,确保复制过程正常运行。 10. 在应用程序中更新连接配置,将主服务器地址改为新的主服务器。 11. 恢复原主服务器的正常操作,可以将其配置为新的从服务器或者禁用复制功能。 这是一个简单的MySQL主从切换过程示例,实际操作中可能会根据具体需求和环境进行调整。在进行主从切换前,建议先进行充分的备份并测试整个过程,以确保数据的完整性和可用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值