mysql-5.6主從同步配置示例

本文環境  
主庫:CentOS6.5 x64 192.168.0.65 mysql-5.6.29    
備庫:CentOS6.5 x64 192.168.0.66 mysql-5.6.29


一、常規配置方式一

1. mysql主服務器配置

# vi /etc/my.cnf

[mysqld]  
log-bin                = master-bin    
log-bin-index          = master-bin.index    
binlog_format          = mixed    
server-id              = 1

# service mysqld restart

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


2. 主服務器配置同步複製帳號

grant replication slave on *.* to 'repl'@'%' identified by '123456';  
flush privileges;


3. mysql從服務器配置

說明: 默認只要server-id不相同即可。

# vi /etc/my.cnf

[mysqld]  
log-bin                = mysql-bin    
binlog_format          = mixed    
server-id              = 11    
relay-log              = slave-relay-bin    
relay-log-index        = slave-relay-bin.index

配置說明: 不同步mysql庫可以實現主從庫有不同的帳號權限,經過測試,mysql5.6.29中只在從庫中配置有效。

其它參數:  
binlog-do-db                = mydb    僅同步一個數據庫    
#replicate-ignore-db        = mysql   忽略掉mysql庫,該參數產生很多意外的同步問題,還是不使用。    
replicate_wild_ignore_table = mysql.% 忽略掉mysql庫

# service mysqld restart


4. 測試示例

CREATE DATABASE `mydb`;

CREATE TABLE `user` (  
  `id` varchar(20) NOT NULL,    
  `username` varchar(20) NOT NULL,    
  `password` char(32) NOT NULL,    
  PRIMARY KEY (`id`)    
) ;

INSERT INTO user  VALUES ('1', 'koumm', '123456');  
INSERT INTO user  VALUES ('2', 'zhangsan', '123456');    
INSERT INTO user  VALUES ('3', 'lisi', '123456');    
INSERT INTO user  VALUES ('4', 'li2si', '123456');    
INSERT INTO user  VALUES ('5', 'abc', '123456');    
INSERT INTO user  VALUES ('6', 'tom', '123456');    
INSERT INTO user  VALUES ('7', 'jk', '123456');    
INSERT INTO user  VALUES ('8', 'xb', '123456');


5. 正常的主從配置過程

(1) 主庫鎖表

mysql> flush tables with read lock;  
Query OK, 0 rows affected (0.00 sec)

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


mysql> show master logs;    
+-------------------+-----------+    
| Log_name          | File_size |    
+-------------------+-----------+    
| master-bin.000001 |       353 |    
+-------------------+-----------+    
1 row in set (0.00 sec)

mysql>


(2) 主庫備份

[root@master ~]# mysqldump -uroot -p -B mydb > mydb.sql  
說明:-B參數有建庫語句。


(3) 主庫解開鎖表功能

mysql> unlock tables;  
Query OK, 0 rows affected (0.00 sec)    
mysql>


6. 從庫導入數據庫

# mysql -uroot -padmin < mydb.sql


7. 從庫配置同步過程

(1) 配置同步,手動執行同步參數,該配置會寫入master.info文件中。

mysql >

CHANGE MASTER TO  
MASTER_HOST='192.168.0.65',    
MASTER_PORT=3306,    
MASTER_USER='repl',    
MASTER_PASSWORD='123456',    
MASTER_LOG_FILE='master-bin.000001',    
MASTER_LOG_POS=353;


(2) 啓動從同步進程

mysql> start slave;  
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;  
*************************** 1. row ***************************    
               Slave_IO_State: Waiting for master to send event    
                  Master_Host: 192.168.0.65    
                  Master_User: repl    
                  Master_Port: 3306    
                Connect_Retry: 60    
              Master_Log_File: master-bin.000001    
          Read_Master_Log_Pos: 353    
               Relay_Log_File: slave-relay-bin.000002    
                Relay_Log_Pos: 284    
        Relay_Master_Log_File: master-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: 353    
              Relay_Log_Space: 457    
              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    
                  Master_UUID: c8bb22a1-024e-11e6-a1e8-000c29225fa0    
             Master_Info_File: /usr/local/mysql/data/master.info    
                    SQL_Delay: 0    
          SQL_Remaining_Delay: NULL    
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it    
           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    
1 row in set (0.00 sec)

ERROR:    
No query specified

mysql>

#查看如下兩個參數爲YES,說明從庫運行正常。  
             Slave_IO_Running: Yes    
            Slave_SQL_Running: Yes


