[Articles] _MySQL practical operation of how to change the name of the database?

[Articles] _MySQL practical operation of how to change the name of the database?

[Introduction]

We came a demand for development of the former group projects created during the early development of a test database storage engine using innodb; this library DataBase / Schema to store some of the tables and indexes, there is no stored procedures, views, triggers, functions, and so on; due to the similar name and the name of the production library, too sensitive, you need to change the next name. This demand Zage do?

If you want to change the name DataBase / Schema for ethanDB, to be renamed ts_db_01.

You a quick view of some common objects under Mysql:

  1. DataBase/Schema
  2. Table
  3. Index
  4. View/Trigger/Function/Procedure

Procedure
if the library is using the MyISAM storage engine, you can go directly to the database directory mv can; as follows:
MYISAM engine library name of the corresponding folder name changes:

  1. Close mysqld
  2. The data directory rename directory db_name new_db_name
  3. Open mysqld

However, the storage engine used in a production environment is Innodb, been tested friend can try, will prompt the relevant table does not exist.

Innodb storage engine DataBase / Schema, probably has the following three ways:

方法1:RENAME database olddbname TO newdbname

mysql> rename database ethandb to ts_db_02;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database ethandb to ethan' at line 1

note:

This non-exception because the command is available version 5.1.7 to 5.1.23, 5.1.23 was canceled. Because I'm at version 5.7.26, the command does not apply.
MySQL> SELECT @@ Version;
± ------------------------------------------ +
| @@ Version |
± ------------------------------------------ +
| 5.7.26-Enterprise-advanced-log-Commercial's |
± ------------------------------------ + ------
1 Row in the SET (0.00 sec)

Method 2: Use Mysqldump logical backup way
of this method, of course feasible, simple and effective; but if larger hit table, takes longer.

Method 3: write your own shell scripts
INNODB engine divided into the following steps:

  1. Create a new library
  2. A backup of the original database triggers on all tables
  3. Use navicat backup repository index / View / Function / Procedure / Events script
  4. Use rename table command to copy the tables from the original database to a new database
  5. Re-create the trigger 2 is deleted in the new database
  6. Re-created on the new database index / View / Function / Procedure / Events, etc.

Because only the tables and indexes referred to herein, it is

RENAME TABLE命令语法:
RENAME TABLE db_name.table1 TO new_db_name.table1,                     \
db_name.table2 TO new_db_name.table2;

According to the command, scripting cp_db_tables.sh as follows:

mysql -uroot -pmysql -h 192.168.0.112 -P 3308 -e \
'create database if not exists ts_db_01'
list_table=$(mysql -uroot -pmysql -h 192.168.0.112 -P 3308 \
-Nse "select table_name from information_schema.TABLES \
where TABLE_SCHEMA='ethandb'")
 
for table in $list_table
do
    mysql -uroot -pmysql -h 192.168.0.112 -P 3308 \
    -e "rename table ethandb.$table to ts_db_01.$table"
done

The script has been uploaded to the personal github, interested friends can view the following links:
https://github.com/polestarYang/mysqlgit/blob/master/cp_db_tables.sh

【to sum up】

  1. As used herein, the script way to quickly create a case a new library, you can quickly create a new library, but only to create a table source library related, trigger / index / View / Function / Procedure / Events also need to re-create; but not a fast method for creating test library is.
  2. If the database is migrated, it was suggested to use mysqldump logical standby, or backup mysqlbackup physical form.

[Reference]
https://blog.csdn.net/ghlfllz/article/details/8092068

If help, please welcome attention to personal micro-channel public number, "a coffee-sum mind"

Published 54 original articles · won praise 3 · Views 5758

Guess you like

Origin blog.csdn.net/db_murphy/article/details/101200851