MySQL 主從復制主要用于實現(xiàn)高可用性和備份。在主從復制中,一個 MySQL 實例(稱為主節(jié)點)將其數(shù)據(jù)更改復制到至少一個其他 MySQL 實例(稱為從節(jié)點)上。主要借助于數(shù)據(jù)庫二進制日志binlog進行數(shù)據(jù)的復制。
主從數(shù)據(jù)庫對應的操作系統(tǒng)、數(shù)據(jù)庫版本要一致。
1、主庫配置
設定一個唯一的server ID,開啟二進制日志。serverId默認值是1,最大可取值2^32-1。
配置文件:my.cnf
[mysqld]
#配置serverId
server_id=1
# 開啟 binlog
log-bin=mysql-bin
#配置不需要同步的庫
binlog-ignore-db = mysql
binlog-ignore-db = sys
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
重啟服務
systemctl restart mysqld
2、從庫配置
配置文件my.cnf
[mysqld]
server-id=21
log-bin=mysql-bin
read_only=1
read_only設置數(shù)據(jù)庫為只讀,注意如果用戶有super權限還是可以修改數(shù)據(jù)的。
3、創(chuàng)建復制用戶
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'repl';
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl'@'%';
數(shù)據(jù)復制只需要REPLICATION SLAVE權限就可以了,REPLICATION CLIENT權限是為了能查看復制狀態(tài)。如下面的show master status命令就需要該權限。
4、獲取二進制日志坐標
mysql> SHOW MASTER STATUS \G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 11199
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
File是當前的binlog日志文件,position是當前位置。
5、創(chuàng)建數(shù)據(jù)快照
如果要同步的數(shù)據(jù)庫有存量數(shù)據(jù),先進行同步數(shù)據(jù)
主庫卸數(shù)
mysqldump -uroot -p --master-data db_test > db_test.sql
db_test是要同步的數(shù)據(jù)庫名稱,也可以不指定數(shù)據(jù)庫,使用–all-databases來dump所有的數(shù)據(jù)庫。
–master-data用來在從庫自動生成一條設置master信息的語句。會設置對應的MASTER_LOG_FILE和MASTER_LOG_POS。這樣可以達到不停機備份同步的效果。
例:
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=11199;
在高版本的mysql中,已經(jīng)過時使用–source-data替代。
備庫導入初始數(shù)據(jù)
首先要創(chuàng)建對應的數(shù)據(jù)庫:create database db_test;
mysql -u root -p db_test < db_test.sql
如果是已經(jīng)命令行登錄,可以在命令行直接執(zhí)行:source 數(shù)據(jù)文件來導入初始數(shù)據(jù)。
6、開啟復制
CHANGE MASTER TO MASTER_HOST='192.168.1.101',MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=9026;
這里幾個參數(shù)都很好理解,host,port是主庫的連接地址,user,passowrd是上面主庫上創(chuàng)建的復制用戶。
master_log_file是主庫當前binlog日志文件,master_log_pos指定從哪個位置開始復制。可以通過上面的show master status獲取后面兩個參數(shù)。
另外上面在導出主庫數(shù)據(jù)時也說了,如果mysqldump加上–master-data參數(shù)在備份數(shù)據(jù)文件會自動生成設置后面兩個參數(shù)的語句。這里可以不用CHANGE MASTER命令配置了就。
CHANGE MASTER命令在高版本(8.0.23)中也替換成了CHANGE REPLICATION SOURCE命令。
開始復制
>start slave;
查看復制狀態(tài)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.1.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 11199
Relay_Log_File: 50-relay-bin.000004
Relay_Log_Pos: 1273
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: 11199
Relay_Log_Space: 2246
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: ceeb46c0-c925-11ed-8e74-fa163efb83e8
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica 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:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
復制狀態(tài)后很多信息,這里可以核對下CHANGE MASTER配置的master基本信息是否正確。Slave_IO_Running和Slave_SQL_Running可以查看當前復制線程狀態(tài)。
Read_Master_Log_Pos和Exec_Master_Log_Pos對比,SQL_Delay數(shù)量可以查看當前復制進度是否偶延遲。也可以通過Slave_SQL_Running_State描述信息來看當前復制狀態(tài)。
如果復制過程中有錯誤,Last_SQL_Errno和Last_SQL_Error會有對應的錯誤信息,更詳細的信息記錄在performance_schema.replication_applier_status_by_worker表中。
跳過錯誤
如果從庫在復制過程中發(fā)生了錯誤(某條語句在從庫上執(zhí)行失敗),如手動修改了從庫,導致復制語句無法執(zhí)行,在經(jīng)過具體錯誤信息分析后,可以通過sql_slave_skip_counter參數(shù)來設置跳過當前錯誤。文章來源:http://www.zghlxwxcb.cn/news/detail-828933.html
>stop slave;
>set global sql_slave_skip_counter = 1;
>start slave;
sql_slave_skip_counter參數(shù)設置跳過數(shù)量,start slave開啟復制使用過該變量后會將該參數(shù)重新歸0.文章來源地址http://www.zghlxwxcb.cn/news/detail-828933.html
到了這里,關于mysql 數(shù)據(jù)庫主從復制搭建的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!