原文地址:https://www.douyacun.com/article/55e857400227ab85f754168cbbb8c189
家里一台联想笔记本作为爬虫服务器,有一台公网阿里云服务器,目的爬虫跑完之后,公网服务器可以主从同步,家里的服务器和公网服务器通信的问题通过内网穿透工具(frp)解决, 优秀简单,具体可以看frp内网穿透_mysql服务_ssh服务_http服务
MYSQL主从同步是在MySQL主从复制(Master-Slave Replication)基础上实现的,通过设置在Master MySQL上的binlog(使其处于打开状态),Slave MySQL上通过一个I/O线程从Master MySQL上读取binlog,然后传输到Slave MySQL的中继日志中,然后Slave MySQL的SQL线程从中继日志中读取中继日志,然后应用到Slave MySQL的数据库中。这样实现了主从数据同步功能。
# 日志文件名
log-bin = mysql-bin
# 主数据库端ID号
server-id = 1
# 不同步哪些数据库
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
# 只同步哪些数据库,除此之外,其他不同步
binlog-do-db = game
# 日志保留时间
expire_logs_days = 10
# 控制binlog的写入频率。每执行多少次事务写入一次
# 这个参数性能消耗很大,但可减小MySQL崩溃造成的损失
sync_binlog = 5
# 日志格式,建议默认mixed
# statement 保存SQL语句
# row 保存影响记录数据
# mixed 前面两种的结合
binlog_format = mixed
-- 创建slave帐号slave,密码123456, 这里host 是 127.0.0.1,frp连接的内网,不需要开放,大家可以写成%
grant replication slave on *.* to 'slave'@'127.0.0.1' identified by '123456';
CREATE DATABASE videos DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 更新数据库权限
flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 196 | | |
+------------------+----------+--------------+------------------+
1 row in set
log-bin=mysql-bin
server-id=20
-- 执行同步命令,设置主数据库ip,同步帐号密码,同步位置
change master to master_host='127.0.0.1',master_port=3307, master_user='slave',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=151459295;
-- 开启同步功能
start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: slave
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 185295
Relay_Log_File: 00h-relay-bin.000002
Relay_Log_Pos: 756
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes -----------> 这里是连接成功
Slave_SQL_Running: Yes -----------> 这里sql执行成功
......
1 row in set (0.00 sec)
-- 停止主从同步
stop slave;
-- 连接断开时,重新连接超时时间
change master to master_connect_retry=50;
-- 开启主从同步
start slave;
binlog
和relaylog
主库binlog
mysqlbinlog --base64-output=decode-rows -v -v /var/lib/mysql/mysql-bin.000001 > binlog
从库relaylog
mysqlbinlog --base64-output=decode-rows -v -v mysql-relay-bin.000001 > relaylog
binlog
relaylog
binlog 配置
mysql> show variables like "%bin%";
+--------------------------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------------------------+--------------------------------+
| bind_address | * |
| binlog_cache_size | 32768 |
| binlog_checksum | CRC32 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_error_action | ABORT_SERVER |
| binlog_format | ROW |
| binlog_group_commit_sync_delay | 0 |
| binlog_group_commit_sync_no_delay_count | 0 |
| binlog_gtid_simple_recovery | ON |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | ON |
| binlog_row_image | FULL |
| binlog_rows_query_log_events | OFF |
| binlog_stmt_cache_size | 32768 |
| binlog_transaction_dependency_history_size | 25000 |
| binlog_transaction_dependency_tracking | COMMIT_ORDER |
| innodb_api_enable_binlog | OFF |
| innodb_locks_unsafe_for_binlog | OFF |
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| log_statements_unsafe_for_binlog | ON |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| sql_log_bin | ON |
| sync_binlog | 1 |
+--------------------------------------------+--------------------------------+
29 rows in set (0.01 sec)
relaylog 配置
mysql> show variables like '%relay%';
+---------------------------+------------------------------------+
| Variable_name | Value |
+---------------------------+------------------------------------+
| max_relay_log_size | 0 |
| relay_log | |
| relay_log_basename | /var/lib/mysql/00h-relay-bin |
| relay_log_index | /var/lib/mysql/00h-relay-bin.index |
| relay_log_info_file | relay-log.info |
| relay_log_info_repository | FILE |
| relay_log_purge | ON |
| relay_log_recovery | OFF |
| relay_log_space_limit | 0 |
| sync_relay_log | 10000 |
| sync_relay_log_info | 10000 |
+---------------------------+------------------------------------+
11 rows in set (0.01 sec)
主从复制原理
主从一致性延时,数据恢复
rpl_semi_sync_master_wait_point
控制master在哪个环节接受slave ack, master接收到ack返回状态给客户端
WAIT_AFTER_COMMIT
: master write binlog -> slave sync binlog -> master commit -> salve ack -> master return result。WAIT_AFTER_SYNC
(默认):master write binlog -> slave sync binlog -> salve ack -> master commit -> master return result如果主从同步延时比较严重,解决方案:
主从一致性检测