`

innodb data file per table

阅读更多

One very interesting thing I noticed with MySQL was that if you delete a database, ibdata file doesn’t shrink by that much space to minimize disk usage. I deleted the database and checked usage of /usr/local/mysql/var folder and noticed that ibdata file is still the same size. So the problem I face now is, how do I claim back this space?

After searching for a bit on google about this problem, apparently only way you can do that is by exporting your mysql databases, delete ibdata1 file, import databases. This creates new ibdata file with correct space usage. Atleast there is a way to get around this issue. But honestly, too much pain on production boxes where we might be trying to remove old databases to reclaim some of the hard drive space.

An preventive measure one can use is to use option: innodb_file_per_table (’put innodb_file_per_table’ in your my.cnf file under [mysqld] section). This will create individual files for tables under database directory. So now when I delete the database, all the space is returned since the directory is now deleted along with database along with all the tables inside the directory. In my test after you put option innodb_file_per_table your my.cnf, you will have to still do export/import to be able to minimize disk usage and have the ability to delete database at your leisure without worrying about reclaiming the disk space. Here are the steps I took. DISCLAIMER: Please make backup of your data and use following steps at your own risk. Doing it on test server is HIGHLY recommended. Please don’t come back and tell me that you lost your data because you followed my steps. They work for me and they may not work for you !

That said, here are the steps:

  1. Add innodb_file_per_table in your my.cnf file under [mysqld] section
  2. run following commands at the prompt. Your path to binaries might be different.
  3. #note: following assumes you are logged in as root
    mkdir -p /temp #temp dir to save our sql dump
    #lets make a backup of current database. -p is used if there is pw set
    /usr/local/mysql/bin/mysqldump -R -q -p --all-databases > /temp/all.sql
    #stop mysql so we can remove all the files in the dir
    /etc/init.d/mysql stop
    rm -fr /usr/local/mysql/var/* #remove all the files
    /usr/local/mysql/bin/mysql_install_db #install default dbs
    #change ownership so mysql user can read/write to/from files
    chown -R mysql.mysql /usr/local/mysql/var/
    #start mysql so we can import our dump
    /etc/init.d/mysql start
    #note there is no -p since defaults don't have mysql pw set
    /usr/local/mysql/bin/mysql < /temp/all.sql
    /etc/init.d/mysql restart

This should be all you need to do. At this point when you remove a database, it will delete the directory of the db and all the data contained within which in turn will give you your disk space back.

REMEMBER: Backup your data and be smart about using code found on internet. If you don’t know what you are doing, hire a consultant who does.

分享到:
评论

相关推荐

    MySql innodb 引擎表存储分析

    - 当启用 `innodb_file_per_table` 参数后,每个表的数据和索引将存储在单独的 `.ibd` 文件中。而其他一些元数据,如撤销 (undo) 信息和系统事务信息,则仍然存储在共享表空间内。 - 数据段 (Data Segment) 和索引...

    MySQL数据库文件放在哪里.docx

    5. **.ibd文件**(InnoDB引擎,file per table模式):当启用file per table配置时,每个InnoDB表将拥有自己的.ibd文件,其中包含该表的数据和索引。 6. **日志文件**:MySQL的日志文件通常有`ib_logfile0`和`ib_...

    数据库优化配置.doc

    [client] port=3306 [mysql] no-beep default-character-set=utf8 [mysqld] datadir=D:/Data port=3306 server-id=...log_file_size=1G innodb_log_buffer_size=8M innodb_flush_log_at_trx_commit=2 innodb_file_per_t

    mysql参数及其优化

    query_cache_size、query_cache_type、innodb_buffer_pool_size、innodb_log_file_size、innodb_log_buffer_size、innodb_flush_logs_at_trx_commit、transaction_isolation、innodb_file_per_table、innodb_open_...

    优化InnoDB表BLOB,TEXT列的存储效率

    启用 innodb_file_per_table 选项,5.5以后可以在线动态修改生效,并且执行 ALTER TABLE xx ENGINE = InnoDB 将现有表转成独立表空间,早于5.5的版本,修改完这个选项后,需要重启才能生效。 2、InnoDB的data page...

    MySQL的InnoDB扩容及ibdata1文件瘦身方案完全解析

    3. **未启用`innodb_file_per_table`**:所有表的数据和索引都会存储在`ibdata1`,而不是单独的表空间文件。 #### 瘦身策略 1. **清理无用事务**:定期检查并结束长时间运行的事务。 2. **启用`innodb_file_per_...

    MySQL提示The InnoDB feature is disabled需要开启InnoDB的解决方法

    - 配置文件中的其他设置可能会影响InnoDB的性能和行为,如`innodb_file_per_table`,`innodb_buffer_pool_size`等,根据实际需求进行调整。 通过以上步骤,你应该能够解决"The InnoDB feature is disabled"的问题...

    探讨:innodb与myisam在存储上有何特点和区别

    如果启用`innodb_file_per_table`选项,那么每个表将拥有自己的独立表空间,数据存储在`.ibd`文件中,这有助于管理和优化磁盘空间,同时允许更灵活的备份策略。 InnoDB对数据的存储方式使其在事务处理上具有优势,...

    MySQL InnoDB表空间加密示例详解

    innodb_file_per_table=1 # 仅对独立表空间生效 ``` 然后,创建并设置加密所需路径的权限: ```shell mkdir -p /data/mysql3306/keyring chown -R mysql:mysql /data/mysql3306/keyring chmod 750 /data/mysql3306...

    Linux安装mysql压缩包安装

    Linux下安装mysql安装包安装及详细步骤 (有安装包及步奏 问题解答) ...innodb_file_per_table=1 lower_case_table_names=1 character_set_server=utf8 init_connect='SET NAMES utf8' max_allowed_packet = 16M

    InnoDB 类型MySql恢复表结构与数据

    2. `.ibd` 文件:当InnoDB引擎启用独立表空间(通过设置`innodb_file_per_table = 1`)时,每个表的数据和索引都会被存储在这个文件中。`.ibd`文件包含了表的数据行、B+树索引等信息。 恢复InnoDB表结构的步骤: 1....

    MySQL问答系列之如何避免ibdata1文件大小暴涨

    当innodb_file_per_table选项打开的话,新创建表的数据和索引则不会存在系统表空间中,而是存放在各自表的.ibd文件中. 显然这个文件会越来越大,innodb_autoextend_increment选项则指定了该文件每次自动增长的步进,...

    Mysql数据库的使用总结之ERROR 1146.pdf

    首先,取消`skip-innodb`的注释,然后确保设置正确的InnoDB参数,如`innodb_file_per_table`等。 在复制数据目录后,直接运行`mysql --console`会显示错误,因为InnoDB的日志文件和数据文件可能与新环境不兼容。`...

    浅析mysql 共享表空间与独享表空间以及他们之间的转化

    需要注意的是,这个设置仅对新创建的表有效,对于已经创建的表,即使更改了innodb_file_per_table参数,也不会影响已经存在的表。 转换共享表空间和独享表空间时,可以手动进行,但是需要注意,一旦共享表空间中的...

    mysql 5.5 配置文档

    21. `innodb_file_per_table`: 如果设为 1,每个 InnoDB 表都有独立的数据文件,便于管理和备份。 22. `innodb_flush_log_at_trx_commit`: 控制事务日志刷新策略,以平衡性能和数据一致性。 23. `innodb_log_...

    Mysql安装入门

    innodb_file_per_table=1 innodb_flush_log_at_trx_commit=2 sync_binlog=1000 transaction-isolation=READ-COMMITTED innodb_flush_method=O_DIRECT innodb_io_capacity_max=4000 innodb_log_buffer_size=8M...

    mysql数据库配置模板

    7. **innodb_file_per_table**: 推荐启用此选项,可以使每个InnoDB表使用独立的数据文件,便于管理和备份。 综上所述,通过对`my.cnf`配置文件中各项参数的合理设置和优化,可以显著提升MySQL服务器的性能表现和...

    安装笔记:centos6+zabbix3.x

    - 可通过设置`innodb_file_per_table`为1来启用每个表一个文件(.ibd文件)模式。 - 修改MySQL配置文件`/etc/my.cnf`中的`[mysqld]`部分,添加或修改如下行: ```ini innodb_file_per_table = 1 ``` - **注**...

    mysql-8.0.27安装

    innodb_file_per_table=1 log_bin_trust_function_creators=1 [mysqld_safe] ``` 使用 systemctl 命令启动 MySQL 使用 systemctl 命令启动 MySQL 服务: ```bash systemctl start mysqld ``` 使用 systemctl ...

Global site tag (gtag.js) - Google Analytics