Centos下MySQL主從同步配置

說明:因爲MySQL不一樣版本之間的(二進制日誌)binlog格式可能會不同,html

所以最好的搭配組合是Master的MySQL版本和Slave的版本相同或者更低,
Master的版本確定不能高於Slave版本。(版本向下兼容)java

1、環境mysql

        主機: master操做系統:centos 7 64位sql

                   IP:192.168.119.253數據庫

                   MySQL版本:5.5.50-MariaDBcentos

         從機:  slave操做系統:centos 7 64位安全

               IP:192.168.119.252服務器

                   MySQL版本:5.5.50-MariaDB網絡

2、建立數據庫socket

分別登陸master機和slave機的mysql:mysql –u root –p

建立數據庫:create database repl;

3、master機和slave機的相關配置

一、修改master機器中mysql配置文件my.cnf,該文件在/etc目錄下

在[mysqld]配置段添加以下字段

server-id=1
log-bin=mysql-bin
log-slave-updates=1
binlog-do-db=repl  #須要同步的數據庫,若是沒有本行表示同步全部的數據庫
binlog-ignore-db=mysql  #被忽略的數據

在master機上爲slave機添加一同步賬號

 MariaDB[(none)]>grant replication slave on *.* to 'repl'@'192.168.119.252' identified by '123456';

 MariaDB[(none)]>flush  privileges;

 

重啓master機的mysql服務:service mysqld restart

用show master status 命令看日誌狀況

MariaDB[(none)]>show master status;

經過該命令得到File和Position,在slave中有用 。注:基準這裏的「mysql-bin.000001」和「245」,在下面 「(3)設置Slave複製」的配置中會用到

二、修改slave機中mysql配置文件

(1)修改slave機器中mysql配置文件my.cnf,該文件在/etc目錄下

一樣在[mysqld]字段下添加以下內容

server-id=2
log-bin= mysql-bin
relay-log= mysql-relay-bin
read-only=1
log-slave-updates=1
replicate-do-db=repl #要同步的數據庫,不寫本行表示同步全部數據庫

而後重啓slave機的mysql:service mysqld restart

(2)在slave機上驗證對主機鏈接

 mysql -h192.168.119.253 -urepl -p123456

MariaDB[(none)]>show grants for repl@192.168.119.252;

(3)設置Slave複製

CHANGE MASTER TO
MASTER_HOST='192.168.119.253',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=245,
MASTER_CONNECT_RETRY=10;

(4)啓動Slave

運行SHOW SLAVE STATUS查看輸出結果:

主要查看Slave_IO_Running和Slave_SQL_Running 兩列是否都爲YES

 

4、測試主從服務器是否能同步

在主服務器上面新建一個表,必須在repl數據下

mysql> use repl

Database changed

mysql> create table test(id int,name char(10));

Query OK, 0 rows affected (0.00 sec)

mysql> insert into test values(1,'zaq');

Query OK, 1 row affected (0.00 sec)

mysql> insert into test values(1,'xsw');

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+------+------+

| id    | name |

+-------+------+

|    1    | zaq   |

|    1    | xsw |

+-------+------+

2 rows in set (0.00 sec)

在從服務器查看是否同步過來

mysql> use repl;

Database changed

mysql> select * from test;

+------+------+

| id    | name |

+------+------+

|     1 | zaq   |

|     1 | xsw |

+------+------+

2 rows in set (0.00 sec)

說明已經配置成功。

 

6、擴展——MySQL主從複製幾個重要的啓動選項

(1) log-slave-updates
log-slave-updates這個參數用來配置從服務器的更新是否寫入二進制日誌,這個選項默認是不打開的,可是,若是這個從服務器B是服務器A的從服務器,同時還做爲服務器C的主服務器,那麼就須要開發這個選項,這樣它的從服務器C才能得到它的二進制日誌進行同步操做

(2) master-connect-retry

master-connect-retry這個參數是用來設置在和主服務器鏈接丟失的時候,重試的時間間隔,默認是60秒

(3) read-only

read-only是用來限制普通用戶對從數據庫的更新操做,以確保從數據庫的安全性,不過若是是超級用戶依然能夠對從數據庫進行更新操做

(4) slave-skip-errors

在複製過程當中,因爲各類的緣由,從服務器可能會遇到執行BINLOG中的SQL出錯的狀況,在默認狀況下,服務器會中止複製進程,再也不進行同步,等到用戶自行來處理。

Slave-skip-errors的做用就是用來定義複製過程當中從服務器能夠自動跳過的錯誤號,當複製過程當中遇到定義的錯誤號,就能夠自動跳過,直接執行後面的SQL語句。

