藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 數 據 庫


    

藍森林 http://www.lslnet.com 2006年8月26日 15:18


配置Mysql5.0雙機同步

平台:Redhat AS4.0  Mysql-max-5.0.16-linux-i686 (原創)
作者:jiang313hua         MSN:jiang313hua@hotmail.com
歡迎大家指出錯誤! 歡迎轉載!
本文經過作者測試過.

要求:
1.首先裝好系統Redhat AS4.0 ;

2.Mysql-max-5.0.16-linux-i686解壓縮不需要編譯,直接安裝就可以:
   shell> groupadd mysql
   shell> useradd -g mysql mysql
   shell> cd /usr/local
   shell> tar zxvf  /data/soft/mysql-max-5.0.16-linux-i686-glibc23.tar.gz
   shell> ln -s mysql-max-5.0.16-linux-i686-glibc23  mysql
   shell> cd mysql
   shell> scripts/mysql_install_db --user=mysql
   shell> chown -R root  .
   shell> chown -R mysql data
   shell> chgrp -R mysql .
   shell> bin/mysqld_safe --user=mysql &
具體的操作在其包裡的INSTALL-BINARY文件中;

3.如果設置了iptables,請將3306端口打開;

4.兩台服務器的ip:192.168.1.56
                 192.168.1.57

具體過程:
1. 建立需要同步的用戶和需要同步的數據;登陸到192.168.1.56服務器的數據庫上:
    shell>mysql -uroot -p123456
    mysql>CREATE DATABASE bak;      //建立數據庫
    mysql>GRANT REPLICATION SLAVE ON *.* TO tongbu@'192.168.1.57' DENTIFIED  BY '123456';     //建立同步的mysql帳號           

                        tongbu,密碼為123456,指定只能從192.168.1.57上使用tongbu登陸,並可同步mysql中任意庫
    mysql>flush privileges;

2. 登陸到192.168.1.57服務器的數據庫上:
    shell>mysql -uroot -p123456
    mysql>CREATE DATABASE bak;
    mysql>GRANT REPLICATION SLAVE ON *.* TO tongbu@'192.168.1.56' DENTIFIED  BY '123456';
    mysql>flush privileges;

3. 把mysql/support-files下面的mysql配置文件複製到/etc目錄下,
   # cp /usr/local/mysql/support-files/my-large.cnf   /etc/my.cnf
    其中.cnf文檔如下:
     my-small.cnf     內存少於或等於64M,只提供很少的的數據庫服務
     my-medium.cnf    內存在32M--64M之間而且和其他服務一起使用,例如web
     my-large.cnf     內存有512M主要提供數據庫服務
     my-huge.cnf      內存有1G到2G,主要提供數據庫服務
  
4. 修改192.168.1.56上的my.cnf文件,在這裡我們把它命名為1號服務器
     #  vi /etc/my.cnf         
      在[mysqld]添加一下內容:
       log-bin=mysql-bin
       master-host=192.168.157         
       master-user=tongbu               
       master-password=123456           
       master-port=3306                 
       binlog-do-db=bak                 

   因為mysql默認是作為主服務器,因此不需要指定 server-id =1 這項

5. 修改192.168.1.57上的my.cnf文件,在這裡我們把它命名為2號服務器
   #  vi /etc/my.cnf  ,首先在my.cnf文件中找到 server-id這一項,然後修改成:
       server-id =2
      在[mysqld]添加一下內容:
      master-host=192.168.1.56
      master-user=tongbu
      master-password=123456
      master-port   =3306
      master-connect-retry=30
      replicate-do-db=bak
      log-bin
      binlog-do-db=bak

    解釋:
     master-host=192.168.1.57 表示本機做1號服務器時的master為192.168.1.57;
     master-user=username   這裡表示2號服務器上開放的一個有權限的用戶,使其可以從1號機器連接到2號機器並進行複製;
     master-password=password 表示授權用戶的密碼;
     master-port=3306  master上MySQL服務Listen3306端口;
     master-connect-retry=30  同步間隔時間30秒;
     replicate-do-db=bak   表示同步bak數據庫;
     log-bin               打開logbin選項以能寫到slave的 I/O線程;
     binlog-do-db=bak    表示別的機器可以同步本機的bak數據庫.

  
6. 為兩個數據庫中的bak數據庫建立或者導入相同的表。

7.  重新啟動Mysql.
   # /usr/local/mysql/support-files/mysql.server restart

8.這一步非常關鍵,致是我在這裡走了很多冤枉路,不知所措!
    登陸到192.168.1.56 數據庫:
    mysql>CHANGE MASTER TO
          MASTER_HOST='192.168.1.57',
         MASTER_USER='tongbu',
         MASTER_PASSWORD='123456'

   登陸到192.168.1.57 數據庫:

        mysql>CHANGE MASTER TO
          MASTER_HOST='192.168.1.56',
          MASTER_USER='tongbu',
          MASTER_PASSWORD='123456'


  在192.168.1.56查看mysql:
  mysql> show processlist;
    +----+-------------+--------------------+------+-------------+------+---------------------------------------------------

--------------------+------------------+
| Id | User        | Host               | db   | Command     | Time | State                                                   

              | Info             |
+----+-------------+--------------------+------+-------------+------+-------------------------------------------------------

----------------+------------------+
|  1 | system user |                    | NULL | Connect     |   71 | Waiting for master to send event                        

              | NULL             |
|  2 | system user |                    | NULL | Connect     | 1150 | Has read all relay log; waiting for the slave I/O

thread to update it | NULL             |
|  3 | root        | localhost          | NULL | Query       |    0 | NULL                                                   

              | show processlist |
|  4 | tongbu   | 192.168.1.57:32787 | NULL | Binlog Dump |   16 | Has sent all binlog to slave; waiting for binlog to be

updated        | NULL             |
+----+-------------+--------------------+------+-------------+------+-------------------------------------------------------

----------------+------------------+
4 rows in set (0.00 sec)

   在192.168.1.57查看mysql:
          mysql> show processlist;
+----+-------------+--------------------+------+-------------+------+-------------------------------------------------------

----------------+------------------+
| Id | User        | Host               | db   | Command     | Time | State                                                   

              | Info             |
+----+-------------+--------------------+------+-------------+------+-------------------------------------------------------

----------------+------------------+
|  1 | system user |                    | NULL | Connect     |   37 | Connecting to master                                    

              | NULL             |
|  2 | system user |                    | NULL | Connect     |   37 | Has read all relay log; waiting for the slave I/O

thread to update it | NULL             |
|  3 | tongbu      | 192.168.1.56:32829 | NULL | Binlog Dump |   33 | Has sent all binlog to slave; waiting for binlog to be

updated        | NULL             |
|  4 | root        | localhost          | NULL | Query       |    0 | NULL                                                   

              | show processlist |
+----+-------------+--------------------+------+-------------+------+-------------------------------------------------------

----------------+------------------+
4 rows in set (0.00 sec)



   如果看到這樣的信息,說明雙向同步成功了,否則檢查以上步驟!

9.現在可以在bak數據庫中,插入記錄。檢查同步情況!



參考文檔:
         Mysql官方文檔
        《做了篇關於Mysql replication的文檔,歡迎大家糾錯....》  http://www.www.lslnet.com/linux  作者:雙眼皮的豬


感謝!∼!∼



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com