Rename MySQL database [duplicate] Rename MySQL database [duplicate] linux linux

Rename MySQL database [duplicate]


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\`"donemysql -e "DROP DATABASE \`old_database\`;"


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.


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"