1. <var id="fe6gj"></var>

    <rp id="fe6gj"><nav id="fe6gj"></nav></rp>

    <noframes id="fe6gj"><cite id="fe6gj"></cite>

    <ins id="fe6gj"><button id="fe6gj"><p id="fe6gj"></p></button></ins>
    1. <tt id="fe6gj"><i id="fe6gj"><sub id="fe6gj"></sub></i></tt>
        億恩科技有限公司旗下門戶資訊平臺!
        服務器租用 4元建網站

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

        現在生產環境MySQL數據庫是一主一從,由于業務量訪問不斷增大,故再增加一臺從庫。前提是不能影響線上業務使用,也就是說不能重啟MySQL服務,為了避免出現其他情況,選擇在網站訪問量低峰期時間段操作。
        不停止MySQL服務增加從庫的兩種方式
        現在生產環境MySQL數據庫是一主一從,由于業務量訪問不斷增大,故再增加一臺從庫。前提是不能影響線上業務使用,也就是說不能重啟MySQL服務,為了避免出現其他情況,選擇在網站訪問量低峰期時間段操作。

        一般在線增加從庫有兩種方式,一種是通過mysqldump備份主庫,恢復到從庫,mysqldump是邏輯備份,數據量大時,備份速度會很慢,鎖表的時間也會很長。另一種是通過xtrabackup工具備份主庫,恢復到從庫,xtrabackup是物理備份,備份速度快,不鎖表。為什么不鎖表?因為自身會監控主庫日志,如果有更新的數據,就會先寫到一個文件中,然后再回歸到備份文件中,從而保持數據一致性。

        服務器信息:


        主庫:192.168.18.212(原有)

        從庫1:192.168.18.213(原有)

        從庫2:192.168.18.214(新增)

        數據庫版本:MySQL5.5

        存儲引擎:Innodb

        測試庫名:weibo

        一、mysqldump方式


        MySQL主從是基于binlog日志,所以在安裝好數據庫后就要開啟binlog。這樣好處是,一方面可以用binlog恢復數據庫,另一方面可以為主從做準備。

        原有主庫配置參數如下:

        #vimy.cnf

        server-id=1#id要唯一

        log-bin=mysql-bin#開啟binlog日志

        auto-increment-increment=1#在Ubuntu系統中MySQL5.5以后已經默認是1

        auto-increment-offset=1

        slave-skip-errors=all#跳過主從復制出現的錯誤

        1. 主庫創建同步賬號

        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#這兩個參數在Ubuntu系統中MySQL5.5以后都已經默認是1

        auto-increment-offset=1

        slave-skip-errors=all#跳過主從復制出現的錯誤

        3. 備份主庫

        #mysqldump-uroot-p123--routines--single_transaction--master-data=2--databasesweibo>weibo.sql

        參數說明:

        --routines:導出存儲過程和函數

        --single_transaction:導出開始時設置事務隔離狀態,并使用一致性快照開始事務,然后unlock tables;而lock-tables是鎖住一張表不能寫操作,直到dump完畢。

        --master-data:默認等于1,將dump起始(change master to)binlog點和pos值寫到結果中,等于2是將change master to寫到結果中并注釋。

        4. 把備份庫拷貝到從庫

        #scpweibo.sqlroot@192.168.18.214:/home/root

        5. 在主庫創建test_tb表,模擬數據庫新增數據,weibo.sql是沒有的

        mysql>createtabletest_tb(idint,namevarchar(30));

        6. 從庫導入備份庫

        #mysql-uroot-p123-e'createdatabaseweibo;'

        #mysql-uroot-p123weibo<weibo.sql

        7. 在備份文件weibo.sql查看binlog和pos值

        #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

        ......

        可以看到IO和SQL線程均為YES,說明主從配置成功。

        9. 從庫查看weibo庫里面的表

        mysql>showtables;

        +---------------------------+

        |Tables_in_weibo|

        +---------------------------+

        |test_tb|

        發現剛才模擬創建的test_tb表已經同步過來!

        二、xtrabackup方式(推薦)


        在上面配置基礎上做實驗,先刪除掉從庫配置:

        mysql>stopslave;#停止同步

        mysql>resetslave;#清除從連接信息

        mysql>showslavestatusG;#再查看從狀態,可以看到IO和SQL線程都為NO

        mysql>dropdatabaseweibo;#刪除weibo庫

        此時,從庫現在和新裝的一樣,繼續前進!

        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#查看已經正常啟動

        mysql88321016:55?00:00:00/usr/sbin/mysqld

        4.在主庫創建test_tb2表,模擬數據庫新增數據

        mysql>createtabletest_tb2(idint,namevarchar(30));

        5. 從備份目錄中xtrabackup_info文件獲取到binlog和pos位置

        #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

        ......

        可以看到IO和SQL線程均為YES,說明主從配置成功。

        9. 從庫查看weibo庫里面的表


        mysql>showtables;

        +---------------------------+

        |Tables_in_weibo|

        +---------------------------+

        |test_tb|

        |test_tb2|

        發現剛才模擬創建的test_tb2表已經同步過來。

        河南億恩科技股份有限公司(www.endtimedelusion.com)始創于2000年,專注服務器托管租用,是國家工信部認定的綜合電信服務運營商。億恩為近五十萬的用戶提供服務器托管、服務器租用、機柜租用、云服務器、網站建設、網站托管等網絡基礎服務,另有網總管、名片俠網絡推廣服務,使得客戶不斷的獲得更大的收益。
        服務器/云主機 24小時售后服務電話:0371-60135900
        虛擬主機/智能建站 24小時售后服務電話:0371-55621053
        網絡版權侵權舉報電話:0371-60135995
        服務熱線:0371-60135900

        0
        0
        分享到:責任編輯:小柳

        相關推介

        共有:0條評論網友評論:

        驗證碼 看不清換一張 換一張

        親,還沒評論呢!速度搶沙發吧!
        av不卡不卡在线观看_最近2018年中文字幕_亚洲欧美一区二区三区_一级A爱做片免费观看国产_日韩在线中文天天更新_伊人中文无码在线