mysql 主從

Mysql主備配置

說明

Mysql服務器爲linux系統(如centOS5.8),運行PAVLL軟件發佈包中install.sh 文件後可選擇安裝mysql,安裝版本爲5.5.27。

mysql服務器1:192.168.12.33  下文簡稱A33

mysql服務器2:192.168.1.13   下文簡稱B13

下文中先配置A33做爲主用機數據同步到作爲備用機B13,後配置B13上數據同步到A33,使得A33和B13互爲主備。

一 準備

1,修改A33和B13的iptables服務(防火牆)開放Mysql端口3306允許遠程訪問

  ①修改防火牆配置文件 #vi /etc/sysconfig/iptables 

增加下面一行:-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

②配置後,重新啓動iptable服務# service iptables restart

2,A33和B13均開放mysql遠程互訪問權限,可互相訪問對方mysql程序

①進入mysql
 #mysql -h localhost -u root
    ②賦予任何主機使用root/123456訪問mysql的權限
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION
出於安全考慮一般對特定IP開放權限:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.13'IDENTIFIED BY '123456' WITH GRANT OPTION;
     ③使修改生效
mysql>FLUSH PRIVILEGES;

   3,A33和B13的手動同步database,其中testd:testa,testb,notify ;testc:testc

      ①在A33上,導出database

#mysqldump --user="root" --password=123456 --host=localhost testa > testa.sql;
#mysqldump --user="root" --password=123456 --host=localhost testb > testb.sql;
#mysqldump --user="root" --password=123456 --host=localhost notify > notify.sql;
#mysqldump --user="root" --password=123456 --host=localhost testc > testc.sql;

      ②在B13上,刪除其原來testd和testc使用的database

#mysql --user=root --password=123456 --host=localhost --execute="drop database if exists testa;CREATE DATABASE testa";
#mysql --user=root --password=123456 --host=localhost --execute="drop database if exists testb;CREATE DATABASE testb";
#mysql --user=root --password=123456 --host=localhost --execute="drop database if exists notify;CREATE DATABASE notify";
#mysql --user=root --password=123456 --host=localhost --execute="drop database if exists testc;CREATE DATABASE testc";


      ③將①中導出的文件拷貝到B13上,然後在B13上導入該數據庫文件

#mysql --user=root --password=123456 --host=localhost testa < testa.sql;
#mysql --user=root --password=123456 --host=localhost testb < testb.sql;
#mysql --user=root --password=123456 --host=localhost notify < notify.sql;
#mysql --user=root --password=123456 --host=localhost testc < testc.sql;

          注:運行命令的文件目錄爲存放數據文件的目錄

二 主mysql服務器(如A33)配置

1,進入mysql後創建一個複製用戶,具有replication slave權限.

mysql>grant replication slave on *.* to 'testdbakup'@'192.168.1.13' identified by '123456';

2, 拷貝生成my.cnf文件

#cp /usr/share/mysql/my-medium.cnf    /etc/my.cnf

3,編輯my.cnf文件#vi  /etc/my.cnf  注意以下內容

server-id=1  (默認就有,保持註釋server-id=2)
log-bin=mysql-bin(默認就有)
binlog-do-db=testc (按需增加)
binlog-do-db=testa  (按需增加)
binlog-do-db=testb    (按需增加)
binlog-do-db=notify (按需增加)

保存修改後重啓mysql服務 # service mysql restart

4,

mysql>show master status;記錄下File和position,從服務器的設置裏會用到該參數.
+--------------------------+-------------+---------------------+----------------------------+
| File                       | Position  | Binlog_Do_DB| Binlog_Ignore_DB |
+--------------------------+-------------+---------------------+----------------------------+
| mysql-bin.000004 |      107   | testc                |                               |
+---------------------------+------------+---------------------+----------------------------+

三 備用mysql服務器(如B13)設置

1,拷貝生成my.cnf文件

#cp /usr/share/mysql/my-medium.cnf    /etc/my.cnf

2,編輯my.cnf文件vi /etc/my.cnf含有以下內容

server-id=2 (默認爲註釋,需修改)
把默認的server-id=1註釋掉


3,change master

重啓動mysql數據庫

# service mysql restart
mysql>stop slave;
mysql>change master to master_host='192.168.12.33',master_user='testdbakup',master_password='123456',ma        ster_log_file='mysql-bin.000004',master_log_pos=107 ;
mysql>start slave;

4,檢驗配置成功

①mysql>show processlist\G;
*************************** 3. row ***************************
     Id: 10
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 380
  State: Slave has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
看到以上數據則表示爲進程正常啓動




mysql>show slave status\G;(以下爲正常數據)
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.12.33
                  Master_User: testdbakup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 228
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 374
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 228
              Relay_Log_Space: 534
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)
ERROR: 
No query specified




四 配置備用mysql(如B13)的數據同步到主機mysql(A33)

   如果做該設置,則2個mysql服務器則互爲主備配置,雙向同步數據

在 B13上設置

1,編輯/etc/my.cnf

其中保持server-id=2
按需添加binlog-do-db=testc
binlog-do-db=testa
binlog-do-db=testb
binlog-do-db=notify
保存修改完後重啓mysql服務:# service mysql restart


2,爲A33創建備份用戶並授權

mysql>grant replication slave on *.* to 'testdbakup'@'192.168.12.33' identified by '123456';
3,mysql>show master status;記錄下File和position,A33的設置裏會用到該參數.
+--------------------------+-------------+---------------------+----------------------------+
| File                       | Position  | Binlog_Do_DB| Binlog_Ignore_DB |
+--------------------------+-------------+---------------------+----------------------------+
| mysql-bin.000005 |      107   | testc                |                               |
+---------------------------+------------+---------------------+----------------------------+


在A33上設置:

mysql>stop slave;
mysql>change master to master_host='192.168.1.13',master_user='testdbakup',master_password='123456',master_log_file='mysql-bin.000005',master_log_pos=107 ;
mysql>start slave;(同上)
mysql>show processlist\G;(同上)
mysql>show slave status\G;(同上)


互爲主備配置檢測:

在任一端mysql的testd或testc的database中的表中新增,刪除,修改數據,然後查看另一端mysql對應表的數據是否一致。如是則說明數據同步成功主備設置成功。

五F&Q

1 偶現主備數據庫所在機器重啓後數據庫不同步()

mysql的同步依賴其Slave_IO_Running和Slave_SQL_Running服務,當這兩個服務有任意一個都會導致數據無法同步。據大多數mysql同步案例來看,當mysql所在服務器由於宕機、斷電等因素重啓後,mysql依賴的同步服務可以會出現無法正常運行的情況。手動配置兩個服務運行後,可恢復同步。未同步的歷史數據也會被處理掉。

詳見http://www.jb51.net/article/27220.htm

JIRA上已有問題單(NANGA-377)跟蹤:http://192.168.1.119:8080/browse/NANGA-377







Error Code: 1005 -- there is a wrong primary key reference in your code


表的字符集不一致,主外鍵會失敗


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章