mysql主從同步配置

        Mysql主從同步的配置

主庫IP:192.168.1.2

從庫IP:192.168.1.3

添加一個用於主從同步的用戶:

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY ‘1q2w3e4r’;

如果監控mysql主從的話,請加上一個super權限:

GRANT SUPER, REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY '1q2w3e4r';

1、主庫的配置

1.1.mysql5.0以下版本的配置

 修改主庫mysql配置配置文件,在[mysqld]段添加以下內容:

server-id = 1

log-bin=/home/mysql/logs/binlog/bin-log

max_binlog_size = 500M

binlog_cache_size = 128K

binlog-do-db = adb

binlog-ignore-db = mysql

log-slave-updates

1.2. mysql5.0以上版本的配置

 修改主庫mysql配置配置文件,在[mysqld]段添加以下內容:

server-id = 1

log-bin=/home/mysql/logs/binlog/bin-log

max_binlog_size = 500M

binlog_cache_size = 128K

binlog-do-db = adb

binlog-ignore-db = mysql

log-slave-updates

expire_logs_day=2

binlog_format="MIXED"

1.3.各個參數的含義和相關注意項:

server-id = 1 #服務器標誌號,注意在配置文件中不能出現多個這樣的標識,如果出現多個的話mysql以第一個爲準,一組主從中此標識號不能重複。

log-bin=/home/mysql/logs/binlog/bin-log #開啓bin-log,並指定文件目錄和文件名前綴。

max_binlog_size = 500M #每個bin-log最大大小,當此大小等於500M時會自動生成一個新的日誌文件。一條記錄不會寫在2個日誌文件中,所以有時日誌文件會超過此大小。

binlog_cache_size = 128K #日誌緩存大小

binlog-do-db = adb #需要同步的數據庫名字,如果是多個,就以此格式在寫一行即可。

binlog-ignore-db = mysql  #不需要同步的數據庫名字,如果是多個,就以此格式在寫一行即可。

log-slave-updates  #當Slave從Master數據庫讀取日誌時更新新寫入日誌中,如果只啓動log-bin 而沒有啓動log-slave-updates則Slave只記錄針對自己數據庫操作的更新。

expire_logs_day=2 #設置bin-log日誌文件保存的天數,此參數mysql5.0以下版本不支持。

binlog_format="MIXED"   #設置bin-log日誌文件格式爲:MIXED,可以防止主鍵重複。

2、從庫的配置

2.1.mysql5.1.7以前版本

修改從庫mysql配置配置文件,在[mysqld]段添加以下內容:

server-id=2

master-host=192.168.1.2

master-user=repl

master-password=1q2w3e4r

master-port=3306

master-connect-retry=30

slave-skip-errors=1062

replicate-do-db = adb

replicate-ignore-db = mysql

slave-skip-errors=1007,1008,1053,1062,1213,1158,1159

master-info-file = /home/mysql/logs/master.info

relay-log = /home/mysql/logs/relay-bin

relay-log-index = /home/mysql/logs/relay-bin.index

relay-log-info-file = /home/mysql/logs/relay-log.info

如果修改了連接主庫相關信息,重啓之前一定要刪除master.info文件,否則重啓之後由於連接信息改變從庫而不會自動連接主庫,造成同步失敗。此文件是保存連接主庫信息的。

2.2.mysql5.1.7以後版本

Mysql5.1.7版本在叢庫上面的配置很少,主要是採用了新的同步信息記錄方式,他不在支持在配置文件中配置連接主庫的相關信息,而是把連接等相關信息記錄在master-info-file = /home/mysql/logs/master.info文件中,如果入庫變了,直接在mysql命令行執行連接信息的改變即可生效,比較靈活了,而不用去重啓mysql。修改從庫mysql配置配置文件,在[mysqld]段添加以下內容:

slave-skip-errors=1007,1008,1053,1062,1213,1158,1159

2.3. 各個參數的含義和相關注意項

這裏只講一下2個參數,其他全部是從庫連接主庫的信息和中間日誌relay-log的設置。

