中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

不停止MySQL服務增加從庫的兩種方式

2017-11-03    來源:互聯(lián)網(wǎng)

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

image.png 

現(xiàn)在生產(chǎn)環(huán)境MySQL數(shù)據(jù)庫是一主一從,由于業(yè)務量訪問不斷增大,故再增加一臺從庫。前提是不能影響線上業(yè)務使用,也就是說不能重啟MySQL服務,為了避免出現(xiàn)其他情況,選擇在網(wǎng)站訪問量低峰期時間段操作。
一般在線增加從庫有兩種方式,一種是通過mysqldump備份主庫,恢復到從庫,mysqldump是邏輯備份,數(shù)據(jù)量大時,備份速度會很慢,鎖表的時間也會很長。另一種是通過xtrabackup工具備份主庫,恢復到從庫,xtrabackup是物理備份,備份速度快,不鎖表。為什么不鎖表?因為自身會監(jiān)控主庫日志,如果有更新的數(shù)據(jù),就會先寫到一個文件中,然后再回歸到備份文件中,從而保持數(shù)據(jù)一致性。

服務器信息: 
主庫:192.168.18.212(原有)
從庫1192.168.18.213(原有)
從庫2192.168.18.214(新增)
數(shù)據(jù)庫版本:MySQL5.5
存儲引擎:Innodb
測試庫名:weibo

