If it's myisam. Just change the name of the library name folder under the data directory OK
If it's innodb. It is not possible to modify the name of the library. Rename database or ALTER DATABASE is not a good place to be on the Internet blind 咧咧.
One method is more conservative. Mysqldump the contents of the old library directly into the new library.
There is also a similar method. First ALTER table's storage engine to MyISAM, then change the name of the library directory, and then change back to InnoDB.
The last method is better. Write it in detail here.
Assuming the source library name is ' SRCDB ', the target library name is ' Trgdb '
First create the target library
Create Database trgdb;
Get table names for all source libraries
Use INFORMATION_SCHEMA;
Select table_name from TABLES where table_schema= ' srcdb ';
Then follow these commands and change them
Rename Table srcdb. [TableName] to TRGDB. [TableName];
After each execution, the table is transferred to the new library.
1. RENAME DATABASE db_name to New_db_name
This one.. This syntax is added to the MySQL 5.1.7, 5.1.23 and removed.
It is said that it is possible to lose data. It's better not to use it.
See: http://dev.mysql.com/doc/refman/5.1/en/rename-database.html
2. If all tables are MyISAM type, you can change the name of the folder
Close Mysqld
Rename the Db_name directory in the data directory to New_db_name
Open mysqld
3. Rename all Tables
The code is as follows:
The code is as follows |
Copy Code |
CREATE DATABASE New_db_name; RENAME TABLE Db_name.table1 to New_db_name.table1, Db_name.table2 to New_db_name.table2; DROP DATABASE db_name; |
4. Mysqldump Export Data and import
The code is as follows:
The code is as follows |
Copy Code |
mysqldump-uxxxx-pxxxx-h xxxx db_name > Db_name_dump. Sql Mysql-uxxxx-pxxxx-h xxxx-e "CREATE DATABASE new_db_name" mysql-uxxxx-pxxxx-h xxxx New_db_name < Db_name_dump. Sql Mysql-uxxxx-pxxxx-h xxxx-e "DROP DATABASE db_name" |
5. Rename all tables using the shell script
The code is as follows:
The code is as follows |
Copy Code |
#!/bin/bash mysqlconn= "Mysql-u xxxx-pxxxx-s/var/lib/mysql/mysql.sock-h localhost" olddb= "Db_name" newdb= "New_db_name" # $mysqlconn-E "CREATE DATABASE $newdb" params=$ ($mysqlconn-N-E "SELECT table_name from INFORMATION_SCHEMA. TABLES WHERE table_schema= ' $olddb ') for name in $params; Todo $mysqlconn-E "RENAME TABLE $olddb. $name to $newdb. $name"; Done # $mysqlconn-E "DROP DATABASE $olddb" |
Finally attach rename usage
Command: Rename table original table name to new name;
For example: In the table MyClass the name is changed to Youclass
Mysql> Rename table MyClass to Youclass;
When you perform RENAME, you cannot have any locked tables or active transactions. You must also have ALTER and DROP permissions on the original table, as well as CREATE and INSERT permissions on the new table.
If MySQL encounters any errors in multiple table renaming, it will reverse the renaming of all renamed tables and return everything to its original state.