溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL5.7主從復(fù)制-異步復(fù)制搭建

發(fā)布時間:2020-08-09 09:54:43 來源:ITPUB博客 閱讀:175 作者:StevenBeijing 欄目:MySQL數(shù)據(jù)庫
  兩臺服務(wù)器,系統(tǒng)是Redhat6.5,MySQL版本是5.7.18。
1、在主庫上,創(chuàng)建復(fù)制使用的用戶,并授予replication slave權(quán)限。這里創(chuàng)建用戶repl,可以從IP為10.10.10.210的主機(jī)進(jìn)行連接。
grant replication slave on *.* to 'repl'@'10.10.10.210' identified by 'mysql';

2、修改主服務(wù)器配置,加入如下配置:
cat /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
log-bin-index=mysql-bin.index
binlog_format=mixed

3、在主庫上,設(shè)置讀鎖,確保沒有數(shù)據(jù)操作,獲得一個一致性的快照
flush tables with read lock;

4、然后在主庫上獲得當(dāng)前二進(jìn)制日志名和偏量值,改操作的目的是從庫啟動之后,從這個點(diǎn)開始恢復(fù)數(shù)據(jù)。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

5、利用mysqldump導(dǎo)出數(shù)據(jù),拷貝至從庫服務(wù)器。

6、主庫備份完成,恢復(fù)寫操作
unlock tables;

7、修改從庫的配置文件,添加如下參數(shù),注意server-id必須是唯一的,不能和主庫相同,多個從庫的話,server-id不能有重復(fù)。
cat /etc/my.cnf
[mysqld]
server-id=2

8、在從庫上,使用--skip-slave-start啟動數(shù)據(jù)庫,這樣不會立即啟動從庫上的復(fù)制進(jìn)程,方便我們進(jìn)行下一步配置。
./bin/mysqld_safe --skip-slave-start &

9、對從庫進(jìn)行配置,指定復(fù)制使用的用戶,主庫的IP、端口以及開始執(zhí)行復(fù)制的日志文件和位置等:
change master to
master_host='10.10.10.200',
master_port=3306,
master_user='real',
master_password='mysql',
master_log_file='mysql-bin.000006',
master_log_pos=120;

10、在從庫上啟動slave線程
start slave;

11、在從庫上查看slave狀態(tài)
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.10.200
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 120
               Relay_Log_File: mysql-relay-bin.000026
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000006
             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: 120
              Relay_Log_Space: 619
              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: 1
                  Master_UUID: 4adfcd1d-4059-11e7-9532-080027d597f9
             Master_Info_File: mysql.slave_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
12、在主庫進(jìn)行DDL或者DML測試,在從庫查看數(shù)據(jù)同步情況
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI