1. 背景
專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)臨潭免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。* MySQL Replication默認(rèn)都是異步(asynchronous),當(dāng)主庫(kù)在執(zhí)行完一些事務(wù)后,是不會(huì)管備庫(kù)的進(jìn)度的。如果備庫(kù)不幸落后,而更不幸的是主庫(kù)此時(shí)又出現(xiàn)Crash(例如宕機(jī)),這時(shí)備庫(kù)中的數(shù)據(jù)就是不完整的。簡(jiǎn)而言之,在主庫(kù)發(fā)生故障的時(shí)候,我們無(wú)法使用備庫(kù)來(lái)繼續(xù)提供數(shù)據(jù)一致的服務(wù)了。
* Semi sync Replication(半同步復(fù)制)是在master上提交完成后,再傳送到slave等待ack應(yīng)答,僅僅在一定情況下事務(wù)的已經(jīng)傳遞到一個(gè)slave上,但是并不確保已經(jīng)在備庫(kù)上執(zhí)行完成,會(huì)造成最后一次events的主備不一致。
* lossless replication(無(wú)損復(fù)制)是在master提前過(guò)程中,傳送到slave中等待應(yīng)答。當(dāng)至少一個(gè)slave request bilog后寫(xiě)入到relay-log并flush disk,就返回ack
2. lossless replication傳輸過(guò)程
3. 環(huán)境
* master 實(shí)例環(huán)境
mysql> system cat /etc/redhat-release CentOS release 6.8 (Final) mysql> system ifconfig eth0 | sed -rn '2s#^.*addr:(.*) Bca.*$#\1#gp' 172.18.0.1 mysql> show variables like 'version'; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.7.18-log | +---------------+------------+ 1 row in set (0.00 sec)* slave 實(shí)例環(huán)境
mysql> system cat /etc/redhat-release CentOS release 6.8 (Final) mysql> system ifconfig eth0 | sed -rn '2s#^.*addr:(.*) Bca.*$#\1#gp' 172.18.4.1 mysql> show variables like 'version'; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.7.18-log | +---------------+------------+ 1 row in set (0.00 sec)* master 實(shí)例my.cnf文件
[mysqld] ########basic settings######## # 主從server-id一定要設(shè)置不同 server-id = 110 port = 3306 user = mysql bind_address = 0.0.0.0 character_set_server=utf8mb4 skip_name_resolve = 1 datadir = /data/mysql_data log_error = error.log #######replication settings######## master_info_repository = TABLE relay_log_info_repository = TABLE # MySQL復(fù)制是基于binlog日志的 log_bin = bin.log sync_binlog = 1 log_slave_updates # MySQL binlog格式搭建主從時(shí)必須設(shè)置為row binlog_format = row relay_log = relay.log relay_log_recovery = 1 slave_skip_errors = ddl_exist_errors ######semi sync replication settings######## # 設(shè)置插件目錄路徑 plugin_dir=/usr/local/mysql/lib/plugin # 加載插件 plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" # 開(kāi)啟master semi sync replication rpl_semi_sync_master_enabled = 1 # 開(kāi)啟slave semi sync replication rpl_semi_sync_slave_enabled = 1 # 等待5秒無(wú)ack應(yīng)答自動(dòng)切換為異步模式 rpl_semi_sync_master_timeout = 5000 # 開(kāi)啟lossless replication rpl_semi_sync_master_wait_point= AFTER_SYNC # 至少有1個(gè)slave接收到日志 rpl_semi_sync_master_wait_for_slave_count = 1* slave 實(shí)例my.cnf文件
[mysqld] ########basic settings######## server-id = 210 port = 3306 user = mysql bind_address = 0.0.0.0 character_set_server=utf8mb4 skip_name_resolve = 1 datadir = /data/mysql_data log_error = error.log # slave上開(kāi)啟只讀,避免應(yīng)用誤寫(xiě)導(dǎo)致主從數(shù)據(jù)不一致 read_only = on super_read_only = on #######replication settings######## master_info_repository = TABLE relay_log_info_repository = TABLE log_bin = bin.log sync_binlog = 1 log_slave_updates binlog_format = row relay_log = relay.log relay_log_recovery = 1 binlog_gtid_simple_recovery = 1 slave_skip_errors = ddl_exist_errors ######semi sync replication settings######## plugin_dir=/usr/local/mysql/lib/plugin plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" loose_rpl_semi_sync_master_enabled = 1 loose_rpl_semi_sync_slave_enabled = 1 loose_rpl_semi_sync_master_timeout = 5000 rpl_semi_sync_master_wait_point = AFTER_SYNC rpl_semi_sync_master_wait_for_slave_count = 14. 搭建無(wú)數(shù)據(jù)基于無(wú)損全復(fù)制主從 [ master原來(lái)無(wú)數(shù)據(jù) ]
* Master 創(chuàng)建復(fù)制所使用的用戶(hù) [ 此處ip設(shè)置為slave服務(wù)IP或者% ]
mysql> grant replication slave on *.* to 'rpl'@'172.18.4.1' identified by '123'; Query OK, 0 rows affected, 1 warning (0.00 sec)* master服務(wù)器上查看binlog文件名和日志位置
mysql> show master status; +------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------+----------+--------------+------------------+-------------------+ | bin.000002 | 689 | | | | +------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)* slave服務(wù)器上設(shè)置master信息
未開(kāi)啟slave服務(wù)時(shí),Slave_IO_Running與Slave_SQL_Running狀態(tài)成No
master_log_file 設(shè)置開(kāi)始復(fù)制文件, master_log_pos 開(kāi)始文件復(fù)制點(diǎn)
mysql> show slave status; # 未開(kāi)啟復(fù)制功能時(shí),slave狀態(tài)是空的 Empty set (0.00 sec) mysql> change master to master_host='172.18.0.1',master_user='rpl',master_password='123',master_log_file='bin.000002',master_log_pos=689; Query OK, 0 rows affected, 2 warnings (0.03 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 172.18.0.1 Master_User: rpl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000002 Read_Master_Log_Pos: 689 Relay_Log_File: relay.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: bin.000002 Slave_IO_Running: No Slave_SQL_Running: No 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: 689 Relay_Log_Space: 154 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: NULL 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: 0 Master_UUID: Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: 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)* 開(kāi)啟slave服務(wù),并查看狀態(tài)
正常開(kāi)啟slave服務(wù)后,Slave_IO_Running與Slave_SQL_Running狀態(tài)成Yes
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.18.0.1 Master_User: rpl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000002 Read_Master_Log_Pos: 689 Relay_Log_File: relay.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: bin.000002 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: 689 Relay_Log_Space: 511 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: 110 Master_UUID: d7d5a01b-6ea0-11e7-9773-00163e0432c5 Master_Info_File: mysql.slave_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)* Master上查看Slave連接信息
mysql> show slave hosts; +-----------+------+------+-----------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +-----------+------+------+-----------+--------------------------------------+ | 210 | | 3306 | 110 | 499ecfb3-6ea2-11e7-aec1-00163e028c02 | +-----------+------+------+-----------+--------------------------------------+ 1 row in set (0.00 sec)* Master上操作創(chuàng)建數(shù)據(jù)庫(kù)與表,并插入數(shù)據(jù)
mysql> create database mytest character set utf8mb4; Query OK, 1 row affected (0.01 sec) mysql> use mytest; Database changed mysql> create table users( -> id BIGINT NOT NULL AUTO_INCREMENT, -> name VARCHAR(255) NOT NULL, -> sex ENUM('M', 'F') NOT NULL DEFAULT 'M', -> age INT SIGNED NOT NULL DEFAULT '0', -> PRIMARY KEY (id) -> )ENGINE=INNODB DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected (0.02 sec) mysql> insert into users values(null, 'tom', 'M', 24), (null, 'jak', 'F', 32), (null, 'sea', 'M', 35), (null, 'lisea', 'M', 29); Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from users; +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 24 | | 2 | jak | F | 32 | | 3 | sea | M | 35 | | 4 | lisea | M | 29 | +----+-------+-----+-----+ 4 rows in set (0.00 sec)* Slave上查看
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mytest | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use mytest; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +------------------+ | Tables_in_mytest | +------------------+ | users | +------------------+ 1 row in set (0.00 sec) mysql> select * from users; +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 24 | | 2 | jak | F | 32 | | 3 | sea | M | 35 | | 4 | lisea | M | 29 | +----+-------+-----+-----+ 4 rows in set (0.00 sec)5.搭建有數(shù)據(jù)基于無(wú)損全復(fù)制主從 [ master原來(lái)有數(shù)據(jù) ]
* 查看mytest庫(kù)內(nèi)容
Database changed mysql> show tables; +------------------+ | Tables_in_mytest | +------------------+ | users | +------------------+ 1 row in set (0.00 sec) mysql> select * from users; +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 24 | | 2 | jak | F | 32 | | 3 | sea | M | 35 | | 4 | lisea | M | 29 | +----+-------+-----+-----+ 4 rows in set (0.00 sec)* 使用mysqldump原子導(dǎo)出master庫(kù)數(shù)據(jù),并記錄binlog [ 測(cè)試只有mytest庫(kù) ]
如果有多個(gè)庫(kù),-B參數(shù)后逗號(hào)分隔。
[root@master ~]# mysqldump --single-transaction --master-data -B mytest -uroot -p > mytest.sql Enter password:* 將導(dǎo)出的備份文件mytest.sql傳輸?shù)絪lave
[root@master ~]# scp ./mytest.sql root@172.18.4.1:/root* slave創(chuàng)建相同的數(shù)據(jù)庫(kù),并將備份導(dǎo)入
mysql> create database mytest character set utf8mb4; Query OK, 1 row affected (0.01 sec) [root@slave ~]# mysql -uroot -p mytest < mytest.sql Enter password:* Master 創(chuàng)建復(fù)制所使用的用戶(hù) [ 此處ip設(shè)置為slave服務(wù)IP或者% ]
mysql> grant replication slave on *.* to 'rpl'@'172.18.4.1' identified by '123'; Query OK, 0 rows affected, 1 warning (5.01 sec)* 查看備份文件mytest.sql查看binlog文件名和日志位置
[root@slave ~]# grep 'CHANGE MASTER TO' mytest.sql CHANGE MASTER TO MASTER_LOG_FILE='bin.000002', MASTER_LOG_POS=1575;* slave服務(wù)器上設(shè)置master信息
未開(kāi)啟slave服務(wù)時(shí),Slave_IO_Running與Slave_SQL_Running狀態(tài)成No
mysql> show slave status; # 未開(kāi)啟復(fù)制功能時(shí),slave狀態(tài)是空的 Empty set (0.00 sec) mysql> change master to master_host='172.18.0.1',master_user='rpl',master_password='123',master_log_file='bin.000002',master_log_pos=1575; Query OK, 0 rows affected, 2 warnings (0.02 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 172.18.0.1 Master_User: rpl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000002 Read_Master_Log_Pos: 1575 Relay_Log_File: relay.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: bin.000002 Slave_IO_Running: No Slave_SQL_Running: No 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: 1575 Relay_Log_Space: 154 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: NULL 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: 0 Master_UUID: Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: 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)* 開(kāi)啟slave服務(wù),并查看狀態(tài)
mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.18.0.1 Master_User: rpl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000002 Read_Master_Log_Pos: 1872 Relay_Log_File: relay.000002 Relay_Log_Pos: 611 Relay_Master_Log_File: bin.000002 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: 1872 Relay_Log_Space: 808 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: 110 Master_UUID: d7d5a01b-6ea0-11e7-9773-00163e0432c5 Master_Info_File: mysql.slave_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)* master上mytest庫(kù)數(shù)據(jù)操作
mysql> select * from mytest.users; +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 24 | | 2 | jak | F | 32 | | 3 | sea | M | 35 | | 4 | lisea | M | 29 | +----+-------+-----+-----+ 4 rows in set (0.00 sec) mysql> insert into mytest.users select null, 'test', 'M', 42; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> update mytest.users set name='seasea' where id = 3; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from mytest.users; +----+--------+-----+-----+ | id | name | sex | age | +----+--------+-----+-----+ | 1 | tom | M | 24 | | 2 | jak | F | 32 | | 3 | seasea | M | 35 | | 4 | lisea | M | 29 | | 5 | test | M | 42 | +----+--------+-----+-----+ 5 rows in set (0.00 sec)* slave上查看
mysql> select * from mytest.users; +----+--------+-----+-----+ | id | name | sex | age | +----+--------+-----+-----+ | 1 | tom | M | 24 | | 2 | jak | F | 32 | | 3 | seasea | M | 35 | | 4 | lisea | M | 29 | | 5 | test | M | 42 | +----+--------+-----+-----+ 5 rows in set (0.00 sec)6. 總結(jié)
以需求驅(qū)動(dòng)技術(shù),技術(shù)本身沒(méi)有優(yōu)略之分,只有業(yè)務(wù)之分。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
文章名稱(chēng):MySQL5.7--------基于無(wú)損復(fù)制搭建主從-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://jinyejixie.com/article12/dijjgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、虛擬主機(jī)、網(wǎng)站排名、網(wǎng)站設(shè)計(jì)公司、軟件開(kāi)發(fā)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容