72

I have some Production servers whose ibdata files increase in size day by day.

It has already consumed 290GB of space.

The tables in the servers are mostly InnoDB and there are high read and write requests.

The log file size also increasing. There is a huge amount of Data in the Tables.

How can I control the growing size of both?

I am not using innodb_file_per_table.

0

1 Answer 1

120

Keep in mind the busiest file in the InnoDB infrastructure is /var/lib/mysql/ibdata1

This file normally houses many classes of information (when innodb_file_per_table is 0)

  • Table Data
  • Table Indexes
  • MVCC (Multiversioning Concurrency Control) Data
    • Rollbacks Segments
    • Undo Tablespace
  • Table Metadata
  • See Pictorial Representation

Many people create multiple ibdata files hoping for better diskspace management and performance. It does not help.

Unfortunately, OPTIMIZE TABLE against an InnoDB table stored in ibdata1 does two things:

  • Makes the table's data and indexes contiguous inside ibdata1
  • It makes ibdata1 grow because the contiguous data is appended to ibdata1

You can segregate Table Data and Table Indexes from ibdata1 and manage them independently using innodb_file_per_table. To shrink ibdata1 once and for all you must do the following

Step 01) MySQLDump all databases into a SQL text file (call it SQLData.sql) (More details here)

Step 02) Drop all databases (except mysql, performance_schema, and information_schema)

Step 03) Shutdown mysql

Step 04) Add the following lines to /etc/my.cnf

[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.

Step 05) Delete ibdata1, ib_logfile0 and ib_logfile1

At this point, there should only be the mysql schema in /var/lib/mysql

Step 06) Restart mysql

This will recreate ibdata1 at 10MB, ib_logfile0 and ib_logfile1 at 1G each

Step 07) Reload SQLData.sql into mysql

ibdata1 will grow but only contain table metadata

Each InnoDB table will exist outside of ibdata1

Suppose you have an InnoDB table named mydb.mytable. If you go into /var/lib/mysql/mydb, you will see two files representing the table

  • mytable.frm (Storage Engine Header)
  • mytable.ibd (Home of Table Data and Table Indexes for mydb.mytable)

ibdata1 will never contain InnoDB data and Indexes anymore.

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 collapsed a 50GB ibdata1 file into 500MB.

Give it a try. If you have further questions on this, email me. Trust me. This will work in the short term and over the long haul. !!!

If you would like to see how much actual data is stored in MyISAM and InnoDB, please run this query:

SELECT IFNULL(B.engine,'Total') "Storage Engine",
CONCAT(LPAD(REPLACE(FORMAT(B.DSize/POWER(1024,pw),3),',',''),17,' '),' ',
SUBSTR(' KMGTP',pw+1,1),'B') "Data Size", CONCAT(LPAD(REPLACE(
FORMAT(B.ISize/POWER(1024,pw),3),',',''),17,' '),' ',
SUBSTR(' KMGTP',pw+1,1),'B') "Index Size", CONCAT(LPAD(REPLACE(
FORMAT(B.TSize/POWER(1024,pw),3),',',''),17,' '),' ',
SUBSTR(' KMGTP',pw+1,1),'B') "Table Size"
FROM (SELECT engine,SUM(data_length) DSize,SUM(index_length) ISize,
SUM(data_length+index_length) TSize FROM information_schema.tables
WHERE table_schema NOT IN ('mysql','information_schema','performance_schema')
AND engine IS NOT NULL GROUP BY engine WITH ROLLUP) B,
(SELECT 3 pw) A ORDER BY TSize;
6
  • 1
    Even with the suggested settings for my.cnf (which I already have), deleting ib_logfile0 and 1 and then starting mysql, the new files are created and within seconds they grow to equal the size of the database as displayed by your above query. Not sure if mysql is copying all tables and indexes to these files while also maintaining each table in a separate file.
    – Allen King
    Commented Jul 7, 2019 at 17:44
  • Thank you for the instructions. They were clear and easy to follow. These procedures reduced my mysql folder size (multiple databases with various engines) from 8.4 GB to 3.2 GB, so generally it is good. ib_logfiles, still consume most of the space although folders are separate for innodb databases, too. Commented Feb 3, 2020 at 9:56
  • I have been running into this problem on a rented vServer. So now I cannot even start the mysql service, that means I cannot do anything on the DB. I just have kb-size databases. but not enought space to even copy the ibdata on the vServer. What do I do?
    – dasLort
    Commented May 21, 2021 at 13:36
  • @dasLort Move all db files to another machine and complete the process there. Or create a temporary additional drive and attach it to the VM to complete the process.
    – KyferEz
    Commented Sep 28, 2021 at 22:50
  • 1
    I have MariaDB 10.3 and this solution doesn't work for me. I'm on a test database without any table and I did a 1 hour benchmarking and the ibdata1 got from 12mb to 14gb, and test database directory from 4kb to 3,5 gb. No Idea why.
    – TimLer
    Commented May 5, 2022 at 10:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.