How to rename a database in mysql

Source: Internet
Author: User
Five methods to change the mysql database name: 1. RENAMEDATABASEdb_nameTOnew_db_name .. This syntax is added in mysql5.1.7 and removed in 5.1.23. Data may be lost. Or don't use it. See: dev.mysql.comdocrefman5.1enrename-database.html 2

Five methods TO change the mysql DATABASE name: 1. rename database db_name TO new_db_name .. This syntax is added to mysql 5.1.7 and removed from mysql 5.1.23. Data may be lost. Or don't use it. See: http://dev.mysql.com/doc/refman/5.1/en/rename-database.html 2

Five methods to change the mysql database name:

1. rename database db_name TO new_db_name
This .. This syntax is added to mysql 5.1.7 and removed from mysql 5.1.23.
Data may be lost. Or don't use it.
See: http://dev.mysql.com/doc/refman/5.1/en/rename-database.html

2. If all tables are of the MyISAM type, you can change the folder name.
Disable mysqld
Rename the db_name directory in the data directory to new_db_name
Enable mysqld

3. Rename all tables
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
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. Use shell scripts to rename all tables
#! /Bin/bash

Mysqlconn = "mysql-u xxxx-pxxxx-S/var/lib/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"
Is the optimized version of method 3.

Original article address: How to rename the database in mysql, thanks to the original author for sharing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.