mysql互爲主備

有server1=192.168.0.17 和 server2=192.168.0.18倆臺mysql server
其中;server1主機上配置server-id=1      server2上配置:server-id=2
 
17 Server1上mysql的配置:
linux 內核:
#uname -r
2.6.32-220.el6.x86_64
 
mysql版本:
#mysql -u root -p
Enter password:輸入mysql密碼
mysql> status
--------------
...............................
mysql  Ver 14.14 Distrib 5.5.22, for Linux (x86_64) using  EditLine wrapper  //數據版本號
................................
--------------
//建立mysql同步用戶,只容許bak這個用戶經過192.168.0.18這臺主機上鍊接。
mysql>grant replication slave on *.* to bak@192.168.0.18 identified by 'wdmysql';
 
編輯mysq1主配置文件,在/etc/my.cnf中添加如下內容
 
server-id = 1   #最好是找到這一行,省的手工添加避免衝突,
character_set_server=utf8
log-bin = mysql-bin
binlog-ignore-db = mysql
binlog-do-db = bbsdb
binlog-do-db = phpmyadmin
replicate-do-db = phpmyadmin
binlog-ignore-db = test
# master-host = 192.168.0.18
#master-user = bak
#master-password = wdmysql
#master-port = 3306 
#master-connect-retry = 60
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-do-db = bbsdb
 
//從新啓動mysql server
#server mysql restart
 
18  Server2上mysql的配置:
linux 內核:
#uname -r
2.6.32-220.el6.x86_64
 
mysql版本:
#mysql -u root -p
Enter password:輸入mysql密碼
mysql> status
--------------
...............................
mysql  Ver 14.14 Distrib 5.5.22, for Linux (x86_64) using  EditLine wrapper  //數據版本號
................................
--------------
建立mysql同步用戶,只容許bak這個用戶經過192.168.0.17這臺主機上鍊接。
grant replication slave on *.* to bak@192.168.0.17 identified by 'wdmysql';
 
編輯mysq2主配置文件,在/etc/my.cnf中添加如下內容
 
[mysqld]       #找到這一行,在這個下面添加如下內容;
server-id = 2                               #//最好是找到這一行,省的手工添加衝突,
 
character_set_server=utf8          #這個對本次試驗沒有時間用處,在網上看見有人改mysql字符集報錯,隨便我在這給加上了
 
#master-host = 192.168.0.17         # 指定主服務器IP地址
#master-user = bak                        # 指定在主服務器上能夠進行同步的用戶名
#master-password = wdmysql            # 密碼
#master-port = 3306                         # 同步所用端口
#master-connect-retry = 60               # 斷點重新鏈接時間
replicate-ignore-db = mysql             # 屏蔽對mysql庫的同步
replicate-ignore-db = test            # 屏蔽對test庫的同步
binlog-ignore-db =mysql
replicate-do-db = bbsdb                      # 同步的數據庫的名稱
binlog-do-db = bbsdb                      # 二進制須要同步的數據庫名
replicate-do-db = phpmyadmin                     # 同步的數據庫的名稱
binlog-do-db = phpmyadmin                      # 二進制須要同步的數據庫名
binlog-ignore-db = test  
//上面帶有 "master_" 都被我註釋掉了,由於個人mysql不支持這些參數;
 
而後從新啓動mysql服務,
#server mysql restart
 
//下面的步驟都須要在倆臺mysql上操做一遍;
 
#mysql -u root -p
Enter password: 輸入mysql密碼
..................
mysql> stop slave;
mysql> start slave;
mysql> show slave status\G
 
//主要看這兩行,是否都爲「yes」,若是不是,請查看日誌文件,通常都有提示;
.................................
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
....................................
 
//通常狀況 Slave_IO_Running:會出現NO,Slave_SQL_Running 通常沒事,若是出現如下狀況請執行如下操做;
 
先登陸mysql。執行如下命令。
 
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.0.18
                  Master_User: bak
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 107
               Relay_Log_File: server7-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
              Replicate_Do_DB: phpmyadmin,bbsdb
          Replicate_Ignore_DB: mysql,test
           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: 107
              Relay_Log_Space: 411
              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: NULL
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: 2
1 row in set (0.00 sec)

 
從上面的狀態能夠看到,此時從主庫沒法同步;
而後登陸server1主數據庫,容許從數據庫同步相關數據;
mysql> grant replication slave on *.* to bak @192.168.0.18 identified by 'wdmysql';
 
再次重啓從數據庫,登陸進去查看狀態時,若是仍是不行的話再繼續往下操做。
 
mysql> stop slave;
//先到另外一臺mysql上查看
mysql> show master status;
+------------------+----------+------------------+------------------+
| File             | Position | Binlog_Do_DB     | Binlog_Ignore_DB |
+------------------+----------+------------------+------------------+
| mysql-bin.000003 |      107 | bbsdb,phpmyadmin | mysql,test       |
+------------------+----------+------------------+------------------+
1 row in set (0.00 sec)
 
//在server1上操做
mysql> change master to master_host='192.168.0.18',master_user='bak',master_password='wdmysql',master_log_file='mysql-bin.000003',master_log_pos=107;
 
mysql> start slave;
 
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.18
                  Master_User: bak
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 107
               Relay_Log_File: server7-relay-bin.000003
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: phpmyadmin,bbsdb
          Replicate_Ignore_DB: mysql,test
           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: 107
              Relay_Log_Space: 557
              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: 2
1 row in set (0.00 sec)
 
而後在server2上面再操做一遍,這樣就能夠實現mysql的實時同步了。
能夠在bbs數據庫建立表,或者建立phpmyadmin數據庫上建立表。
而後到另外一臺mysql上查看是否多了一個新表。若是有實驗成功~
相關文章
相關標籤/搜索