MySQL互爲主從雙配置

1)       AB兩臺mysql服務器mysql

1、服務器參數,編輯/etc/my.cnf

[A
服務器]
server-id = 1
binlog-do-db = test
binlog-ignore-db = mysql
replicate-do-db = test
replicate-ignore-db = mysql
master-host     =  
192.168.255.195
master-user     =   repl
master-password =   repl
master-port     =  3306
master-connect-retry = 10
sync-binlog = 1
log-bin=mysql-bin
sql


[B
服務器]
server-id = 2
binlog-do-db = test
binlog-ignore-db = mysql
replicate-do-db = test
replicate-ignore-db = mysql
master-host     =   192.168.255.194
master-user     =   repl
master-password =   repl
master-port     =  3306
master-connect-retry = 10
sync-binlog = 1
shell

log-bin=mysql-bin數據庫

 

2)       AB上進行數據庫操做bash

A/B: Slave stop;服務器

A/B: Reset master;ide

A: grant replication slave on *.* to repl@192.168.255.194 identified by 'repl';測試

B: grant replication slave on *.* to repl@192.168.255.195 identified by 'repl';spa

A/B: show master status;orm

A: change master to master_host='192.168.255.194',master_user='repl',master_password='repl',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=98;(log根據 master status)

B:

change master to master_host='192.168.255.195',master_user='repl',master_password='repl',master_port=3306,master_log_file='mysql-bin.000006',master_log_pos=98; (log根據 master status)

A/B: slave start;

A/B: show slave status\G;(查看同步狀態)

看到以下狀態就表示同步成功

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Seconds_Behind_Master: 0

 

3)       測試數據庫同步,在Atest庫裏增長表,在Btest庫裏刪除表

Use test

A:Create table username (id int(15));

查看B中是否有username這個表

B: drop table username

查看A中是否已經刪除usernam這個表

A/B : show slave status\G;(再次查看同步狀態,若是沒問題就表示成功)

 

4)       互爲同步配置實例

1. A B 互爲主從同步test, 不一樣步mysql
兩個數據庫配置中均設置:binlog-do-db=test, binlog-ignore-db=mysqlreplicate-do-db=testreplicate-ignore-db=mysql
2. A B
互爲主從只同步test,不一樣步其餘數據庫,新建立的也不會同步
兩個數據庫配置中均設置:binlog-do-db=testreplicate-do-db=test
3. A B
互爲主從不一樣步mysql, 同步其餘數據庫,譬如建立的新數據庫也會同步
兩個數據庫配置中均設置:binlog-ignore-db=mysqlreplicate-ignore-db=mysql
4. A B
互爲主從同步全部數據庫,包括新建的數據庫
兩個數據庫配置中均不設置上述四項

 

5) 實現自動同步的shell腳本

1.       增長superreplication client on權限

grant super  on *.* to repl@192.168.255.194 identified by 'repl';

grant replication client on *.* to repl@192.168.255.194 identified by 'repl';

 

2.      自動同步腳本autosync.sh

#!/bin/bash

while true

do

status=`/usr/bin/mysql -uroot -e "show slave status\G;"|grep Slave_SQL_Running|cut -f2 -d":"|sed 's/ //'`

if [ $status != "Yes" ]

then

A=`/usr/bin/mysql -urepl -prepl -h192.168.255.194 -e "show master status"|grep mysql-bin|awk '{print $2}'`

/usr/bin/mysql -uroot -e "slave stop"

/usr/bin/mysql -uroot -e "change master to master_host='192.168.255.194',master_user='repl',master_password='repl',master_port=3306,

master_log_file='mysql-bin.000001',master_log_pos=$A"

/usr/bin/mysql -uroot -e "slave start"

fi

sleep 10

done

 

3.      後臺執行

nohup ./autosync.sh >/dev/null 2>&1 &

相關文章
相關標籤/搜索