mysql主從、主主復制(雙主復制)配置步驟
一:MySQL復制:
MySQL復制簡介:
將master服務器中主數據庫的ddl和dml操作通過二進制日志傳到slaves服務器上,然后在master服務器上將這些日志文件重新執行,從而使得slave服務器和master服務器上的數據信息保持同步。
Mysql復制的原理:
將數據分布到多個系統上去,是通過將Mysql的某一台master主機的數據復制到其它(slave)主機上,並重新執行一遍來實現的;
復制過程中一個服務器充當master服務器,而一台或多台其它服務器充當slave服務器。master服務器將更新寫入二進制日志文件,並維護文件的一個索引以跟蹤日志循環。
這些日志可以記錄發送到slave服務器的更新。當一個slaves服務器連接master服務器時,它通知master服務器從服務器在日志中讀取的最后一次成功更新的位置。slave服務器接收從那時起發生的任何更新,然后封鎖並等待master服務器通知新的更新。
mysql復制的優點:
在slave服務器上執行查詢操作,降低master服務器的訪問壓力
當master服務器上出現了問題可以切換到slave服務器上,不會造成訪問中斷等問題
在slave服務器上進行備份,以避免備份期間影響master服務器的服務使用及日常訪問
Mysql自身的復制功能:是構建大型、高性能應用程序的基礎。
mysql支持的復制類型:
基於語句的復制:在主服務器上執行的SQL語句,在從服務器上執行同樣的語句。MySQL默認采用基於語句的復制,效率比較高。一旦發現沒法精確復制時,會自動選着基於行的復制。
基於行的復制:把改變的內容復制過去,而不是把命令在從服務器上執行一遍. 從mysql5.0開始支持
混合類型的復制::默認采用基於語句的復制,一旦發現基於語句的無法精確的復制時,就會采用基於行的復制。
MySQL復制技術的特點:
數據分布 (Data distribution )
備份(Backups)
負載平衡(load balancing)
高可用性和容錯性 High availability and failover
復制的工作過程:
master將改變記錄到二進制日志(binary log)中(這些記錄叫做二進制日志事件,binary log events);
slave將master的binary log events拷貝到它的中繼日志(relay log);
slave重做中繼日志中的事件,將改變反映它自己的數據。
第一步:master記錄二進制日志。在每個事務更新數據完成之前,master在二日志記錄這些改變。MySQL將事務串行的寫入二進制日志,即使事務中的語句都是交叉執行的。在事件寫入二進制日志完成后,master通知存儲引擎提交事務;
第二步:slave將master的binary log拷貝到自己的中繼日志。首先,slave開始一個工作線程——I/O線程。I/O線程在master上打開一個普通的連接,然后開始binlog dump process。Binlog dump process從master的二進制日志中讀取事件,如果已經跟上master,它會睡眠並等待master產生新的事件。I/O線程將這些事件寫入中繼日志;
第三步:SQL slave thread(SQL從線程)處理該過程的最后一步。SQL線程從中繼日志讀取事件,並重放其中的事件而更新slave的數據,使其與master中的數據一致。只要該線程與I/O線程保持一致,中繼日志通常會位於OS的緩存中,所以中繼日志的開銷很小。
下面使用一次實驗配置主從、主主架構
環境:三台虛擬機
192.168.0.29 master1
192.168.0.30 master2
192.168.0.32 slave1
192.168.0.34 VIP
其中:master1和slave1是主從關系,master1和master2互為主從
一、先修改配置文件
服務器master1(192.168.0.29)配置如下
server-id = 1
log_slave_updates = 1
auto-increment-increment = 2
auto-increment-offset = 1
服務器master2(192.168.0.30)配置
server-id = 2
log_slave_updates = 1
auto-increment-increment = 2
auto-increment-offset = 2
服務器slave1(192.168.0.32) 配置:
server-id = 3
log_slave_updates = 1
三台服務器mysql都重啟
shell > service mysqld restart
注:雙主之間只有server-id不同和 auto-increment- offset不同
auto-increment-offset是用來設定數據庫中自動增長的起點的,回為這兩能服務器都設定了一次自動增長值2,所以它們的起點必須得不同,這樣才能避免兩台服務器數據同步時出現主鍵沖突
另:auto-increment-increment的值應設為整個結構中主庫服務器的總數,本案例用到兩台主庫服務器,所以值設為2
二、同步數據
在master1上:
#先授權,這樣導出的sql文件中就包含用戶名和密碼,如果你不需要同步系統庫,那么,需要在從庫change master之前,主從mysql實例都進行授權
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY '123456';
mysql > grant replication slave on *.* to repl@'127.0.0.1' identified by '123456';
mysql> flush privileges;
使用mysqldump導出所有數據庫的數據備份,備份數據前先鎖表,保證數據一致性
mysql> FLUSH TABLES WITH READ LOCK;
開啟另外一個會話終端執行數據備份:
shell > /usr/local/services/mysql/bin/mysqldump -uroot -p'xx' --opt --default-character-set=utf8 --triggers -R --hex-blob --single-transaction --no-autocommit --all-databases > all.sql
查看binlog位置,並記下這些信息,后邊會用到:
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000004
Position: 107
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
解表鎖:
mysql> UNLOCK TABLES;
把備份文件傳送到master2和slave1上:
shell > scp all.sql 192.168.0.30:/tmp
shell > scp all.sql 192.168.0.32:/tmp
導出數據時也可以不手動加鎖解鎖,mysqldump加一個參數:--master-data=2,該參數會把change master 語句加到all.sql文件中,以注釋的形式寫入,包含master_log_file和master_log_pos,復制出來帶上主庫IP、用戶名和密碼(master_host,master_user,master_password)待從庫導入數據后,即可在從庫上執行change master語句。
grep '\-\- CHANGE MASTER' all.sql
三、互告bin-log信息
在master2中導入all.sql文件,並執行change master語句:
shell > /usr/local/services/mysql/bin/mysql -uroot -p'xx' < /tmp/all.sql
mysql> change master to master_host='192.168.0.29',master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=107;
在slave1上執行下面的操作:
導入all.sql文件,並執行change master語句:
shell > /usr/local/services/mysql/bin/mysql -uroot -p'xx' < /tmp/all.sql
mysql> change master to master_host='192.168.0.29',master_user='repl',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=107;
在master2上面查看binlog位置:
show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 509270
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
在master1上執行change master
mysql> change master to master_host='192.168.0.30',master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=509270;
這里要注意:搭建雙主的時候,只需要一邊使用mysqldump導出數據就可以了,如上所示,master2配置為master1的主庫時,只需要查看一下master2的binlog位置,然后在master1上change master一下就可以了,不需要再次把數據用mysqldump倒回去。因為此時master2所有數據本身就是來自master1,sql_thread線程在執行中繼日志時會忽略自身server-id的記錄,所以隨便指定一個master_log_file對應的master_log_pos位置,直接change過去即可。
大概過程:
在A上導出的SQL,導入了B,搭建好了AàB的主從,再要搭建B--》A的主從就不能把B的數據導出sql到A了,只需要在B上show master status一下,隨便找一個position,在A上change master一下即可(因為B上的數據本來就是從A同步過去的,B的二進制日志中的server id全是A的,也就是說B要作為主庫同步數據到A,B的二進制日志全部帶有A自己的server id,不需要把自己同步給B的數據再同步回來,所以在B上隨便找一個二進制日志的position就可以了)
四、在三服務器都執行以下命令
mysql> start slave;
五、查看狀態
mysql> show slave status\G
master1狀態如下:
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.30
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 509270
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-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:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 509270
Relay_Log_Space: 413
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: 2
1 row in set (0.00 sec)
master2狀態如下:
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.29
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 107
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000004
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: 107
Relay_Log_Space: 413
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
slave1狀態如下:
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.29
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 107
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000004
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: 107
Relay_Log_Space: 413
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
當看到了兩個yes,即:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
說明已經配置成功了
接下來看可以做一下實驗,測試一下是否同步
在master1上面創建庫xiaoboluo,創建表t1,並插入測試數據:
mysql > create database xiaoboluo;
mysql > create table t1 (id int unsigned not null primary key auto_increment, aa varchar(100));
mysql > insert into t1 values('','test1');
mysql > insert into t1 values('','test2');
查詢t1表:
mysql> select * from t1;
+----+-------+
| id | aa |
+----+-------+
| 1 | test1 |
| 3 | test2 |
+----+-------+
在master1上查詢t1表:
mysql> select * from t1;
+----+-------+
| id | aa |
+----+-------+
| 1 | test1 |
| 3 | test2 |
+----+-------+
在slave1上查詢t1表:
mysql> select * from t1;
+----+-------+
| id | aa |
+----+-------+
| 1 | test1 |
| 3 | test2 |
+----+-------+
可以發現master1上創建的庫,表,插入的測試數據,在master2和slave1中都已經同步了
現在到master2上插入幾行測試數據:
mysql> insert into t1 values('','test3');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into t1 values('','test4');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from t1;
+----+-------+
| id | aa |
+----+-------+
| 1 | test1 |
| 3 | test2 |
| 4 | test3 |
| 6 | test4 |
+----+-------+
4 rows in set (0.00 sec)
到master1中查詢t1表:
mysql> select * from t1;
+----+-------+
| id | aa |
+----+-------+
| 1 | test1 |
| 3 | test2 |
| 4 | test3 |
| 6 | test4 |
+----+-------+
4 rows in set (0.00 sec)
到slave1中查詢t1表:
mysql> select * from t1;
+----+-------+
| id | aa |
+----+-------+
| 1 | test1 |
| 3 | test2 |
| 4 | test3 |
| 6 | test4 |
+----+-------+
4 rows in set (0.00 sec)
可以發現在master2中插入的數據,在master1中和slave1中都已經同步
注意:如果你只需要雙主,那么把slave1的過程去掉,如果你只需要主從,那么把master2的步驟去掉。
六、現在把keepalived給添加上去:
1.安裝keepalived,在兩台master上安裝
shell > yum install keepalived -y
2.配置keepalived:
A:master1:
shell > cat /etc/keepalived/keepalived.conf
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_USER=repluser
MYSQL_PASSWORD=replpass
MYSQL_PORT=3306
# 日志文件
LOG_FILE=/etc/keepalived/check_mysql.log
# 檢查次數
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -P${MYSQL_PORT} -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
#echo `date --date=today +"%Y-%M-%d %H:%m:%S"` - [INFO] - mysql available: success[$MYSQL_OK] >> $LOG_FILE
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
echo `date --date=today +"%Y-%M-%d %H:%m:%S"` - [INFO] - mysql invaild. keepalived stop. >> $LOG_FILE
exit 1
fi
sleep 1
done
shell > cat /etc/keepalived/keepalived.conf
checkMySQL.sh腳本內容與master1一樣,並添加執行權限,手動測試執行
3.master1和master2都啟動keepalived:
shell > service keepalived start
shell > chkconfig keepalived on
4.測試
在master1上查看下VIP:
shell > ip addr
在master2上查看下VIP,正常情況master2是沒有VIP的:
在192.168.0.%網段的任意主機連接192.168.0.34,看看能否成功訪問,這里如果想要看到VIP連到了哪個服務器,可以使用show slave status\G或者show processlist;來判斷,查看到的是主庫信息,即就說明VIP連接的是這個主庫的從庫:
mysql > \s --查看下相關信息
現在把master1的mysqld停掉(大概等待一分半鍾再次看看兩個master的vip信息,因為keepalived.conf里配置的檢測間隔是60s,如果發現mysql連不上會重試3次):
master1,發現VIP沒了:
master2,發現VIP有了:
shell > ip addr
再次遠程連接VIP:
shell > mysql -urepl -p'123456' -h 192.168.0.34 -P3306
把master1的mysqld啟動起來,然后把master2的關掉,看看VIP能否切換到master1上,過程跟上面的類似,只需要按照上面的過程再次驗證下能否正常訪問即可。這里要注意,要先啟動mysqld,並檢測主從數據是否一致,如果發現數據不一致,那么就先修復好數據,然后再啟動keepalived,否則貿然啟動keepalived可能發生VIP切換到問題主庫上導致悲催的事情發生(注:在mysqld實例停止或無法訪問時,keepalived中的checkMySQL.sh腳本檢測到問題時候,會自動把keepalived關掉)。