環(huán)境
服務器
兩臺 centos6.5 (ip:192.168.1.121钞瀑、192.168.1.212)
軟件
- mysql5.6
- haproxy-1.5.14
- keepalived-1.2.19
步驟
- 在兩臺服務器上分別安裝mysql環(huán)境(完全相同的安裝即可)
- 離線安裝流程參考:https://my.oschina.net/JustLoveIT/blog/499208
- 在線安裝流程:參考 官方文檔 中文文檔
注:本文中的例子使用的mysql5.6努释,目前不確定該配置流程在mysql5.7上是不是好使的魔眨,貌似5.7對主從復制這塊有優(yōu)化暇务,大家可以自行嘗試一下躏救。)
- 配置mysql主主復制(參考 http://www.cnblogs.com/phpstudy2015-6/p/6485819.html#_label7)
何為主主復制:就是兩個mysql都能讀能寫票摇,數(shù)據(jù)記錄通過二進制傳達給對方從而保持數(shù)據(jù)的一致性化焕。
實現(xiàn):
192.168.1.121(主)+192.168.1.212(從) 主從復制 +
192.168.1.212(主)+192.168.1.121(從) 主從復制 =
192.168.1.121+192.168.1.212 主主復制烛亦。
2.1. 修改mysql配置文件
shell> sudo vim /etc/my.cnf
192.168.1.121 mysql配置文件內(nèi)容:
[mysqld]
server-id=1 #任意自然數(shù)n左腔,只要保證兩臺MySQL主機不重復就可以了唧垦。
log-bin=mysql-bin #開啟二進制日志
auto_increment_increment=2 #步進值auto_imcrement。一般有n臺主MySQL就填n
auto_increment_offset=1 #起始值液样。一般填第n臺主MySQL振亮。此時為第一臺主MySQL
binlog-ignore=mysql #忽略mysql庫【我一般都不寫】
binlog-ignore=information_schema #忽略information_schema庫【我一般都不寫】
#replicate-do-db=aa #要同步的數(shù)據(jù)庫,默認所有庫
192.168.1.212 mysql配置文件內(nèi)容:
[mysqld]
#mysql負載均衡配置
server-id=2
log-bin=mysql-bin
auto_increment_increment=2 #步進值auto_imcrement鞭莽。一般有n臺主MySQL就填n
auto_increment_offset=2 #起始值坊秸。一般填第n臺主MySQL。此時為第二臺主MySQL
#replicate-do-db=aa #要同步的數(shù)據(jù)庫澎怒,默認所有庫
兩臺服務器的配置文件修改好后均重啟mysql服務褒搔。
2.2. 配置192.168.1.121(主)+192.168.1.212(從) 主從復制
使用root用戶登錄192.168.1.121主機的mysql,創(chuàng)建一個可以從192.168.1.212登錄的mysql用戶并授權喷面。
mysql> create user 'mysql212'@'192.168.1.212' identified by 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘mysql212’@’192.168.1.121’ IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;
查看192.168.1.121 mysql服務器二進制日志
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000076
Position: 1173222
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
告知從服務器主服務器的二進制文件名和位置
使用root用戶登錄192.168.1.212主機的mysql星瘾,在192.168.1.121中執(zhí)行:
mysql> change master to
-> master_host='192.168.1.121',
-> master_user='mysql212',
-> master_password='password',
-> master_log_file='mysql-bin.000076',
-> master_log_pos=1173222;
192.168.1.212服務器開啟復制:
mysql> start slave;
查看主從復制是否配置成功
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.121
Master_User: mysql212
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000076
Read_Master_Log_Pos: 1173222
Relay_Log_File: bogon-relay-bin.000006
Relay_Log_Pos: 1173385
Relay_Master_Log_File: mysql-bin.000076
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: 1173222
Relay_Log_Space: 1173721
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
Master_UUID: e69cc9bd-109e-11e5-b9e7-00e066233823
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
當看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明狀態(tài)正常惧辈。
2.3. 配置192.168.1.212(主)+192.168.1.121(從) 主從復制
步驟同2.2琳状。
2.4. 測試主主復制配置是否成功。