--slave-skip-errors=[err1,err2,…….|ALL]

但必須注意的是,啓動這個參數,若是處理不當,極可能形成主從數據庫的數據不一樣步,在應用中須要根據實際狀況,若是對數據完整性要求不是很嚴格,那麼這個選項確實能夠減輕維護的成本

7、問題排查

1.重啓發現mariaDB沒法啓動能夠經過查看日誌來肯定問題

 2.錯誤在主機中執行了

CHANGE MASTER TO
MASTER_HOST='192.168.119.253',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=714,
MASTER_CONNECT_RETRY=10;

要清除slave,那麼主機mariaDB中執行 reset slave all; 既能夠

 

8.SHOW SLAVE STATUS輸出的各列的含義詳解

MASTER_LOG_FILE and MASTER_LOG_POS are the coordinates at which the slave I/O thread should begin reading from the master the next time the thread starts. 
RELAY_LOG_FILE and RELAY_LOG_POS are the coordinates at which the slave SQL thread should begin reading from the relay log the next time the thread starts. 
If you specify either of MASTER_LOG_FILE or MASTER_LOG_POS, you cannot specify RELAY_LOG_FILE or RELAY_LOG_POS. 
If you specify either of MASTER_LOG_FILE or MASTER_LOG_POS, you also cannot specify MASTER_AUTO_POSITION = 1 (described later in this section). 
If neither of MASTER_LOG_FILE or MASTER_LOG_POS is specified, the slave uses the last coordinates of the slave SQL thread before CHANGE MASTER TO was issued. 
This ensures that there is no discontinuity in replication, even if the slave SQL thread was late compared to the slave I/O thread, 
when you merely want to change, say, the password to use.
翻譯:
MASTER_LOG_FILEMASTER_LOG_POS 下次slave線程啓動從主機中讀取數據的位置
RELAY_LOG_FILERELAY_LOG_POS 下次SQL線程啓動從relay log中讀取數據的位置
若是指定了MASTER_LOG_FILE 和MASTER_LOG_POS,那麼就不能在指定 RELAY_LOG_FILERELAY_LOG_POS ,同時也不能指定MASTER_AUTO_POSITION=1
若是MASTER_LOG_FILE 和MASTER_LOG_POS都不指定, 從機將採起執行 CHANGE MASTER TO以前的最後一次的位置


MASTER_BIND is for use on replication slaves having multiple network interfaces, and determines which of the slave's network interfaces is chosen for connecting to the master.
翻譯:
MASTER_BIN 針對有多個網絡接口的經過它來決定使用那個網絡接口MASTER_CONNECT_RETRY specifies how many seconds to wait between connect retries. The default is 60. 翻譯:
MASTER_CONNECT_RETRY 指定多久重連,默認是60秒

MASTER_DELAY specifies how many seconds behind the master the slave must lag. An event received from the master is not executed until at least interval seconds later than its execution
on the master. The default is 0. An error occurs if interval is not a nonnegative integer in the range from 0 to 231−1
翻譯:
MASTER_DELAY 指定相對於主機延遲多久 MASTER_USER and MASTER_PASSWORD are the user name and password of the account to use for connecting to the master
翻譯:
MASTER_USER 和 MASTER_PASSWORD 連接主機的帳號密碼 MASTER_HOST and MASTER_PORT are the host name (or IP address) of the master host and its TCP/IP port
   Note   Replication cannot use Unix socket files. You must be able to connect to the master MySQL server using TCP/IP.
翻譯:
MASTER_HOST 和 MASTER_PORT 主機的計算機名或IP地址,以及端口號
  注意:主從複製必須使用TCP/IP,不能使用socke
MASTER_RETRY_COUNT limits the number of reconnection attempts and updates the value of the Master_Retry_Count column in the output of SHOW SLAVE STATUS. 翻譯:
  MASTER_RETRY_COUNT 限制重連的次數

RELAY_LOG_FILE can use either an absolute or relative path, and uses the same base name as MASTER_LOG_FILE. (Bug #12190)
翻譯:
  RELAY_LOG_FILE 可使用相對或絕對的路徑,或使用 MASTER_LOG_FILE相同的基礎的文件名 IGNORE_SERVER_IDS takes a comma-separated list of 0 or more server IDs.
翻譯:
  IGNORE_SERVER_IDS 使用都好分割的server IDS

  

 

參考:http://www.cnblogs.com/meetrice/p/5311839.html

How to Setup MariaDB (Master-Slave) Replication in CentOS/RHEL 7 and Debian 8

相關文章
相關標籤/搜索