larry*wei 2010-08-11 07:20 采纳率: 0%
浏览 1024
已采纳

如何在 MySQL 中缩小 / 清除 ibdata1文件

I am using MySQL in localhost as a "query tool" for performing statistics in R, that is, everytime I run a R script, I create a new database (A), create a new table (B), import the data into B, submit a query to get what I need, and then I drop B and drop A.

It's working fine for me, but I realize that the ibdata file size is increasing rapidly, I stored nothing in MySQL, but the ibdata1 file already exceeded 100 MB.

I am using more or less default MySQL setting for the setup, is there a way for I can automatically shrink/purge the ibdata1 file after a fixed period of time?

转载于:https://stackoverflow.com/questions/3456159/how-to-shrink-purge-ibdata1-file-in-mysql

  • 写回答

8条回答 默认 最新

  • Didn"t forge 2010-08-11 09:07
    关注

    That ibdata1 isn't shrinking is a particularly annoying feature of MySQL. The ibdata1 file can't actually be shrunk unless you delete all databases, remove the files and reload a dump.

    But you can configure MySQL so that each table, including its indexes, is stored as a separate file. In that way ibdata1 will not grow as large. According to Bill Karwin's comment this is enabled by default as of version 5.6.6 of MySQL.

    It was a while ago I did this. However, to setup your server to use separate files for each table you need to change my.cnf in order to enable this:

    [mysqld]
    innodb_file_per_table=1
    

    http://dev.mysql.com/doc/refman/5.5/en/innodb-multiple-tablespaces.html

    As you want to reclaim the space from ibdata1 you actually have to delete the file:

    1. Do a mysqldump of all databases, procedures, triggers etc except the mysql and performance_schema databases
    2. Drop all databases except the above 2 databases
    3. Stop mysql
    4. Delete ibdata1 and ib_log files
    5. Start mysql
    6. Restore from dump

    When you start MySQL in step 5 the ibdata1 and ib_log files will be recreated.

    Now you're fit to go. When you create a new database for analysis, the tables will be located in separate ibd* files, not in ibdata1. As you usually drop the database soon after, the ibd* files will be deleted.

    http://dev.mysql.com/doc/refman/5.1/en/drop-database.html

    You have probably seen this:
    http://bugs.mysql.com/bug.php?id=1341

    By using the command ALTER TABLE <tablename> ENGINE=innodb or OPTIMIZE TABLE <tablename> one can extract data and index pages from ibdata1 to separate files. However, ibdata1 will not shrink unless you do the steps above.

    Regarding the information_schema, that is not necessary nor possible to drop. It is in fact just a bunch of read-only views, not tables. And there are no files associated with the them, not even a database directory. The informations_schema is using the memory db-engine and is dropped and regenerated upon stop/restart of mysqld. See https://dev.mysql.com/doc/refman/5.7/en/information-schema.html.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。