英文:
MySQL "data" folder keeps growing, even after DROP TABLE
问题
我正在使用Python 3和mysql.connector
包与MySQL(8.0.33-winx64,适用于Windows)。
最初,我的mysql-8.0.33-winx64\data
文件夹相当小:< 100 MB。
然后,在进行了一些CREATE TABLE...
,INSERT...
和DROP TABLE...
的测试之后,我注意到即使我完全删除了表,data
文件夹仍然在增长:
#innodb_redo
文件夹似乎保持在最大100 MB。#innodb_temp
似乎很小。binlog.000001
:这个文件似乎是罪魁祸首,即使我删除表,它仍然在增长!
在删除表后,如何清理这个数据存储,以避免MySQL中的未使用磁盘空间?
是否可以直接使用Python3的mysql.connector
API进行操作?还是需要执行SQL命令(我已经尝试过"OPTIMIZE"但没有成功)?或者我需要手动使用操作系统功能(例如os.remove(...)
)?
英文:
I'm using MySQL (8.0.33-winx64, for Windows) with Python 3 and mysql.connector
package.
Initially my mysql-8.0.33-winx64\data
folder was rather small: < 100 MB.
Then after a few tests of CREATE TABLE...
, INSERT...
and DROP TABLE...
, I notice that even after I totally drop the tables, the data
folder keeps growing:
#innodb_redo
folder seems to stay at max 100 MB#innodb_temp
seems to be smallbinlog.000001
: this one seems to be the culprit: it keeps growing even if I drop tables!
How to clean this data store after I drop tables, to avoid unused disk space with MySQL?
Is it possible directly from Python3 mysql.connector
API? Or from a SQL command to be execute (I already tried "OPTIMIZE" without success)? Or do I need to use an OS function manually (os.remove(...)
)?
<strike>Note: the config file seems to be in mysql-8.0.33-winx64\data\auto.cnf
in the portable Windows version (non-used as a service, but started with mysqld --console
)</strike> (no default config file is created after a first run of the server, we can create it in mysql-8.0.33-winx64\my.cnf
)
答案1
得分: 3
你可以通过在my.cnf
文件中设置disable_log_bin
并重新启动MySQL服务器来禁用二进制日志。你无法动态更改二进制日志。有关详细信息,请参见此链接。在二进制日志滚动到新的二进制日志文件时,你可以使旧日志自动过期,以限制总体存储。有关更多信息,请查阅此链接。在决定禁用二进制日志之前,你需要了解二进制日志的用途,因为你可能会需要它。
二进制日志通常用于以下三到四个方面:
- 复制
- 恢复到特定时间点的数据
- 变更数据捕获(CDC)工具
- 一种较差的变更审计形式。实际的审计日志更好,但是有些站点未安装审计日志插件。
关于撤销日志和二进制日志的区别,二进制日志用于记录数据的逻辑更改,只有在提交事务时才会写入二进制日志。它不用于回滚,因为根据定义,二进制日志中的任何内容都已经提交。而撤销日志仅用于InnoDB存储引擎,当你在事务中对数据进行更改时,旧版本的数据会被添加到撤销日志中(在一些文档中也称为回滚段)。因此,如果回滚事务,InnoDB可以恢复原始数据。如果提交事务,则该事务的撤销日志内容将被丢弃。
注意:
- 在
my.cf
中设置disable_log_bin
并重新启动MySQL服务器不会删除旧的二进制日志。 - 如果首先设置
disable_log_bin
并重新启动服务器,然后执行FLUSH LOGS; PURGE BINARY LOGS BEFORE NOW();
,它不会删除旧的二进制日志。 - 你必须首先执行
FLUSH LOGS; PURGE BINARY LOGS BEFORE NOW();
,然后才能编辑mysql-8.0.33-winx64\my.cnf
配置文件,包含以下内容:[mysqld] disable_log_bin
然后旧日志将被删除,不会创建新的二进制日志。
英文:
You can disable the binary log, but only by setting disable_log_bin
in your my.cnf
file and restarting the MySQL Server. (See Disable MySQL binary logging with log_bin variable
) You can't change binary logging dynamically. See https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#sysvar_log_bin
You can make the binary log automatically expire old logs as it rolls over to a new binlog file. This helps to limit the overall storage. See https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#sysvar_binlog_expire_logs_seconds
You do need to understand what the binary log is used for before you decide to disable it. You might need it!
The binary log is commonly used for three or four things:
-
Replication
-
Point-in-time recovery
-
Change Data Capture (CDC) tools
-
A poor form of change auditing. A real audit log is better, but some sites don't have the audit log plugin installed.
> can you edit to clarify the difference between undo log vs. bin log?
The binary log is for logging logical changes to your data. Nothing is written to the binary log until you COMMIT a transaction. It is not used for rollback, because by definition anything in the binary log has been committed. The binary log applies to all storage engines in MySQL.
The undo log is only for the InnoDB storage engine. As you make changes to data during a transaction, the old version of the data is added to the undo log (this is also called the rollback segment in some documentation). So if you ROLLBACK, InnoDB can restore the original data. If you COMMIT, then the contents of the undo log for that transaction is discarded.
Notes:
-
setting
disable_log_bin
inmy.cf
and restarting the MySQL server won't delete old binlogs. -
if you set
disable_log_bin
and restart the server first, and then doFLUSH LOGS; PURGE BINARY LOGS BEFORE NOW();
, it won't delete old binlogs -
you have to do
FLUSH LOGS; PURGE BINARY LOGS BEFORE NOW();
first, and only then edit the configmysql-8.0.33-winx64\my.cnf
to include:[mysqld] disable_log_bin
Then the old logs are deleted, and no new binlog will be created.
答案2
得分: 2
二进制日志用于存储写入更改。它仅存储完整的事务,因此不包含部分写入。如果发生意外情况,比如您的表被删除,那么二进制日志将非常有用,因为您可以使用它来恢复表。它还用于复制目的。请参阅https://dev.mysql.com/doc/refman/8.0/en/binary-log.html
现在,如果您确信不再需要二进制日志,那么可以清除它们,例如:
FLUSH LOGS;
PURGE BINARY LOGS BEFORE NOW();
请参阅https://dev.mysql.com/doc/refman/8.0/en/purge-binary-logs.html
英文:
The binlog is being used to store write changes. It only stores full transactions, so partial writes are not in it. If something happens accidentally, like your tables are being dropped, then the binlog will be useful so you will be able to recover them. It is also useful for replication purposes. See https://dev.mysql.com/doc/refman/8.0/en/binary-log.html
Now, if you are absolutely sure you no longer need the binlogs, then you can purge them, like
FLUSH LOGS;
PURGE BINARY LOGS BEFORE NOW();
see https://dev.mysql.com/doc/refman/8.0/en/purge-binary-logs.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论