centos7 Mysql原生AB复制(主从备份)

4.1 环境介绍
4.1.1 Master环境介绍
1)操作系统:centos7
2)Mysql版本:5.7.23
3)IP:10.0.0.31

4.1.2 Slave环境介绍:
1)操作系统:centos7
2)Mysql版本:5.7.23
3)IP:10.0.0.32

4.2 配置
4.2.1 Master配置
1)my.cnf配置

#vim /etc/my.cnf

[root@ok_1 ~]# vim /etc/my.cnf

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

## replication
server_id=31
binlog-ignore-db=mysql
log-bin=master-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=30

2)重启mysql

[root@ok_1 ~]# systemctl restart mysqld

3)在主服务器上建立帐户并授权slave

[root@ok_1 ~]# mysql -uroot -p
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

这里要写这两句画的原因是mysql5.7不允许写简单密码,所以这里要通过命令更改mysql配置文件使其支持简单密码。

mysql> GRANT REPLICATION SLAVE ON *.* to 'mysqlsync'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

//这里
//这里也不用写Slave端的用户名,因为这里的mysql用户名是专门用来做复制的!
//一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.8.1,加强安全。
//"mysqlsync"也不用写Slave端的用户名,因为这里是mysql专门用来做复制的!
4) 登录mysql,查询master的状态

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.01 sec)
##把数据表先锁住,不让Position 变化!

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

注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化。

4.2.2 Slave配置
1)my.cnf配置

[root@0k_2 ~]# vim /etc/my.cnf

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


## replication
server_id=32     
binlog-ignore-db=mysql
log-bin=mysql-slave-bin
binlog_cache_size = 1M
binlog_format=mixed      ##mixed是开启混合同步模式
expire_logs_days=30
relay_log=mysql-relay-bin
log_slave_updates=1
read_only=1

2)重启mysql

[root@0k_2 ~]# systemctl restart mysqld

3)配置从服务器Slave:

[root@0k_2 ~]# mysql -uroot -p
mysql> change master to 
    -> master_host='10.0.0.31',
    -> master_user='mysqlsync',
    -> master_password='123456',
    -> master_port=3306,
    -> master_log_file='master-mysql-bin.000001',
    -> master_log_pos=434,
    -> master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.12 sec)

//注意不要断开,“434”无单引号,master_host的地址一定不能写错!

mysql> start slave;    #启动从服务器复制功能
Query OK, 0 rows affected (0.00 sec)

回到Master端,解锁数据表

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
  1. 回到从服务器检查从服务器复制功能状态:
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.31
                  Master_User: mysqlsync
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: master-mysql-bin.000001
          Read_Master_Log_Pos: 434
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: master-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: 434
              Relay_Log_Space: 534
              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: 31
                  Master_UUID: 19a7156d-213a-11e9-b107-5254004efc34
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.01 sec)

特别注意:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态。

5)开始验证
在master机器上查看状态,可以看见slave的连接信息

mysql> show processlist\G;
*************************** 1. row ***************************
     Id: 2
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: starting
   Info: show processlist
*************************** 2. row ***************************
     Id: 3
   User: mysqlsync
   Host: 10.0.0.32:52842
     db: NULL
Command: Binlog Dump
   Time: 313
  State: Master has sent all binlog to slave; waiting for more updates
   Info: NULL
2 rows in set (0.00 sec)

在slave机器上查看状态,可以看见master的连接信息

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

(1)初始状态
在这里插入图片描述
(2)新建test表
在这里插入图片描述
(3)验证结果
在这里插入图片描述
成功!!

如果出现

 Slave_IO_Running: Yes
Slave_SQL_Running: Yes

但是mysql主从依然不能同步数据时
解决方案:
salve从库端:

#停止slave服务
mysql> stop slave;
#跳过一步错误,后面的数字可变,如果还不行可执行多次
mysql> set global sql_slave_skip_counter =1;
#开始slave服务
mysql> start slave;
#查看slave状态
mysql> show slave status\G

备注:
此方法的前提条件是主从端mysql的数据是一模一样的起始数据库内容(表、字段、内部值都必须一样!)
这种同步方式并不是实时同步。会有滞后甚至slave端数据丢失的情况出现。在高并发的生产环境下不建议使用!!!!
此方法的价值在于便于理解主从复制的原理!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章