MYSQL?增加從庫方式介紹
一、MySQL主從復(fù)制
常見的主從架構(gòu):
- 一主一從:一個(gè) Master,一個(gè) Slave
- 一主多從:一個(gè) Master,多個(gè) Slave
具體,參考下圖:
實(shí)現(xiàn)細(xì)節(jié)
MySQL 在主從同步時(shí),其底層實(shí)現(xiàn)細(xì)節(jié)又是什么?為此后分析主從延遲原因以及優(yōu)化方案,做好理論準(zhǔn)備。
總結(jié)來說,MySQL 的主從復(fù)制:異步單線程。
Master
上 1 個(gè)IO線程
,負(fù)責(zé)向Slave傳輸binary log
(binlog
)- Slave上 2 個(gè)線程:
IO 線程
和執(zhí)行SQL的線程
,其中:IO線程
:將獲取的日志信息,追加到relay log上;執(zhí)行SQL的線程
:檢測(cè)到relay log中內(nèi)容有更新,則在Slave上執(zhí)行sql;
特別說明:MySQL 5.6.3 開始支持「
多線程
的主從復(fù)制
」,一個(gè)數(shù)據(jù)庫
一個(gè)線程
,多個(gè)數(shù)據(jù)庫
可多個(gè)線程
。
完整的 Master & Slave 之間主從復(fù)制過程:
二、增加一個(gè)slave
需求:目前我的master上有一個(gè)數(shù)據(jù)庫ucenter,需要為其增加一臺(tái)slave(目前有一臺(tái)slave)
分析:
- 我這里有一個(gè)前一天的4:00的數(shù)據(jù)庫備份。
- 有一臺(tái)已存在的ucenter的slave
- 重點(diǎn)是尋找binlog的時(shí)間點(diǎn)
操作:
1、在slave機(jī)器上配置slave信息,修改mysql.cfg配置并重啟slave數(shù)據(jù)庫
在[mysqld]中添加
replicate-do-db = ucenter #同步的數(shù)據(jù)庫名字 slave-skip-errors=all #同步是忽略錯(cuò)誤 server-id = 1112 #和master與其他slave保持不通
2、查找數(shù)據(jù)庫前一天的備份,將其copy到新增的slave機(jī)器上,并導(dǎo)庫
[root@ucenter_slave /data]# mysql ucenter < ucenter_20171102.sql[root@ucenter_slave /data]# du -sh ucenter_20171102.sql 24G ucenter_20171102.sql
3、定位binlog時(shí)間戳(重點(diǎn))
在master上查找binlog
[root@Ucenter /data/mysqldata]# ll -t *bin* -rw-rw---- 1 mysql mysql 30709744 Nov 2 21:32 mysql-bin.000268 -rw-rw---- 1 mysql mysql 171 Nov 2 19:41 mysql-bin.index -rw-rw---- 1 mysql mysql 1021101094 Nov 2 19:41 mysql-bin.000267 -rw-rw---- 1 mysql mysql 1073742415 Oct 30 06:07 mysql-bin.000266 -rw-rw---- 1 mysql mysql 1073742062 Oct 26 12:03 mysql-bin.000265 -rw-rw---- 1 mysql mysql 1073742636 Oct 22 19:21 mysql-bin.000264 -rw-rw---- 1 mysql mysql 1073741961 Oct 18 22:49 mysql-bin.000263 -rw-rw---- 1 mysql mysql 1073742141 Oct 15 12:47 mysql-bin.000262 -rw-rw---- 1 mysql mysql 1073742284 Oct 11 10:18 mysql-bin.000261 -rw-rw---- 1 mysql mysql 1073742184 Oct 7 09:49 mysql-bin.000260
我的備份時(shí)間是2號(hào)4點(diǎn)開始,所以應(yīng)該在mysql-bin.000267里查找2號(hào)4點(diǎn)前的pos,開始定位
[root@Ucenter /data/mysqldata]# /usr/local/mysql/bin/mysqlbinlog mysql-bin.000267 |grep '3:59:' |grep -v '13:59:' #171102 3:59:58 server id 21323 end_log_pos 824385760 Query thread_id=3023086382 exec_time=0 error_code=0 #171102 3:59:58 server id 21323 end_log_pos 824386335 Query thread_id=3023086386 exec_time=0 error_code=0 #171102 3:59:58 server id 21323 end_log_pos 824386363 Intvar #171102 3:59:58 server id 21323 end_log_pos 824386698 Query thread_id=3023086386 exec_time=0 error_code=0 #171102 3:59:58 server id 21323 end_log_pos 824387706 Query thread_id=3023086388 exec_time=0 error_code=0 #171102 3:59:58 server id 21323 end_log_pos 824387734 Intvar #171102 3:59:58 server id 21323 end_log_pos 824388209 Query thread_id=3023086388 exec_time=0 error_code=0 #171102 3:59:58 server id 21323 end_log_pos 824388534 Query thread_id=3023086398 exec_time=0 error_code=0
所以定位到最后 end_log_pos 824388534
4、在另外一臺(tái)slave上查看master.info
[root@LeduPass02/data/mysqldata]# cat master.info? 15 mysql-bin.000268 11367810 192.168.100.70 omsync om@123 3306 60 0 0 0
5、在slave 配置slave,進(jìn)行同步
在Slave上配置相關(guān)設(shè)置,以便告訴Slave如何去連接Master,這包括主機(jī)地址,登陸憑證等等
[root@ucenter_slave /data]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.1.51-Community-Server-log Source Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql (none)>change master to master_host='192.168.100.70',master_port=3306,master_user='omsync',master_password='om@123',master_log_file='mysql-bin.000267',master_log_pos=824388534;
開啟slave
mysql (none)>start slave;
查看slave狀態(tài):
完成。
到此這篇關(guān)于MYSQL 增加從庫方式介紹的文章就介紹到這了,更多相關(guān)MYSQL增加從庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
windows系統(tǒng)mysql5.7.18安裝圖文教程
這篇文章主要為大家詳細(xì)介紹了windows系統(tǒng)下mysql5.7.18安裝圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Mysql支持的數(shù)據(jù)類型(列類型總結(jié))
MySQL支持大量的列類型,它可以被分為3類:數(shù)字類型、日期和時(shí)間類型以及字符串(字符)類型。本節(jié)首先給出可用類型的一個(gè)概述,并且總結(jié)每個(gè)列類型的存儲(chǔ)需求,然后提供每個(gè)類中的類型性質(zhì)的更詳細(xì)的描述2016-12-12MySql使用skip-name-resolve解決外網(wǎng)鏈接客戶端過慢問題
在騰訊云上面搭建的mysql使用開發(fā)的電腦上navicat進(jìn)行訪問時(shí)總是特別的慢,原來是Mysql會(huì)對(duì)請(qǐng)求的地址進(jìn)行域名解析,開發(fā)的電腦并沒有域名,所以會(huì)導(dǎo)致特別的慢,下面通過本文給大家分享MySql使用skip-name-resolve解決外網(wǎng)鏈接客戶端過慢問題2017-07-07- 在講MySQL的Join語法前還是先回顧一下聯(lián)結(jié)的語法,呵呵,其實(shí)連我自己都忘得差不多了,那就大家一起溫習(xí)吧,這里我有個(gè)比較簡(jiǎn)便的記憶方法,內(nèi)外聯(lián)結(jié)的區(qū)別是內(nèi)聯(lián)結(jié)將去除所有不符合條件的記錄,而外聯(lián)結(jié)則保留其中部分。外左聯(lián)結(jié)與外右聯(lián)結(jié)的區(qū)別在于如果用A左聯(lián)結(jié)B則A中所有記錄都會(huì)保留在結(jié)果中,此時(shí)B中只有符合聯(lián)結(jié)條件的記錄,而右聯(lián)結(jié)相反,這樣也就不會(huì)混淆了。2014-05-05