程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> Mysql 主從數據庫同步(centos篇)

Mysql 主從數據庫同步(centos篇)

編輯:MySQL綜合教程

Mysql 主從數據庫同步(centos篇)。本站提示廣大學習愛好者:(Mysql 主從數據庫同步(centos篇))文章只能為提供參考,不一定能成為您想要的結果。以下是Mysql 主從數據庫同步(centos篇)正文


情況:

主辦事器:centos 5.2 mysql 5.1.35 源碼 IP:192.168.1.22 從辦事器:centos 5.2 mysql 5.1.35 源碼  IP:192.168.1.33

設置裝備擺設:

1、主辦事器     1.1、創立一個復制用戶,具有replication slave 權限。 mysql>grant replication slave on *.* to 'repl'@'192.168.1.22' identified by 'repl';     1.2、編纂my.cnf文件 vi /etc/my.cnf
添加
server-id=1
並開啟log-bin二進制日記文件
log-bin=mysql-bin 注:須要把默許的server-id=1去失落     1.3、啟動mysql數據庫 mysqld_safe --user=mysql &     1.4、設置讀鎖 mysql>flush tables with read lock;     1.5、獲得binlog日記文件名和偏移量 mysql>show master status; +------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.0000010 |      106|              |                  |
+------------------+----------+--------------+------------------+     1.6、備份要同步的數據庫 mysqldump test > test.sql     1.7、解鎖 mysql>unlock tables;   2、從辦事器         2.1、編纂my.cnf文件 vi /etc/my.cnf
添加
server-id=2 注:須要把默許的server-id=1去失落     2.2、啟動從數據庫 mysqld_safe --user=mysql &         2.3、對從數據庫停止響應設置 mysql> change master to
        -> master_host='192.168.1.22'
        -> master_user='repl'
        -> master_password='repl'
        -> master_log_file='mysql-bin.0000010'
        -> master_log_pos=106;         2.4、啟動從辦事器slave線程 mysql>start slave; 履行show processlist敕令顯示以下過程: mysql>show processlist\G
*************************** 2. row ***************************
         Id: 2
     User: system user
     Host:    
         db: NULL
Command: Connect
     Time: 2579
    State: Has read all relay log; waiting for the slave I/O thread to update it
     Info: NULL表現slave曾經銜接上master,開端接收並履行日記          2.5、檢查slave線程狀況 mysql>show slave status; *************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.22                   Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.0000010
          Read_Master_Log_Pos: 106
               Relay_Log_File: centos-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.0000010
             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: 106
              Relay_Log_Space: 830
              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:
1 row in set (0.00 sec)  

驗證能否設置裝備擺設准確

在從辦事器上履行 show slave status\G;
Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes 如以上二行同時為Yes 解釋設置裝備擺設勝利
 

測試

1、在主辦事器test數據庫中創立user表 mysql>use test;
mysql>create table user(id int); 2、在從辦事器中檢查user表 mysql>use test;
mysql> show tables like 'user';
+----------------------+
| Tables_in_test(user) |
+----------------------+
| user                 |    
+----------------------+
1 row in set (0.00 sec) 解釋主從數據同步勝利。  

成績?

1.在從數據庫中檢查slave狀況時湧現 The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it) 解釋方從辦事器裡my.cnf中的server-id有雷同的。 處理方法: 修正my.cnf裡的server-id,偏重啟數據庫辦事。my.cnf文件默許有server-id=1   其它解釋 主辦事器my.cnf #binlog-do-db=須要備份的數據庫名,可寫多行
#binlog-ignore-db=不須要備份的數據庫名,可寫多行
從辦事器my.cnf # replicate-do-db=test  須要備份的數據庫名
# replicate-ignore-db=mysql 疏忽的數據庫
# master-connect-retry=60 假如從辦事器發明主辦事器斷失落,從新銜接的時光差(秒) 以下設置也可直接修正my.cnf設置裝備擺設文件 log-bin=mysql-bin
master-host=192.168.1.22 master-user=repl master-password=repl
master-port=3306

主從辦事器同步保護 因為各類緣由,招致主從數據紛歧致,在負載低的時刻,停止手動同步. 在主辦事器上履行   mysql>flush tables with read lock; Query OK,rows affected (0.01 sec) mysql>show master status; +------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.0000011 |      260|              |                  |
+------------------+----------+--------------+------------------+ 在從辦事器上履行 先獲得以後主辦事器的二進制文件名和偏移量,履行敕令使從辦事器與主辦事器同步 mysql>select master_pos_wait('mysql-bin.0000011','260');
+--------------------------------------------------+
| master_pos_wait('mysql-bin.0000011','260')            |
+--------------------------------------------------+
|  0                                                                   |   
+--------------------------------------------------+
1 row in set (0.01 sec) 同步完成後,在主辦事器上履行解鎖 mysql>unlock tables;   切換主從辦事器   當主辦事器湧現毛病時,可將從辦事器當主辦事器來應用.步調以下: 1、包管一切從數據庫都曾經履行了relay log中的全體更新,在從辦事器中履行 stop slave io_thread,用show processlist檢討,檢查狀況能否是Has read all relay log,表現更新完成. mysql>stop slave io_thread;
Query OK,0 affected (0.00 sec)
mysql>show processlist\G; *************************** 2. row ***************************
     Id: 2
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 4757
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL 2、在從辦事器上履行stop slave,reset master敕令,重置成主數據庫 mysql>stop slave;
Query OK,0 affected (0.00 sec)
mysql>reset master;
Query OK,0 affected (0.00 sec)

3、刪除新的主辦事器數據庫目次中的master.info和relay-log.info文件,不然下次重啟時還會依照從辦事器來啟動.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved