Centos下MySQL主從同步配置


說明:由於MySQL不同版本之間的(二進制日志)binlog格式可能會不一樣,

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

一、環境

        主機: master操作系統:centos 7 64位

                   IP:192.168.119.253

                   MySQL版本:5.5.50-MariaDB

         從機:  slave操作系統:centos 7 64位

               IP:192.168.119.252

                   MySQL版本:5.5.50-MariaDB

二、創建數據庫

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

創建數據庫:create database repl;

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

1、修改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復制”的配置中會用到

2、修改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

 

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

在主服務器上面新建一個表,必須在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)

說明已經配置成功。

 

六、擴展——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]

但必須注意的是,啟動這個參數,如果處理不當,很可能造成主從數據庫的數據不同步,在應用中需要根據實際情況,如果對數據完整性要求不是很嚴格,那么這個選項確實可以減輕維護的成本

七、問題排查

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM