1. 環境
マスターノード | CentOS 7.6 | 192.168.111.132 | mysql 5.7 |
スレーブノード | CentOS 7.6 | 192.168.111.133 | mysql 5.7 |
2. 同期時間
## 下载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
2. マスター/スレーブ レプリケーションを実現するには、次の 3 つの原則があります。
- /etc/my.cnf に log-bin=mysql-bin を追加して、マスター ノードのバイナリ ログを有効にします。このファイルには、データベースへの追加、削除、確認、変更の操作が記録されます。
- スレーブノードは、マスターノードのバイナリファイル内の SQL ステートメントを自身のリレーログにコピーします。
- スレーブノードはリレーログを自身のログにコピーし、sql コマンドを再実行します。
1. マスターノードでバイナリログを有効にします。
[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. 同期用のアカウントを作成します。
その中で、使用するファイルと位置です。
## 创建用户
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. スレーブノードの構成。
[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
4番目に、データベースを初期化します
このうち、master_host はマスター ノードの IP アドレスです。master_user は、先ほど許可したユーザーです。master_host はユーザーのパスワード、master_log_file と master_log_pos は、前にバイナリ ファイルを生成するために使用した 2 つの値です。
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;
このうちsalve_io_runningとslave_sql_runningがyesで成功です。