首頁  >  文章  >  資料庫  >  如何設定MySQL的主從複製

如何設定MySQL的主從複製

不言
不言原創
2019-03-04 16:52:332212瀏覽

這篇文章要跟大家介紹的是關於在MySQL伺服器之間設定主從複製,下面我們來看具體內容。

如何設定MySQL的主從複製

設定細節:

#主機伺服器:192.168.1.10 

##從伺服器:192.168.1.20 

資料庫: mydb

1.設定MySQL主伺服器

在主伺服器上建立一個具有REPLICATION SLAVE權限的mysql帳戶,複製客戶端將連接到master。

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'192.168.1.20' IDENTIFIED BY 'secretpassword';
mysql> FLUSH PRIVILEGES;

在所有表上都有block write語句,因此不要在備份後進行更改。


mysql> use mydb;
mysql> FLUSH TABLES WITH READ LOCK;
mysql> exit;

編輯mysql設定檔並在[mysqld]部分下新增以下程式碼。

# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog-do-db=mydb
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1

重新啟動master mysql伺服器以使變更生效。

# service mysqld restart

使用下列命令檢查目前二進位日誌檔案名稱(File)和目前偏移量(Position)值。

mysql > SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      107 | mydb         |                  |
+------------------+----------+--------------+------------------+

以上輸出顯示目前二進位檔案使用的是mysql-bin.000002,偏移值為107。記下這些值以在從屬伺服器上使用。

備份資料庫並將其複製到slave mysql server。

# mysqldump -u root -p mydb > mydb.sql
# scp mydb.sql 192.168.1.20:/opt/

完成備份後,從表中刪除READ LOCK,以便進行更改。

mysql> UNLOCK TABLES;

2.設定MySQL Slave Server

編輯salve mysql設定檔並在[mysqld]部分下新增下列值。

# vim /etc/my.cnf
[mysqld]
server-id=2
replicate-do-db=mydb

server-id總是非零數值。這些值永遠不會與其他主伺服器和從伺服器相似。

重啟mysql slave server,如果你已經設定了複製,請在啟動時使用-skip-slave-start,不要立即連接到主伺服器。

# /etc/init.d/mysqld restart

使用下列命令在從屬伺服器上設定選項值。

mysql>  CHANGE MASTER TO MASTER_HOST='192.168.1.10',
    -> MASTER_USER='repl_user',
    -> MASTER_PASSWORD='secretpassword',
    -> MASTER_LOG_FILE='mysql-bin.000002',
    -> MASTER_LOG_POS=107;

最後啟動從屬執行緒

mysql> SLAVE START;

檢查從伺服器的狀態。


mysql> show slave status G
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.1.15
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 107
               Relay_Log_File: mysqld-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: mydb
          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: 107
              Relay_Log_Space: 107
              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: 1
1 row in set (0.00 sec)
mysql>

MySQL主從複製已在你的系統和工作模式下成功設定。

以上是如何設定MySQL的主從複製的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn