mysql 主從複製

安裝mysql過程略。

服務器環境

操作系統:RHEL 6.4 x64

主服務器IP:192.168.1.251

從服務器IP:192.168.1.252


一:配置主服務器master

# vi /etc/my.cnf

log-bin=mysql.bin

server-id=251

添加上面兩行,第一行是用於開啓二進制日誌記錄,第二行的server-id唯一,這裏最好設置爲服務器的ip地址。


二:配置從服務器slave

# vi /etc/my.cnf

server-id=252

從服務器只需要添加一個這個就行。使用從服務器ip地址來定義,方便區分。


三:啓動主從服務器的mysql


四:操作主服務器master

# mysql -u root -p

mysql> GRANT REPLICATION SLAVE ON *.* to 'sync'@'192.168.1.252' IDENTIFIED BY '123456';

mysql> show slave;

mysql> show master status;
+--------------+----------+--------------+------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000003 |      260 |              |                  |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)

第一步是添加用戶sync,僅允許192.168.1.252從服務器訪問登陸,密碼123456,只能操作從複製權限(replication slave)。

第二步是顯示一下slave的狀態,記錄下log_file名,和log_pos的值,會在從服務器進行使用。


五:從服務器配置slave

# mysql -u root -p

mysql> CHANGE MASTER TO master_host='192.168.1.251',master_user='sync',master_password='123456',master_log_file='mysql.000003',master_log_pos=260;

mysql> start slave;

mysql> show slave status\G;

第一步是更改從服務器用於與主服務器進行連接和通訊的一些參數。

第二步是開啓從複製。

第三步是檢查從服務器屬性。(結果如下)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.251
                  Master_User: sync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql.000003
          Read_Master_Log_Pos: 260
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 247
        Relay_Master_Log_File: mysql.000003
             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: 260
              Relay_Log_Space: 403
              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)
ERROR:
No query specified


至此,Slave_IO_Running和Slave_SQL_Running的值爲Yes,表示已經成功。

可以在主服務器添加數據來看從服務器是否複製成功。


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