137

Is it possible to clean a mysql innodb storage engine so it is not storing data from deleted tables?

Or do I have to rebuild a fresh database every time?

Bob
  • 5,510
  • 9
  • 48
  • 80
700 Software
  • 85,281
  • 83
  • 234
  • 341

4 Answers4

370

Here is a more complete answer with regard to InnoDB. It is a bit of a lengthy process, but can be worth the effort.

Keep in mind that /var/lib/mysql/ibdata1 is the busiest file in the InnoDB infrastructure. It normally houses six types of information:

InnoDB Architecture

InnoDB Architecture

Many people create multiple ibdata files hoping for better disk-space management and performance, however that belief is mistaken.

Can I run OPTIMIZE TABLE ?

Unfortunately, running OPTIMIZE TABLE against an InnoDB table stored in the shared table-space file ibdata1 does two things:

  • Makes the table’s data and indexes contiguous inside ibdata1
  • Makes ibdata1 grow because the contiguous data and index pages are appended to ibdata1

You can however, segregate Table Data and Table Indexes from ibdata1 and manage them independently.

Can I run OPTIMIZE TABLE with innodb_file_per_table ?

Suppose you were to add innodb_file_per_table to /etc/my.cnf (my.ini). Can you then just run OPTIMIZE TABLE on all the InnoDB Tables?

Good News : When you run OPTIMIZE TABLE with innodb_file_per_table enabled, this will produce a .ibd file for that table. For example, if you have table mydb.mytable witha datadir of /var/lib/mysql, it will produce the following:

  • /var/lib/mysql/mydb/mytable.frm
  • /var/lib/mysql/mydb/mytable.ibd

The .ibd will contain the Data Pages and Index Pages for that table. Great.

Bad News : All you have done is extract the Data Pages and Index Pages of mydb.mytable from living in ibdata. The data dictionary entry for every table, including mydb.mytable, still remains in the data dictionary (See the Pictorial Representation of ibdata1). YOU CANNOT JUST SIMPLY DELETE ibdata1 AT THIS POINT !!! Please note that ibdata1 has not shrunk at all.

InnoDB Infrastructure Cleanup

