設(shè)置MySQL主從復(fù)制的方法

設(shè)置MySQL主從復(fù)制的方法?這個問題可能是我們?nèi)粘W習或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

創(chuàng)新互聯(lián)是專業(yè)的貴南網(wǎng)站建設(shè)公司,貴南接單;提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行貴南網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

設(shè)置細節(jié):

服務(wù)器:192.168.1.10

從服務(wù)器:192.168.1.20

數(shù)據(jù)庫:mydb

1.設(shè)置MySQL主服務(wù)器

在主服務(wù)器上創(chuàng)建一個具有REPLICATION SLAVE權(quán)限的mysql帳戶,復(fù)制客戶端將連接到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服務(wù)器以使更改生效。

# 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。記下這些值以在從屬服務(wù)器上使用。

備份數(shù)據(jù)庫并將其復(fù)制到slave mysql server。

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

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

mysql> UNLOCK TABLES;

2.設(shè)置MySQL Slave Server

編輯salve mysql配置文件并在[mysqld]部分下添加以下值。

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

server-id始終為非零數(shù)值。這些值永遠不會與其他主服務(wù)器和從服務(wù)器相似。

重啟mysql slave server,如果你已經(jīng)配置了復(fù)制,請在啟動時使用-skip-slave-start,不要立即連接到主服務(wù)器。

# /etc/init.d/mysqld restart

使用以下命令在從屬服務(wù)器上設(shè)置選項值。

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;

檢查從服務(wù)器的狀態(tài)。

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主從復(fù)制已在你的系統(tǒng)和工作模式下成功配置。

感謝各位的閱讀!看完上述內(nèi)容,你們對設(shè)置MySQL主從復(fù)制的方法大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁標題:設(shè)置MySQL主從復(fù)制的方法
分享URL:http://www.muchs.cn/article36/pgdjpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)App開發(fā)、關(guān)鍵詞優(yōu)化定制網(wǎng)站、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計