5 Ways to Change Mysql Database Name Safely and Quickly

1. RENAME DATABASE db_name TO new_db_name

this. . This syntax was added in MySQL 5.1.7 and removed in 5.1.23.
It is said that there is a possibility of data loss. Still don't use it.
See: http://dev.mysql.com/doc/refman/5.1/en/rename-database.html

2. If all tables are of MyISAM type, you can change the name of the folder to
close mysqld
and rename the db_name directory in the data directory to new_db_name to
open mysqld

3. Rename all tables

Copy the code The code is as follows:
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 then import

Copy the code The code is as follows:
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 shell script

Copy the code The code is as follows:
#!/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; do
$mysqlconn -e “RENAME TABLE $olddb.$name to $newdb.$name”;
done;

#$mysqlconn -e “DROP DATABASE $olddb”

 

It is an optimized version of method 3.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326355105&siteId=291194637