浅析mysql内存参数之-- binlog_cache_size

本文详细解析MySQL binlog_cache_size参数的作用、意义、属性、选择原则及配置方法,通过展示binlog_cache_size的使用情况和注意事项,帮助读者优化数据库性能,避免常见错误。了解如何根据业务需求和现有配置调整此参数,实现更高效的二进制日志记录。

binlog_cache_size

0.前提:

服务器配置了 log-bin

1.含义:

 为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存。

2.作用:

提高记录bin-log的效率

3.属性:

参数:global

默认值:32768   -- 即 32K

范围:4096 .. 4294967295

4.大小选择:

a.根据业务:

没有什么大事务,dml也不是很频繁的情况下可以设置小一点,如果事务大而且多,dml操作也频繁,则可以适当的调大一点。

前者建议是1048576  --1M

后者建议是: 2097152 -- 4194304  即 2--4M

b.根据参数:

show global status like 'bin%';

上述语句我们可以得到当前 数据库binlog_cache_size的使用情况

+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Binlog_cache_disk_use | ???
| Binlog_cache_use      | ?????

Binlog_cache_disk_use表示因为我们binlog_cache_size设计的内存不足导致缓存二进制日志用到了临时文件的次数

Binlog_cache_use  表示 用binlog_cache_size缓存的次数

当对应的Binlog_cache_disk_use 值比较大的时候 我们可以考虑适当的调高 binlog_cache_size 对应的值

 

4.注意点:

a.max_binlog_cache_size 表示的是binlog 能够使用的最大cache 内存大小

当我们执行多语句事务的时候 所有session的使用的内存超过max_binlog_cache_size的值时

就会报错:“Multi-statement transaction required more than 'max_binlog_cache_size' bytes ofstorage”

b.设置太大的话,会比较消耗内存资源;设置太小又会使用到临时文件即disk

 

5.配置

a.配置文件设置my.cnf

binlog_cache_size = 1M

b. set global binlog_cache_size = 1048576;

 

 

D:\work\w\sql\mysql\mysql-5.7.29.0\exe\MySQL Server 5.7\bin>mysqld --print-defaults mysqld would have been started with the following arguments: --port=3306 --basedir=D:/work/w/sql/mysql/mysql-5.7.29.0/exe/MySQL Server 5.7/ --datadir=D:/work/w/sql/mysql/mysql-5.7.29.0/exe/data/Data --default-storage-engine=INNODB --sql-mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION --log-output=FILE --general-log=0 --general_log_file=PC-87YHGBFR2W.log --slow-query-log=1 --slow_query_log_file=PC-87YHGBFR2W-slow.log --long_query_time=10 --log-error=PC-87YHGBFR2W.err --relay_log=PC-87YHGBFR2W-relay --server-id=1 --report_port=3306 --lower_case_table_names=1 --secure-file-priv=D:/work/w/sql/mysql/mysql-5.7.29.0/exe/data/Uploads --max_connections=151 --table_open_cache=2000 --tmp_table_size=56M --thread_cache_size=10 --myisam_max_sort_file_size=100G --myisam_sort_buffer_size=104M --key_buffer_size=8M --read_buffer_size=64K --read_rnd_buffer_size=256K --innodb_flush_log_at_trx_commit=1 --innodb_log_buffer_size=1M --innodb_buffer_pool_size=8M --innodb_log_file_size=48M --innodb_thread_concurrency=33 --innodb_autoextend_increment=64 --innodb_buffer_pool_instances=8 --innodb_concurrency_tickets=5000 --innodb_old_blocks_time=1000 --innodb_open_files=300 --innodb_stats_on_metadata=0 --innodb_file_per_table=1 --innodb_checksum_algorithm=0 --back_log=80 --flush_time=0 --join_buffer_size=256K --max_allowed_packet=4M --max_connect_errors=100 --open_files_limit=4161 --sort_buffer_size=256K --table_definition_cache=1400 --binlog_row_event_max_size=8K --sync_master_info=10000 --sync_relay_log=10000 --sync_relay_log_info=10000 D:\work\w\sql\mysql\mysql-5.7.29.0\exe\MySQL Server 5.7\bin>
09-16
你运行了以下命令: ```bash mysqld --print-defaults ``` 输出显示 MySQL 服务将会使用如下配置启动: ```bash mysqld would have been started with the following arguments: --port=3306 --basedir=D:/work/w/sql/mysql/mysql-5.7.29.0/exe/MySQL Server 5.7/ --datadir=D:/work/w/sql/mysql/mysql-5.7.29.0/exe/data/Data ... ``` --- ## ✅ 问题分析 从输出中可以看出: - `basedir`:MySQL 安装目录 - `datadir`:数据目录为 `D:/work/w/sql/mysql/mysql-5.7.29.0/exe/data/Data` - 配置中包含大量 `InnoDB`、日志、连接数等参数 - 没有直接显示 `my.ini` 文件路径,但说明这些配置是从配置文件中加载的 这说明: ✅ MySQL 确实加载了某个 `my.ini` 文件,但你可能不清楚它具体加载的是哪一个。 --- ## ✅ 如何确认 MySQL 实际加载的 `my.ini` 文件? ### ✅ 方法 1:查看 MySQL 启动日志 如果你配置了日志文件,比如: ```ini [mysqld] log_error = "D:/work/w/sql/mysql/mysql-5.7.29.0/exe/data/Data/mysql_error.log" ``` 打开该日志文件,你会看到类似如下内容: ``` [Note] mysqld: Using config file D:\work\w\sql\mysql\mysql-5.7.29.0\exe\MySQL Server 5.7\my.ini ``` ### ✅ 方法 2:使用 `SHOW VARIABLES LIKE 'config_file';`(如果服务已启动) 连接 MySQL: ```bash mysql -u root -p ``` 执行: ```sql SHOW VARIABLES LIKE 'config_file'; ``` 输出示例: ``` +---------------+--------------------------------------------------+ | Variable_name | Value | +---------------+--------------------------------------------------+ | config_file | D:\work\w\sql\mysql\mysql-5.7.29.0\my.ini | +---------------+--------------------------------------------------+ ``` ### ✅ 方法 3:使用 `mysqld --help --verbose` 查看配置文件搜索路径 运行: ```bash mysqld --help --verbose ``` 输出中会显示 MySQL 按顺序查找的 `my.ini` 文件路径,例如: ``` Default options are read from the following files in the given order: C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf D:\work\w\sql\mysql\mysql-5.7.29.0\exe\MySQL Server 5.7\my.ini D:\work\w\sql\mysql\mysql-5.7.29.0\exe\MySQL Server 5.7\my.cnf ``` MySQL 会使用第一个存在的配置文件。 --- ## ✅ 总结 | 方法 | 说明 | |------|------| | `SHOW VARIABLES LIKE 'config_file';` | 如果服务已启动,直接查询配置文件路径 | | `mysqld --help --verbose` | 查看 MySQL 查找 `my.ini` 的路径顺序 | | MySQL 错误日志 | 查看日志文件中记录的配置文件路径 | | 手动检查配置路径 | 检查 MySQL 默认查找路径中是否存在 `my.ini` | --- ## ✅ 示例代码:连接 MySQL 查询 `config_file` ```sql -- 连接 MySQL mysql -u root -p -- 查询配置文件路径 SHOW VARIABLES LIKE 'config_file'; ``` --- ##
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值