Rename MySQL database

MysqlDatabaseLinuxRename

Mysql Problem Overview


I created a database with the name of hrms. Now I need to change database name to sunhrm. But, It is disabled in MySQL workbench. Can I do that on the Linux server itself?

Mysql Solutions


Solution 1 - Mysql

In case you need to do that from the command line, just copy, adapt & paste this snippet:

mysql -e "CREATE DATABASE \`new_database\`;"
for table in `mysql -B -N -e "SHOW TABLES;" old_database`
do 
  mysql -e "RENAME TABLE \`old_database\`.\`$table\` to \`new_database\`.\`$table\`"
done
mysql -e "DROP DATABASE \`old_database\`;"

Solution 2 - Mysql

I don't think you can do this. Basic answers will work in many cases, and in others cause data corruptions. A strategy needs to be chosen based on heuristic analysis of your database. That is the reason this feature was implemented, and then removed. [doc]

You'll need to dump all object types in that database, create the newly named one and then import the dump. If this is a live system you'll need to take it down. If you cannot, then you will need to setup replication from this database to the new one.

If you want to see the commands that could do this, @satishD has the details, which conveys some of the challenges around which you'll need to build a strategy that matches your target database.

Solution 3 - Mysql

It's possible to copy database via mysqldump command without storing dump into file:

  1. mysql -u root -p -e "create database my_new_database"
  2. mysqldump -u root -p original_database | mysql -u root -p my_new_database
  3. mysql -u root -p -e "drop database original_database"

Solution 4 - Mysql

You can create a new database exactly as the previous database existed and then drop the old database when you're done. Use the mysqldump tool to create a .sql backup of the database via mysqldump orig_db > orig_db.sql or if you need to use a username and password then run mysqldump -u root -p orig_db > orig_db.sql. orig_db is the name of the database you want to "rename", root would be the user you're logging in as and orig_db.sql would be the file created containing the backup. Now create a new, empty database with the name you want for the database. For example, mysql -u root -p -e "create database new_db". Once that's done, then run mysql -u root -p new_db < orig_db.sql. new_db now exists as a perfect copy of orig_db. You can then drop the original database as you now have it existing in the new database with the database name you wanted.

The short, quick steps without all the above explanation are:

  1. mysqldump -u root -p original_database > original_database.sql
  2. mysql -u root -p -e "create database my_new_database"
  3. mysql -u root -p my_new_database < original_database.sql
  4. mysql -u root -p -e drop database originl_database

Hope this helps and this is a reliable means to accomplish it without using some ad-hoc method that will corrupt your data and create inconsistencies.

Solution 5 - Mysql

You can do it by RENAME statement for each table in your "current_db" after create the new schema "other_db"

RENAME TABLE current_db.tbl_name TO other_db.tbl_name

Source [Rename Table Syntax][1]

[1]: http://dev.mysql.com/doc/refman/5.5/en/rename-table.html "Rename Table Syntax"

Solution 6 - Mysql

In short no. It is generally thought to be too dangerous to rename a database. MySQL had that feature for a bit, but it was removed. You would be better off using the workbench to export both the schema and data to SQL then changing the CREATE DATABASE name there before you run/import it.

Solution 7 - Mysql

I used following method to rename the database

  1. take backup of the file using mysqldump or any DB tool eg heidiSQL,mysql administrator etc

  2. Open back up (eg backupfile.sql) file in some text editor.

  3. Search and replace the database name and save file.

  4. Restore the edited SQL file

Solution 8 - Mysql

For impatient mysql users (like me), the solution is:

/etc/init.d/mysql stop
mv /var/lib/mysql/old_database /var/lib/mysql/new_database 
/etc/init.d/mysql start

Solution 9 - Mysql

If your DB contains only MyISAM tables (do not use this method if you have InnoDB tables):

  1. shut down the MySQL server
  2. go to the mysql data directory and rename the database directory (Note: non-alpha characters need to be encoded in a special way)
  3. restart the server
  4. adjust privileges if needed (grant access to the new DB name)

You can script it all in one command so that downtime is just a second or two.

Solution 10 - Mysql

First backup the old database called HRMS and edit the script file with replace the word HRMS to SUNHRM. After this step import the database file to the mysql

Solution 11 - Mysql

Another way to rename the database or taking image of the database is by using Reverse engineering option in the database tab. It will create a ERR diagram for the database. Rename the schema there.

after that go to file menu and go to export and forward engineer the database.

Then you can import the database.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDhileepanView Question on Stackoverflow
Solution 1 - MysqljanView Answer on Stackoverflow
Solution 2 - MysqlNew AlexandriaView Answer on Stackoverflow
Solution 3 - MysqlHryhorii HrebiniukView Answer on Stackoverflow
Solution 4 - MysqljetoleView Answer on Stackoverflow
Solution 5 - MysqlCristian PortaView Answer on Stackoverflow
Solution 6 - MysqlLuke WyattView Answer on Stackoverflow
Solution 7 - MysqlAdarshaView Answer on Stackoverflow
Solution 8 - Mysqluser1972813View Answer on Stackoverflow
Solution 9 - MysqlrustyxView Answer on Stackoverflow
Solution 10 - MysqlDhileepanView Answer on Stackoverflow
Solution 11 - MysqlBennyView Answer on Stackoverflow