個人認(rèn)為,90%的公司的數(shù)據(jù)體量和并發(fā)量壓根用不上從服務(wù)器,結(jié)合Redis,一臺性能強(qiáng)勁的云MySQL服務(wù)器,做好日常備份。足夠了。
概念
一個MySQL主(Master)服務(wù)器上的數(shù)據(jù)自動復(fù)制到至少一個的MySQL從(Slave)服務(wù)器的過程,利用bin log,主服務(wù)器上的數(shù)據(jù)更改會被自動地同步到從服務(wù)器,以保持至少兩個服務(wù)器上數(shù)據(jù)的一致性。
注意:客戶端請求MySQL Server,寫主還是寫從,讀主還是讀從,決定權(quán)在客戶端。
解決了什么問題?
- 負(fù)載均衡:一臺機(jī)器讀寫可能扛不住,多個服務(wù)器過來幫忙,主負(fù)責(zé)寫,從負(fù)責(zé)讀。
- 讀寫分離:起到了數(shù)據(jù)備份的作用,雞蛋不要放到一個籃子里。
- 高可用性:一臺服務(wù)器宕機(jī),可切換到另一臺服務(wù)器上,提供繼續(xù)服務(wù)的能力。
缺點
- 增加運維復(fù)雜度。
- 無法保證主從實時通信,可能出現(xiàn)數(shù)據(jù)不一致的情況。
主從通信推還是拉?
拉,通過從機(jī)上配置主機(jī)的IP就能看出來,如果是推,則是主機(jī)上配置從機(jī)IP。
主服務(wù)器主動請求從服務(wù)器或推送,這是推。
從服務(wù)器主動請求主服務(wù)器,這是拉。
配置一主一從
mysql有大量的可選主從的配置,很多不一定能用上,具體可查看MySQL官網(wǎng):https://dev.mysql.com/doc/refman/8.0/en/replication-configuration.html
最好先ping對方的服務(wù)器,能互相ping通,說明可通信。
防火墻開啟3306端口:firewall-cmd --zone=public --add-port=3306/tcp --permanent && systemctl restart firewalld
文章來源:http://www.zghlxwxcb.cn/news/detail-837789.html
主服務(wù)器配置
#主服務(wù)器id
server-id=180
#bin log日志名稱
log-bin=mysql-bin
#需要從機(jī)復(fù)制的數(shù)據(jù)庫名
binlog-do-db=test
從服務(wù)器配置
#從服務(wù)器id
server-id=181
#設(shè)置只讀
read-only=1
mysql5.7及以下版本,在主服務(wù)器上執(zhí)行一下MySQL指令
grant replication slave on *.* to '從服務(wù)器用戶名'@'從服務(wù)器IP' identified by '從服務(wù)器密碼';
mysql8,在主服務(wù)器上執(zhí)行一下MySQL指令
create user '從機(jī)用戶名'@'%' identified by '從機(jī)密碼';
grant replication slave on *.* to '從機(jī)用戶名'@'%';
alter user '從機(jī)用戶名'@'%' identified with mysql_native_password by '從機(jī)密碼';
flush privileges;
主機(jī)執(zhí)行
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000032 | 157 | test | | |
+------------------+----------+--------------+------------------+-------------------+
在從機(jī)執(zhí)行
change master to master_host='主機(jī)IP',master_user='主機(jī)創(chuàng)建的從機(jī)用戶名',master_password='主機(jī)創(chuàng)建的從機(jī)密碼',master_log_file='主機(jī)執(zhí)行show master status的bin log名稱',master_log_pos=主機(jī)執(zhí)行show master status的position值;
在從機(jī)執(zhí)行
start slave;
在從機(jī)執(zhí)行:
show slave status;只要發(fā)現(xiàn)Slave_IO_Running : Yes和Slave_SQL_Running : Yes;就說明配置完成。
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.3.180
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000032
Read_Master_Log_Pos: 157
Relay_Log_File: lnmp-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000032
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: 157
Relay_Log_Space: 535
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: 180
Master_UUID: fbdac062-db17-11ee-9a5f-000c29d1c19b
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:
1 row in set, 1 warning (0.00 sec)
值得一提的是,在主機(jī)的配置上,有一個binlog_format的屬性,用于指定二進(jìn)制日志文件中記錄的事件格式文章來源地址http://www.zghlxwxcb.cn/news/detail-837789.html
- statement:如執(zhí)行now()這種函數(shù)時,從機(jī)會照搬復(fù)制主機(jī)的數(shù)據(jù)。
- row:默認(rèn)值。 如執(zhí)行now()這種函數(shù)時,從機(jī)會獲取自身的數(shù)據(jù),能夠避免由于不確定性造成的錯誤。但可能生成更大的二進(jìn)制日志文件,因為它記錄了每一行數(shù)據(jù)的變化。
- mixed:中庸策略,在某些情況下,它會記錄 SQL 語句,而在另一些情況下會記錄數(shù)據(jù)行更改。
到了這里,關(guān)于簡單搭建MySQL主從復(fù)制的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!