主從復(fù)制功能通過(guò)在主服務(wù)器和從服務(wù)器之間切分處理客戶查詢的負(fù)荷,可以得到更好的客戶響應(yīng)時(shí)間,SELECT查詢可以發(fā)送到從服務(wù)器,以降低主服務(wù)器 的查詢處理速度,修改數(shù)據(jù)的語(yǔ)句發(fā)送到主服務(wù)器,使主從服務(wù)器保持同步。
一、工作原理
主從復(fù)制通過(guò)3個(gè)過(guò)程實(shí)現(xiàn),其中一個(gè)發(fā)生在主服務(wù)器,其他兩個(gè)發(fā)生在從服務(wù)器。
- 1. 主服務(wù)器將用戶對(duì)數(shù)據(jù)庫(kù)的更新操作以二進(jìn)制格式保存到Binary Log日志文件中,然后通過(guò)Binlog Dump線程將Binary Log日志文件傳輸給從服務(wù)器。
- 2. 從服務(wù)器通過(guò)一個(gè)I/O線程將主服務(wù)器 的Binary Log日志文件中的更新操作復(fù)制到一個(gè)叫Relay Log的中繼日志文件中。
- 3. 從服務(wù)器通過(guò)另一個(gè)SQL線程將Relay Log中繼日志文件中的操作依次在本地執(zhí)行,從而實(shí)現(xiàn)主從同步復(fù)制。
二、如何配置
假定有兩臺(tái)服務(wù)器,方案如下
master: 192.168.1.1 service-id:1
slave : 192.168.1.2 service-id:2 同步賬號(hào):sync 同步密碼:sync
1. 確定主從服務(wù)器的MySQL版本
不同版本的binlog格式可能不一樣,最好用同版本,最起碼確保主服務(wù)器版本不高于從服務(wù)器版本。
2. 配置主服務(wù)器
2.1 創(chuàng)建從庫(kù)連接賬號(hào)
在主服務(wù)器上創(chuàng)建從服務(wù)器連接賬戶sync,該賬戶由指定ip訪問,然后授予REPLICATION SLAVE權(quán)限,如果有多個(gè)從服務(wù)器要?jiǎng)?chuàng)建多個(gè)賬號(hào),也可以把ip改成host,但是這樣不推薦。
mysql> CREATE USER 'sync'@'192.168.1.2' IDENTIFIED BY 'sync_password';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'sync'@'192.168.1.2';
mysql> FLUSH PRIVILEGES;
mysql> exit
2.2 修改mysql配置
打開mysql配置,指定唯一Server ID(一般默認(rèn)存在有,找到確認(rèn)一下即可)
[mysqld]
log-bin = mysql-bin
server-id = 1
2.3. 重啟主服務(wù)器
進(jìn)入mysql命令行運(yùn)行show master status語(yǔ)句查看是否成功,復(fù)制File列的mysql-bin.000007備用。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 3151 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3. 配置從服務(wù)器
3.1 配置mysql
與主服務(wù)器配置類似,配置唯一的Server ID:
[mysqld]
log-bin = mysql-bin
server-id = 2
上這樣是默認(rèn)整個(gè)mysql同步,也可以配置同步指定庫(kù)(多個(gè)庫(kù)就寫多行):
replicate-do-db = up-master
可以指定表,%是通配符,代表app_db庫(kù)中以u(píng)ser開頭的所有表:
replicate-wild-do-table = app_db.user%
3.2 指定主服務(wù)器信息
在從服務(wù)器進(jìn)入mysql命令行,使用CHANGE MASTER TO語(yǔ)句指定主服務(wù)器信息,注意下面的內(nèi)容都是主服務(wù)器的信息:主服務(wù)器host、賬號(hào)、密碼、LOG_FILE、LOG_POS,賬號(hào)密碼在2.1創(chuàng)建,最后2項(xiàng)是從2.3里面復(fù)制:
mysql> CHANGE MASTER TO MASTER_HOST = '192.168.1.1',
-> MASTER_USER = 'sync',
-> MASTER_PASSWORD = 'sync',
-> MASTER_LOG_FILE = 'mysql-bin.000007',
-> MASTER_LOG_POS = 3151;
指定MASTER_LOG_POS為0是希望從日志的開始位置開始讀。
3.3 連接主從服務(wù)器開始同步數(shù)據(jù)
- 執(zhí)行START SLAVE語(yǔ)句開始復(fù)制:
mysql> START SLAVE;
- 查看同步結(jié)果,主要查看Slave_IO_State、Slave_IO_Running和Slave_SQL_Running
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 3151
Relay_Log_File: hecs-222032-relay-bin.000002
Relay_Log_Pos: 1079
Relay_Master_Log_File: mysql-bin.000007
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: 3151
Relay_Log_Space: 1292
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: 33a67c62-7ac3-11ed-b035-fa163e7dd5de
Master_Info_File: /www/server/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
如上Slave_IO_Running和Slave_SQL_Running都為Yes就代表成功了。
至此配置完畢。
注意:從服務(wù)器是通過(guò)讀取主服務(wù)器的二進(jìn)制日志文件實(shí)現(xiàn)自我更新,對(duì)數(shù)據(jù)庫(kù)進(jìn)行的修改操作都應(yīng)該放在主服務(wù)器上執(zhí)行,從服務(wù)器只用來(lái)查詢,這也是所謂的讀寫分離。
錯(cuò)誤2:
Last_IO_Errno: 1130
Last_IO_Error: error connecting to master 'sync@192.168.1.1:3306' - retry-time: 60 retries: 1
解決方案:查看3306端口是否放通,查看sync賬號(hào)是否正確創(chuàng)建,查看是否授權(quán)本機(jī)IP訪問等等。
錯(cuò)誤2:
Last_IO_Errno: 1045
Last_IO_Error: error connecting to master 'sync@192.168.1.1:3306' - retry-time: 60 retries: 1
解決方案:使用 show slave status\G 命令查看,確保MASTER_LOG_FILE和MASTER_LOG_POS 與master保持一致,查看master機(jī)器中指定賬戶的授權(quán)
mysql> show grants for 'sync'@'192.168.1.2';
+-----------------------------------------------------------+
| Grants for sync@192.168.1.2 |
+-----------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'sync'@'192.168.1.2' |
+-----------------------------------------------------------+
1 row in set (0.00 sec)
如果不是跟上面的輸出有出入,建議回到本人第二步正確配置授權(quán)。
PS:主主部署、雙機(jī)熱備只需要按照上述步驟,將server1作為從機(jī),server2作為主機(jī)再操作一遍即可。