MySQL主從同步配置

MySQL主從同步的步驟:

  • 在主服務器上開啟bin-log二進制日志功能,設置唯一的server_id(除了多實例外一般設置為ip地址最后一位),設置完成重啟mysqld服務
  • 在從服務器上同樣設置唯一的server_id
  • 在主服務器上授權REPLICATION SLAVE權限,允許從服務器同步數據
  • 復制數據前在主服務器上SHOW MASTER STATUS記錄file和position
1. 準備兩臺MySQL服務器,確保服務器之間時間同步

master:192.168.42.129
slave : 192.168.42.130
MySQL數據庫的安裝配置見:
http://www.lxweimin.com/p/31572010fe03

2. 設置主服務器bin-log文件
vim /etc/my.cnf文件

在 [mysqld] 主配置文件下,加入

log-bin=replicate-bin # 啟用二進制日志,設置二進制文件名(自定義)
log-bin
重啟mysql服務
[root@master ~]# /etc/init.d/mysqld restart
3. 創建復制賬號

這個賬號必須具有REPLICATION SLAVE的權限,你可以為不同的從服務器創建不同的賬號,也可以為所有從服務器創建統一的賬號

進入master mysql服務:
[root@master ~]# mysql -uroot -p
Enter password: 
創建replicate賬號用于復制
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replicate'@'%' identified by 'replicate';
Query OK, 0 rows affected (0.07 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

獲取主服務器的二進制日志信息:

只讀鎖定后,所有數據庫的寫操作都將被拒絕,但是讀操作可以繼續。執行鎖定可以防止在查看二進制日志信息的同時有人對數據進行修改操作,查看二進制日志信息后使用UNLOCK TABLES解鎖即可

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.19 sec)

mysql> SHOW MASTER STATUS;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| replicate-bin.000001 |     1234 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
3. 對現有數據庫進行快照備份

如果在使用二進制日志進行數據復制之前,主服務已經存在大量的數據庫文件,可以使用mysqldump進行數據的備份還原操作。

主服務器上進行備份操作
[root@master ~]# mysqldump -uroot -pqinger --all-databases --lock-all-tables > /tmp/test.sql
Warning: Using a password on the command line interface can be insecure.
[root@master ~]# ls /tmp/test.sql 
/tmp/test.sql
將備份的sql文件復制到從服務器上進行還原
[root@master ~]# scp /tmp/test.sql root@192.168.42.130:/tmp/
root@192.168.42.130's password: 
test.sql                                                                                                                                         100%  633KB 632.6KB/s   00:00 
[root@slave ~]# mysql -uroot -pqinger < /tmp/test.sql 
Warning: Using a password on the command line interface can be insecure.
4. 配置從服務器連接主服務器進行數據同步
進入mysql,連接到master服務器

指定主機名,用戶名,密碼,log文件,和log位置

mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.42.129',
    -> MASTER_USER='replicate',
    -> MASTER_PASSWORD='replicate',
    -> MASTER_LOG_FILE='replicate-bin.000001',
    -> MASTER_LOG_POS=1309;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
開啟同步,查看同步狀態

顯示
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
表示同步正在進行

mysql> START SLAVE;
Query OK, 0 rows affected (0.08 sec)

mysql> SHOW SLAVE STATUS \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.42.129
                  Master_User: replicate
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: replicate-bin.000001
          Read_Master_Log_Pos: 1309
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 287
        Relay_Master_Log_File: replicate-bin.000001
             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: 1309
              Relay_Log_Space: 464
              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: 129
                  Master_UUID: c8baa742-b6c8-11e6-bac3-000c295183f1
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)
master服務器創建數據庫,slave服務器上查看是否存在
master
slave
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容