master-connect-retry=30 #這個選項控制重試間隔,默認爲60秒。

slave-skip-errors=1007,1008,1053,1062,1213,1158,1159 #這個是在同步過程中忽略掉的錯誤,這些錯誤不會影響數據的完整性,有事經常出現的錯誤,一般設置忽略。其中1062爲主鍵重複錯誤。

3、實現主從同步

3.1.實現數據庫的統一

檢查主從數據庫的配置文件,查看是否已正確配置。首次實現 同步要備份主庫上需要同步的數據庫,然後完整的導入到從庫中。注:mysql5.0之前的版本涉及到mysql本身複製過濾存在問題,需要把所有的數據庫都備份導入到叢庫,保持。

3.2.查看並記錄主庫bin-log信息

進入主庫mysql中,執行:show master status;顯示信息如下:

mysql> show master status;

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

| File        | Position | Binlog_do_db | Binlog_ignore_db |

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

| bin-log.003 | 4        | adb          | mysql            |

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

1 row in set (0.00 sec)

記錄File 和Position信息;

3.3.在從庫上執行同步語句

進入mysql,執行以下語句:

slave stop;

change master to

master_host='192.168.1.2',

master_user='repl',

master_password='1q2w3e4r',

master_port=3306,

master_log_file='bin-log.003',

master_log_pos=4;

slave start;

3.4.查看主從同步狀態

    進入mysql,執行show slave status\G;顯示如下(mysql版本不同查詢的結果不同,但是重要的指標還是一樣的):

Mysql5.0之前的版本如下:

 

wKiom1meyxPhfvs_AAEZIymJVls993.jpg

Mysql5.5之前的版本如下:

 wKioL1mey0fSFaDHAAFEMAfRTGs264.jpg

Mysql5.5的版本如下:

 

wKiom1mey5HCe7UMAAFFjD3vmGw066.jpg

重要的指標爲:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Master_Log_File: bin-log.003

Relay_Master_Log_File: bin-log.003

Read_Master_Log_Pos: 4

Exec_master_log_pos: 4

Seconds_Behind_Master: 0(5.0之前版本沒有這個選項)

以上選項是兩兩對應的,只要結果是一致的,就說明主從同步成功。

3.5.同步中的常見的錯誤和處理

1、現象:在從庫上面show slave status\G;出現下列情況,

          Slave_IO_Running: Yes

          Slave_SQL_Running: No

          Seconds_Behind_Master: NULL

原因:

a.程序可能在slave上進行了寫操作;

b.也可能是slave機器重起後,事務回滾造成的;

c.有可能是在同步過程中遇到某種錯誤,這個會在查看從庫中狀態時看到錯誤提示,最少見的就是主鍵重複1062的錯誤。

解決方法:

進入master

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000040 | 324 |adb | mysql|

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

然後到slave服務器上執行手動同步

slave stop;

change master to

master_host='10.14.0.140',

master_user='repl',

master_password='1q2w3e4r',

master_port=3306,

master_log_file='mysql-bin.000040',

master_log_pos=324;

slave start;

show slave status\G;

2、現象:從數據庫無法同步,show slave status顯示:

          Slave_IO_Running: No

          Slave_SQL_Running: Yes

          Seconds_Behind_Master: NULL

   解決:首先查看數據庫的err日誌,查看是什麼錯誤提示,看從庫連接主庫的IP、用戶、密碼等相關信息是否有誤,如果有誤,重新執行同步;如果確認無誤,重啓主數據庫。

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000001 | 98 | adb| mysql|

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

進入從庫mysql,執行:

slave stop;

change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;

slave start;

或是這樣:

stop slave;

set global sql_slave_skip_counter =1;

start slave;

這個現象主要是master數據庫存在問題,由於連接主庫信息錯誤、主庫數據庫掛掉如果說常見錯等原因引起的,我在實際的操作中先重啓master後重啓slave即可解決這問題,出現此問題,必須要要重啓master數據庫。

四、mysql主主和主主集羣

