官方文档
MySQL 5.7 Document - Replication: https://dev.mysql.com/doc/refman/5.7/en/replication.html
注意事项
- 要保证 MySQL 版本的一致, 或者从服务器版本要高于主服务器
- 本例采用二进制包安装的 MySQL,安装目录为
/usr/local/mysql/
- 所有命令以
root
权限运行, 如果你不是使用的root
账户, 记得加sudo
命令 - 主从复制必须保证两台数据库实例的
server-id
不一致 - 主服务器必须开启二进制日志
log-bin
;从服务器必须开启中继日志relay-log
\ - 主从复制搭建必须保证初始数据一致
- 主服务器必须要给从服务器创建一个复制用户,并授予复制权限
- 在主从架构中,主数据库写会同步到从数据库;而从数据库写不会同步到主数据库, 注意保持数据一致性
环境规划
主机名 | 主机IP | 角色 |
---|---|---|
h11 | 10.0.0.11 | 主数据库 master |
h12 | 10.0.0.12 | 从数据库 slave |
配置
同步时间
本例使用 ntpdate
进行时间同步, 需要安装. 也可以使用
本例使用的是阿里云的 ntp 服务, 也可以使用国家授时中心的 ntp 服务: ntp.ntsc.ac.cn
yum install -y ntpdate
ntpdate ntp.aliyun.com
配置主数据库的 my.cnf
先关闭主数据库
systemctl stop mysql.server
在编辑 my.cnf
文件
vim /usr/local/mysql/my.cnf
my.cnf
文件的内容为:
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/master.err
log-bin=/usr/local/mysql/data/binlog # 一定要开启二进制日志
server-id=11
character_set_server=utf8mb4 # utf8mb4相当于utf8升级版
修改后重启主数据库
systemctl start mysql.server
配置从数据库的 my.cnf
从数据库不需要初始化, 如果已经初始化过从数据库, 可以停止数据库后, 删除数据目录, 在本例中是 /usr/local/mysql/data
systemctl stop mysql.server
rm -rf /usr/local/mysql/data
同步数据
在主服务器上, 将数据同步到从服务器上
rsync -av /usr/local/mysql/data root@10.0.0.12:/usr/local/mysql/
配置主从同步同步
启动主数据库和服数据库
systemctl start mysql.server
在主数据库中创建一个 slave
账号, 专门用于数据同步
create user 'slave'@'10.0.0.%' identified by 'slave_password';
grant replication slave on *.* to 'slave'@'10.0.0.%';
flush privileges;
之后在主数据库中锁表, 并查看 binlog 及写入位置
flush tables with read lock;
show master status\G;
本例的 binlog 及写入位置如下, binlog 为 binlog.000004
, 写入位置为 278419514
***************************[ 1. row ]***************************
File | binlog.000004
Position | 278419514
Binlog_Do_DB |
Binlog_Ignore_DB |
Executed_Gtid_Set |
在从数据库中, 指定主服务器
change master to
master_host='10.0.0.11',
master_user='slave',
master_password='slave_password',
master_port=3306,
master_log_file='binlog.000004',
master_log_pos=278419514;
再在从数据库中启动数据同步, 并查看从服务器状态
start slave;
show slave status\G;
本例的从服务器状态如下. 重点看 Slave_IO_Running
和 Slave_SQL_Running
两项是否为 Yes
, 如果不是的话, 说明上面配置主从架构的过程出现了问题, 具体可以看看下面的常见问题
***************************[ 1. row ]***************************
Slave_IO_State | Waiting for master to send event
Master_Host | 10.0.0.31
Master_User | slave
Master_Port | 3306
Connect_Retry | 60
Master_Log_File | binlog.000004
Read_Master_Log_Pos | 278419514
Relay_Log_File | relay.000008
Relay_Log_Pos | 278419721
Relay_Master_Log_File | binlog.000004
Slave_IO_Running | Yes
Slave_SQL_Running | Yes
Replicate_Do_DB |
Replicate_Ignore_DB |
Replicate_Do_Table |
Replicate_Ignore_Table |
Replicate_Wild_Do_Table |
Replicate_Wild_Ignore_Table |
Last_Errno | 0
Last_Error |
Skip_Counter | 0
Exec_Master_Log_Pos | 278419514
Relay_Log_Space | 278419962
Until_Condition | None
Until_Log_File |
Until_Log_Pos | 0
Master_SSL_Allowed | No
Master_SSL_CA_File |
Master_SSL_CA_Path |
Master_SSL_Cert |
Master_SSL_Cipher |
Master_SSL_Key |
Seconds_Behind_Master | 0
Master_SSL_Verify_Server_Cert | No
Last_IO_Errno | 0
Last_IO_Error |
Last_SQL_Errno | 0
Last_SQL_Error |
Replicate_Ignore_Server_Ids |
Master_Server_Id | 31
Master_UUID | cee36bac-2aad-11ee-a570-000c29737926
Master_Info_File | /usr/local/mysql/data/master.info
SQL_Delay | 0
SQL_Remaining_Delay | <null>
Slave_SQL_Running_State | Slave has read all relay log; waiting for more updates
Master_Retry_Count | 86400
Master_Bind |
Last_IO_Error_Timestamp |
Last_SQL_Error_Timestamp |
Master_SSL_Crl |
Master_SSL_Crlpath |
Retrieved_Gtid_Set |
Executed_Gtid_Set |
Auto_Position | 0
Replicate_Rewrite_DB |
Channel_Name |
Master_TLS_Version |
最后, 出去主数据库中的对所有表加的读锁
unlock tables;
从服务器误写入数据后的处理方案
如果误写入数据不多, 可以在从服务器上临时跳过事务
set global sql_slave_skip_counter = N; # 跳过 N 个事务
stop slave;
start slave;
如果误写入数据较多, 直接重新搭建主从复制架构吧
另外, 只有传统的主从复制可以使用 sql_slave_skip_counter
变量, 在基于 GTIDs 的主从复制架构上并不可用.
常见问题
常见问题1:MySQL无法启动
# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
Failed to start mysqld.service: Unit not found.
出现以上问题的主要原因在于/etc/init.d目录中没有mysqld这个文件换句话说,就是你没有cp mysql.server脚本
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
记住:/etc/init.d目录中的文件叫什么名字,我们service的时候就应该使用什么名字
# service mysqld start
常见错误2:MySQL没有安装在/usr/local/mysql目录中,service也无法启动
# service mysqld start
无法启动的原因可能是你的MySQL并没有安装在/usr/local/mysql目录中,因为/etc/init.d/mysqld脚本中的basedir与datadir默认指向的都是/usr/local/mysql
所以如果我们更换了mysql的安装位置,则必须更改/etc/init.d/mysqld脚本中basedir与datadir目录
# vim /etc/init.d/mysqld
basedir=你的安装路径
datadir=你的安装路径/data
常见问题3:因为my.cnf配置文件导致mysql无法启动
# vim my.cnf
[mysqld]
basedir=/usr/local/mysql => 安装路径
datadir=/usr/local/mysql/data => 数据目录
socket=/tmp/mysql.sock => GLIBC默认就是/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/slave.err => 错误日志到底放在哪里
relay-log=/usr/local/mysql/data/relaylog
server-id=100
character_set_server=utf8mb4
常见问题4: mysql
用户对 /usr/local/mysql
没有写入权限
启动报错:
Starting MySQL.2020-08-31T07:17:06.554270Z mysqld_safe error: log-error set to '/usr/local/mysql/slave.err', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/usr/local/mysql/data/slave.itcast.cn.pid).
产生以上问题的主要原因在于mysql这个用户对/usr/local/mysql文件夹没有写入权限
ll -d /usr/local/mysql
# drwxr-xr-x 11 7161 31415 174 Aug 31 15:16 /usr/local/mysql
发现文件拥有者位置与所属组位置都是两个数字,正常应该是文件拥有者的名称与文件所属组的名称。但是由于GLIBC已经提前打包了,我们解压后,如果在我们系统中,找不到原文件对应的文件拥有着与所属组,则以两个数字代替文件拥有者与所属组的显示。
解决方案:
方案一:建议把错误日志,丢在数据目录中
# vim my.cnf
...
log-error=/usr/local/mysql/data/slave.err
方案二:直接更改/usr/local/mysql目录的权限
chown -R mysql.mysql /usr/local/mysql