(3) 測試同步情況

# 主庫插入一條記錄

mysql> use mydb;  
Database changed    
mysql> select * from stu;    
+----+----------+----------+    
| id | username | password |    
+----+----------+----------+    
|  1 | koumm    | 123456   |    
|  2 | zhangsan | 123456   |    
|  3 | lisi     | 123456   |    
|  4 | wangwu   | 123456   |    
|  5 | zhaoliu  | 123456   |    
|  6 | zhouqi   | 123456   |    
+----+----------+----------+    
6 rows in set (0.00 sec)

mysql>    
mysql> INSERT INTO stu  VALUES ('7', 'tom', '123456');      
Query OK, 1 row affected (0.05 sec)

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

mysql>


# 從庫查詢同步情況

mysql> use mydb;  
Database changed    
mysql> select * from stu;    
+----+----------+----------+    
| id | username | password |    
+----+----------+----------+    
|  1 | koumm    | 123456   |    
|  2 | zhangsan | 123456   |    
|  3 | lisi     | 123456   |    
|  4 | wangwu   | 123456   |    
|  5 | zhaoliu  | 123456   |    
|  6 | zhouqi   | 123456   |    
|  7 | tom      | 123456   |    
+----+----------+----------+    
7 rows in set (0.00 sec)

mysql>

小結:

本文只是配置mysql主從的一個過程。還是有很多需要考慮與改進的地址。  
(1) 主從庫同步用戶權限的問題考慮,是直接從主庫同步權限呢,還是單獨考慮從庫的權限。    
(2) mysql5.5有半自動主從同步複製    
(3) mysql5.6有基於GTID主從複製


二、快速配置從庫方式二


1,主庫備份(全庫備份)

mysqldump -uroot -p -A -B --events --master-data=1 > mydb.sql

全庫備份記錄了數據庫備份時的master-bin.000003', MASTER_LOG_POS=583位置,在配置從庫時無需要在主庫鎖表,記錄POS位置,解鎖等配置。

[root@localhost ~]# egrep -v "#|\*|--|^$" mydb.sql |head

CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000003', MASTER_LOG_POS=583;
USE `mydb`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` varchar(20) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` char(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
LOCK TABLES `user` WRITE;


2,從庫創建同步的數據庫, 並導入數據

mysql -uroot -padmin
create database mydb;
mysql -uroot -padmin mydb < mydb.sql


3,配置主從同步

mysql>
CHANGE MASTER TO
MASTER_HOST='192.168.0.65',
MASTER_PORT=3306,
MASTER_USER='repl',
MASTER_PASSWORD='123456';

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.65
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 583
               Relay_Log_File: testdb-relay-bin.000004
                Relay_Log_Pos: 747
        Relay_Master_Log_File: master-bin.000003
             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: mysql.%
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 583
              Relay_Log_Space: 2536
              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
                  Master_UUID: c8bb22a1-024e-11e6-a1e8-000c29225fa0
             Master_Info_File: /usr/local/mysql-5.6.29-linux-glibc2.5-x86_64/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.00 sec)


三、主從同步簡單管理

1. 停止MYSQL同步

STOP SLAVE IO_THREAD;     #停止IO進程
STOP SLAVE SQL_THREAD;    #停止SQL進程
STOP SLAVE;               #停止IO和SQL進程

2. 啓動MYSQL同步

START SLAVE IO_THREAD;    #啓動IO進程
START SLAVE SQL_THREAD;   #啓動SQL進程
START SLAVE;              #啓動IO和SQL進程

3. 重置MYSQL同步

RESET SLAVE;
清除主從同步參數,它會刪除master.info和relay-log.info文件,以及所有的中繼日誌,並啓動一個新的中繼日誌。
適用重新再次配置一次從庫的情況。


4. 查看MYSQL同步狀態

SHOW SLAVE STATUS;


5. 臨時跳過MYSQL同步錯誤

確保數據一致的情況下臨時的跳過這個錯誤操作如下,有可能要多次。

STOP SLAVE;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
START SLAVE;

# vi /etc/my.cnf

[mysqld]
#slave-skip-errors=1062,1053,1146 #跳過指定error no類型的錯誤
#slave-skip-errors=all            #跳過所有錯誤

這裏出現一次1146報錯,是因爲配置binlog-do-db過濾,出現該配置的BUG問題,所有要慎用binlog-do-db。

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