MySQL 复制问题。为什么 binlog 增长得这么快?

MySQL 复制问题。为什么 binlog 增长得这么快?

我有 2 个 MySQL 服务器:
主服务器:mysql 版本 5.7.14
从属服务器:Docker 容器中的 mysql 版本 5.7.14(来自官方 docker hub)。GTID
复制。

我有两个问题:

  1. Binlogs 增长非常快。我设置了 2 天的限制轮换,但这没有帮助 - 每天 binlog 文件夹至少增加两次(第一天 25Gb,第二天 50,第三天 80 等)。
  2. 从属服务器“落后主服务器的秒数”增加。

本地网络中的服务器(100 mbit/s)、SSD 磁盘、数据库大小接近 40Gb。
我使用 Percona Xtrabackup 克隆从属数据库。

也许服务器配置不正确并且我需要修复一些东西?

这是Master的my.cnf文件:

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

max_allowed_packet = 256M

log-error       = /var/log/mysql/error.log

symbolic-links=0

!includedir /etc/mysql/conf.d/

innodb_buffer_pool_size=15000M
innodb_buffer_pool_instances=1
sql_mode = ''

slow_query_log = 1
slow_query_log_file     = '/var/log/mysql/slow.log'
long_query_time = 1
log_queries_not_using_indexes = 0

#skip-grant-tables

default_week_format = 1

skip-name-resolve
sort_buffer_size=4M
join_buffer_size=4M
innodb_sort_buffer_size=4M
tmp_table_size=5000M
max_heap_table_size=5000M

[mysqld]
server-id = 1
binlog-format = row
gtid_mode=ON
enforce-gtid-consistency=ON
log-slave-updates
log_bin = /var/log/mysql/mysql-bin.log
performance_schema_max_digest_length = 8192
max_digest_length = 8192
max_binlog_size= 1G
expire_logs_days = 2
binlog-ignore-db=check_passport
replicate-ignore-db=check_passport

sync_binlog = 1
innodb_flush_log_at_trx_commit = 1

key_buffer_size             = 16M
innodb_flush_method         = O_DIRECT
max_connections             = 200

#innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:5G

这是从服务器的my.cnf

[client]
port                        = 3306
socket                      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket                      = /var/run/mysqld/mysqld.sock
nice                        = 0

[mysqld]
sql_mode                    = ""
character_set_server        = utf8
collation_server            = utf8_general_ci
user                        = mysql
pid-file                    = /var/run/mysqld/mysqld.pid
socket                      = /var/run/mysqld/mysqld.sock
port                        = 3306
basedir                     = /usr
datadir                     = /var/lib/mysql
tmpdir                      = /tmp
language                    = /usr/share/mysql/english
old_passwords               = 0
bind-address                = 127.0.0.1
skip-host-cache
skip-name-resolve

skip-external-locking

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

max_allowed_packet          = 256M
#key_buffer_size             = 16M
innodb_buffer_pool_size     = 2048M
innodb_log_file_size        = 256M
innodb_file_per_table       = 1
innodb_flush_method         = O_DIRECT
innodb_flush_log_at_trx_commit  = 1

max_connections             = 136

query_cache_size            = 0

slow_query_log              = /var/log/mysql/mysql-slow.log
long_query_time             = 1

expire_logs_days            = 2
max_binlog_size             = 1G

[mysqldump]
quick
quote-names
max_allowed_packet          = 16M


[mysqld]
server-id = 2
binlog-format = row
gtid_mode=ON
enforce-gtid-consistency=ON

relay-log = /var/log/mysql/mysql-relay-bin.log

log_bin = /var/log/mysql/mysql-bin.log

skip_slave_start

log_slave_updates = 0
read_only = ON

innodb_file_per_table = ON
#innodb_buffer_pool_size = 3G
innodb_flush_method         = O_DIRECT
innodb_flush_log_at_trx_commit  = 1

max_binlog_size = 1G

#max_relay_log_size = 1G
#relay_log_space_limit = 20G
relay_log_recovery = ON

