MySQL主從複製
- mysql主從複製是一個異步的複製過程,主庫發送更新事件到從庫,從庫讀取更新記錄,並執行更新記錄,使得從庫的內容與主庫保持一致。
實驗環境
主機 | IP地址 | 主要軟件 |
---|---|---|
主服務器 | 192.168.27.128 | mysql5.7.17;ntp |
從服務器1 | 192.168.27.139 | mysql5.7.17;ntp |
從服務器2 | 192.168.27.142 | mysql5.7.17;ntp |
搭建主從複製
1、主服務器建立時間同步環境
systemctl stop firewalld.service
setenforce 0
yum install ntp -y
vim /etc/ntp.cnf
service 127.127.1.0 #本地時鐘源
fudge 127.127.1.0 stratum 8 #設置層級爲8
systemctl start ntp.service #開啓服務
2、兩臺從服務器進行時間同步
systemctl stop firewalld.service
setenforce 0
yum install ntp -y
systemctl start ntp.service
yum install ntpdata -y
/usr/sbin/ntpdata 192.168.27.128 #進行時間同步
3、配合主服務器
vim /etc/my.cnf
server-id = 11
log-bin=master-bin #主服務器二進制日誌
log-slave-updates=true #允許從服務器和主服務器同步
systemctl restart mysqld.service
mysql -u root -p #進入目mysql
grant replication slave on . to 'myslave'@'192.168.27.%'identified by'123456'; #允許192.168.27.0網段的從服務器使用賬號密碼登錄
flush privileges; #更新權限
show master status; #查看主服務器狀態
4、配置從服務器(兩臺從服務器配置相同)
vim /etc/my.cnf
server-id = 22 #兩臺從服務器不同
relay-log=relay-log-bin #開啓中繼日誌
relay-log-index=slave-relay-bin.index #確定中繼位置
systemctl restart mysqld.service
mysql -u root -p
change master to master_host='192.168.27.128',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=603; #配置同步
start slave;
show slave status\G; #查看slave狀態
5、測試
-
在主服務器創建test01數據庫,在從服務器上會同步到;
-
主服務器:
- 從服務器:
![](https://s1.51cto.com/images/blog/201807/11/dd5d22ab2dc551a830eaa8afe32f19de.jpg?x-oss- process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)