A、環境描述
服務器A(主) 192.168.0.105
服務器B(主) 192.168.0.108
Mysql版本: 5.6.21
System OS:CentOS release 6.5
主從需同步的數據庫內容保持一致。
B、主主配置過程
(1)創建同步用戶
在主服務器上為從服務器建立一個連接帳戶,該帳戶必須授予REPLICAITON SLAVE權限。 服務器A和服務器B互為主從,所以都要分別建立一個同步用戶
mysql> grant replication client,replication slave on *.* to 'repluser'@'192.168.0.%' identified by '123456';
Query OK, 0 rows affected (0.22 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.24 sec)
(2)修改mysql配置文件
服務器A
[mysqld]
server-id = 1
log-bin=mysql-bin-1
#binlog-do-db = test #需要同步的庫
#binlog-ignore-db=mysql #忽略不同步的庫
#主主需加入的部分
log-slave-updates
sync_binlog=1
auto_increment_offset=1
auto_increment_increment=2
#replicate-do-db = test
#replicate-ignore-db = mysql,information_schema
服務器B
[mysqld]
server-id = 2
log-bin = mysql-bin-2
#replicate-do-db = test
#replicate-ignore-db = mysql,information_schema
#主主需要加入部分
#binlog-do-db = test
#binlog-ignore-db=mysql
log-slave-updates
sync_binlog=1
auto_increment_offset=2
auto_increment_increment=2
(3)配置參數說明
server-id:ID值唯一的標識了復制群集中的主從服務器,因此它們必須各不相同。master_id必須為1到232–1之間的一個正整數值,slave_id值必須為2到232–1之間的一個正整數值。
log-bin:表示打開binlog,打開該選項才可以通過I/O寫到Slave的relay-log,也是可以進行replication的前提;
binlog-do-db:表示需要記錄進制日志的數據庫。如果有多個數據庫可用逗號分隔,或者使用多個binlog-do-db選項
binlog-ignore-db:表示不需要記錄二進制日志的數據庫。如果有多個數據庫可用逗號分隔,或者使用多個binlog-do-db選項
replicate-do-db:表示需要同步的數據庫,如果有多個數據庫可用逗號分隔,或者使用多個replicate-do-db選項
replicate-ignore-db=mysql:表示不需要同步的數據庫,如果有多個數據庫可用逗號分隔,或者使用多個replicate-ignore-db=mysql選項
log-slave-updates:配置從庫上的更新操作是否寫入二進制文件,如果這臺從庫,還要做其他從庫的主庫,那么就需要打這個參數,以便從庫的從庫能夠進行日志同步
slave-skip-errors:在復制過程,由于各種原因導致binlog中的sql出錯,默認情況下,從庫會停止復制,要用戶介入。可以設置Slave-skip-errors來定義錯誤號,如果復制過程中遇到的錯誤號是定義的錯誤號,便可以跳過。如果從庫是用來做備份,設置這個參數會存在數據不一致,不要使用。如果是分擔主庫的查詢壓力,可以考慮。
sync_binlog=1 or N:sync_binlog的默認值是0,這種模式下,MySQL不會同步到磁盤中去。這樣的話,MySQL依賴操作系統來刷新二進制日志binary log,就像操作系統刷其他文件的機制一樣。因此如果操作系統或機器(不僅僅是MySQL服務器)崩潰,有可能binlog中最后的語句丟失了。
要想防止這種情況,你可以使用sync_binlog全局變量,使binlog在每N次binlog寫入后與硬盤同步。當sync_binlog變量設置為1是最安全的,因為在crash崩潰的情況下,你的二進制日志binary log只有可能丟失最多一個語句或者一個事務。但是,這也是最慢的一種方式(除非磁盤有使用帶蓄電池后備電源的緩存cache,使得同步到磁盤的操作非常快)。
即使sync_binlog設置為1,出現崩潰時,也有可能表內容和binlog內容之間存在不一致性。
auto_increment_offset和auto_increment_increment:auto_increment_increment和auto_increment_offset用于主-主服務器(master-to-master)復制,并可以用來控制auto_increment列的操作。兩個變量均可以設置為全局或局部變量,并且假定每個值都可以為1到65,535之間的整數值。將其中一個變量設置為0會使該變量為1。
這兩個變量影響auto_increment列的方式:auto_increment_increment控制列中的值的增量值,auto_increment_offset確定auto_increment列值的起點。
如果auto_increment_offset的值大于auto_increment_increment的值,則auto_increment_offset的值被忽略。
(4)服務器A和服務器B分別啟動mysql服務
服務器A:
[
root@shenma
?~]#?service?mysqld?restart
Shutting?down?MySQL.....?SUCCESS!?
Starting?MySQL.........................?SUCCESS!?
服務器B:
[
root@shenma1
?~]#?service?mysqld?restart
Shutting?down?MySQL.....?SUCCESS!?
Starting?MySQL...........?SUCCESS!
(5)分別查看主備服務器狀態
服務器A:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.03 sec)
注:這里鎖表的目的是為了生產環境中不讓進新的數據,好讓從服務器定位同步位置。同步完成后,記得解鎖
mysql>
unlock tables;
mysql> show master status \G;
*************************** 1. row ***************************
File: mysql-bin-1.000001
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
?
服務器B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.07 sec)
mysql> show master status\G
*************************** 1. row ***************************
? ? ? ? ? ? ?File: mysql-bin-2.000001
? ? ? ? ?Position: 120
? ? ?Binlog_Do_DB:
?Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.07 sec)
(6) 分別在服務器A、B上用change master語句指定同步位置
服務器A:
mysql> change master to
? ? -> master_host='192.168.0.108',
? ? -> master_user='repluser',
? ? -> master_password='123456',
? ? -> master_log_file='mysql-bin-2.000001',
? ? -> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.84 sec)
服務器B:
mysql> change master to
? ? -> master_host='192.168.0.105',
? ? -> master_user='repluser',
? ? -> master_password='123456',
? ? -> master_log_file='mysql-bin-1.000001',
? ? -> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.11 sec)
(7)分別在服務器A,B上啟動從服務器線程
服務器A:
mysql> start slave;
Query OK, 0 rows affected (0.23 sec)
服務器B:
mysql> start slave;
Query OK, 0 rows affected (0.23 sec)
(8)分別在服務器A,B查看從服務器狀態
服務器A:
mysql> show slave status\G;
*************************** 1. row ***************************
? ? ? ? ? ? ? ?Slave_IO_State: Waiting for master to send event
? ? ? ? ? ? ? ? ? Master_Host: 192.168.0.108
? ? ? ? ? ? ? ? ? Master_User: repluser
? ? ? ? ? ? ? ? ? Master_Port: 3306
? ? ? ? ? ? ? ? Connect_Retry: 60
? ? ? ? ? ? ? Master_Log_File: mysql-bin-2.000001
? ? ? ? ? Read_Master_Log_Pos: 120
? ? ? ? ? ? ? ?Relay_Log_File: shenma-relay-bin.000002
? ? ? ? ? ? ? ? Relay_Log_Pos: 285
? ? ? ? Relay_Master_Log_File: mysql-bin-2.000001
? ? ? ? ?? ? ?Slave_IO_Running: Yes
? ? ? ? ? ? Slave_SQL_Running: Yes
服務器B:
mysql> show slave status\G;
*************************** 1. row ***************************
? ? ? ? ? ? ? ?Slave_IO_State: Waiting for master to send event
? ? ? ? ? ? ? ? ? Master_Host: 192.168.0.105
? ? ? ? ? ? ? ? ? Master_User: repluser
? ? ? ? ? ? ? ? ? Master_Port: 3306
? ? ? ? ? ? ? ? Connect_Retry: 60
? ? ? ? ? ? ? Master_Log_File: mysql-bin-1.000001
? ? ? ? ? Read_Master_Log_Pos: 120
? ? ? ? ? ? ? ?Relay_Log_File: shenma1-relay-bin.000002
? ? ? ? ? ? ? ? Relay_Log_Pos: 285
? ? ? ? Relay_Master_Log_File: mysql-bin-1.000001
? ? ? ? ? ? ??Slave_IO_Running: Yes
? ? ? ? ? ? Slave_SQL_Running: Yes
(9)測試主主同步
服務器A:創建數據庫wql
mysql> create database wql;
Query OK, 1 row affected (0.06 sec)
mysql> show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql ? ? ? ? ? ? ?|
| performance_schema |
| test ? ? ? ? ? ? ? |
| wordpress ? ? ? ? ?|
| wql ? ? ? ? ? ? ? ?|
+--------------------+
6 rows in set (0.00 sec)
服務器B:查看是否同步wql數據庫
mysql> show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql ? ? ? ? ? ? ?|
| performance_schema |
| test ? ? ? ? ? ? ? |
| wql ? ? ? ? ? ? ? ?|
+--------------------+
5 rows in set (0.07 sec)
(10)雙向測試
服務器B:創建shenma庫
mysql> create database shenma;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;?
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql ? ? ? ? ? ? ?|
| performance_schema |
|? shenma ?? ? ? ? ? ? |
| test ? ? ? ? ? ? ? |
| wql ? ? ? ? ? ? ? ?|
+--------------------+
6 rows in set (0.00 sec)
服務器A:查看shenma庫是否同步
mysql> show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql ? ? ? ? ? ? ?|
| performance_schema |
| shenma ? ? ? ? ? ? |
| test ? ? ? ? ? ? ? |
| wordpress ? ? ? ? ?|
| wql ? ? ? ? ? ? ? ?|
+--------------------+
7 rows in set (0.00 sec) 務器的tmysql> show databases;?
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql ? ? ? ? ? ? ?|
| performance_schema |
| shenma ? ? ? ? ? ? |
| test ? ? ? ? ? ? ? |
| wordpress ? ? ? ? ?|
| wql ? ? ? ? ? ? ? ?|
+--------------------+
7 rows in set (0.00 sec) ret gdf g
?
?
聯系方式QQ:1486483698
QQ交流群:431392633
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