To shrink ibdata1 once and for all you must do the following:

  1. Dump (e.g., with mysqldump) all databases into a .sql text file (SQLData.sql is used below)

  2. Drop all databases (except for mysql and information_schema) CAVEAT : As a precaution, please run this script to make absolutely sure you have all user grants in place:

    mkdir /var/lib/mysql_grants
    cp /var/lib/mysql/mysql/* /var/lib/mysql_grants/.
    chown -R mysql:mysql /var/lib/mysql_grants
    
  3. Login to mysql and run SET GLOBAL innodb_fast_shutdown = 0; (This will completely flush all remaining transactional changes from ib_logfile0 and ib_logfile1)

  4. Shutdown MySQL

  5. Add the following lines to /etc/my.cnf (or my.ini on Windows)

    [mysqld]
    innodb_file_per_table
    innodb_flush_method=O_DIRECT
    innodb_log_file_size=1G
    innodb_buffer_pool_size=4G
    

    (Sidenote: Whatever your set for innodb_buffer_pool_size, make sure innodb_log_file_size is 25% of innodb_buffer_pool_size.

    Also: innodb_flush_method=O_DIRECT is not available on Windows)

  6. Delete ibdata* and ib_logfile*, Optionally, you can remove all folders in /var/lib/mysql, except /var/lib/mysql/mysql.

  7. Start MySQL (This will recreate ibdata1 [10MB by default] and ib_logfile0 and ib_logfile1 at 1G each).

  8. Import SQLData.sql

Now, ibdata1 will still grow but only contain table metadata because each InnoDB table will exist outside of ibdata1. ibdata1 will no longer contain InnoDB data and indexes for other tables.

For example, suppose you have an InnoDB table named mydb.mytable. If you look in /var/lib/mysql/mydb, you will see two files representing the table:

  • mytable.frm (Storage Engine Header)
  • mytable.ibd (Table Data and Indexes)

With the innodb_file_per_table option in /etc/my.cnf, you can run OPTIMIZE TABLE mydb.mytable and the file /var/lib/mysql/mydb/mytable.ibd will actually shrink.

I have done this many times in my career as a MySQL DBA. In fact, the first time I did this, I shrank a 50GB ibdata1 file down to only 500MB!

Give it a try. If you have further questions on this, just ask. Trust me; this will work in the short term as well as over the long haul.

CAVEAT

At Step 6, if mysql cannot restart because of the mysql schema begin dropped, look back at Step 2. You made the physical copy of the mysql schema. You can restore it as follows:

mkdir /var/lib/mysql/mysql
cp /var/lib/mysql_grants/* /var/lib/mysql/mysql
chown -R mysql:mysql /var/lib/mysql/mysql

Go back to Step 6 and continue

UPDATE 2013-06-04 11:13 EDT

With regard to setting innodb_log_file_size to 25% of innodb_buffer_pool_size in Step 5, that's blanket rule is rather old school.

Back on July 03, 2006, Percona had a nice article why to choose a proper innodb_log_file_size. Later, on Nov 21, 2008, Percona followed up with another article on how to calculate the proper size based on peak workload keeping one hour's worth of changes.

I have since written posts in the DBA StackExchange about calculating the log size and where I referenced those two Percona articles.

Personally, I would still go with the 25% rule for an initial setup. Then, as the workload can more accurate be determined over time in production, you could resize the logs during a maintenance cycle in just minutes.

Community
  • 1
  • 1
RolandoMySQLDBA
  • 43,883
  • 16
  • 91
  • 132
  • 9
    I've also used the innodb_file_per_table option to great effect, having 200 databases with 200 tables each on a single server, I was able to symlink difference databases onto different partitions, therefore using more IO buffers and spindles that would otherwise have been available :) – Dave Rix Oct 06 '11 at 13:40
  • This is a great solution. We were running out of diskspace on two of our machines and this solution cleared about a third of disk free. It's a scary process since you are deleting your databases, but it works! We've even seen a slight performance boost – SeanDowney May 11 '12 at 20:33
  • 3
    @SeanDowney BTW remember to raise `innodb_open_tables` if necessary. The default is 300. – RolandoMySQLDBA May 11 '12 at 20:36
  • For step 1 could one use: `mysqldump -u root -p --add-drop-database --all-databases > all_dbs.sql`? Also, as per http://stackoverflow.com/questions/67117/speeding-up-mysql-dumps-and-imports would adding `innodb_flush_log_at_trx_commit = 2` make the import faster? – giorgio79 May 29 '12 at 06:03
  • 2
    @giorgio79 you need to set your bulk insert to a larger value. This is a good point. I will add the essence of your question to my answer. – RolandoMySQLDBA May 30 '12 at 00:41
  • note that on windows this is the my.ini file, and not having an "=..." will cause the mysql service to be unable to run (you cannot have assignmentless properties in windows .ini files). If you get a 1067 error, use innodb_file_per_table=true – Mike 'Pomax' Kamermans Jan 12 '13 at 16:05
  • Thanks, but I still don't get it. In step 2. should I also drop the mysql-db (but keeping the folder `mysql` in `/var/lib/mysql`)? – 244an Jan 19 '13 at 22:26
  • Yes. I know it sound redundant. Step 2 cleans up databases as seen by mysql. I included dropping folders in case DB users create extraneous folders that are not mysql accessible. In any case, the innodb files must go. Please make sure /var/lib/mysql/mysql remains prior to startup of mysql. – RolandoMySQLDBA Jan 19 '13 at 22:41
  • Thanks so far. I'm doing it right now on a "real" server so I'm a bit nervous. There is still a folder `performance_schema` in `/var/lib/mysql`, I didn't drop that db since it isn't included in the dump, should I delete that db to? And delete the folder? (BTW, the `mysql` folder was gone after deleting the db, let's see how it all ends :) – 244an Jan 19 '13 at 22:56
  • Did you leave `/var/lib/mysql/mysql` in place ? Did you remove ibdata1 and ib_logfile* ? Bigger question? Did you `mysqldump ---databases`. – RolandoMySQLDBA Jan 20 '13 at 13:23
  • I added the innodb file per table flag and then altered all my innodb tables to be engine=innodb which had the effect of putting them each in their own table; I verified this by looking for a file named table.ibd for each table reported by mysql to be using the innodb engine (SELECT table_name FROM information_schema.tables WHERE engine=’innodb). At this point I figure all my innodb data is out of the shared space so is it safe to stop mysqld, mv ibdata* and ib_log* out of the way and restart mysqld? – Ram Jan 21 '13 at 01:05
  • @Ram PLEASE DO NOT ERASE `ibdata1`. I'll update my answer as to why. See the section `Can I run OPTIMIZE TABLE with innodb_file_per_table ?` – RolandoMySQLDBA Jan 21 '13 at 04:36
  • I ended up just doing the regular process: mysqldump mydb > mydb.sql, stop mysql, delete ib* mydb/*frm mydb/*ibd, start mysql, mysql mydb < mydb.sql. thanks – Ram Jan 21 '13 at 06:14
  • 4
    In 32 bits systems a 4Gb value for *innodb_buffer_pool_size* is not allowed. Mysql will silently start with innodb disabled and the tables restored will be changed to myisam. Use a slightly smaller value to fix it. – David Jan 30 '13 at 13:13
  • 1
    @David Of course, you are right. I discussed that aspect in the DBA StackExchange back on `April 14, 2011`: http://dba.stackexchange.com/a/2194/877 – RolandoMySQLDBA Jan 30 '13 at 13:47
  • #2 could be rewritten as one command: `cp -a /var/lib/mysql/mysql/ /var/lib/mysql_grants/` – Andrew Ensley Apr 26 '13 at 19:49
  • 2
    The MySQL performance blog states that "the common advice to size the logs as a fraction of the buffer pool size is just wrong." http://www.mysqlperformanceblog.com/2008/11/21/how-to-calculate-a-good-innodb-log-file-size/ – mak Jun 04 '13 at 13:00
  • @mak I know. I learned that later. I wrote about it in the DBA StackExchange back on Aug 27, 2012 : http://dba.stackexchange.com/questions/23189/proper-tuning-for-30gb-innodb-table-on-server-with-48gb-ram/23214#23214 – RolandoMySQLDBA Jun 04 '13 at 13:04
  • @mak Thank for pointing that out. I need to reference that in point #5 of this answer. As you can tell, it is an old answer from Oct 29, 2010. – RolandoMySQLDBA Jun 04 '13 at 13:07
  • 5
    Good god. I just wanna say this is perhaps one of the best answers i've ever seen on S.O. Damn fine job, sir. Helped me figure out a solution to my problem when I was getting an ERROR 2013 (HY000) when importing a 154g db. Thanks for the excellent answer! – Josh Brown Apr 26 '14 at 00:32
  • You set `innodb_fast_shutdown=0;` to process the transaction logs when MySQL is stopped. Is it possible some changes in the logs will be lost when the databases are restored, since the backup is taken beforehand? – shinypenguin Jun 19 '15 at 00:09
  • Can this 'InnoDB Infrastructure Cleanup' help reducing the size of these files mentioned here: http://dba.stackexchange.com/questions/83353/what-does-fts-file-mean-after-converting-from-myisam-to-innodb – SAVAFA Jan 29 '17 at 19:58
  • @RolandoMySQLDBA great great post, thanks so much! One question: what would you do if you have a mixture of innodb (created before having innodb_file_per_table) and myisam tables? Would re-import the exported individual tables shrink the idata size, and if we're converting all myisam tables now, would it write to idata or the corresponding idb files? – David Zhao Mar 20 '19 at 19:26
  • @DavidZhao Nope. ibdata1 gets bloated for tables inside plus undo logs gone wold. Please see my post in the DBA StackExchange. https://dba.stackexchange.com/questions/40730/how-can-innodb-ibdata1-file-grows-by-5x-even-with-innodb-file-per-table-set/40734#40734 – RolandoMySQLDBA Mar 20 '19 at 19:29
  • @RolandoMySQLDBA how about copy the table a to table b (new table), then drop table a and rename table b to a? – David Zhao Mar 22 '19 at 21:04
  • @RolandoMySQLDBA Why is it necessary to drop all of the databases (except mysql) before shutting down the service, if you will later remove the data files? Also does the mysql schema get dumped and restored by mysqldump? Great post, I may need to do this soon to properly support utf8mb4 / Barracuda / large indexes. I don't know if I need to do anything special with the mysql schema though. – jamieburchell Aug 21 '20 at 07:38
  • @jamieburchell You should never drop the mysql schema. That's where the grants live. It would be highly recommended to dump the mysql schema anyway. You need to mysqldump the mysql schema manually in a separate file. Even better, use pt-show-grants to dump grants. For MySQL 8.0, every table in mysql schema uses InnoDB storage engine. I had recommended dropping the ibdata1 file (system tablespace) a file that can grow without restraint (See my other post https://dba.stackexchange.com/questions/40730/how-can-innodb-ibdata1-file-grows-by-5x-even-with-innodb-file-per-table-set/40734#40734) – RolandoMySQLDBA Aug 21 '20 at 16:25
  • @jamieburchell Please read Percona Docs on pt-show-grants : https://www.percona.com/doc/percona-toolkit/LATEST/pt-show-grants.html – RolandoMySQLDBA Aug 21 '20 at 16:26
  • @RolandoMySQLDBA I know the mysql schema shouldn't get dropped, I was querying why you suggest dropping the other schemas if you are then saying to remove the actual files afterwards anyway. – jamieburchell Aug 21 '20 at 18:51
5

The InnoDB engine does not store deleted data. As you insert and delete rows, unused space is left allocated within the InnoDB storage files. Over time, the overall space will not decrease, but over time the 'deleted and freed' space will be automatically reused by the DB server.

You can further tune and manage the space used by the engine through an manual re-org of the tables. To do this, dump the data in the affected tables using mysqldump, drop the tables, restart the mysql service, and then recreate the tables from the dump files.

bigjeff
  • 81
  • 1
2

I follow this guide for a complete reset (as root):

mysqldump --all-databases --single-transaction | gzip -c > /tmp/mysql.all.sql.gz
service mysql stop
mv /var/lib/mysql /var/lib/mysql.old; mkdir -m700 /var/lib/mysql; chown mysql:mysql /var/lib/mysql

mysql_install_db                # mysql 5.5
mysqld --initialize-insecure    # mysql 5.7

service mysql start
zcat /tmp/mysql.all.sql.gz | mysql
service mysql restart
ggrandes
  • 2,067
  • 22
  • 16
1

What nobody seems to mention is the impact innodb_undo_log_truncate setting can have.

Take a look at my answer at How to shrink/purge ibdata1 file in MySQL.

Slam
  • 389
  • 3
  • 6