master-master 就是兩臺服務器互爲主從:
服務器 A(master1) && 服務器 B(master2)
master2: 10.236.152.29
配置A
mysql 版本最好一致
給從機賦予登陸權限 如果有多個從屬的機子, 都要添加權限
創建給B登錄用的用戶名密碼:
mysql> GRANT FILE ON *.* TO 'backup'@'10.236.152.29' IDENTIFIED BY '123456';
mysql> create database replication_test default charset utf8;
配置A的my.cnf
# vim /etc/my.cnf
[mysqld]
server-id=1 #主機id
#開啓二進制日誌,並且名稱爲 /var/lib/mysql/mysql-bin.***
# 如果是個路徑則,保存到該路徑下(log-bin=/var/log/mysql-bin.log -> /var/log/mysql-bin.***)
log-bin=mysql-bin
read-only=0 #主機讀寫權限,讀寫都可以
binlog-do-db=replication_test #記錄日誌的數據庫:需要的備份數據,多個寫多行
binlog-ignore-db=mysql #不記錄日誌的數據庫:不需要備份的數據庫,多個寫多行
binlog-ignore-db=test
binlog-ignore-db=information_schema
# 自增字段奇數遞增,防止衝突(1, 3, 5, ...,)
auto-increment-increment = 2 # 每次遞增的步長
auto-increment-offset = 1 # 初始值
重啓 mysql: serivce mysqld restart 或者 /etc/init.d/mysql restart
註釋掉bind-address 127.0.0.1 這句, 這樣3306 可以監聽到來自非本機的請求
註釋掉bind-address 127.0.0.1 這句, 這樣3306 可以監聽到來自非本機的請求
註釋掉bind-address 127.0.0.1 這句, 這樣3306 可以監聽到來自非本機的請求
哈哈, 重要的事情說三遍, 我在配置的時候忘記了, 導致登陸不了,然後一直Slave_IO_Running 出connect狀態, 報錯2003
導出數據庫鎖定數據庫,不要退出 mysql shell
mysql>FLUSH TABLES WITH READ LOCK;
mysql># flush tables with read lock;
新開一個終端,導出數據庫
mysqldump --master-data -uroot -p replication_test > replication_test.sql
查看主服務器的狀態
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 98
Binlog_Do_DB: replication_test
Binlog_Ignore_DB: mysql,test,information_schema
1 row in set (0.00 sec)
ERROR:
No query specified
記下 Position 和 File 的值。 解鎖
mysql> UNLOCK TABLES;
mysql> # unlock tables;
A的配置告一段落
這裏配置 B 的相關屬性
編輯 my.cnf
# vim /etc/my.cnf
[mysqld]
server-id=2
log-bin=mysql-bin
replicate-do-db =replication_test #只複製某個庫,多個寫多行
replicate-ignore-db=mysql #不復制某個庫
replicate-ignore-db=test
replicate-ignore-db=information_schema
relay-log=mysqld-relay-bin # 開啓日誌中繼
log-slave-updates # slave將複製事件寫進自己的二進制日誌
#5.5
#log-slave-updates = ON
#5.1
#log-slave-updates = 1
導入 master 導出的數據庫:
mysql> create database replication_test default charset utf8;
mysql -uroot -p replication_test < replication_test.sql
在B上配置 master 信息:
mysql> slave stop;
mysql> CHANGE MASTER TO
-> MASTER_HOST='10.236.152.63',
-> MASTER_USER='backup',
-> MASTER_PASSWORD='123456',
-> MASTER_CONNECT_RETRY=60, // 如果從服務器發現主服務器斷掉,重新連接的時間差(秒)
-> MASTER_LOG_FILE='mysql-bin.000001', //主服務器二進制日誌的文件名(前面要求記住的 File 參數)
-> MASTER_LOG_POS=98; //日誌文件的開始位置(前面要求記住的 Position 參數)
mysql> CHANGE MASTER TO MASTER_HOST='10.236.152.63', MASTER_USER='backup', MASTER_PASSWORD='123456', MASTER_CONNECT_RETRY=60, MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=98;
mysql > slave start;
重啓 master, slave: service mysqld restart
查看 slave 狀態:
進入 B
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 10.236.152.63
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000001
Relay_Log_Pos: 98
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: replication_test
Replicate_Ignore_DB: mysql,test,information_schema
Exec_Master_Log_Pos: 98
Relay_Log_Space: 98
Until_Log_Pos: 0
Seconds_Behind_Master: NULL
1 row in set (0.00 sec)
注意一定要有下面兩項,沒有的話查看錯誤日誌(less /var/log/mysqld.log):
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
測試
進入A服務器
mysql> use replication_test;
Database changed
mysql> create table test(id int);
mysql> insert int test set id=1;
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000002 # 注意這裏
Position: 276 # 注意這裏
Binlog_Do_DB: replication_test
Binlog_Ignore_DB: mysql,test,information_schema
1 row in set (0.00 sec)
B 服務器
mysql> use replication_test;
mysql> show tables;
+-----------------+
| Tables_in_replication_test |
+-----------------+
| test |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from test;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.236.152.63
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002 # 跟 master 一樣
Read_Master_Log_Pos: 276 # 跟 master 一樣
Relay_Log_File: mysqld-relay-bin.000003
Relay_Log_Pos: 413
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: replication_test
Replicate_Ignore_DB: mysql,test,information_schema
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 276
Relay_Log_Space: 413
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
進入B服務器
mysql> grant replication slave on *.* to 'backup'@'10.236.152.63' identified by '123456';
mysql> grant FILE on *.* to 'backup'@'10.236.152.63' identified by '123456';
mysql> flush tables with read lock;
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 276
Binlog_Do_DB:
Binlog_Ignore_DB:
mysql> unlock tables;
vim /etc/my.cnf:
# as master
#開啓二進制日誌,並且名稱爲 /var/log/mysql/mysql-bin.***
log-bin=mysql-bin
read-only=0 #主機讀寫權限,讀寫都可以
binlog-do-db=replication_test #記錄日誌的數據庫:需要的備份數據,多個寫多行
binlog-ignore-db=mysql #不記錄日誌的數據庫:不需要備份的數據庫,多個寫多行
binlog-ignore-db=test
binlog-ignore-db=information_schema
# 自增字段偶數遞增,防止衝突(2, 4, 6, ...,)
auto-increment-increment = 2 # 每次遞增的步長
auto-increment-offset = 2 # 初始值
service mysqld restart
進入A服務器
mysql> slave stop;
mysql> CHANGE MASTER TO MASTER_HOST='10.236.152.29', MASTER_USER='backup', MASTER_PASSWORD='123456', MASTER_CONNECT_RETRY=60, MASTER_LOG_FILE='mysql-bin.000002, MASTER_LOG_POS=276';
mysql> slave start;
vim /etc/my.cnf
# as slave
replicate-do-db =replication_test #只複製某個庫,多個寫多行
replicate-ignore-db=mysql #不復制某個庫
replicate-ignore-db=test
replicate-ignore-db=information_schema
relay-log=mysqld-relay-bin # 開啓日誌中繼
log-slave-updates # slave將複製事件寫進自己的二進制日誌
serivce mysqld restart
下面測試一下是否正常
B服務器
mysql> use replication_test;
mysql> insert into test set id=2;
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 187
Binlog_Do_DB: replication_test
Binlog_Ignore_DB: mysql,test,information_schema
1 row in set (0.00 sec)
A服務器
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.236.152.29
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 187
Relay_Log_File: mysqld-relay-bin.000003
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: replication_test
Replicate_Ignore_DB: mysql,test,information_schema
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 187
Relay_Log_Space: 324
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Seconds_Behind_Master: 0
mysql> use replication_test;
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
最後的完整的配置文件
A服務器
# /etc/my.cnf
[mysqld]
# as master
server-id=1 #主機id,整數
#開啓二進制日誌,並且名稱爲 /var/lib/mysql/mysql-bin.***
log-bin=mysql-bin
read-only=0 #主機讀寫權限,讀寫都可以
binlog-do-db=replication_test #記錄日誌的數據庫:需要的備份數據,多個寫多行
# binlog-do-db=replication_test2
binlog-ignore-db=mysql #不記錄日誌的數據庫:不需要備份的數據庫,多個寫多行
binlog-ignore-db=test
binlog-ignore-db=information_schema
# as slave
replicate-do-db =replication_test #只複製某個庫,多個寫多行
replicate-ignore-db=mysql #不復制某個庫
replicate-ignore-db=test
replicate-ignore-db=information_schema
relay-log=mysqld-relay-bin # 開啓日誌中繼
log-slave-updates # slave將複製事件寫進自己的二進制日誌
# 自增字段奇數遞增,防止衝突(1, 3, 5, ...,)
auto-increment-increment = 2 # 每次遞增的步長
auto-increment-offset = 1 # 初始值
B服務器
# /etc/my.cnf
[mysqld]
# as slave
server-id=2
log-bin=mysql-bin
replicate-do-db =replication_test #只複製某個庫,多個寫多行
replicate-ignore-db=mysql #不復制某個庫
replicate-ignore-db=test
replicate-ignore-db=information_schema
relay-log=mysqld-relay-bin # 開啓日誌中繼
log-slave-updates # 示slave將複製事件寫進自己的二進制日誌
# as master
#開啓二進制日誌,並且名稱爲 /var/lib/mysql/mysql-bin.***
log-bin=mysql-bin
read-only=0 #主機讀寫權限,讀寫都可以
binlog-do-db=replication_test #記錄日誌的數據庫:需要的備份數據,多個寫多行
binlog-ignore-db=mysql #不記錄日誌的數據庫:不需要備份的數據庫,多個寫多行
binlog-ignore-db=test
binlog-ignore-db=information_schema
# 自增字段偶數遞增,防止衝突(2, 4, 6, ...,)
auto-increment-increment = 2 # 每次遞增的步長
auto-increment-offset = 2 # 初始值
總結一下, 開始的時候用了兩個不同版本的機子作爲從機, 結果報2003錯誤, slave_io_running 一直是 connecting 狀態, 找了好久, 才發現主機的端口沒開, 我這裏沒有防火牆, 但是要設置 my.cnf 裏面的bind_address 這裏默認是隻監聽 本機的,註釋掉以後, 就可以監聽到其他機子過來的請求了, 另外如果slave_sql_running 是no的, 那估計是你的同步的數據庫可能不是一個, 他把其它數據庫也同步了, 然後出現了各種寫錯誤, 這個可以在主機裏面添加ignore_db, 然後 重新change master to ..... 注意postion 的位置可能有改變, 所以要重新在master 上 show master status; 然後在slave 上重新執行一下 change master to ...... 問題應該不大,祝大家都能順利完成配置.