1. Environnement
nœud maître | CentOS 7.6 | 192.168.111.132 | MySQL 5.7 |
nœud esclave | CentOS 7.6 | 192.168.111.133 | MySQL 5.7 |
2. Temps de synchronisation
## 下载ntp
yum -y install ntp
## 启动ntp
systemctl enable --now ntpd
## master节点
在/etc/ntp.conf文件尾部加入以下两段,注意网段。
server 127.127.111.0
fudge 127.127.111.0 stratum 8
## 重启ntp
systemctl restart ntpd
## slave节点
执行命令,IP地址为你master节点的IP地址
ntpdate 192.168.111.133
Deuxièmement, réalisez la réplication maître-esclave, le principe est en trois points
- Ajoutez log-bin=mysql-bin à /etc/my.cnf pour activer le journal binaire du nœud maître. Ce fichier enregistrera nos ajouts, suppressions, vérifications et modifications dans la base de données.
- Le nœud esclave copie l'instruction SQL dans le fichier binaire du nœud maître dans son propre journal de relais.
- Le nœud salve copiera le journal du relais dans son propre journal et réexécutera la commande sql.
1. Le nœud maître ouvre le journal binaire.
[root@master ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin=mysql-bin
server-id=1
2. Créez un compte pour la synchronisation.
Parmi eux, le fichier et la position que nous utiliserons.
## 创建用户
create user 'repl'@'192.168.111.133' identified by '123456';
## 将创建的repl用户授权给192.168.111.133使用。
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.111.133';
## 刷新权限
flush privileges;
查看我们的二进制文件
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1079 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3. Configuration du nœud esclave.
[root@slave ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=2
Quatrièmement, initialisez la base de données esclave
Parmi eux, master_host est l'adresse IP de notre nœud maître. master_user est l'utilisateur que nous avons autorisé plus tôt. master_host est le mot de passe de l'utilisateur, master_log_file et master_log_pos sont les deux valeurs que nous avons utilisées pour générer le fichier binaire plus tôt.
mysql> change master to master_host='192.168.111.132', master_user='repl', master_password='123456',master_log_file='mysql-bin.000001', master_log_pos=1079;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
## 开启从节点.
mysql> start slave;
## 查看从数据库的状态
mysql> show slave status\G;
Parmi eux, salve_io_running et slave_sql_running sont oui et c'est réussi.