数据库的主从复制

一、环境

master节点 CentOS 7.6 192.168.111.132 mysql 5.7
slave节点 CentOS 7.6 192.168.111.133 mysql 5.7

二、同步时间

## 下载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

二,实现主从复制,其原理三点

  1. 在/etc/my.cnf中添加log-bin=mysql-bin开启master节点的二进制日志,这个文件将会记录我们在数据库中的增删查改的操作。
  2. slave节点将master节点的二进制文件中的sql语句复制到自己的中继日志relay log中。
  3. salve节点将会将中继日志复制到自己的日志下,重新执行sql命令。

一、master节点开启二进制日志。

[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

二、创建用于同步的账号。

其中file和position我们将会用到。

## 创建用户
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)

三、从节点配置。

[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

四,初始化从数据库

其中master_host为我们master节点的IP地址。master_user 为我们前面授权的那个用户。master_host为用户的密码,master_log_file和master_log_pos为我们前面生成二进制文件的那两个值。

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就成功了。

猜你喜欢

转载自blog.csdn.net/qq_48480384/article/details/127676018
今日推荐