<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jojo's blog--快樂憂傷都與你同在
    為夢(mèng)想而來,為自由而生。 性情若水,風(fēng)起水興,風(fēng)息水止,故時(shí)而激蕩,時(shí)又清平……
    posts - 11,  comments - 30,  trackbacks - 0

    1 首先確定MasterSlave的數(shù)據(jù)庫(kù)版本,Master數(shù)據(jù)庫(kù)的版本不能高于Slave數(shù)據(jù)的版本。

    這里我是使用MySql 5.0.27 作為Master數(shù)據(jù)庫(kù),MySql 6.0.3alpha)作為Slave進(jìn)行測(cè)試。

     

    2:首先修改Master數(shù)據(jù)庫(kù)的配置文件my.ini (windows), /etc/my.cnf(linux)

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    bind-address=173.45.243.171 #master server ipadreess
    port=3306
    set-variable = connect_timeout=10
    set-variable = key_buffer_size=50M
    set-variable = thread_cache_size=950
    set-variable = table_cache=4200
    set-variable = max_connect_errors=999999999
    set-variable = max_connections=800
    set-variable = tmp_table_size=10M
    set-variable = wait_timeout=120
    set-variable = max_write_lock_count=120
    set-variable = query_cache_type=1
    set-variable = query_cache_size=50M
    log-slow-queries
    memlock
    skip-innodb
    skip-locking
    skip-name-resolve

    # Try number of CPU's*2 for thread_concurrency
    thread_concurrency=16

    #########################
    # start master settings #
    #########################
    #binlog-do-db=database
    #binlog-ignore-db=db_name
    set-variable = max_binlog_size=10M
    log-bin=/var/log/mysql/mysqlbinlog/pts1-log-bin
    //使用的二進(jìn)制日志文件名
    log-bin-index=/var/log/mysql/mysqlbinlog/pts1-log-bin.index
    server-id=1
    //服務(wù)器編號(hào)
    #######################
    # end master settings #
    #######################

    [mysql.server]
    user=mysql

    [mysqld_safe]
    log-error=/var/log/mysql/mysqld.log
    pid-file=/var/log/mysql/mysqld.pid


       Slave數(shù)據(jù)庫(kù)的配置文件my.ini, or  my.cnf里添加server-id 項(xiàng)

       eg:[mysqld]

            server-id = 2

    (這里需要理解的是Slave本身也是一個(gè)獨(dú)立的服務(wù)器,它作為‘從數(shù)據(jù)庫(kù)’是從它通過‘主服務(wù)器’日志更新數(shù)據(jù)角度上理解的。可以把 server-id 想象成為IP地址:這些ID標(biāo)識(shí)了整個(gè)同步組合中的每個(gè)服務(wù)器。如果沒有指定 server-id 的值,如果也沒定義 master-host,那么它的值就為1,否則為2。注意,如果沒有設(shè)定 server-id,那么master就會(huì)拒絕所有的slave連接,同時(shí)slave也會(huì)拒絕連接到master上。)


    整個(gè)修改如下:

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    bind-address=173.45.243.96
    #slave server ipadreess
    port=3306
    set-variable = connect_timeout=10
    set-variable = key_buffer_size=50M
    set-variable = thread_cache_size=950
    set-variable = table_cache=4200
    set-variable = max_connect_errors=999999999
    set-variable = max_connections=800
    set-variable = tmp_table_size=10M
    set-variable = wait_timeout=120
    set-variable = max_write_lock_count=120
    set-variable = query_cache_type=1
    set-variable = query_cache_size=50M
    log-slow-queries
    memlock
    skip-innodb
    skip-locking
    skip-name-resolve

    # Try number of CPU's*2 for thread_concurrency
    thread_concurrency=16

    #########################
    # start master settings #
    #########################
    #set-variable = max_binlog_size=10M
    #log-bin=/var/log/mysql/mysqlbinlog/pts2-log-bin
    #log-bin-index=/var/log/mysql/mysqlbinlog/pts2.index
    #server-id=1
    #######################
    # end master settings #
    #######################

    #########################
    # start slave settings #
    #########################
    relay-log-space-limit=50M
    set-variable = max-relay-log-size=10M
    set-variable = report-host=173.45.243.96:3306
    master-host=173.45.243.171
    master-user=repl
    master-password=repl12
    master-port=3306
    master-connect-retry=60
    server-id=1562
    #replicate-do-db=test #let slave only do test db's update
    #######################
    # end slave settings #
    #######################

    [mysql.server]
    user=mysql

    [mysqld_safe]
    log-error=/var/log/mysql/mysqld.log
    pid-file=/var/log/mysql/mysqld.pid

     

    3:修改配置后啟動(dòng)Master數(shù)據(jù)服務(wù)。在Master數(shù)據(jù)庫(kù)上建立一個(gè)用戶,用于Slave數(shù)據(jù)連接以便同步數(shù)據(jù)。一般來說Slave數(shù)據(jù)只用于同步數(shù)據(jù),所以我們?cè)诮⑦@個(gè)用戶時(shí)只授予它REPLICATION SLAVE 權(quán)限。

      eg: GRANT REPLICATION SLAVE ON *.* TO 'repl'@173.45.243.96 IDENTIFIED BY ‘repl12’;

     

    4:在Master數(shù)據(jù)庫(kù)上執(zhí)行 FLUSH TABLES WITH READ LOCK; 命令以刷新數(shù)據(jù)并阻止對(duì)Master數(shù)據(jù)的寫入操作。然后將Master數(shù)據(jù)的data目錄復(fù)制一份覆蓋Slave數(shù)據(jù)庫(kù)的data目錄,這樣MasterSlaver就有了相同的數(shù)據(jù)庫(kù)了。在復(fù)制時(shí)可能不需要同步 mysql 數(shù)據(jù)庫(kù),因?yàn)樵?/span>slave上的權(quán)限表和master不一樣。這時(shí),復(fù)制的時(shí)候要排除它。同時(shí)不能包含任何`master.info~ `relay-log.info` 文件。覆蓋好后執(zhí)行 UNLOCK TABLES; 釋放鎖定。

     

    5:在Master數(shù)據(jù)庫(kù)上執(zhí)行SHOW MASTER STATUS; 查看當(dāng)前Master數(shù)據(jù)庫(kù)上的一些我們將要使用的信息:

     

    File 表示 Master用于記錄更新數(shù)據(jù)操作的日志文件,Position 表示當(dāng)前日志的記錄位置,這也是Slave 需要開始同步數(shù)據(jù)的位置。

     

    6:?jiǎn)?dòng)Slave數(shù)據(jù)庫(kù) 執(zhí)行:(這點(diǎn)連接Master數(shù)據(jù)庫(kù)所要的參數(shù))

    mysql> stop slave;

    mysql> CHANGE MASTER TO

        ->  MASTER_HOST='173.45.243.171',  //Master服務(wù)器地址(if是在本機(jī)上安裝兩個(gè)數(shù)據(jù)庫(kù)的, 則用127.0.0.1)

        ->  MASTER_USER='slaver,  //Slave服務(wù)器更新時(shí)連接Master使用的用戶名

        ->  MASTER_PASSWORD='slaver',  // Slave服務(wù)器更新時(shí)連接Master使用的密碼

        ->  MASTER_LOG_FILE='mysql-bin.000004', //更新操作日志

        ->  MASTER_LOG_POS=837016;   //同步數(shù)據(jù)的開始位置

     

    上面命令執(zhí)行完畢后,執(zhí)行START SLAVE; 命令啟動(dòng)數(shù)據(jù)更新。在Slave 數(shù)據(jù)庫(kù)上執(zhí)行:

    SHOW SLAVE STATUS; 查看從數(shù)據(jù)跟主數(shù)據(jù)庫(kù)的連接狀態(tài)是否正常,如果顯示的信息中

    Slave-IO-Running Slave_SQL_Running 值為 yes,表示用于數(shù)據(jù)同步的 io線程和sql操作線程已經(jīng)成功啟動(dòng)。

    eg:

    mysql> show slave status;
    +----------------------------------+----------------+-------------+-------------+---------------+---------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
    | Slave_IO_State                   | Master_Host    | Master_User | Master_Port | Connect_Retry | Master_Log_File     | Read_Master_Log_Pos | Relay_Log_File          | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master |
    +----------------------------------+----------------+-------------+-------------+---------------+---------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
    | Waiting for master to send event | 173.45.243.171 | root        |        3306 |            60 | pts1-log-bin.000005 |                 839 | mysqld-relay-bin.000005 |           979 | pts1-log-bin.000005   | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 839 |             979 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 |
    +----------------------------------+----------------+-------------+-------------+---------------+---------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
    1 row in set (0.00 sec)


    一般情況下,重新啟動(dòng)SLAVE的MYSQL就可以了。

    如果Slave-IO-Running=No,  重新啟動(dòng)MASTER和SLAVE的MYSQL再試一下。

     

    7:到此已經(jīng)建立MasterSlave數(shù)據(jù)庫(kù)的同步了。你可以在Master數(shù)據(jù)庫(kù)上更新一個(gè)表的數(shù)據(jù),然后查看Slave數(shù)據(jù)庫(kù)上對(duì)應(yīng)表是否做了相應(yīng)的更改。

    注: slave開始同步后,就能在數(shù)據(jù)文件目錄下找到2個(gè)文件 `master.info` `relay-log.info`slave利用這2個(gè)文件來跟蹤處理了多少master的二進(jìn)制日志。master.info 記錄了slave 連接master進(jìn)行數(shù)據(jù)同步的參數(shù),relay-log.info 記錄了slave進(jìn)行數(shù)據(jù)更新使用的中續(xù)日志的的信息。

    Master 與 Slave 數(shù)據(jù)同步后對(duì)性能進(jìn)行粗略的測(cè)試:

    (1). 在Master數(shù)據(jù)庫(kù)上建立一個(gè)新的數(shù)據(jù)庫(kù)testdata,并建立表testdata。建立腳本如下

    CREATE DATABASE `testdata` /*!40100 DEFAULT CHARACTER SET utf8 */;

    DROP TABLE IF EXISTS `testdata`.`testms`;
    CREATE TABLE  `testdata`.`testms` (
      `name` 
    text NOT NULL
    ) ENGINE
    =InnoDB DEFAULT CHARSET=utf8;
    注意:由于上面我們建立的Master和Slave之間的數(shù)據(jù)同步是針對(duì)所有數(shù)據(jù)庫(kù)的,所以當(dāng)你在Master上建立數(shù)據(jù)庫(kù)和表后,Slave 上自動(dòng)也會(huì)建立對(duì)應(yīng)的數(shù)據(jù)庫(kù)和表,如果發(fā)現(xiàn)Slave上沒有對(duì)應(yīng)的數(shù)據(jù)庫(kù)和表則必須檢查是否Master和Slave是否正常同步數(shù)據(jù)。

     

    (2). 測(cè)試代碼如下(Java):

    分別調(diào)用兩個(gè)不同的方法,進(jìn)行兩種類型的測(cè)試



    package com.ckcs.test;
    /**/

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    /** */
    public class TestMS ...

     

    (3).測(cè)試所得數(shù)據(jù):

    1)測(cè)試數(shù)據(jù)比較

    注: 查詢數(shù)據(jù)次數(shù)為 100000; 插入數(shù)據(jù)次數(shù)為1000

                  測(cè)試

    類型

    測(cè)試次數(shù)

    用時(shí)

      對(duì)Master數(shù)據(jù)庫(kù)進(jìn)行插入和查詢操作

    對(duì)Master數(shù)據(jù)庫(kù)進(jìn)行插入操作,對(duì)Slave數(shù)據(jù)庫(kù)進(jìn)行查詢操作

     插入

    查詢

      插入

     查詢

      第一次用時(shí)

    63141

    18172

    66078

    10656

      第二次用時(shí)

    67875

    20109

    68969

    11860

      第三次用時(shí)

    65796

    18265

    65672

    10906

      平均用時(shí)

    65604

    18848

    66906

    11140

                                          (測(cè)試數(shù)據(jù)統(tǒng)計(jì)表)

    雖然數(shù)據(jù)測(cè)試是很粗糙的,但確實(shí)反映出性能的一定改善。

    posted on 2009-02-12 15:51 Blog of JoJo 閱讀(750) 評(píng)論(2)  編輯  收藏 所屬分類: Linux 技術(shù)相關(guān)

    FeedBack:
    # re: 在MySql上實(shí)現(xiàn)Replication(Master 與 Slave 數(shù)據(jù)同步)
    2009-02-24 16:42 | Blog of JoJo
    Re: Could not initialize master info structure, more error messages can be found in the MySQL error log

    Login to mysql, then perform these steps:
    1.)mysql> reset slave;
    2.)mysql> start slave;

    Done.

    http://forums.mysql.com/read.php?26,163227,220616#msg-220616  回復(fù)  更多評(píng)論
      
    # re: 在MySql上實(shí)現(xiàn)Replication(Master 與 Slave 數(shù)據(jù)同步)
    2009-05-21 18:51 | Blog of JoJo
    By the way, if still can not work, pls note to stop iptable
    /etc/init.d/iptables stop

    vi /etc/selinux/config

    #SELINUX=enforcing
    SELINUX=disabled  回復(fù)  更多評(píng)論
      

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章分類

    文章檔案

    新聞分類

    新聞檔案

    相冊(cè)

    收藏夾

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 在线人成免费视频69国产| av永久免费网站在线观看| 国产好大好硬好爽免费不卡| 免费h片在线观看网址最新| 深夜国产福利99亚洲视频| 在线人成免费视频69国产| 久久精品网站免费观看| 久久精品国产精品亚洲人人 | 男女一边桶一边摸一边脱视频免费| 最近最新高清免费中文字幕| 免费欧洲毛片A级视频无风险| 亚洲欧洲第一a在线观看| 国产精品亚洲а∨无码播放不卡 | jzzijzzij在线观看亚洲熟妇| 中文字幕久精品免费视频| 国产成人无码区免费A∨视频网站| 亚洲高清国产AV拍精品青青草原| 亚洲aⅴ无码专区在线观看春色| 午夜老司机永久免费看片| 可以免费观看一级毛片黄a| 亚洲国产成人久久综合一区| a一级毛片免费高清在线| 思思99re66在线精品免费观看| 亚洲成AV人片在线观看WWW| 国产天堂亚洲国产碰碰| 无码国产精品一区二区免费| 亚洲国产精华液网站w| 羞羞视频网站免费入口| 毛片a级毛片免费观看品善网| 国产女高清在线看免费观看| 亚洲最大黄色网址| a成人毛片免费观看| 又色又污又黄无遮挡的免费视| 亚洲人成网站在线观看播放青青| 在线看片免费人成视频久网下载| 波多野结衣中文一区二区免费| 国产成人亚洲综合一区| 成人免费视频69| 亚洲国产精品人久久| 中文字幕久精品免费视频| 久久久久亚洲AV无码专区网站|