expire_logs_days = 2

#slave-parallel-workers = 0

binlog-ignore-db=check_passport
replicate-ignore-db=check_passport

replicate-ignore-table=gfk.application_insurance
replicate-ignore-table=gfk.archive_client_building
replicate-ignore-table=gfk.comments_passwords
replicate-ignore-table=gfk.date_interval

输出显示从属状态\G

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.4
                  Master_User: slave_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.004720
          Read_Master_Log_Pos: 518759418
               Relay_Log_File: mysql-relay-bin.000188
                Relay_Log_Pos: 213202356
        Relay_Master_Log_File: mysql-bin.004703
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: check_passport
           Replicate_Do_Table: 
       Replicate_Ignore_Table: gfk.application_insurance,gfk.date_interval,gfk.archive_client_building,gfk.comments_passwords
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 213202143
              Relay_Log_Space: 18773097825
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: Yes
           Master_SSL_CA_File: /var/lib/master_cert/ca.pem
           Master_SSL_CA_Path: 
              Master_SSL_Cert: /var/lib/master_cert/client-cert.pem
            Master_SSL_Cipher: 
               Master_SSL_Key: /var/lib/master_cert/client-key.pem
        Seconds_Behind_Master: 14488
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: 1
                  Master_UUID: 8ab33cfb-bb00-11e6-84cd-fa163eb352dd
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: System lock
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 8ab33cfb-bb00-11e6-84cd-fa163eb352dd:62276836-70424802
            Executed_Gtid_Set: 8ab33cfb-bb00-11e6-84cd-fa163eb352dd:1-67413143
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 

答案1

主节点有 是否可行binlog_ignore = check_passport?如果是,并且该数据库有大量流量,那么 binlog 将会“大量”缩小。

DELETEsUPDATEs占用大量 binlog 空间,从而使 binlog 变得巨大。(例如:更新百万行表的每一行。)提供具体信息;这可能是重要的解决方法。

副本上运行着多少个副本线程?这会影响Seconds_behind_master。(线程越多,达到一定限度,落后的线程数可能会减少。)

innodb_flush_log_at_trx_commit = 1--> 改为 2。这会牺牲一些稳健性(跨崩溃),但提高吞吐量。

每个有多少 RAM?我发现主节点的缓冲池更大。 通常Replica 应该是更强大的机器。

100Mbs 似乎很低;您能检查一下它是否已经饱和吗?

每日更新数据

这是一个有效的方法来代替全部表中的数据:

  1. CREATE TABLE new LIKE real;
  2. LOAD DATA INFILE INTO new ... -- 或者任何需要
  3. `将表 real 重命名为 old,将 new 重命名为 REAL;
  4. DROP TABLE old;

步骤 #2 是最慢的部分。
步骤 #3 非常快;这是您的表不可用的唯一时间。
没有 binlog 密集型UPDATEDELETE

答案2

我找到了适合我的情况的解决方案

首先,我搜索一下哪些数据库和表会产生如此大的binlog(哪些表没有主键或唯一键):

SELECT t.table_schema,t.table_name,engine
FROM information_schema.tables t
INNER JOIN information_schema .columns c
on t.table_schema=c.table_schema
and t.table_name=c.table_name
and t.table_schema not in ('performance_schema','information_schema','mysql')
GROUP BY t.table_schema,t.table_name
HAVING sum(if(column_key in ('PRI','UNI'), 1,0)) =0;

然后:

  1. 我的网速已经是1Gbit/s了,一切正常。
  2. 我从每天重新创建的复制和日志数据库中排除。
  3. 我将从属 RAM 增加到 23Gb(与主 RAM 相同)
  4. 我将从属服务器上的 binlog 从 SSD 移动到 HDD - 没问题 - HDD 的速度足够了。
  5. 我设置了 log_slave_updates = 1,因为我的复制方案是主>从>从。

这些步骤解决了我的问题!目前我的 binlog 大小没有超过 10Gb。

相关内容