MYSQL?增加從庫方式介紹
一、MySQL主從復(fù)制
常見的主從架構(gòu):
- 一主一從:一個 Master,一個 Slave
- 一主多從:一個 Master,多個 Slave
具體,參考下圖:


實(shí)現(xiàn)細(xì)節(jié)
MySQL 在主從同步時,其底層實(shí)現(xiàn)細(xì)節(jié)又是什么?為此后分析主從延遲原因以及優(yōu)化方案,做好理論準(zhǔn)備。

總結(jié)來說,MySQL 的主從復(fù)制:異步單線程。
Master上 1 個IO線程,負(fù)責(zé)向Slave傳輸binary log(binlog)- Slave上 2 個線程:
IO 線程和執(zhí)行SQL的線程,其中:IO線程:將獲取的日志信息,追加到relay log上;執(zhí)行SQL的線程:檢測到relay log中內(nèi)容有更新,則在Slave上執(zhí)行sql;
特別說明:MySQL 5.6.3 開始支持「
多線程的主從復(fù)制」,一個數(shù)據(jù)庫一個線程,多個數(shù)據(jù)庫可多個線程。
完整的 Master & Slave 之間主從復(fù)制過程:

二、增加一個slave
需求:目前我的master上有一個數(shù)據(jù)庫ucenter,需要為其增加一臺slave(目前有一臺slave)
分析:
- 我這里有一個前一天的4:00的數(shù)據(jù)庫備份。
- 有一臺已存在的ucenter的slave
- 重點(diǎn)是尋找binlog的時間點(diǎn)
操作:
1、在slave機(jī)器上配置slave信息,修改mysql.cfg配置并重啟slave數(shù)據(jù)庫
在[mysqld]中添加
replicate-do-db = ucenter #同步的數(shù)據(jù)庫名字 slave-skip-errors=all #同步是忽略錯誤 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時間戳(重點(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
我的備份時間是2號4點(diǎn)開始,所以應(yīng)該在mysql-bin.000267里查找2號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、在另外一臺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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql 5.7 docker 主從復(fù)制架構(gòu)搭建教程
這篇文章主要為大家詳細(xì)介紹了mysql 5.7 docker 主從復(fù)制架構(gòu)搭建教程,感興趣的小伙伴們可以參考一下2016-07-07
pycharm2017實(shí)現(xiàn)python3.6與mysql的連接
這篇文章主要為大家詳細(xì)介紹了PyCharm連接MySQL數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
在Linux系統(tǒng)的命令行中為MySQL創(chuàng)建用戶的方法
這篇文章主要介紹了在Linux系統(tǒng)的命令行中為MySQL創(chuàng)建用戶的方法,包括對所建用戶的權(quán)限管理,需要的朋友可以參考下2015-06-06
Mysql 自定義隨機(jī)字符串的實(shí)現(xiàn)方法
前段時間接了一個項目,需要用到隨機(jī)字符串,但是mysql的庫函數(shù)沒有直接提供,需要我們自己實(shí)現(xiàn)此功能,下面小編給大家介紹下Mysql 自定義隨機(jī)字符串的實(shí)現(xiàn)方法,需要的朋友參考下吧2016-08-08
mysql如何用sql語句建立學(xué)生課程數(shù)據(jù)庫基本表
這篇文章主要給大家介紹了關(guān)于mysql如何用sql語句建立學(xué)生課程數(shù)據(jù)庫基本表的相關(guān)資料,學(xué)生表是一個常見的數(shù)據(jù)表,用于存儲學(xué)生的個人信息和成績等相關(guān)數(shù)據(jù),文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
MySQL——修改root密碼的4種方法(以windows為例)
本文以windows為例為大家詳細(xì)介紹下MySQL修改root密碼的4種方法,大家可以可以根據(jù)的自己的情況自由選擇,希望對大家有所幫助2013-07-07

