FeaturesWP CloudPricingEE CLIContactMy Account
Features
Team Access & User Permissions
Install
Resources
Community
CLEAR
Tutorials
Mysql
Enable innodb_file_per_table
Topics
On this page
-
- Getting Started
- Git and File-System Layout (non-default setup)
- Version Constraints
- Managing Themes and Plugins (for entire WordPress site)
- Updating Themes and Plugins (for entire WordPress site)
- Adding Composer Support to Your Own Themes and Plugins (for developers)
- Using Composer to Manage Own Theme/Plugin Dependencies (for developers)
-
- Assigning Multiple IP Addresses to Single LAN Card
- Disable IPv6 on Ubuntu 12.04
- dsh – distributed shell
- fdupes – find & replace duplicate files with hardlinks
- GPG Keys Cheatsheet
- Increase "Open Files Limit"
- kill all php, nginx, mysql or any kind of processes
- Passwordless Authentication for SSH
- Screen
- search replace in multiple files useing grep xargs sed
- Setup sftp
- sysctl.conf
- Timezone Sync
- Using Google Public DNS on Ubuntu Server
- Using openssl to match private key, cerificate and CSR
- Configure Postfix to Use Gmail SMTP on Ubuntu
- Logrotate Example for Custom Logs
- PHP 5.5, MySQL, Postfix, Nginx and WordPress on Ubuntu
- Serverdensity
- Upgrade Ubuntu 10.04 to 12.04
- Using find and sed to replace strings in multiple files
- Webmin
-
- .my.cnf – mysql user & password
- Analyse slow-query-log using Anemometer
- Improving MySQL Query Cache
- MySQL Query Profiling
- Reset MySQL root password
- tuning-primer.sh – an alternative for mysqltuner
- Using MariaDB 10
- Install/Upgrade to MySQL 5.6 on Ubuntu 12.04 LTS
- Change WordPress Domain Name
- Character Sets and Collations
- Enable innodb_file_per_table
- Enable Remote Access (Grant)
- Installing Percona Toolkit
- Using tmpfs for temporary folder creation
- YARPP and InnoDB
- Convert MyISAM to InnoDB
- Using tmpfs for mysqldump & mysqlimport
- Convert from INNODB to MYISAM
- Analyse slow-query-log using mysqldumpslow & pt-query-digest
- mysqlcheck with cron to optimize automatically
- Using MySQLTuner to Optimize MySQL configuration
-
-
- Directly connect to PHP-FPM
- Generating core-dump for php5-fpm
- GeoIP
- Having php5 and php7 on same system.
- Install Xdebug and configure it with webgrind
- Installation of PHP Code Review Tools
- memcache
- PHP's Zend Opcache Config & Web Viewer
- Setting Sqlite support with PHP
- Using HHVM with PHP-FPM Fallback
- Using Redis for PHP Sessions on Ubuntu Server
- Installing PhpMyAdmin Quickly
- Moving PHP's session storage to tmpfs
- Install Xdebug and configure it with Netbeans
- Nginx – Enable PHP-FPM Status Page
- PHP-FPM: Socket vs TCP/IP and sysctl tweaking
- APC Cache Optimization & Monitoring Using Web Interface
- Checking if PHP/WordPress can send mails
- Debugging PHP Scripts Using slow_log and more
- Increase file upload size limit in PHP-Nginx
- Increase PHP script execution time with Nginx
-
- Performance Optimization
- Remove Query string of css/js files
- Setup ElasticSearch with ElasticPress
- Using Redis for WordPress Object-Cache
- WooCommerce Window Shopping Caching Technique
- Moving WordPress To New Server (Faster)
- Better wp-cron using linux's crontab
- 404 error- Page not Found!!!
- Debugging WordPress with Nginx
Backup First
Copy mysql data files (raw)
Take mysqldump
Drop Databases
Remove InnoDB files
Enable innodb_file_per_table
Time to import from mysqldump
Enable innodb_file_per_table
Note: You may wish to convert MyISAM tables to InnoDB tables before you proceed.
innodb_file_per_table is by default ON Mysql 5.6.6 and onwards. There is plenty of stuff on Google about pros & cons ofinnodb_file_per_table.
This post details how to enable innodb_file_per_table on an existing database. Because innodb_file_per_table affects new tables only, created after innodb_file_per_table is enabled, we need to recreate old databases to force innodb_file_per_table on old tables and reclaim some disk space.
Backup First
Create a dir to take backups:
cd ~
mkdir backup
cd backup
Copy mysql data files (raw)
If all goes well, we will not need this.
For better results, shut down PHP and other apps/scripts which update mysql. You can keep Nginx running and server non-logged in visitors cached content.
service mysql stop && cp -ra /var/lib/mysql mysqldata && service mysql start
Take mysqldump
As soon as above line completes, take a mysqldump of all databases
mysqldump --routines --events --flush-privileges --all-databases > all-db.sql
Drop Databases
Create a sql file to drop all databases EXCEPT mysql database
mysql -e "SELECT DISTINCT CONCAT ('DROP DATABASE ',TABLE_SCHEMA,' ;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA 'mysql' AND TABLE_SCHEMA 'information_schema';" | tail -n+2 > drop.sql
Verify if drop.sql has correct database names and then execute drop.sql queries.
mysql < drop.sql
Verify all InnoDB tables gone
SELECT table_name, table_schema, engine FROM information_schema.tables WHERE engine = 'InnoDB';
Remove InnoDB files
Stop mysql server first
service mysql stop
Then
rm /var/lib/mysql/ibdata1 && rm /var/lib/mysql/ib_logfile0 && rm /var/lib/mysql/ib_logfile1
At this point most likely you will have only /var/lib/mysql/mysql directory only.
Enable innodb_file_per_table
Open my.cnf file
vim /etc/mysql/my.cnf
Add following lines
innodb_file_per_table = 1
innodb_file_format = barracuda
Time to import from mysqldump
Start mysql server now
service mysql start
Run mysql import
mysql < all-db.sql
Force mysql_upgrade (to generate performance_schema)
mysql_upgrade --force
That’s All!
Character Sets and Collations
PREVIOUS
Enable Remote Access (Grant)
NEXT
Comments
-
RavanH
Excellent tutorial. Thanks!
Some small remarks:
— On my Debian 7 a command like this:
service mysql.server stop && cp -ra /var/lib/mysql mysqldata && service mysql.server startresults in :
mysql.server: unrecognized serviceSimply removing the ‘.server’ from that statement resolves the issue 🙂
— There is a ‘-‘ missing in front of ‘-events’ in this line:
mysqldump --routines -events --flush-privileges --all-databases > all-db.sql-
RavanH
Plus: maybe at the start of this how-to refer to the useful mysql user/password tip on http://rtcamp.com/wordpress-nginx/tutorials/mysql/mysqltuner/ to make all run as smoothly as it did for me 🙂
-
I did not know that somebody will find it so useful. I always have that kind of file on server.
There is no point in hiding mysql password once root-access to server is compromised! 😉
I will move that portion to a dedicated page so it be used in other cases as well.
-
-
Sorry but
mysql.serverwas carried from previous tutorial – http://rtcamp.com/tutorials/mysql/mysql-5-6-ubuntu-12-04/I updated tutorial to use commands with
mysqlonly as we are using percona-mysql now and it did not renamemysqlbinary tomysql.server
-
-
Jan
Nice Tutorial. There’s a typo in the mysqldump, though, “-events” should probably be “–events”.
-
Yep. It was a typo. Thanks for correcting me. 🙂
-
-
>>>Add following lines
innodb_file_per_table = 1
innodb_file_format = barracudaAdd following lines in [mysqld] section, not in the end of config
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
innodb_file_per_table = 1 -
thanks for the tutorial !
in the drop statements, you might wanna wrap the db-names in accent marks. I got an error with dasherized db-namesmysql -e "SELECT DISTINCT CONCAT ('DROP DATABASE `',TABLE_SCHEMA,'` ;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA <> 'mysql' AND TABLE_SCHEMA <> 'information_schema';" | tail -n+2 > drop.sql-
forgot to escape…
mysql -e "SELECT DISTINCT CONCAT ('DROP DATABASE \`',TABLE_SCHEMA,'\` ;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA <> 'mysql' AND TABLE_SCHEMA <> 'information_schema';" | tail -n+2 > drop.sql -
Thanks for your tip. Might help others. 🙂
-
-
Works like a charm! Thanks.
#!/bin/bash -x if test "$USER" != "root" ; then echo "Must be root ..." exit 1 fi mysql_dba=root mysql_pass=your_mysql_dba_password mkdir /var/lib/mysql.bak cd /var/lib/mysql.bak service mysql stop && cp -ra /var/lib/mysql mysqldata && service mysql start mysqldump -u$mysql_dba -p$mysql_pass --routines --events --flush-privileges --all-databases > all-db.sql mysql -u$mysql_dba -p$mysql_pass -e "SELECT DISTINCT CONCAT ('DROP DATABASE ',TABLE_SCHEMA,' ;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA <> 'mysql' AND TABLE_SCHEMA <> 'information_schema';" | tail -n+2 > drop.sql cat drop.sql mysql -u$mysql_dba -p$mysql_pass < drop.sql echo "SELECT table_name, table_schema, engine FROM information_schema.tables WHERE engine = 'InnoDB';" > innotables.sql mysql -u$mysql_dba -p < innotables.sql service mysql stop rm /var/lib/mysql/ibdata1 && rm /var/lib/mysql/ib_logfile0 && rm /var/lib/mysql/ib_logfile1 cp -p /etc/mysql/my.cnf /etc/mysql/my.cnf.date +%Y.%m.%d.%H.%M.%S sed -e 's/([mysqld])/\1\ninnodb_file_per_table = 1\ninnodb_file_format = barracuda /' /etc/mysql/my.cnf > /tmp/my.cnf mv /tmp/my.cnf /etc/mysql/my.cnf service mysql start mysql -u$mysql_dba -p$mysql_pass < all-db.sql mysql_upgrade -u$mysql_dba -p$mysql_pass --force # eof-
Thanks for your codes. 🙂
It will be better to use https://gist.github.com/ or http://pastebin.com/ or some service likt that to paste code snippets. 🙂
I just hope WordPress did not changed your codes!
-
-
RTJ
Great article – thanks! However I am running into an issue…
I follow all the steps, and when I go to the step to import the sql data at the end, mysql errors out and says that a mysql.innodb_something_index_something (sorry can’t remember the exact table) tablespace needs to be truncated or something like that. How can I get passed this error? I’ve been stuck for hours trying various things. Thanks in advance.
-
Please post exact error message. Better post it on http://dba.stackexchange.com/
-
Features
- Server Setup & Management
- Site Creation & Management
- Create WP Cloud Sites
- Speed & Caching
- Firewall Protection
- Backups & Recovery
- Logs & Monitoring
- Deployments & Developer Tools
- Team Access & User Permissions
Useful Links
Product
Other Products
Subscribe for curated product updates, tips, and announcements.
"*" indicates required fields
Company
This field is for validation purposes and should be left unchanged.
Email*
SIGN ME UP
Δ
© rtCamp, since 2009. All rights reserved.