MySQL 8.0 主從同步


一.簡介

    一台服務器充當主數據庫服務器,另一台或多台服務器充當從數據庫服務器,主服務器中的數據自動復制到從服務器之中。MySQL主從復制的基礎是主服務器對數據庫修改記錄二進制日志,從服務器通過主服務器的二進制日志自動執行更新。

環境:

  主庫主機(master): 172.17.7.21
  從庫主機(slave): 172.17.7.20

二.步驟

  1.mysql 配置文件

 

        (1)、打開master的配置文件my.cnf    

 1 [mysqld]
 2 server-id = 1        # 節點ID,確保唯一
 3 
 4 # log config
 5 log-bin = mysql-bin     #開啟mysql的binlog日志功能
 6 sync_binlog = 1         #控制數據庫的binlog刷到磁盤上去 , 0 不控制,性能最好,1每次事物提交都會刷到日志文件中,性能最差,最安全
 7 binlog_format = mixed   #binlog日志格式,mysql默認采用statement,建議使用mixed
 8 expire_logs_days = 7                           #binlog過期清理時間
 9 max_binlog_size = 100m                    #binlog每個日志文件大小
10 binlog_cache_size = 4m                        #binlog緩存大小
11 max_binlog_cache_size= 512m              #最大binlog緩存大
12 binlog-ignore-db=mysql #不生成日志文件的數據庫,多個忽略數據庫可以用逗號拼接,或者 復制這句話,寫多行
13 auto-increment-offset = 1     # 自增值的偏移量
14 auto-increment-increment = 1  # 自增值的自增量
15 slave-skip-errors = all #跳過從庫錯誤
View Code

   (2)、打開slave的配置文件my.cnf    

1 [mysqld]
2 server-id = 2
3 log-bin=mysql-bin
4 relay-log = mysql-relay-bin
5 replicate-wild-ignore-table=mysql.%
6 replicate-wild-ignore-table=test.%
7 replicate-wild-ignore-table=information_schema.%
View Code

   (3)、重啟mysql (net stop mysql、net start MySQL)

 

  2.master數據庫,創建復制用戶並授權

 

   (1)、進入master的數據庫,為master創建復制用戶

create user 'slaveuser'@'172.17.7.20' identified by 'ysp_password';

   

grant replication slave on *.* to 'slaveuser'@'172.17.7.20' 
FLUSH PRIVILEGES;

     (2)、查看master的狀態

 

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001      782|              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

  

  3.slave數據庫

   

mysql> CHANGE MASTER TO 
MASTER_HOST = '172.17.7.21',  
MASTER_USER = 'slaveuser', 
MASTER_PASSWORD = 'ysp_password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=782;

# MASTER_LOG_FILE='mysql-bin.000001',#與主庫File 保持一致
# MASTER_LOG_POS=782 , #與主庫Position 保持一致

   (1)、啟動從庫slave進程

mysql> start slave;
Query OK, 0 rows affected (0.04 sec)

   (2)、查看主從同步狀態

mysql> show slave status\G

   開啟主從之后,如果狀態如上圖所示,那么說明主從信息就已經配置好了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM