Centos7下部署Mysql5.7.31主从复制二(主从切换)

Centos7下部署Mysql5.7.31主从复制二(主从切换)

主从复制参考:https://blog.csdn.net/xjjj064/article/details/111869542
一、环境描述
系统:Centos7.8.2003
Mysql版本:5.7.31
防火墙、selinux:防火墙关闭或放行数据库端口,selinux设置为disabled
①、切换前主从
主Mysql服务器:192.168.6.101
从Mysql服务器:192.168.6.102
②、切换后主从
主Mysql服务器:192.168.6.102
从Mysql服务器:192.168.6.101

二、主从切换
①、主库操作
停止主服务器mysql服务(模拟master服务器故障)
192.168.6.101上执行

[root@server1 src]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      999/haproxy         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      950/sshd            
tcp        0      0 0.0.0.0:35672           0.0.0.0:*               LISTEN      999/haproxy         
tcp        0      0 0.0.0.0:55672           0.0.0.0:*               LISTEN      999/haproxy         
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1405/master         
tcp        0      0 0.0.0.0:514             0.0.0.0:*               LISTEN      953/rsyslogd        
tcp6       0      0 :::22                   :::*                    LISTEN      950/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1405/master         
tcp6       0      0 :::514                  :::*                    LISTEN      953/rsyslogd        
tcp6       0      0 :::3301                 :::*                    LISTEN      14495/mysqld        
[root@server1 src]# kill 14495

②、从库操作
停止数据库 io_thread 线程
一定要确保出现 Slave has read all relay log,才能继续往下运行

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql> show processlist \G;
*************************** 1. row ***************************
     Id: 2
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 64697
  State: Slave has read all relay log; waiting for more updates
   Info: NULL
*************************** 2. row ***************************
     Id: 10
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: starting
   Info: show processlist
2 rows in set (0.00 sec)

ERROR: 
No query specified

mysql> 

③、在从数据库102停止slave服务,然后执行reset master重置成主数据库

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

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

mysql> 

此时从数据库B已经成功设置切换成为主数据库,下面接着设置从数据库.
三、配置数据库主从

①、在原从数据B(192.168.6.102)上添加具有replication权限的用户

mysql> grant replication slave on *.* to 'tepl'@'192.168.6.101' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

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

②、在原主数据A(192.168.6.101)配置从数据库

mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.6.102',
    -> MASTER_PORT=3301,
    -> MASTER_USER='tepl',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_FILE='binlog.000001',
    -> MASTER_LOG_POS=601;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

③、执行show slave status 命令查看从数据服务是否成功开启

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.6.102
                  Master_User: tepl
                  Master_Port: 3301
                Connect_Retry: 60
              Master_Log_File: binlog.000001
          Read_Master_Log_Pos: 601
               Relay_Log_File: server1-relay-bin.000002
                Relay_Log_Pos: 317
        Relay_Master_Log_File: binlog.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: 601
              Relay_Log_Space: 526
              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: 22
                  Master_UUID: d8a3e74e-48db-11eb-b05f-000c29347c2a
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

④、在主库(192.168.6.102)上执行添加删除验证主从复制是否成功!
主库abc库a表有三条数据
在这里插入图片描述
从库也有三条数据
在这里插入图片描述
主库测试删除id为3的行
在这里插入图片描述
查看从库数据
在这里插入图片描述
至此,主从数据库成功地发生切换!
参考:https://www.kancloud.cn/uuphp/mysql/1260404
参考:https://www.cnblogs.com/ctypyb2002/p/9793012.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值