一、mysqldump方式 
MySQL主從是基于binlog日志,所以在安裝好數(shù)據(jù)庫后就要開啟binlog。這樣好處是,一方面可以用binlog恢復數(shù)據(jù)庫,另一方面可以為主從做準備。
原有主庫配置參數(shù)如下:
#vimy.cnf
server-id=1#id要唯一
log-bin=mysql-bin#開啟binlog日志
auto-increment-increment=1#Ubuntu系統(tǒng)中MySQL5.5以后已經(jīng)默認是1
auto-increment-offset=1
slave-skip-errors=all#跳過主從復制出現(xiàn)的錯誤
1. 主庫創(chuàng)建同步賬號
mysql>grantallon*.*to'sync'@'192.168.18.%'identifiedby'sync';
2. 從庫配置MySQL
#vimy.cnf
server-id=3#這個設置3
log-bin=mysql-bin#開啟binlog日志
auto-increment-increment=1#這兩個參數(shù)在Ubuntu系統(tǒng)中MySQL5.5以后都已經(jīng)默認是1
auto-increment-offset=1
slave-skip-errors=all#跳過主從復制出現(xiàn)的錯誤
3. 備份主庫
#mysqldump-uroot-p123--routines--single_transaction--master-data=2--databasesweibo>weibo.sql
參數(shù)說明:
--routines:導出存儲過程和函數(shù)
--single_transaction:導出開始時設置事務隔離狀態(tài),并使用一致性快照開始事務,然后unlock tables;lock-tables是鎖住一張表不能寫操作,直到dump完畢。
--master-data:默認等于1,將dump起始(change master tobinlog點和pos值寫到結(jié)果中,等于2是將change master to寫到結(jié)果中并注釋。
4. 把備份庫拷貝到從庫
#scpweibo.sqlroot@192.168.18.214:/home/root
5. 在主庫創(chuàng)建test_tb表,模擬數(shù)據(jù)庫新增數(shù)據(jù),weibo.sql是沒有的
mysql>createtabletest_tb(idint,namevarchar(30));
6. 從庫導入備份庫
#mysql-uroot-p123-e'createdatabaseweibo;'
#mysql-uroot-p123weibo<weibo.sql
7. 在備份文件weibo.sql查看binlogpos
#head-25weibo.sql
--CHANGEMASTERTOMASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=107;#大概22
8. 從庫設置從這個日志點同步,并啟動
mysql>changemastertomaster_host='192.168.18.212',
->master_user='sync',
->master_password='sync',
->master_log_file='mysql-bin.000001',
->master_log_pos=107;
mysql>startslave;
mysql>showslavestatusG;
ERROR2006(HY000):MySQLserverhasgoneaway
Noconnection.Tryingtoreconnect...
Connectionid:90
Currentdatabase:***NONE***
***************************1.row***************************
Slave_IO_State:Waitingformastertosendevent
Master_Host:192.168.18.212
Master_User:sync
Master_Port:3306
Connect_Retry:60
Master_Log_File:mysql-bin.000001
Read_Master_Log_Pos:358
Relay_Log_File:mysqld-relay-bin.000003
Relay_Log_Pos:504
Relay_Master_Log_File:mysql-bin.000001
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
......
可以看到IOSQL線程均為YES,說明主從配置成功。
9. 從庫查看weibo庫里面的表
mysql>showtables;
+---------------------------+
|Tables_in_weibo|
+---------------------------+
|test_tb|
發(fā)現(xiàn)剛才模擬創(chuàng)建的test_tb表已經(jīng)同步過來!

二、xtrabackup方式(推薦) 
在上面配置基礎上做實驗,先刪除掉從庫配置:
mysql>stopslave;#停止同步
mysql>resetslave;#清除從連接信息
mysql>showslavestatusG;#再查看從狀態(tài),可以看到IOSQL線程都為NO
mysql>dropdatabaseweibo;#刪除weibo
此時,從庫現(xiàn)在和新裝的一樣,繼續(xù)前進!
1. 主庫使用xtrabackup備份
#innobackupex--user=root--password=123./
生成一個以時間為命名的備份目錄:2015-07-01_16-49-43
#ll2015-07-01_16-49-43/
total18480
drwxr-xr-x5rootroot4096Jul116:49./
drwx------4rootroot4096Jul116:49../
-rw-r--r--1rootroot188Jul116:49backup-my.cnf
-rw-r-----1rootroot18874368Jul116:49ibdata1
drwxr-xr-x2rootroot4096Jul116:49mysql/
drwxr-xr-x2rootroot4096Jul116:49performance_schema/
drwxr-xr-x2rootroot12288Jul116:49weibo/
-rw-r--r--1rootroot21Jul116:49xtrabackup_binlog_info
-rw-r-----1rootroot89Jul116:49xtrabackup_checkpoints
-rw-r--r--1rootroot563Jul116:49xtrabackup_info
-rw-r-----1rootroot2560Jul116:49xtrabackup_logfile
2. 把備份目錄拷貝到從庫上
#scp-r2015-07-01_16-49-43root@192.168.18.214:/home/root
3. 從庫上把MySQL服務停掉,刪除datadir目錄,將備份目錄重命名為datadir目錄
#sudorm-rf/var/lib/mysql/
#sudomv2015-07-01_16-49-43//var/lib/mysql
#sudochownmysql.mysql-R/var/lib/mysql
#sudo/etc/init.d/mysqlstart
#ps-ef|grepmysql#查看已經(jīng)正常啟動
mysql88321016:55?00:00:00/usr/sbin/mysqld
4.在主庫創(chuàng)建test_tb2表,模擬數(shù)據(jù)庫新增數(shù)據(jù)
mysql>createtabletest_tb2(idint,namevarchar(30));
5. 從備份目錄中xtrabackup_info文件獲取到binlogpos位置
#cat/var/lib/mysql/xtrabackup_info
uuid=201af9db-1fce-11e5-96b0-525400e4239d
name=
tool_name=innobackupex
tool_command=--user=root--password=..../
tool_version=1.5.1-xtrabackup
ibbackup_version=xtrabackupversion2.2.11basedonMySQLserver5.6.24Linux(x86_64)(revisionid:)
server_version=5.5.43-0ubuntu0.12.04.1-log
start_time=2015-07-0116:49:43
end_time=2015-07-0116:49:46
lock_time=1
binlog_pos=filename'mysql-bin.000001',position429#這個位置
innodb_from_lsn=0
innodb_to_lsn=1598188
partial=N
incremental=N
format=file
compact=N
compressed=N
6. 從庫設置從這個日志點同步,并啟動
mysql>changemastertomaster_host='192.168.18.212',
->master_user='sync',
->master_password='sync',
->master_log_file='mysql-bin.000001',
->master_log_pos=429;
mysql>startslave;
mysql>showslavestatusG;
***************************1.row***************************
Slave_IO_State:Waitingformastertosendevent
Master_Host:192.168.18.212
Master_User:sync
Master_Port:3306
Connect_Retry:60
Master_Log_File:mysql-bin.000001
Read_Master_Log_Pos:539
Relay_Log_File:mysqld-relay-bin.000002
Relay_Log_Pos:363
Relay_Master_Log_File:mysql-bin.000001
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
.....
可以看到IOSQL線程均為YES,說明主從配置成功。
9. 從庫查看weibo庫里面的表
mysql>showtables;
+---------------------------+
|Tables_in_weibo|
+---------------------------+
|test_tb|
|test_tb2|
發(fā)現(xiàn)剛才模擬創(chuàng)建的test_tb2表已經(jīng)同步過來。

西部數(shù)碼數(shù)據(jù)庫服務器:http://bingfeng168.cn/services/server/


標簽: https linux Mysql 服務器 數(shù)據(jù)庫 網(wǎng)站 西部數(shù)碼 選擇

版權(quán)申明:本站文章部分自網(wǎng)絡,如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:網(wǎng)站如何設置才能更安全

下一篇:mysql安裝過程中最易出現(xiàn)問題及解決方案