说明:本文介绍如何搭建MySQL主从结构;
原理
主从复制原理如下:
(1)master数据写入,更新binlog;
(2)master创建一个dump线程向slave推送binlog;
(3)slave连接到master,创建一个IO线程接收binlog,并记录到relay log中继日志中;
(4)slave开启一个sql线程读取relay log并在slave执行,完成同步;
(5)slave记录自己的binlog;
搭建
Step1:安装MySQL
首先,在两台服务器上安装好MySQL服务,如下:
(ECS_1)
(ECS_2)
安装步骤参考:CentOS 7安装、卸载MySQL数据库(一)
安装完,需要确保这两台服务器能够互相感知对方,简单说就是能互相ping通,且相关端口是开放的。正式情况网络一般都是连通的,如果是自己练习,可在本地和虚拟机之间搭建,或者在虚拟机和docker之间搭建。我这里是斥巨资(60块/月)买了一台轻量级应用服务器,来和云服务器ECS搭建主从。
Step2:主节点
接着,修改两台MySQL服务器的配置文件,对于主节点,配置文件新增如下配置:
# mysql服务ID,保证在整个集群环境中唯一
server_id=1
# 是否只读,1 代表只读,0代表读写,主数据库需要读写,设置0
read-only=0
# 开启binlog
log-bin=mysql-bin
另外,如需要指定同步某些数据库、忽略某些数据库不同步,可增加如下配置
# 忽略的,不同步的数据库
binlog-ignore-db=mysql
# 指定的,需要同步的数据库
binlog-do-db=mydb
修改完后,需重启MySQL服务
# 重启服务
systemctl restart mysql
连接数据库,输入下面SQL,开放给账户主从复制权限;
grant replication slave on *.* to 'admin'@'%';
查看binlog日志的信息
show master status;
记住文件名,位置,后面需要用到
Step3:从节点
从节点配置如下:
# mysql服务ID,保证在整个集群环境中唯一
server_id=2
# 是否只读,1 代表只读,0代表读写,从数据库仅读,设置1
read-only=1
修改完后,需重启MySQL服务
# 重启服务
systemctl restart mysql
连接数据库,敲下面的语句,设置同步的主库
change master to source_host='主节点IP', source_user='admin', source_password='MySQL@3306', source_log_file='mysql-bin.000001', source_log_pos=538;
敲下面的命令,开始同步:
start replica;
没有报错,继续敲下面的命令,查看主从同步状态
show replica status\G;
这里我遇到了许多问题,搭建成功,如下图方框中的状态:
遇到问题,参考下面这篇文章,希望能帮到你
- 搭建MySQL主从结构时的问题
如果是8.0.23前版本的数据库,敲下面的语句
(建立连接)
change master to master_host='主节点IP', master_user='admin', master_password='MySQL@3306', master_log_file='binlog.000002', master_log_pos=545;
(开始同步)
start slave;
(查看从节点状态)
show slave status\G;
Step4:测试
现在,我们打开Navicat,在主节点创建一个数据库,查看从节点有没有同步;
(刷新,Pink!,从库也出现了test数据库)
到这里,MySQL主从就搭建完成了,后续往主数据修改,从库也会同步修改。但需要注意,如果一开始主从库内容不一致,比如有的库只在主库中有,而从库中没有,在主从状态下删除掉主库中从库没有的数据库,就会报错。
如下,在搭建前,主从没搭建成功,我忘把主库中的test数据库删掉,搭建完之后,把只在主库中有的test数据库删掉,从库报下面的错误,说不能删除一个不存在数据库。更坑的是,报错之后主从就不能同步了。需要停止同步,重新建立连接。
另外
另外,搭建主从MySQL,对配置文件的修改实际上非常少,这里我把完整的配置文件复制到这里,以供参考:
(主节点)
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# 注意这行配置,如果你报了相关错误,考虑开启此行配置
default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# mysql服务ID,保证在整个集群环境中唯一
server-id=1
#
# 是否只读,1 代表只读,0代表读写,主数据库需要读写,设置0
read-only=0
# 开启binlog
log-bin=mysql-bin
(从节点)
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
port = 3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# mysql服务ID,保证在整个集群环境中唯一
server-id=2
# 是否只读,1 代表只读,0代表读写,从数据库仅读,设置1
read-only=0
总结
本文介绍了MySQL主从结构搭建的过程,参考B站视频:
- 黑马MySQL数据库进阶教程,轻松掌握mysql主从复制从原理到搭建全流程