1、mysql主主的實現

    在實際的生產應用中,爲了在主庫出現崩潰或是主服務器出現嚴重故障時快速的恢復業務,會直接切換到從庫上,當主庫故障處理完成後讓他直接作爲叢庫來運行,此時主主就是一個不錯的選擇。

  

五、mysql主從的監控

在mysql主從的應用中,只要進行了合理設置,基本上不會出現問題,但是對他的監控是必不可少的,以免由於真的出現問題又不知道而造成不必要的數據損失。

1、mysql主從監控的主要思路

Mysql主從的監控,其主要是監控從庫上的一些重要參數:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Master_Log_File: bin-log.003

Relay_Master_Log_File: bin-log.003

Read_Master_Log_Pos: 4

Exec_master_log_pos: 4

Seconds_Behind_Master: 0(5.0之前版本沒有這個選項)

通過以上的參數可以反映出主庫和從庫狀態是否正常,從庫是否落後於主庫等。值得一提的是在mysql5.0以前的版本,Slave_IO_Running這個狀態指標不可靠,會在主庫直接掛掉的情況下不會變成NO,Seconds_Behind_Master參數也不存在。監控以上參數即可監控mysql主從。

2、mysql主從監控的實現

不管mysql是那個版本,其中的從庫上的Exec_master_log_pos、Exec_master_log_pos;主庫上的 Master上的Log_File, Position,這四個參數可以判斷出當前主從的狀態。以下是適用於mysql所有版本的主從監控shell腳本:

#/bin/sh

user=repl

passwd=123415

master_ip="192.168.1.2"

log="/data3/check_repl.log"

value()

{

 master=`/usr/local/mysql/bin/mysql -u$user -p$passwd -h$master_ip -e "show master status\G;"|egrep "File|Position"`

 #mysql 4.0

 slave=`/usr/local/mysql/bin/mysql -u$user -p$passwd -h127.0.0.1 -e "show slave status\G;"|egrep "Relay_Master_Log_File|Exec_master_log_pos"`

 #mysql 5.0

 #slave=`mysql -u$user -p$passwd -e "show slave status\G;"|egrep "Relay_Master_Log_File|Exec_Master_Log_Pos"`

 #取主庫上的bin-log號及寫入的當前日誌位置   

 Master_Log=`echo $master |awk '{print $2}'|awk -F "." '{print $2}'`

 Master_Log_Pos=`echo $master |awk '{print $4}'`

 #取從庫上當前同步主庫的位置

 Relay_Master_Log_File=`echo $slave |awk '{print $2}'|awk -F "." '{print $2}'`

 Exec_Master_Log_Pos=`echo $slave |awk '{print $4}'`

 echo "Master_Log:"$Master_Log>>$log

 echo "Master_Log_Pos:"$Master_Log_Pos>>$log

 echo "Relay_Master_Log_File:"$Relay_Master_Log_File>>$log

 echo "Exec_Master_Log_Pos:"$Exec_Master_Log_Pos>>$log

}

for((i=1;i<=10;i++));

do

 echo "#################################">>$log

 value

 time=`date +"%Y-%m-%d %H:%M:%S"`

 if [ $Master_Log -eq $Relay_Master_Log_File ];then

       A=`expr $Master_Log_Pos - $Exec_Master_Log_Pos`

       if [ $A -lt 0 ];then

             A=`expr 0 - $A`

       fi

       echo $A>>$log

       if [ $A -lt 10000 ];then

             echo "$time Master-Slave is OK.">>$log

             #echo "$i"

             break

       else

             if [ $i ge 3 ];then              

                  echo "$time Warning:Slave-Master lag $A " >>$log

                  echo "$i"

             fi

             sleep 30

             continue

       fi

 else

       sleep 60

       fi

       if [ $i -eq 10 ];then

             echo "$i"

             echo "$time Error:Slave-Master must be check !" >>$log

       fi

done

在mysql5.0以後的版本,mysql主從已經相當的成熟了,可以只監控Slave_IO_Running,Slave_SQL_Running,Seconds_Behind_Master狀態就可以了,這裏不再做說明。


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