MYSQL - Convert tables from shared tablespace to individual tablespace and vice-versa

Convert All Tables From Shared Tablespace to Individual Tablespace and vice versa

To convert tables to use a shared tablespace that are currently using individual tablespaces we must first check the file-per-table setting in my.cnf and set it to 0.  This will ensure any future created tables use the shared tablespace. Mysql requires a restart for this to take effect. It can be changed on the fly by set global innodb_file_per_table = 0;.
You should then ensure that your shared tablespace is configured correctly to the locations you desire in my.cnf by checking the innodb_data_file_path setting. If this is not set it will take the default mysql setting which will be autogrow in the datadir area.  If adding innodb_data_file path then also be sure to add innodb_data_home_dir and set to empty.Usually we will be converting for a reason which could be to move into a partition to free space on the original area.  Therefore this will need to be set up first with the above variables set correctly.  Extra data files can be specified in the innodb_data_file_path with new locations if required.  Be very careful with this and always run a full back up first.  Any change to innodb_data_file_path will require a restart of mysql to take effect.
To convert tables to use individual tablespaces that are currently using the shared tablespaces we must first check the file-per-table setting in my.cnf and set it to 1.  This will ensure any future created tables use it's own individual tablespace. Mysql requires a restart for this to take effect. It can be changed on the fly by set global innodb_file_per_table = 1
You should then ensure that the datadir parameter in my.cnf has been set to where the database files will be located.  Prior to mysql 5.6 all individual tablespaces need to be in the same directory. Any change to datadir will require a restart of mysql to take effect.
Run this from linux to generate a script called rebuild.sql.  This script can then be run from within mysql command line using source and pointing to the script.  Running this script will effectively rebuild all tables and lock the tables for writing whilst the table is being converted.
To convert all tables for a specified database:
mysql -h<hostname> -uroot -p<password> -S <socket path and file> -A --skip-column-names -e"SELECT CONCAT('ALTER TABLE ',table_schema,'.',table_name,' ENGINE=InnoDB;') InnoDBConversionSQL FROM information_schema.tables WHERE table_schema = '<database name>' and engine = 'InnoDb' ORDER BY (data_length+index_length)" > rebuild.sql
Back up first!
Run the above by using source rebuild.sql

Comments

Popular posts from this blog

SQL SERVER – Event ID 107- Report Server Windows Service (MSSQLSERVER) cannot connect to the report server database.

SQL Server Builds Information

Using DBCA silent install and disabling automatic memory management