CentOS-7 MySQL5.7.31 配置主從復制


1、環境准備

  兩台CentOS-7主機:node1(主):192.168.137.128,node2(從):192.168.137.129

  兩台主機均已安裝MySQL5.7.31 ,且網絡互通

2、配置過程

主從原理大致有三個步驟:

  • 在主庫上把數據更改記錄到二進制日志中(Binary Log)中,這些記錄稱為二進制日志事件。
  • 從庫通過IO線程將主庫上的日志復制到自己的中繼日志(Relay Log)中。
  • 從庫通過SQL線程讀取中繼日志中的事件,將其重放到自己數據上。

  配置文件確定

配置主庫:

修改my.cnf 文件,在[mysqld] 段下添加:

# 服務id
server-id = 1
# 開啟mysql binlog功能
log-bin = mysql-bin
# binlog模式
binlog_format = MIXED

配置好后重啟mysql服務

[root@swarm-node1 mysql]# cd support-files/
[root@swarm-node1 support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@swarm-node1 support-files]# ./mysql.server restart

 

 檢查data目錄是否有binlog日志生成

登錄主數據庫,新建一個復制賬號

[root@swarm-node1 bin]# ./mysql -S/usr/local/mysql/mysql.sock -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> grant replication slave on *.* to 'cpp'@'%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

配置從庫:

修改my.cnf 文件,在[mysqld] 段下添加:

# 服務id
server-id = 2
# 開啟mysql 啟動中繼日志
relay-log=relay-log

修改完后重啟MySQL

登錄從數據庫,配置主從

在從庫上指定主庫地址,以及 同步賬號、密碼,並啟動同步

mysql> 
mysql> 
mysql> change master to master_host='192.168.137.128',master_user='cpp',master_password='123.com';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> 
mysql> 

 show slave status\G;   查看從庫狀態,查看Slave_IO_Running: Yes和 Slave_SQL_Running: Yes 為yes主從配置完成

 如果啟動同步后,第一個為Connecting,第二個為Yes,大部分為連接問題,請檢查網絡是否通,配置的用戶名密碼是否正確

 


免責聲明!

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



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