search
HomeDatabaseMysql TutorialExample analysis of table space transfer in mysql

Description: MySQL (5.6.6 and above), innodb_file_per_table is turned on.

1.1. Operation steps:

0. The target server creates the same table structure
1. Destination server: ALTER TABLE t DISCARD TABLESPACE;
2. Source server: FLUSH TABLES t FOR EXPORT;
3. Copy t.ibd, t.cfg files from the source server to the destination server
4. Source server: UNLOCK TABLES;
5. Destination server: ALTER TABLE t IMPORT TABLESPACE;

1.2. Demonstration
Transfer the test_purge table under the burn_test library in [mysql5711] in multiple instances to the test_purge table under the burn_test2 library in [mysql57112]

1.2.1. Preparation

1. Create a table space on the target server

-- Source server[mysql5711]

mysql> select * from burn_test.test_purge;
---- ------
| a | b |
---- ------
| 1 | 10 |
| 3 | 30 |
| 4 | 40 |
| 5 | 50 |
| 6 | 60 |
| 7 | 70 |
| 8 | 80 |
| 10 | 100 |
---- -- ----
8 rows in set (0.01 sec)

-- Target server [mysql57112]
--
-- test_purge does not exist on the target server, create the table first
mysql> CREATE TABLE `test_purge` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` int(11) DEFAULT NULL,
PRIMARY KEY (`a`),
UNIQUE KEY `b` (`b`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.16 sec)

2. Creation completed Check afterwards

## Target server

#[root@MyServer burn_test_2]> ll | grep test_purge
-rw-r-----. 1 mysql mysql 8578 Mar 21 10:31 test_purge.frm # Table structure
-rw-r-----. 1 mysql mysql 57344 Mar 21 10:31 test_purge.ibd # Table space, table space files need to be deleted through DISCARD
ALTER TABLE test_purge DISCARD TABLESPACE; means to keep the test_purge.frm file and delete test_purge.ibd

3. Use discard to delete the ibd file

--Target server

mysql> ; alter table test_purge discard tablespace;
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
------------------- ----
| Tables_in_burn_test_2 |
-----------------------
| test_backup1 |
| test_purge |
-----------------------
2 rows in set (0.00 sec)
mysql> select * from test_purge;
ERROR 1814 (HY000): Tablespace has been discarded for table 'test_purge'
[root@MyServer burn_test_2]> ll | grep test_purge
-rw-r-----. 1 mysql mysql 8578 Mar 21 10:31 test_purge.frm

1.2.2. Export table space
1. On the source server, use the export command to export the table space (add a read lock at the same time)

--Source server

mysql> flush table test_purge for export; -- Actually add a read lock to this table
Query OK, 0 rows affected (0.00 sec)
2. Export the cfg file and ibd file , copy to the database of the target server

## source server

#[root@MyServer burn_test]> ll | grep test_purge
-rw-r-----. 1 mysql mysql 462 Mar 21 10:58 test_purge.cfg # After export, the extra file contains some metadata information
-rw-r-----. 1 mysql mysql 8578 Mar 4 15:41 test_purge.frm
-rw-r-----. 1 mysql mysql 57344 Mar 5 15:28 test_purge.ibd
[root@MyServer burn_test]> cp test_purge.cfg test_purge.ibd /data/mysql_data /5.7.11_2/burn_test_2/ # Copy the table space and cfg file. Please use scp remotely (local multi-instance demonstration, the library name here is different)
3. After exporting the table space, unlock it as soon as possible

-- Source server

mysql> unlock tables; -- Unlock as soon as possible
Query OK, 0 rows affected (0.00 sec)
Note: Be sure to copy the cfg and ibd files first, and then unlock, because when unlocking, the cfg file will be deleted
# Logs on the source server
[Note] InnoDB: Stopping purge #In fact, to stop purge, just find a test table for export
[Note ] InnoDB: Writing table metadata to './burn_test/test_purge.cfg'
[Note] InnoDB: Table `burn_test`.`test_purge` flushed to disk
[Note] InnoDB: Deleting the meta-data file ' ./burn_test/test_purge.cfg' # After unlocking the table, the file is automatically deleted
[Note] InnoDB: Resuming purge # After unlocking, restore the purge thread
4. Modify the cfg file and ibd file on the target server Permissions

## Target server

#[root@MyServer burn_test_2]> chown mysql.mysql test_purge.cfg test_purge.ibd
5. Run the import command on the target server Import tablespace
-- Target server
--
mysql> alter table test_purge import tablespace; -- Import tablespace
Query OK, 0 rows affected (0.24 sec)
mysql> select * from test_purge; -- You can read the data copied from the source server
---- ------
| a | b |
---- ----- -
| 1 | 10 |
| 3 | 30 |
| 4 | 40 |
| 5 | 50 |
| 6 | 60 |
| 7 | 70 |
| 8 | 80 |
| 10 | 100 |
---- ------
8 rows in set (0.00 sec)

# error.log The message that appears
InnoDB: Importing tablespace for table 'burn_test/test_purge' that was exported from host 'MyServer'

Note:
The names of the tables must be the same. After the above test, the library names can be different
This method can also be used for the backup and recovery of partition tables

The above is the detailed content of Example analysis of table space transfer in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools