MySQL-技術專題-主從複製原理

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"主從複製簡介"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 生產中,爲了解決Mysql的單點故障已經提高MySQL的整體服務性能,一般都會採用"},{"type":"text","marks":[{"type":"strong"}],"text":"「主從複製」"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 比如:在複雜的業務系統中,有一句sql執行後導致鎖表,並且這條sql的的執行時間有比較長,那麼此sql執行的期間導致服務不可用,這樣就會嚴重影響用戶的體驗度。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 主從複製中分爲"},{"type":"text","marks":[{"type":"strong"}],"text":"「主服務器(master)"},{"type":"text","text":"「和」"},{"type":"text","marks":[{"type":"strong"}],"text":"從服務器(slave)」"},{"type":"text","text":","},{"type":"text","marks":[{"type":"strong"}],"text":"「主服務器負責寫,而從服務器負責讀」"},{"type":"text","text":",Mysql的主從複製的過程是一個"},{"type":"text","marks":[{"type":"strong"}],"text":"「異步的過程」"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 這樣讀寫分離的過程能夠是整體的服務性能提高,即使寫操作時間比較長,也不影響讀操作的進行。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"主從複製的原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 首先放一張Mysql主從複製的原理圖,總的來說Mysql的主從複製原理還是比較好理解的,原理非常的簡單。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0d/0d4f0dbbdfc3def676b0abb8c6898462.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":" "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Mysql的主從複製中主要有三個線程:"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"master(binlog dump thread)、slave(I/O thread 、SQL thread)"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":",Master一條線程和Slave中的兩條線程。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong"}],"text":"master(binlog dump thread)"}]},{"type":"text","marks":[{"type":"strong"}],"text":"主要負責Master庫中有數據更新的時候,會按照"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong"}],"text":"binlog"}],"marks":[{"type":"strong"}]},{"type":"text","marks":[{"type":"strong"}],"text":"格式,將更新的事件類型寫入到主庫的"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong"}],"text":"binlog"}],"marks":[{"type":"strong"}]},{"type":"text","marks":[{"type":"strong"}],"text":"文件中"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":" 並且,Master會創建"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"log dump"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"線程通知Slave主庫中存在數據更新,這就是爲什麼主庫的binlog日誌一定要開啓的原因。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"codeinline","content":[{"type":"text","text":"I/O thread"}]},{"type":"text","text":"線程在Slave中創建,該線程用於請求Master,Master會返回binlog的名稱以及當前數據更新的位置、binlog文件位置的副本。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 然後,將"},{"type":"codeinline","content":[{"type":"text","text":"binlog"}]},{"type":"text","text":"保存在 "},{"type":"text","marks":[{"type":"strong"}],"text":"「relay log(中繼日誌)」"},{"type":"text","text":" 中,中繼日誌也是記錄數據更新的信息。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SQL線程也是在Slave中創建的,當Slave檢測到中繼日誌有更新,就會將更新的內容同步到Slave數據庫中,這樣就保證了主從的數據的同步。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 以上就是主從複製的過程,當然,主從複製的過程有不同的策略方式進行數據的同步,主要包含以下幾種:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"「同步策略」"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":":Master會等待所有的Slave都回應後纔會提交,這個主從的同步的性能會嚴重的影響。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"「半同步策略」"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":":Master至少會等待一個Slave迴應後提交。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"「異步策略」"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":":Master不用等待Slave迴應就可以提交。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"「延遲策略」"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":":Slave要落後於Master指定的時間。"}]}]}]},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"對於不同的業務需求,有不同的策略方案,但是一般都會採用最終一致性,不會要求強一致性,畢竟強一致性會嚴重影響性能。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"主從搭建"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 下面我們就來實操搭建主從,使用的是兩臺centos7並且安裝的是Mysql 8來搭建主從,有一臺centos 7然後直接克隆就行了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(1)首先檢查centos 7裏面的Mysql安裝包和依賴包:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"rpm -qa |grep mysql"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 執行後,在我本機上的顯示如下:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/19/192fd0fadf1e2e8591043620a937ad17.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(2)接着可以刪除上面的安裝包和依賴包:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo yum remove mysql*"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(3)繼續檢查一下是否存在Mariadb,若是存在直接刪除Mariadb"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"// 檢查是否存在Mariadb\nrpm -qa |grep mariadb\n// 刪除Mariadb\nsudo rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(4)然後,就是刪除Mysql的配置文件,可以使用下面的命令查找Msqyl配置文件的路徑:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo find / -name mysql"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我本機上的顯示的Mysql配置文件的路徑如下:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2e/2ebb8d2b2ca54d878df1dd4ce61e25cf.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(5)然後,通過下面的命令,將他們逐一刪除:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo rm -rf /usr/lib64/mysql\n......"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(6)接着就開始安裝Mysql 8了,使用wget命令下載Mysql 8的repo源,並且執行安裝:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm\nsudo yum -y install mysql80-community-release-el7-3.noarch.rpm\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝完後會在/etc/yum.repos.d/目錄下生成下面的兩個文件,說明安裝成功了:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"mysql-community.repo\nmysql-community-source.repo\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0c/0c1bd5aba009a5e94fac32a67d49f10d.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"image"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(7)安裝完Mysql8後,接着來更新一下yum源,並且查看yum倉庫中的Mysql:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"// 更新yum源\nyum clean all\nyum makecache\n// 查看yum倉庫中的Mysql\nyum list | grep mysql\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(8)可以查看到倉庫中存在mysql-community-server.x86_64,直接安裝就行了:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo yum -y install mysql-community-server\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(9)接着啓動Mysql,並檢查Mysql的狀態:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"// 啓動Mysql\nsystemctl start mysqld.service\n// 檢查Mysql的狀態\nsystemctl status mysqld\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"確保查看Mysql的狀態是"},{"type":"codeinline","content":[{"type":"text","text":"active(running)"}]},{"type":"text","text":",表示正在運行,並且配置一下Mysql開機啓動:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"systemctl enable mysqld\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/86/8661f9892165563e97f1e2b1e118e277.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"image"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(10)因爲Mysql是新安裝的,所以要修改一下初始密碼,先查看一下初始密碼:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"grep \"password\" /var/log/mysqld.log\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可能找出來有多個,可能是你之前安裝卸載後的文件沒有刪乾淨,這裏你就直接看時間,時間對應你現在的時間,就是你的初始密碼:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e6/e600bbfab88219a4348f716cb44d00e4.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"image"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(11)然後使用初始密碼,登陸數據庫,並且修改密碼:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"mysql -uroot -p\nALTER USER 'root'@'localhost' IDENTIFIED BY '[email protected];\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(12)此時在創建一個可以用於給兩一臺centos連接的用戶,默認的root用戶只有本機才能連接:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"// 創建連接用戶\ncreate user 'test'@'%' identified by 'LDCldc-2020';\n// 並且把防火牆給關了,或者配置一下3306端口\nsystemctl stop firewalld.service;\n// 設置防火牆開機自動關閉\nsystemctl disable firewalld.service;\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(13)測試:到這裏就Mysql的安裝教程就就講完了,可以測試一下,兩臺centos是否可以ping通:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"ping 192.168.163.156\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/48/48d5c17cf67254d25bae4e3922d97209.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"image"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我這裏的兩臺機是可以互通的,"},{"type":"codeinline","content":[{"type":"text","text":"Master:192.168.163.156,Slave:192.168.163.155"}]},{"type":"text","text":",並且Slave使用下面的命令可以登陸到Master的Mysql:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"mysql -u[user] -p[密碼] -h[遠程主機ip]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"確保了這兩項測試成功後,就可以進行下面的主從搭建了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(14)我這裏使用的使用兩臺centos 7的vmware的ip分別是"},{"type":"codeinline","content":[{"type":"text","text":"192.168.163.155(Slave)"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"192.168.163.156(Master)"}]},{"type":"text","text":"作爲測試,首先在192.168.163.156(Master)中創建一個測試庫test:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"// 創建測試庫\ncreate database test default character set utf8mb4 collate utf8mb4_general_ci;\n// 並且授權\ngrant all privileges on test.* to 'test'@'%';\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(15)然後編輯Master中的my.cnf文件,此文件位於/etc/my.cnf,執行下面的sql,並添加下面的信息:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo vi /etc/my.cnf\n\n==========以下是配置文件中的信息=============\n# 配置編碼爲utf8\ncharacter_set_server=utf8mb4\ninit_connect='SET NAMES utf8mb4'\n\n# 配置要給Slave同步的數據庫\nbinlog-do-db=test\n# 不用給Slave同步的數據庫,一般是Mysql自帶的數據庫就不用給Slave同步了\nbinlog-ignore-db=mysql\nbinlog-ignore-db=information_schema\nbinlog-ignore-db=performance_schema\nbinlog-ignore-db=sys\n# 自動清理30天前的log文件\nexpire_logs_days=30\n# 啓用二進制日誌\nlog-bin=mysql-bin\n# Master的id,這個要唯一,唯一是值,在主從中唯一\nserver-id=3\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(16)配置完後重啓Mysql服務,並查看Mysql的log_bin日誌是否啓動成功:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"systemctl restart mysqld\n# 查看log_bin日誌是否啓動成功\nshow variables like '%log_bin%';\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0c/0c87c433f145a96dc7d3978fa046657b.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(17)接着查看Master的狀態:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"show master status;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e2/e2a653a13ea54759e281ba2b55df9354.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"image"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這兩個數據"},{"type":"codeinline","content":[{"type":"text","text":"File"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"Position"}]},{"type":"text","text":"要記住,後面配置Slave的時候要使用到這兩個數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(18)最後登陸Master的數據庫,並創建一個用戶用於同步數據:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"create user 'backup'@'%' IDENTIFIED BY 'LDCldc-2020';\ngrant file on *.* to 'backup'@'%';\nGRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* to 'backup'@'%';\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到這裏Master的配置就配置完了,後面就進行Slave的配置。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(19)在Slave中同樣要創建test數據庫,並且授權給test用戶:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# 創建同步數據的test數據庫\ncreate database test default character set utf8mb4 collate utf8mb4_general_ci;\n# 授權\ngrant all privileges on test.* to 'test'@'%';\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(20)接着編輯Slave中my.cnf文件,同樣是在/etc/my.cnf路徑下,加入如下配置:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# 配置從服務器的ID,唯一的\nserver-id=4\n#加上以下參數可以避免更新不及時,SLAVE 重啓後導致的主從複製出錯。\nread_only = 1\nmaster_info_repository=TABLE\nrelay_log_info_repository=TABLE\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(21)並且重啓Slave中的Mysql服務:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"systemctl restart mysqld\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(22)在Slave中添加Master的信息:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# master_host是Master的ip,master_log_file和master_log_pos就是配置之前查看Master狀態時顯示的File和Position信息\nchange master to master_host='192.168.163.156',master_port=3306,master_user='backup',master_password='LDCldc-2020',master_log_file='mysql-bin.000001',master_log_pos=1513;\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(23)最後查看Slave的狀態:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"show slave status\\G\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2b/2ba2295e20f2b097d1d2d4cf3a0d459c.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"image"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當看到"},{"type":"codeinline","content":[{"type":"text","text":"Slave_IO_Running"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"Slave_SQL_Running"}]},{"type":"text","text":"都是yes的時候,這表示主從配置成功。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「Slave_IO_Running也就是Slave中的IO線程用於請求Master,Slave_SQL_Running時sql線程將中繼日誌中更新日誌同步到Slave數據庫中。」"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是,有時候Slave_IO_Running會爲no,而Slave_SQL_Running爲yes,這時候需要檢查一下原因,因爲我自己初次搭建的時候,也是出現這個問題。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先看重啓一下"},{"type":"codeinline","content":[{"type":"text","text":"Slave"}]},{"type":"text","text":"的MySQL服務:"},{"type":"codeinline","content":[{"type":"text","text":"systemctl restart mysqld"}]},{"type":"text","text":",然後執行:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"stop slave;\nstart slave;\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣就能夠使"},{"type":"codeinline","content":[{"type":"text","text":"Slave_IO_Running"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"Slave_SQL_Running"}]},{"type":"text","text":"顯示都是yes了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(24)最後就是測試了,測試使用的是之前創建的test庫,Master是用來寫的,在Master的test庫中隨機創建一個表,你會發現Slave也會有這個表,插入數據也一樣,都會被同步到Slave中。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"主從面試"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"❝"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Mysql主從有什麼優點?爲什麼要選擇主從?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"❞"}]}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"高性能方面:主從複製通過水平擴展的方式,解決了原來單點故障的問題,並且原來的併發都集中到了一臺Mysql服務器中,現在將單點負載分散到了多臺機器上,實現讀寫分離,不會因爲寫操作過長鎖表而導致讀服務不能進行的問題,提高了服務器的整體性能。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"可靠性方面:主從在對外提供服務的時候,若是主庫掛了,會有通過主從切換,選擇其中的一臺Slave作爲Master;若是Slave掛了,還有其它的Slave提供讀服務,提高了系統的可靠性和穩定性。"}]}]}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"❝"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"若是主從複製,達到了寫性能的瓶頸,你是怎麼解決的呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"❞"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主從模式對於寫少讀多的場景確實非常大的優勢,但是總會寫操作達到瓶頸的時候,導致性能提不上去。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這時候可以在設計上進行解決採用分庫分表的形式,對於業務數據比較大的數據庫可以採用分表,使得數據表的存儲的數據量達到一個合理的狀態。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"也可以採用分庫,按照業務進行劃分,這樣對於單點的寫,就會分成多點的寫,性能方面也就會大大提高。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"❝"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主從複製的過程有數據延遲怎麼辦?導致Slave被讀取到的數據並不是最新數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"❞"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主從複製有不同的複製策略,對於不同的場景的適應性也不同,對於數據的實時性要求很高,要求強一致性,可以採用同步複製策略,但是這樣就會性能就會大打折扣。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"若是主從複製採用異步複製,要求數據最終一致性,性能方面也會好很多。只能說,對於數據延遲的解決方案沒有最好的方案,就看你的業務場景中哪種方案使比較適合的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章