centos7 Mysql AB複製(主從備份)

4.1 環境介紹
4.1.1 Master環境介紹
1)操作系統:centos7
2)Mysql版本:5.7.23
3)IP:10.0.0.61

4.1.2 Slave環境介紹:
1)操作系統:centos7
2)Mysql版本:5.7.23
3)IP:10.0.0.62

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

log-bin=mysql-bin   #[必須]啓用二進制日誌 
server-id=61       #[必須]服務器唯一ID,默認是1,一般取IP最後一段 

2)重啓mysql

[root@ok_1 ~]# systemctl restart mysqld

3)在主服務器上建立帳戶並授權slave

[root@ok_1 ~]# mysql -uroot -p123456
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.245.139,加強安全。
//"mysqlsync"也不用寫Slave端的用戶名,因爲這裏是mysql專門用來做複製的!
4) 登錄mysql,查詢master的狀態

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      434 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 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


log-bin=mysql-bin        #[必須]啓用二進制日誌
server-id=62             #[必須]服務器唯一ID,默認是1,一般取IP最後一段

2)重啓mysql

[root@0k_2 ~]# systemctl restart mysqld

3)配置從服務器Slave:

[root@0k_2 ~]# mysql -uroot -p123456
mysql> change master to master_host='10.0.0.61',master_user='mysqlsync',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=434;
Query OK, 0 rows affected, 2 warnings (0.00 sec) 

//注意不要斷開,“434”無單引號,master_host的地址一定不能寫錯!

mysql> start slave;    #啓動從服務器複製功能
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.61
                  Master_User: mysqlsync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 434
               Relay_Log_File: 0k_2-relay-bin.000002
                Relay_Log_Pos: 320
        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: 434
              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: 61
                  Master_UUID: 91df0025-134c-11e9-8264-000c2921ff55
             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.00 sec)

特別注意:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,否則都是錯誤的狀態。

5)開始驗證
(1)初始狀態
在這裏插入圖片描述
(2)新建test表
在這裏插入圖片描述
(3)驗證結果
在這裏插入圖片描述
成功!!

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