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端數據丟失的情況出現。在高併發的生產環境下不建議使用!!!!
此方法的價值在於便於理解主從複製的原理!!!

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