<p>The MySQL manual at <a href="http://web.archive.org/web/20160504181056/https://dev.mysql.com/doc/refman/5.1/en/rename-database.html" rel="noreferrer">MySQL</a> covers this.</p>
<p>Usually I just dump the database and reimport it with a new name. This is not an option for very big databases. Apparently <code>RENAME {DATABASE | SCHEMA} db_name TO new_db_name;</code> <a href="http://web.archive.org/web/20160504181056/https://dev.mysql.com/doc/refman/5.1/en/rename-database.html" rel="noreferrer">does bad things, exists only in a handful of versions, and is a bad idea overall</a>.</p>
<p>This needs to work with <a href="http://en.wikipedia.org/wiki/InnoDB" rel="noreferrer">InnoDB</a>, which stores things very differently than <a href="http://en.wikipedia.org/wiki/MyISAM" rel="noreferrer">MyISAM</a>.</p>

[{'answer_id': 67123, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 4, 'selected': False, 'text': '<p>When you rename a database in PHPMyAdmin it creates a dump, then drops and recreates the database with the new name.</p>\n'}, {'answer_id': 67142, 'author': 'bryanpearson', 'author_id': 9803, 'author_profile': 'https://Stackoverflow.com/users/9803', 'pm_score': 4, 'selected': False, 'text': '<p>MySQL does not support the renaming of a database through its command interface at the moment, but you can rename the database if you have access to the directory in which MySQL stores its databases. For default MySQL installations this is usually in the Data directory under the directory where MySQL was installed. Locate the name of the database you want to rename under the Data directory and rename it. Renaming the directory could cause some permissions issues though. Be aware.</p>\n\n<p><strong>Note:</strong> You must stop MySQL before you can rename the database</p>\n\n<p>I would recommend creating a new database (using the name you want) and export/import the data you need from the old to the new. Pretty simple.</p>\n'}, {'answer_id': 67165, 'author': 'longneck', 'author_id': 8250, 'author_profile': 'https://Stackoverflow.com/users/8250', 'pm_score': 5, 'selected': False, 'text': '<p>Three options:</p>\n\n<ol>\n<li><p>Create the new database, bring down the server, move the files from one database folder to the other, and restart the server. Note that this will only work if ALL of your tables are MyISAM.</p></li>\n<li><p>Create the new database, use CREATE TABLE ... LIKE statements, and then use INSERT ... SELECT * FROM statements.</p></li>\n<li><p>Use mysqldump and reload with that file.</p></li>\n</ol>\n'}, {'answer_id': 67187, 'author': 'DeeCee', 'author_id': 5895, 'author_profile': 'https://Stackoverflow.com/users/5895', 'pm_score': 5, 'selected': False, 'text': "<h3>The simple way</h3>\n<p>Change to the database directory:</p>\n<pre><code>cd /var/lib/mysql/\n</code></pre>\n<p>Shut down MySQL... This is important!</p>\n<pre><code>/etc/init.d/mysql stop\n</code></pre>\n<p>Okay, this way doesn't work for InnoDB or BDB-Databases.</p>\n<p>Rename database:</p>\n<pre><code>mv old-name new-name\n</code></pre>\n<p>...or the table...</p>\n<pre><code>cd database/\n\nmv old-name.frm new-name.frm\n\nmv old-name.MYD new-name.MYD\n\nmv old-name.MYI new-name.MYI\n</code></pre>\n<p>Restart MySQL</p>\n<pre><code>/etc/init.d/mysql start\n</code></pre>\n<p>Done...</p>\n<p>OK, this way doesn't work with InnoDB or BDB databases. In this case you have to dump the database and re-import it.</p>\n"}, {'answer_id': 165675, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>In MySQL Administrator do the following:</p>\n\n<ol>\n<li>Under Catalogs, create a new database schema.</li>\n<li>Go to Backup and create a backup of\nthe old schema.</li>\n<li>Execute backup.</li>\n<li>Go to Restore and open the file\ncreated in step 3.</li>\n<li>Select 'Another Schema' under Target\nSchema and select the new database\nschema.</li>\n<li>Start Restore.</li>\n<li>Verify new schema and, if it looks\ngood, delete the old one.</li>\n</ol>\n"}, {'answer_id': 362408, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Here is a batch file I wrote to automate it from the command line, but it for Windows/MS-DOS.</p>\n\n<p>Syntax is rename_mysqldb database newdatabase -u [user] -p[password]</p>\n\n<pre><code>:: ***************************************************************************\n:: FILE: RENAME_MYSQLDB.BAT\n:: ***************************************************************************\n:: DESCRIPTION\n:: This is a Windows /MS-DOS batch file that automates renaming a MySQL database \n:: by using MySQLDump, MySQLAdmin, and MySQL to perform the required tasks.\n:: The MySQL\\bin folder needs to be in your environment path or the working directory.\n::\n:: WARNING: The script will delete the original database, but only if it successfully\n:: created the new copy. However, read the disclaimer below before using.\n::\n:: DISCLAIMER\n:: This script is provided without any express or implied warranties whatsoever.\n:: The user must assume the risk of using the script.\n::\n:: You are free to use, modify, and distribute this script without exception.\n:: ***************************************************************************\n\n:INITIALIZE\n@ECHO OFF\nIF [%2]==[] GOTO HELP\nIF [%3]==[] (SET RDB_ARGS=--user=root) ELSE (SET RDB_ARGS=%3 %4 %5 %6 %7 %8 %9)\nSET RDB_OLDDB=%1\nSET RDB_NEWDB=%2\nSET RDB_DUMPFILE=%RDB_OLDDB%_dump.sql\nGOTO START\n\n:START\nSET RDB_STEP=1\nECHO Dumping "%RDB_OLDDB%"...\nmysqldump %RDB_ARGS% %RDB_OLDDB% &gt; %RDB_DUMPFILE%\nIF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT\nSET RDB_STEP=2\nECHO Creating database "%RDB_NEWDB%"...\nmysqladmin %RDB_ARGS% create %RDB_NEWDB%\nIF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT\nSET RDB_STEP=3\nECHO Loading dump into "%RDB_NEWDB%"...\nmysql %RDB_ARGS% %RDB_NEWDB% &lt; %RDB_DUMPFILE%\nIF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT\nSET RDB_STEP=4\nECHO Dropping database "%RDB_OLDDB%"...\nmysqladmin %RDB_ARGS% drop %RDB_OLDDB% --force\nIF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT\nSET RDB_STEP=5\nECHO Deleting dump...\nDEL %RDB_DUMPFILE%\nIF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT\nECHO Renamed database "%RDB_OLDDB%" to "%RDB_NEWDB%".\nGOTO END\n\n:ERROR_ABORT\nIF %RDB_STEP% GEQ 3 mysqladmin %RDB_ARGS% drop %NEWDB% --force\nIF %RDB_STEP% GEQ 1 IF EXIST %RDB_DUMPFILE% DEL %RDB_DUMPFILE%\nECHO Unable to rename database "%RDB_OLDDB%" to "%RDB_NEWDB%".\nGOTO END\n\n:HELP\nECHO Renames a MySQL database.\nECHO Usage: %0 database new_database [OPTIONS]\nECHO Options: Any valid options shared by MySQL, MySQLAdmin and MySQLDump.\nECHO          --user=root is used if no options are specified.\nGOTO END    \n\n:END\nSET RDB_OLDDB=\nSET RDB_NEWDB=\nSET RDB_ARGS=\nSET RDB_DUMP=\nSET RDB_STEP=\n</code></pre>\n'}, {'answer_id': 1072988, 'author': 'hendrasaputra', 'author_id': 76045, 'author_profile': 'https://Stackoverflow.com/users/76045', 'pm_score': 9, 'selected': False, 'text': '<p>Use these few simple commands:</p>\n\n<pre><code>mysqldump -u username -p -v olddatabase &gt; olddbdump.sql\nmysqladmin -u username -p create newdatabase\nmysql -u username -p newdatabase &lt; olddbdump.sql\n</code></pre>\n\n<p>Or to reduce I/O use the following as suggested by @Pablo Marin-Garcia:</p>\n\n<pre><code>mysqladmin -u username -p create newdatabase\nmysqldump -u username -v olddatabase -p | mysql -u username -p -D newdatabase\n</code></pre>\n'}, {'answer_id': 2298602, 'author': 'Thorsten', 'author_id': 277222, 'author_profile': 'https://Stackoverflow.com/users/277222', 'pm_score': 11, 'selected': True, 'text': '<p>For <strong>InnoDB</strong>, the following seems to work: create the new empty database, then rename each table in turn into the new database:</p>\n\n<pre><code>RENAME TABLE old_db.table TO new_db.table;\n</code></pre>\n\n<p>You will need to adjust the permissions after that.</p>\n\n<p>For scripting in a shell, you can use either of the following:</p>\n\n<pre><code>mysql -u username -ppassword old_db -sNe \'show tables\' | while read table; \\ \n    do mysql -u username -ppassword -sNe "rename table old_db.$table to new_db.$table"; done\n</code></pre>\n\n<p>OR</p>\n\n<pre><code>for table in `mysql -u root -ppassword -s -N -e "use old_db;show tables from old_db;"`; do mysql -u root -ppassword -s -N -e "use old_db;rename table old_db.$table to new_db.$table;"; done;\n</code></pre>\n\n<hr>\n\n<p>Notes:</p>\n\n<ul>\n<li>There is no space between the option <code>-p</code> and the password. If your database has no password, remove the <code>-u username -ppassword</code> part.</li>\n<li><p>If some table has a trigger, it cannot be moved to another database using above method (will result <code>Trigger in wrong schema</code> error). If that is the case, use a traditional way to clone a database and then drop the old one:</p>\n\n<p><code>mysqldump old_db | mysql new_db</code></p></li>\n<li><p>If you have stored procedures, you can copy them afterwards:</p>\n\n<p><code>mysqldump -R old_db | mysql new_db</code></p></li>\n</ul>\n'}, {'answer_id': 2661806, 'author': 'cclark', 'author_id': 179931, 'author_profile': 'https://Stackoverflow.com/users/179931', 'pm_score': 3, 'selected': False, 'text': '<p>I <a href="https://serverfault.com/questions/126509/how-to-proxy-to-different-named-databases-on-the-same-server-using-mysql-proxy">posed a question on Server Fault</a> trying to get around downtime when restoring very large databases by using MySQL Proxy. I didn\'t have any success, but I realized in the end what I wanted was RENAME DATABASE functionality because dump/import wasn\'t an option due to the size of our database.</p>\n\n<p>There is a RENAME TABLE functionality built in to MySQL so I ended up writing a simple Python script to do the job for me. I\'ve <a href="http://github.com/cclark/Rename-MySQL-DB" rel="nofollow noreferrer">posted it on GitHub</a> in case it could be of use to others.</p>\n'}, {'answer_id': 2788771, 'author': 'Amr Mostafa', 'author_id': 43597, 'author_profile': 'https://Stackoverflow.com/users/43597', 'pm_score': 4, 'selected': False, 'text': "<p>I've only recently came across a very nice way to do it, works with MyISAM and InnoDB and is very fast:</p>\n\n<pre><code>RENAME TABLE old_db.table TO new_db.table;\n</code></pre>\n\n<p>I don't remember where I read it but credit goes to someone else not me.</p>\n"}, {'answer_id': 2867823, 'author': 'TodoInTX', 'author_id': 240384, 'author_profile': 'https://Stackoverflow.com/users/240384', 'pm_score': 3, 'selected': False, 'text': '<p>It is possible to rename all tables within a database to be under another database without having to do a full dump and restore.</p>\n\n<pre>\nDROP PROCEDURE IF EXISTS mysql.rename_db;\nDELIMITER ||\nCREATE PROCEDURE mysql.rename_db(IN old_db VARCHAR(100), IN new_db VARCHAR(100))\nBEGIN\nSELECT CONCAT(\'CREATE DATABASE \', new_db, \';\') `# create new database`;\nSELECT CONCAT(\'RENAME TABLE `\', old_db, \'`.`\', table_name, \'` TO `\', new_db, \'`.`\', table_name, \'`;\') `# alter table` FROM information_schema.tables WHERE table_schema = old_db;\nSELECT CONCAT(\'DROP DATABASE `\', old_db, \'`;\') `# drop old database`;\nEND||\nDELIMITER ;\n\n$ time mysql -uroot -e "call mysql.rename_db(\'db1\', \'db2\');" | mysql -uroot\n</pre>\n\n<p>However any triggers in the target db will not be happy. You\'ll need to drop them first then recreate them after the rename.</p>\n\n<pre>\nmysql -uroot -e "call mysql.rename_db(\'test\', \'blah2\');" | mysql -uroot\nERROR 1435 (HY000) at line 4: Trigger in wrong schema\n</pre>\n'}, {'answer_id': 3152013, 'author': 'nicky', 'author_id': 380366, 'author_profile': 'https://Stackoverflow.com/users/380366', 'pm_score': -1, 'selected': False, 'text': '<p>Really, the simplest answer is to export your old database then import it into the new one that you\'ve created to replace the old one. Of course, you should use <a href="http://en.wikipedia.org/wiki/PhpMyAdmin" rel="nofollow noreferrer">phpMyAdmin</a> or command line to do this.</p>\n\n<p><strong>Renaming and Jerry-rigging the database is a BAD-IDEA! DO NOT DO IT.</strong> (Unless you are the "hacker-type" sitting in your mother\'s basement in the dark and eating pizza sleeping during the day.)</p>\n\n<p>You will end up with more problems and work than you want.</p>\n\n<p>So,</p>\n\n<ol>\n<li>Create a new_database and name it the correct way.</li>\n<li>Go to your phpMyAdmin and open the database you want to export.</li>\n<li>Export it (check the options, but you should be OK with the defaults.</li>\n<li>You will get a file like or similar to this.</li>\n<li><p>The extension on this file is .sql</p>\n\n<p>-- phpMyAdmin SQL Dump\n-- version 3.2.4</p>\n\n<h2>-- <a href="http://www.phpmyadmin.net" rel="nofollow noreferrer">http://www.phpmyadmin.net</a></h2>\n\n<p>-- Host: localhost\n-- Generation Time: Jun 30, 2010 at 12:17 PM\n-- Server version: 5.0.90\n-- PHP Version: 5.2.6</p>\n\n<p>SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";</p>\n\n<p>/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT <em>/;\n/</em>!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS <em>/;\n/</em>!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION <em>/;\n/</em>!40101 SET NAMES utf8 */;</p>\n\n<p>--</p>\n\n<h2>-- Database: <code>mydatab_online</code></h2>\n\n<hr>\n\n<p>--</p>\n\n<h2>-- Table structure for table <code>user</code></h2>\n\n<p>CREATE TABLE IF NOT EXISTS <code>user</code> (\n  <code>timestamp</code> int(15) NOT NULL default \'0\',\n  <code>ip</code> varchar(40) NOT NULL default \'\',\n  <code>file</code> varchar(100) NOT NULL default \'\',\n  PRIMARY KEY  (<code>timestamp</code>),\n  KEY <code>ip</code> (<code>ip</code>),\n  KEY <code>file</code> (<code>file</code>)\n) ENGINE=MyISAM DEFAULT CHARSET=latin1;</p>\n\n<p>--</p>\n\n<h2>-- Dumping data for table <code>user</code></h2>\n\n<p>INSERT INTO <code>user</code> (<code>timestamp</code>, <code>ip</code>, <code>file</code>) VALUES\n(1277911052, \'999.236.177.116\', \'\'),\n(1277911194, \'999.236.177.116\', \'\');</p></li>\n</ol>\n\n<p>This will be your .sql file. The one that you\'ve just exported.</p>\n\n<p>Find it on your hard-drive; usually it is in <code>/temp</code>. Select the empty database that has the correct name (the reason why you are reading this).\nSAY: Import - GO</p>\n\n<p>Connect your program to the correct database by entering it into what usually is a configuration.php file. Refresh the server (both. Why? Because I am a UNIX <strong>oldtimer</strong>, and I said so.\nNow, you should be in good shape. If you have any further questions visit me on the web.</p>\n'}, {'answer_id': 3853838, 'author': 'mathew', 'author_id': 465634, 'author_profile': 'https://Stackoverflow.com/users/465634', 'pm_score': -1, 'selected': False, 'text': '<p>If you are using <a href="http://en.wikipedia.org/wiki/PhpMyAdmin" rel="nofollow">phpMyAdmin</a> then you just go to the <code>mysql</code> folder in the xamp, close phpMyAdmin and just rename the folder you just see there as your database name and just restart your phpMyAdmin. You can see that that database as renamed.</p>\n'}, {'answer_id': 3917812, 'author': 'Morgan Christiansson', 'author_id': 34516, 'author_profile': 'https://Stackoverflow.com/users/34516', 'pm_score': 2, 'selected': False, 'text': '<p>This works for all databases and works by renaming each table with <a href="http://www.maatkit.org/" rel="nofollow">maatkit mysql toolkit</a></p>\n\n<p>Use mk-find to print and rename each table. The <a href="http://www.maatkit.org/doc/mk-find.html" rel="nofollow">man page</a> has many more options and examples</p>\n\n<pre><code>mk-find --dblike OLD_DATABASE --print --exec "RENAME TABLE %D.%N TO NEW_DATABASE.%N"\n</code></pre>\n\n<p>If you have maatkit installed (<a href="http://www.maatkit.org/download" rel="nofollow">which is very easy</a>), then this is the simplest way to do it.</p>\n'}, {'answer_id': 4044140, 'author': 'eaykin', 'author_id': 143179, 'author_profile': 'https://Stackoverflow.com/users/143179', 'pm_score': 4, 'selected': False, 'text': '<p>This is what I use:</p>\n\n<pre><code>$ mysqldump -u root -p olddb &gt;~/olddb.sql\n$ mysql -u root -p\nmysql&gt; create database newdb;\nmysql&gt; use newdb\nmysql&gt; source ~/olddb.sql\nmysql&gt; drop database olddb;\n</code></pre>\n'}, {'answer_id': 6035371, 'author': 'user757945', 'author_id': 757945, 'author_profile': 'https://Stackoverflow.com/users/757945', 'pm_score': 3, 'selected': False, 'text': "<p>TodoInTX's stored procedure didn't quite work for me. Here's my stab at it:</p>\n\n<pre>\n-- stored procedure rename_db: Rename a database my means of table copying.\n-- Caveats: \n-- Will clobber any existing database with the same name as the 'new' database name.\n-- ONLY copies tables; stored procedures and other database objects are not copied.\n-- Tomer Altman (taltman@ai.sri.com)\n\ndelimiter //\nDROP PROCEDURE IF EXISTS rename_db;\nCREATE PROCEDURE rename_db(IN old_db VARCHAR(100), IN new_db VARCHAR(100))\nBEGIN\n    DECLARE current_table VARCHAR(100);\n    DECLARE done INT DEFAULT 0;\n    DECLARE old_tables CURSOR FOR select table_name from information_schema.tables where table_schema = old_db;\n    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;\n\n    SET @output = CONCAT('DROP SCHEMA IF EXISTS ', new_db, ';'); \n    PREPARE stmt FROM @output;\n    EXECUTE stmt;\n\n    SET @output = CONCAT('CREATE SCHEMA IF NOT EXISTS ', new_db, ';');\n    PREPARE stmt FROM @output;\n    EXECUTE stmt;\n\n    OPEN old_tables;\n    REPEAT\n        FETCH old_tables INTO current_table;\n        IF NOT done THEN\n        SET @output = CONCAT('alter table ', old_db, '.', current_table, ' rename ', new_db, '.', current_table, ';');\n        PREPARE stmt FROM @output;\n        EXECUTE stmt;\n\n        END IF;\n    UNTIL done END REPEAT;\n\n    CLOSE old_tables;\n\nEND//\ndelimiter ;\n</pre>\n"}, {'answer_id': 6666803, 'author': 'ecruz', 'author_id': 567652, 'author_profile': 'https://Stackoverflow.com/users/567652', 'pm_score': 2, 'selected': False, 'text': '<p>If you are using <a href="http://en.wikipedia.org/wiki/PhpMyAdmin" rel="nofollow">phpMyAdmin</a> you can go to the "operations" tab once you have selected the database you want to rename. Then go to the last section "copy database to" (or something like that), give a name, and select the options below. In this case, I guess you must select "structure and data" and "create database before copying" checkboxes and, finally, press the "go" button in that section. </p>\n\n<p>By the way, I\'m using phpMyAdmin in Spanish so I\'m not sure what the names of the sections are in English.</p>\n'}, {'answer_id': 7674554, 'author': 'Nadav Benedek', 'author_id': 900919, 'author_profile': 'https://Stackoverflow.com/users/900919', 'pm_score': 2, 'selected': False, 'text': '<p>This is the batch script I wrote for renaming a database on Windows:</p>\n\n<pre><code>@echo off\nset olddb=olddbname\nset newdb=newdbname\nSET count=1\nSET act=mysql -uroot -e "select table_name from information_schema.tables where table_schema=\'%olddb%\'"\nmysql -uroot -e "create database %newdb%"\necho %act%\n FOR /f "tokens=*" %%G IN (\'%act%\') DO (\n  REM echo %count%:%%G\n  echo mysql -uroot -e "RENAME TABLE %olddb%.%%G to %newdb%.%%G"\n  mysql -uroot -e "RENAME TABLE %olddb%.%%G to %newdb%.%%G"\n  set /a count+=1\n )\nmysql -uroot -e "drop database %olddb%"\n</code></pre>\n'}, {'answer_id': 8276073, 'author': 'ErichBSchulz', 'author_id': 894487, 'author_profile': 'https://Stackoverflow.com/users/894487', 'pm_score': 7, 'selected': False, 'text': '<p>You can use SQL to generate an SQL script to transfer each table in your source database to the destination database. </p>\n\n<p>You must create the destination database before running the script generated from the command.  </p>\n\n<p>You can use either of these two scripts (I originally suggested the former and someone "improved" my answer to use <code>GROUP_CONCAT</code>. Take your pick, but I prefer the original):</p>\n\n<pre><code>SELECT CONCAT(\'RENAME TABLE $1.\', table_name, \' TO $2.\', table_name, \'; \')\nFROM information_schema.TABLES \nWHERE table_schema=\'$1\';\n</code></pre>\n\n<p>or</p>\n\n<pre><code>SELECT GROUP_CONCAT(\'RENAME TABLE $1.\', table_name, \' TO $2.\', table_name SEPARATOR \'; \')\nFROM information_schema.TABLES \nWHERE table_schema=\'$1\';\n</code></pre>\n\n<p>($1 and $2 are source and target respectively)</p>\n\n<p>This will generate a SQL command that you\'ll have to then run.</p>\n\n<p>Note that <code>GROUP_CONCAT</code> has a default length limit that may be exceeded for databases with a large number of tables. You can alter that limit by running <code>SET SESSION group_concat_max_len = 100000000;</code> (or some other large number).</p>\n'}, {'answer_id': 8740623, 'author': 'i.jolly', 'author_id': 1093601, 'author_profile': 'https://Stackoverflow.com/users/1093601', 'pm_score': -1, 'selected': False, 'text': '<p>Simplest of all, open <strong>MYSQL >> SELECT DB</strong> whose name you want to change <strong>>> Click on "operation"</strong> then <strong>put New name in "Rename database to:" field</strong> then click <strong>"Go"</strong> button</p>\n\n<p>Simple!</p>\n'}, {'answer_id': 8872770, 'author': 'raphie', 'author_id': 424543, 'author_profile': 'https://Stackoverflow.com/users/424543', 'pm_score': 8, 'selected': False, 'text': '<p>I think the solution is simpler and was suggested by some developers. phpMyAdmin has an operation for this.</p>\n<p>From phpMyAdmin, select the database you want to select. In the tabs there\'s one called Operations, go to the rename section. That\'s all.</p>\n<p>It does, as many suggested, create a new database with the new name, dump all tables of the old database into the new database and drop the old database.</p>\n<p><img src="https://i.stack.imgur.com/WuMcs.jpg" alt="Enter image description here" /></p>\n<h3>UPDATE 2022-09-22: MySQL 8.0+ added a more simpler solution:</h3>\n<p>Not sure since when has been added <code>RENAME TO</code> keywords, but sure is simpler. Though I would do a back up of the table before trying it, especially if you have a very large table. Anyway, it can be implemented as follow:</p>\n<pre class="lang-sql prettyprint-override"><code>ALTER TABLE `schema_name`.`table_name` \nRENAME TO  `schema_name`.`new_table_name` ;\n</code></pre>\n<p>I put it in two lines for easy reading but it can go in a single line as follow:</p>\n<pre class="lang-sql prettyprint-override"><code>ALTER TABLE `schema_name`.`table_name` RENAME TO `schema_name`.`new_table_name` ;\n</code></pre>\n'}, {'answer_id': 9721378, 'author': 'gerrit damen', 'author_id': 1271733, 'author_profile': 'https://Stackoverflow.com/users/1271733', 'pm_score': 3, 'selected': False, 'text': '<p>For your convenience, below is a small shellscript that has to be executed with two parameters: db-name and new db-name.</p>\n\n<p>You might need to add login-parameters to the mysql-lines if you don\'t use the .my.cnf-file in your home-directory. Please make a backup before executing this script.</p>\n\n<hr>\n\n<pre><code>#!/usr/bin/env bash\n\nmysql -e "CREATE DATABASE $2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"\nfor i in $(mysql -Ns $1 -e "show tables");do\n    echo "$1.$i -&gt; $2.$i"\n    mysql -e "rename TABLE $1.$i to $2.$i"\ndone\nmysql -e "DROP DATABASE $1"\n</code></pre>\n'}, {'answer_id': 9763752, 'author': 'coffeefiend', 'author_id': 1265141, 'author_profile': 'https://Stackoverflow.com/users/1265141', 'pm_score': 2, 'selected': False, 'text': "<p>Here is a one-line Bash snippet to move all tables from one schema to another:</p>\n\n<pre><code>history -d $((HISTCMD-1)) &amp;&amp; mysql -udb_user -p'db_password' -Dold_schema -ABNnqre'SHOW TABLES;' | sed -e's/.*/RENAME TABLE old_schema.`&amp;` TO new_schema.`&amp;`;/' | mysql -udb_user -p'db_password' -Dnew_schema\n</code></pre>\n\n<p>The history command at the start simply ensures that the MySQL commands containing passwords aren't saved to the shell history.</p>\n\n<p>Make sure that <code>db_user</code> has read/write/drop permissions on the old schema, and read/write/create permissions on the new schema.</p>\n"}, {'answer_id': 11908987, 'author': 'Marciano', 'author_id': 1059937, 'author_profile': 'https://Stackoverflow.com/users/1059937', 'pm_score': 6, 'selected': False, 'text': '<p>Emulating the missing <code>RENAME DATABASE</code> command in MySQL:</p>\n<ol>\n<li><p>Create a new database</p>\n</li>\n<li><p>Create the rename queries with:</p>\n<pre><code> SELECT CONCAT(\'RENAME TABLE \',table_schema,\'.`\',table_name,\n     \'` TO \',\'new_schema.`\',table_name,\'`;\')\n FROM information_schema.TABLES\n WHERE table_schema LIKE \'old_schema\';\n</code></pre>\n</li>\n<li><p>Run that output</p>\n</li>\n<li><p>Delete old database</p>\n</li>\n</ol>\n<p>It was taken from <em><a href="http://blog.shlomoid.com/2010/02/emulating-missing-rename-database.html" rel="noreferrer">Emulating The Missing RENAME DATABASE Command in MySQL</a></em>.</p>\n'}, {'answer_id': 11979761, 'author': 'xelber', 'author_id': 1478008, 'author_profile': 'https://Stackoverflow.com/users/1478008', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://dev.mysql.com/doc/refman/5.1/en/alter-database.html" rel="nofollow"><code>ALTER DATABASE</code></a> is the proposed way around this by MySQL and <code>RENAME DATABASE</code> is dropped.</p>\n\n<p>From <em><a href="http://dev.mysql.com/doc/refman/5.1/en/rename-database.html" rel="nofollow">13.1.32 RENAME DATABASE Syntax</a></em>:</p>\n\n<pre><code>RENAME {DATABASE | SCHEMA} db_name TO new_db_name;\n</code></pre>\n\n<p>This statement was added in MySQL 5.1.7, but it was found to be dangerous and was removed in MySQL 5.1.23.</p>\n'}, {'answer_id': 12300435, 'author': 'jeeva', 'author_id': 1645503, 'author_profile': 'https://Stackoverflow.com/users/1645503', 'pm_score': 2, 'selected': False, 'text': '<p>You can do it in two ways.</p>\n\n<ol>\n<li>RENAME TABLE old_db.table_name TO new_db.table_name;</li>\n<li>Goto operations-> there you can see Table options tab. you can edit table name there.</li>\n</ol>\n'}, {'answer_id': 13637538, 'author': 'Duke', 'author_id': 479017, 'author_profile': 'https://Stackoverflow.com/users/479017', 'pm_score': 4, 'selected': False, 'text': '<p>For those who are Mac users, Sequel Pro has a Rename Database option in the Database menu.\n<a href="http://www.sequelpro.com/">http://www.sequelpro.com/</a></p>\n'}, {'answer_id': 14287770, 'author': 'Milosz', 'author_id': 1042595, 'author_profile': 'https://Stackoverflow.com/users/1042595', 'pm_score': 2, 'selected': False, 'text': "<p>Neither TodoInTx's solution nor user757945's adapted solution worked for me on MySQL 5.5.16, so here is my adapted version:</p>\n\n<pre><code>DELIMITER //\nDROP PROCEDURE IF EXISTS `rename_database`;\nCREATE PROCEDURE `rename_database` (IN `old_name` VARCHAR(20), IN `new_name` VARCHAR(20))\nBEGIN\n  DECLARE `current_table_name` VARCHAR(20);\n  DECLARE `done` INT DEFAULT 0;\n  DECLARE `table_name_cursor` CURSOR FOR SELECT `table_name` FROM `information_schema`.`tables` WHERE (`table_schema` = `old_name`);\n  DECLARE CONTINUE HANDLER FOR NOT FOUND SET `done` = 1;\n\n  SET @sql_string = CONCAT('CREATE DATABASE IF NOT EXISTS `', `new_name` , '`;');\n  PREPARE `statement` FROM @sql_string;\n  EXECUTE `statement`;\n  DEALLOCATE PREPARE `statement`;\n\n  OPEN `table_name_cursor`;\n  REPEAT\n    FETCH `table_name_cursor` INTO `current_table_name`;\n    IF NOT `done` THEN\n\n      SET @sql_string = CONCAT('RENAME TABLE `', `old_name`, '`.`', `current_table_name`, '` TO `', `new_name`, '`.`', `current_table_name`, '`;');\n      PREPARE `statement` FROM @sql_string;\n      EXECUTE `statement`;\n      DEALLOCATE PREPARE `statement`;\n\n    END IF;\n  UNTIL `done` END REPEAT;\n  CLOSE `table_name_cursor`;\n\n  SET @sql_string =  CONCAT('DROP DATABASE `', `old_name`, '`;');\n  PREPARE `statement` FROM @sql_string;\n  EXECUTE `statement`;\n  DEALLOCATE PREPARE `statement`;\nEND//\nDELIMITER ;\n</code></pre>\n\n<p>Hope it helps someone who is in my situation! Note: <code>@sql_string</code> will linger in the session afterwards. I was not able to write this function without using it.</p>\n"}, {'answer_id': 14745363, 'author': 'Grijesh Chauhan', 'author_id': 1673391, 'author_profile': 'https://Stackoverflow.com/users/1673391', 'pm_score': 5, 'selected': False, 'text': '<p>You may use this shell script:</p>\n\n<p>Reference: <a href="https://serverfault.com/questions/195221/how-to-rename-a-mysql-database"><em>How to rename a MySQL database?</em> </a></p>\n\n<pre><code>#!/bin/bash\nset -e # terminate execution on command failure\n\nmysqlconn="mysql -u root -proot"\nolddb=$1\nnewdb=$2\n$mysqlconn -e "CREATE DATABASE $newdb"\nparams=$($mysqlconn -N -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES \\\n                           WHERE table_schema=\'$olddb\'")\nfor name in $params; do\n      $mysqlconn -e "RENAME TABLE $olddb.$name to $newdb.$name";\ndone;\n$mysqlconn -e "DROP DATABASE $olddb"\n</code></pre>\n\n<p>It\'s working:</p>\n\n<pre><code>$ sh rename_database.sh oldname newname\n</code></pre>\n'}, {'answer_id': 14745469, 'author': 'Fathah Rehman P', 'author_id': 991065, 'author_profile': 'https://Stackoverflow.com/users/991065', 'pm_score': 3, 'selected': False, 'text': '<p>The simplest method is to use HeidiSQL software. It\'s free and open source. It runs on Windows and on any Linux with <a href="http://en.wikipedia.org/wiki/Wine_%28software%29" rel="noreferrer">Wine</a> (run Windows applications on Linux, BSD, Solaris and Mac&nbsp;OS&nbsp;X).</p>\n\n<p>To download HeidiSQL, goto <a href="http://www.heidisql.com/download.php" rel="noreferrer">http://www.heidisql.com/download.php</a>.</p>\n\n<p>To download Wine, goto <a href="http://www.winehq.org/" rel="noreferrer">http://www.winehq.org/</a>.</p>\n\n<p>To rename a database in HeidiSQL, just right click on the database name and select \'Edit\'. Then enter a new name and press \'OK\'.</p>\n\n<p>It is so simple.</p>\n'}, {'answer_id': 18017923, 'author': 'Lawrence', 'author_id': 1435079, 'author_profile': 'https://Stackoverflow.com/users/1435079', 'pm_score': 0, 'selected': False, 'text': "<p>You guys are going to shoot me for this, and most probably this won't work every time, and sure, it is against all logic blah blah... But what I just tried is... STOP the MySQL engine, log on as root and simply renamed the DB on the file system level....</p>\n\n<p>I am on OSX, and only changed the case, from bedbf to BEDBF. To my surprise it worked...</p>\n\n<p>I would not recommend it on a production DB. I just tried this as an experiment...</p>\n\n<p>Good luck either way :-)</p>\n"}, {'answer_id': 18350130, 'author': 'murtaza.webdev', 'author_id': 2024056, 'author_profile': 'https://Stackoverflow.com/users/2024056', 'pm_score': 2, 'selected': False, 'text': '<p>in <strong>phpmyadmin</strong> you can easily rename the database</p>\n\n<pre><code>select database \n\n  goto operations tab\n\n  in that rename Database to :\n\n  type your new database name and click go\n</code></pre>\n\n<p>ask to drop old table and reload table data click OK in both</p>\n\n<p>Your database is renamed</p>\n'}, {'answer_id': 18733248, 'author': 'Adarsha', 'author_id': 2767342, 'author_profile': 'https://Stackoverflow.com/users/2767342', 'pm_score': 1, 'selected': False, 'text': '<p>I used following method to rename the database</p>\n\n<ol>\n<li><p>take backup of the file using mysqldump  or any DB tool eg heidiSQL,mysql administrator etc</p></li>\n<li><p>Open back up (eg backupfile.sql) file in some text editor.</p></li>\n<li><p>Search and replace the database name and save file.</p></li>\n</ol>\n\n<p>4.Restore the edited sql file</p>\n'}, {'answer_id': 21429390, 'author': 'Sathish D', 'author_id': 925144, 'author_profile': 'https://Stackoverflow.com/users/925144', 'pm_score': 4, 'selected': False, 'text': '<p>Well there are 2 methods:</p>\n\n<p><strong>Method 1:</strong> A well-known method for renaming database schema is by dumping the schema using Mysqldump and restoring it in another schema, and then dropping the old schema (if needed).</p>\n\n<p>From Shell</p>\n\n<pre><code> mysqldump emp &gt; emp.out\n mysql -e "CREATE DATABASE employees;"\n mysql employees &lt; emp.out \n mysql -e "DROP DATABASE emp;"\n</code></pre>\n\n<p>Although the above method is easy, it is time and space consuming. What if the schema is more than a <strong>100GB?</strong> There are methods where you can pipe the above commands together to save on space, however it will not save time.</p>\n\n<p>To remedy such situations, there is another quick method to rename schemas, however, some care must be taken while doing it.</p>\n\n<p><strong>Method 2:</strong> MySQL has a very good feature for renaming tables that even works across different schemas. This rename operation is atomic and no one else can access the table while its being renamed. This takes a short time to complete since changing a table’s name or its schema is only a metadata change. Here is procedural approach at doing the rename:</p>\n\n<p>Create the new database schema with the desired name.\nRename the tables from old schema to new schema, using MySQL’s “RENAME TABLE” command.\nDrop the old database schema.\n<code>If there are views, triggers, functions, stored procedures in the schema, those will need to be recreated too</code>. MySQL’s “RENAME TABLE” fails if there are triggers exists on the tables. To remedy this we can do the following things :</p>\n\n<p><strong>1)</strong> <code>Dump the triggers, events and stored routines in a separate file.</code> This done using -E, -R flags (in addition to -t -d which dumps the triggers) to the mysqldump command. Once triggers are dumped, we will need to drop them from the schema, for RENAME TABLE command to work.</p>\n\n<pre><code> $ mysqldump &lt;old_schema_name&gt; -d -t -R -E &gt; stored_routines_triggers_events.out\n</code></pre>\n\n<p><strong>2)</strong> Generate a list of only “BASE” tables. These can be found using a query on <code>information_schema.TABLES</code> table.</p>\n\n<pre><code> mysql&gt; select TABLE_NAME from information_schema.tables where \n    table_schema=\'&lt;old_schema_name&gt;\' and TABLE_TYPE=\'BASE TABLE\';\n</code></pre>\n\n<p><strong>3)</strong> Dump the views in an out file. Views can be found using a query on the same <code>information_schema.TABLES</code> table.</p>\n\n<pre><code>mysql&gt; select TABLE_NAME from information_schema.tables where \n   table_schema=\'&lt;old_schema_name&gt;\' and TABLE_TYPE=\'VIEW\';\n $ mysqldump &lt;database&gt; &lt;view1&gt; &lt;view2&gt; … &gt; views.out\n</code></pre>\n\n<p><strong>4)</strong> Drop the triggers on the current tables in the old_schema.</p>\n\n<pre><code>mysql&gt; DROP TRIGGER &lt;trigger_name&gt;;\n...\n</code></pre>\n\n<p><strong>5)</strong> Restore the above dump files once all the “Base” tables found in step #2 are renamed.</p>\n\n<pre><code>mysql&gt; RENAME TABLE &lt;old_schema&gt;.table_name TO &lt;new_schema&gt;.table_name;\n...\n$ mysql &lt;new_schema&gt; &lt; views.out\n$ mysql &lt;new_schema&gt; &lt; stored_routines_triggers_events.out\n</code></pre>\n\n<p>Intricacies with above methods : We may need to update the GRANTS for users such that they match the correct schema_name. These could fixed with a simple UPDATE on mysql.columns_priv, mysql.procs_priv, mysql.tables_priv, mysql.db tables updating the old_schema name to new_schema and calling “Flush privileges;”. Although “method 2″ seems a bit more complicated than the “method 1″, this is totally scriptable. A simple bash script to carry out the above steps in proper sequence, can help you save space and time while renaming database schemas next time.</p>\n\n<p>The Percona Remote DBA team have written a script called “rename_db” that works in the following way :</p>\n\n<pre><code>[root@dba~]# /tmp/rename_db\nrename_db &lt;server&gt; &lt;database&gt; &lt;new_database&gt;\n</code></pre>\n\n<p>To demonstrate the use of this script, used a sample schema “emp”, created test triggers, stored routines on that schema. Will try to rename the database schema using the script, which takes some seconds to complete as opposed to time consuming dump/restore method.</p>\n\n<pre><code>mysql&gt; show databases;\n+--------------------+\n| Database           |\n+--------------------+\n| information_schema |\n| emp                |\n| mysql              |\n| performance_schema |\n| test               |\n+--------------------+\n\n\n[root@dba ~]# time /tmp/rename_db localhost emp emp_test\ncreate database emp_test DEFAULT CHARACTER SET latin1\ndrop trigger salary_trigger\nrename table emp.__emp_new to emp_test.__emp_new\nrename table emp._emp_new to emp_test._emp_new\nrename table emp.departments to emp_test.departments\nrename table emp.dept to emp_test.dept\nrename table emp.dept_emp to emp_test.dept_emp\nrename table emp.dept_manager to emp_test.dept_manager\nrename table emp.emp to emp_test.emp\nrename table emp.employees to emp_test.employees\nrename table emp.salaries_temp to emp_test.salaries_temp\nrename table emp.titles to emp_test.titles\nloading views\nloading triggers, routines and events\nDropping database emp\n\nreal    0m0.643s\nuser    0m0.053s\nsys     0m0.131s\n\n\nmysql&gt; show databases;\n+--------------------+\n| Database           |\n+--------------------+\n| information_schema |\n| emp_test           |\n| mysql              |\n| performance_schema |\n| test               |\n+--------------------+\n</code></pre>\n\n<p>As you can see in the above output the database schema “emp” was renamed to “emp_test” in less than a second. Lastly, This is the script from Percona that is used above for “method 2″.</p>\n\n<pre><code>#!/bin/bash\n# Copyright 2013 Percona LLC and/or its affiliates\nset -e\nif [ -z "$3" ]; then\n    echo "rename_db &lt;server&gt; &lt;database&gt; &lt;new_database&gt;"\n    exit 1\nfi\ndb_exists=`mysql -h $1 -e "show databases like \'$3\'" -sss`\nif [ -n "$db_exists" ]; then\n    echo "ERROR: New database already exists $3"\n    exit 1\nfi\nTIMESTAMP=`date +%s`\ncharacter_set=`mysql -h $1 -e "show create database $2\\G" -sss | grep ^Create | awk -F\'CHARACTER SET \' \'{print $2}\' | awk \'{print $1}\'`\nTABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema=\'$2\' and TABLE_TYPE=\'BASE TABLE\'" -sss`\nSTATUS=$?\nif [ "$STATUS" != 0 ] || [ -z "$TABLES" ]; then\n    echo "Error retrieving tables from $2"\n    exit 1\nfi\necho "create database $3 DEFAULT CHARACTER SET $character_set"\nmysql -h $1 -e "create database $3 DEFAULT CHARACTER SET $character_set"\nTRIGGERS=`mysql -h $1 $2 -e "show triggers\\G" | grep Trigger: | awk \'{print $2}\'`\nVIEWS=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema=\'$2\' and TABLE_TYPE=\'VIEW\'" -sss`\nif [ -n "$VIEWS" ]; then\n    mysqldump -h $1 $2 $VIEWS &gt; /tmp/${2}_views${TIMESTAMP}.dump\nfi\nmysqldump -h $1 $2 -d -t -R -E &gt; /tmp/${2}_triggers${TIMESTAMP}.dump\nfor TRIGGER in $TRIGGERS; do\n    echo "drop trigger $TRIGGER"\n    mysql -h $1 $2 -e "drop trigger $TRIGGER"\ndone\nfor TABLE in $TABLES; do\n    echo "rename table $2.$TABLE to $3.$TABLE"\n    mysql -h $1 $2 -e "SET FOREIGN_KEY_CHECKS=0; rename table $2.$TABLE to $3.$TABLE"\ndone\nif [ -n "$VIEWS" ]; then\n    echo "loading views"\n    mysql -h $1 $3 &lt; /tmp/${2}_views${TIMESTAMP}.dump\nfi\necho "loading triggers, routines and events"\nmysql -h $1 $3 &lt; /tmp/${2}_triggers${TIMESTAMP}.dump\nTABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema=\'$2\' and TABLE_TYPE=\'BASE TABLE\'" -sss`\nif [ -z "$TABLES" ]; then\n    echo "Dropping database $2"\n    mysql -h $1 $2 -e "drop database $2"\nfi\nif [ `mysql -h $1 -e "select count(*) from mysql.columns_priv where db=\'$2\'" -sss` -gt 0 ]; then\n    COLUMNS_PRIV="    UPDATE mysql.columns_priv set db=\'$3\' WHERE db=\'$2\';"\nfi\nif [ `mysql -h $1 -e "select count(*) from mysql.procs_priv where db=\'$2\'" -sss` -gt 0 ]; then\n    PROCS_PRIV="    UPDATE mysql.procs_priv set db=\'$3\' WHERE db=\'$2\';"\nfi\nif [ `mysql -h $1 -e "select count(*) from mysql.tables_priv where db=\'$2\'" -sss` -gt 0 ]; then\n    TABLES_PRIV="    UPDATE mysql.tables_priv set db=\'$3\' WHERE db=\'$2\';"\nfi\nif [ `mysql -h $1 -e "select count(*) from mysql.db where db=\'$2\'" -sss` -gt 0 ]; then\n    DB_PRIV="    UPDATE mysql.db set db=\'$3\' WHERE db=\'$2\';"\nfi\nif [ -n "$COLUMNS_PRIV" ] || [ -n "$PROCS_PRIV" ] || [ -n "$TABLES_PRIV" ] || [ -n "$DB_PRIV" ]; then\n    echo "IF YOU WANT TO RENAME the GRANTS YOU NEED TO RUN ALL OUTPUT BELOW:"\n    if [ -n "$COLUMNS_PRIV" ]; then echo "$COLUMNS_PRIV"; fi\n    if [ -n "$PROCS_PRIV" ]; then echo "$PROCS_PRIV"; fi\n    if [ -n "$TABLES_PRIV" ]; then echo "$TABLES_PRIV"; fi\n    if [ -n "$DB_PRIV" ]; then echo "$DB_PRIV"; fi\n    echo "    flush privileges;"\nfi\n</code></pre>\n'}, {'answer_id': 23184794, 'author': 'E.R.Rider', 'author_id': 2650618, 'author_profile': 'https://Stackoverflow.com/users/2650618', 'pm_score': 0, 'selected': False, 'text': '<p>I posted this <a href="https://stackoverflow.com/questions/19806065/how-to-change-database-name-using-mysql/23183723#23183723">How do I change the database name using MySQL?</a> today after days of head scratching and hair pulling.\nThe solution is quite simple export a schema to a .sql file and open the file and change the database/schema name in the sql CREAT TABLE section at the top. There are three instances or more and may not be at the top of the page if multible schemas are saved to the file.\nIt is posible to edit the entire database this way but I expect that in large databases it could be quite a pain following all instances of a table property or index. </p>\n'}, {'answer_id': 25250493, 'author': 'yantaq', 'author_id': 554060, 'author_profile': 'https://Stackoverflow.com/users/554060', 'pm_score': 2, 'selected': False, 'text': '<p>Here is a quick way to generate renaming sql script, if you have many tables to move.</p>\n\n<pre><code>SELECT DISTINCT CONCAT(\'RENAME TABLE \', t.table_schema,\'.\', t.table_name, \' TO \',     \nt.table_schema, "_archive", \'.\', t.table_name, \';\' ) as Rename_SQL \nFROM information_schema.tables t\nWHERE table_schema=\'your_db_name\' ;\n</code></pre>\n'}, {'answer_id': 25622768, 'author': 'rajesh', 'author_id': 2551728, 'author_profile': 'https://Stackoverflow.com/users/2551728', 'pm_score': 0, 'selected': False, 'text': '<p>I).There is no way directly by which u can change the name of an existing DB\nBut u can achieve ur target by following below steps:-\n1). Create newdb.\n2). Use newdb.\n3). create table table_name(select * from olddb.table_name);</p>\n\n<p>By doing above, u r copying data from table of olddb and inserting those in newdb table. Give name of the table same.</p>\n\n<p>II). RENAME TABLE old_db.table_name TO new_db.table_name;</p>\n'}, {'answer_id': 32140744, 'author': 'gadelat', 'author_id': 524965, 'author_profile': 'https://Stackoverflow.com/users/524965', 'pm_score': 1, 'selected': False, 'text': '<p>If you use hierarchical views (views pulling data from other views), import of raw output from mysqldump may not work since mysqldump doesn\'t care for correct order of views. Because of this, I <a href="https://github.com/gadelat/mysqldump-view-reorder.git" rel="nofollow">wrote script</a> which re-orders views to correct order on the fly.</p>\n\n<p>It loooks like this:</p>\n\n<pre><code>#!/usr/bin/env perl\n\nuse List::MoreUtils \'first_index\'; #apt package liblist-moreutils-perl\nuse strict;\nuse warnings;\n\n\nmy $views_sql;\n\nwhile (&lt;&gt;) {\n    $views_sql .= $_ if $views_sql or index($_, \'Final view structure\') != -1;\n    print $_ if !$views_sql;\n}\n\nmy @views_regex_result = ($views_sql =~ /(\\-\\- Final view structure.+?\\n\\-\\-\\n\\n.+?\\n\\n)/msg);\nmy @views = (join("", @views_regex_result) =~ /\\-\\- Final view structure for view `(.+?)`/g);\nmy $new_views_section = "";\nwhile (@views) {\n    foreach my $view (@views_regex_result) {\n        my $view_body = ($view =~ /\\/\\*.+?VIEW .+ AS (select .+)\\*\\/;/g )[0];\n        my $found = 0;\n        foreach my $view (@views) {\n            if ($view_body =~ /(from|join)[ \\(]+`$view`/) {\n                $found = $view;\n                last;\n            }\n        }\n        if (!$found) {\n            print $view;\n            my $name_of_view_which_was_not_found = ($view =~ /\\-\\- Final view structure for view `(.+?)`/g)[0];\n            my $index = first_index { $_ eq $name_of_view_which_was_not_found } @views;\n            if ($index != -1) {\n                splice(@views, $index, 1);\n                splice(@views_regex_result, $index, 1);\n            }\n        }\n    }\n}\n</code></pre>\n\n<p>Usage:<br>\n<code>mysqldump -u username -v olddatabase -p | ./mysqldump_view_reorder.pl | mysql -u username -p -D newdatabase</code></p>\n'}, {'answer_id': 34794989, 'author': 'Steve Chambers', 'author_id': 1063716, 'author_profile': 'https://Stackoverflow.com/users/1063716', 'pm_score': 4, 'selected': False, 'text': '<p>Simplest bullet-and-fool-proof way of doing a <strong>complete</strong> rename <em>(including dropping the old database at the end so it\'s a rename rather than a copy)</em>:</p>\n\n<pre class="lang-none prettyprint-override"><code>mysqladmin -uroot -pmypassword create newdbname\nmysqldump -uroot -pmypassword --routines olddbname | mysql -uroot -pmypassword newdbname\nmysqladmin -uroot -pmypassword drop olddbname\n</code></pre>\n\n<p><strong>Steps:</strong></p>\n\n<ol>\n<li>Copy the lines into Notepad.</li>\n<li>Replace all references to "olddbname", "newdbname", "mypassword" (+ optionally "root") with your equivalents.</li>\n<li>Execute one by one on the command line (entering "y" when prompted).</li>\n</ol>\n'}, {'answer_id': 37379876, 'author': 'Tim Duncklee', 'author_id': 1163122, 'author_profile': 'https://Stackoverflow.com/users/1163122', 'pm_score': -1, 'selected': False, 'text': '<p>There are many really good answers here already but I do not see a PHP version. This copies an 800M DB in about a second.</p>\n\n<pre><code>$oldDbName = "oldDBName";\n$newDbName = "newDBName";\n$oldDB     = new mysqli("localhost", "user", "pass", $oldDbName);\nif($oldDB-&gt;connect_errno){\n    echo "Failed to connect to MySQL: (" . $oldDB-&gt;connect_errno . ") " . $oldDB-&gt;connect_error;\n    exit;\n}\n$newDBQuery = "CREATE DATABASE IF NOT EXISTS {$newDbName}";\n$oldDB-&gt;query($newDBQuery);\n$newDB = new mysqli("localhost", "user", "pass");\nif($newDB-&gt;connect_errno){\n    echo "Failed to connect to MySQL: (" . $newDB-&gt;connect_errno . ") " . $newDB-&gt;connect_error;\n    exit;\n}\n\n$tableQuery  = "SHOW TABLES";\n$tableResult = $oldDB-&gt;query($tableQuery);\n$renameQuery = "RENAME TABLE\\n";\nwhile($table = $tableResult-&gt;fetch_array()){\n    $tableName = $table["Tables_in_{$oldDbName}"];\n    $renameQuery .= "{$oldDbName}.{$tableName} TO {$newDbName}.{$tableName},";\n}\n$renameQuery = substr($renameQuery, 0, strlen($renameQuery) - 1);\n$newDB-&gt;query($renameQuery);\n</code></pre>\n'}, {'answer_id': 42125897, 'author': 'ryantm', 'author_id': 823, 'author_profile': 'https://Stackoverflow.com/users/823', 'pm_score': 4, 'selected': False, 'text': '<p>Most of the answers here are wrong for one of two reasons:</p>\n\n<ol>\n<li>You cannot just use RENAME TABLE, because there might be views and triggers. If there are triggers, RENAME TABLE fails</li>\n<li>You cannot use mysqldump if you want to "quickly" (as requested in the question) rename a big database</li>\n</ol>\n\n<p>Percona has a blog post about how to do this well:\n<a href="https://www.percona.com/blog/2013/12/24/renaming-database-schema-mysql/" rel="noreferrer">https://www.percona.com/blog/2013/12/24/renaming-database-schema-mysql/</a></p>\n\n<p>and script posted (made?) by Simon R Jones that does what is suggested in that post. I fixed a bug I found in the script. You can see it here:</p>\n\n<p><a href="https://gist.github.com/ryantm/76944318b0473ff25993ef2a7186213d" rel="noreferrer">https://gist.github.com/ryantm/76944318b0473ff25993ef2a7186213d</a></p>\n\n<p>Here is a copy of it:</p>\n\n<pre><code>#!/bin/bash\n# Copyright 2013 Percona LLC and/or its affiliates\n# @see https://www.percona.com/blog/2013/12/24/renaming-database-schema-mysql/\nset -e\nif [ -z "$3" ]; then\n    echo "rename_db &lt;server&gt; &lt;database&gt; &lt;new_database&gt;"\n    exit 1\nfi\ndb_exists=`mysql -h $1 -e "show databases like \'$3\'" -sss`\nif [ -n "$db_exists" ]; then\n    echo "ERROR: New database already exists $3"\n    exit 1\nfi\nTIMESTAMP=`date +%s`\ncharacter_set=`mysql -h $1 -e "SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = \'$2\'" -sss`\nTABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema=\'$2\' and TABLE_TYPE=\'BASE TABLE\'" -sss`\nSTATUS=$?\nif [ "$STATUS" != 0 ] || [ -z "$TABLES" ]; then\n    echo "Error retrieving tables from $2"\n    exit 1\nfi\necho "create database $3 DEFAULT CHARACTER SET $character_set"\nmysql -h $1 -e "create database $3 DEFAULT CHARACTER SET $character_set"\nTRIGGERS=`mysql -h $1 $2 -e "show triggers\\G" | grep Trigger: | awk \'{print $2}\'`\nVIEWS=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema=\'$2\' and TABLE_TYPE=\'VIEW\'" -sss`\nif [ -n "$VIEWS" ]; then\n    mysqldump -h $1 $2 $VIEWS &gt; /tmp/${2}_views${TIMESTAMP}.dump\nfi\nmysqldump -h $1 $2 -d -t -R -E &gt; /tmp/${2}_triggers${TIMESTAMP}.dump\nfor TRIGGER in $TRIGGERS; do\n    echo "drop trigger $TRIGGER"\n    mysql -h $1 $2 -e "drop trigger $TRIGGER"\ndone\nfor TABLE in $TABLES; do\n    echo "rename table $2.$TABLE to $3.$TABLE"\n    mysql -h $1 $2 -e "SET FOREIGN_KEY_CHECKS=0; rename table $2.$TABLE to $3.$TABLE"\ndone\nif [ -n "$VIEWS" ]; then\n    echo "loading views"\n    mysql -h $1 $3 &lt; /tmp/${2}_views${TIMESTAMP}.dump\nfi\necho "loading triggers, routines and events"\nmysql -h $1 $3 &lt; /tmp/${2}_triggers${TIMESTAMP}.dump\nTABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema=\'$2\' and TABLE_TYPE=\'BASE TABLE\'" -sss`\nif [ -z "$TABLES" ]; then\n    echo "Dropping database $2"\n    mysql -h $1 $2 -e "drop database $2"\nfi\nif [ `mysql -h $1 -e "select count(*) from mysql.columns_priv where db=\'$2\'" -sss` -gt 0 ]; then\n    COLUMNS_PRIV="    UPDATE mysql.columns_priv set db=\'$3\' WHERE db=\'$2\';"\nfi\nif [ `mysql -h $1 -e "select count(*) from mysql.procs_priv where db=\'$2\'" -sss` -gt 0 ]; then\n    PROCS_PRIV="    UPDATE mysql.procs_priv set db=\'$3\' WHERE db=\'$2\';"\nfi\nif [ `mysql -h $1 -e "select count(*) from mysql.tables_priv where db=\'$2\'" -sss` -gt 0 ]; then\n    TABLES_PRIV="    UPDATE mysql.tables_priv set db=\'$3\' WHERE db=\'$2\';"\nfi\nif [ `mysql -h $1 -e "select count(*) from mysql.db where db=\'$2\'" -sss` -gt 0 ]; then\n    DB_PRIV="    UPDATE mysql.db set db=\'$3\' WHERE db=\'$2\';"\nfi\nif [ -n "$COLUMNS_PRIV" ] || [ -n "$PROCS_PRIV" ] || [ -n "$TABLES_PRIV" ] || [ -n "$DB_PRIV" ]; then\n    echo "IF YOU WANT TO RENAME the GRANTS YOU NEED TO RUN ALL OUTPUT BELOW:"\n    if [ -n "$COLUMNS_PRIV" ]; then echo "$COLUMNS_PRIV"; fi\n    if [ -n "$PROCS_PRIV" ]; then echo "$PROCS_PRIV"; fi\n    if [ -n "$TABLES_PRIV" ]; then echo "$TABLES_PRIV"; fi\n    if [ -n "$DB_PRIV" ]; then echo "$DB_PRIV"; fi\n    echo "    flush privileges;"\nfi\n</code></pre>\n\n<p>Save it to a file called <code>rename_db</code> and make the script executable with <code>chmod +x rename_db</code> then use it like <code>./rename_db localhost old_db new_db</code></p>\n'}, {'answer_id': 44889494, 'author': 'RotS', 'author_id': 5053266, 'author_profile': 'https://Stackoverflow.com/users/5053266', 'pm_score': 0, 'selected': False, 'text': '<p>In the case where you start from a dump file with several databases, you can perform a sed on the dump:</p>\n\n<pre><code>sed -i -- "s|old_name_database1|new_name_database1|g" my_dump.sql\nsed -i -- "s|old_name_database2|new_name_database2|g" my_dump.sql\n...\n</code></pre>\n\n<p>Then import your dump. Just ensure that there will be no name conflict.</p>\n'}, {'answer_id': 45315768, 'author': 'Samra', 'author_id': 6412823, 'author_profile': 'https://Stackoverflow.com/users/6412823', 'pm_score': 2, 'selected': False, 'text': '<p>I did it this way: \nTake backup of your existing database. It will give you a db.zip.tmp and then in command prompt write following</p>\n\n<blockquote>\n  <p>"C:\\Program Files (x86)\\MySQL\\MySQL Server 5.6\\bin\\mysql.exe" -h\n  localhost -u root -p[password] [new db name] &lt; "C:\\Backups\\db.zip.tmp"</p>\n</blockquote>\n'}, {'answer_id': 46347714, 'author': 'Tuncay Göncüoğlu', 'author_id': 1372570, 'author_profile': 'https://Stackoverflow.com/users/1372570', 'pm_score': 3, 'selected': False, 'text': '<p>Seems noone mentioned this but here is another way:</p>\n\n<pre><code>create database NewDatabaseName like OldDatabaseName;\n</code></pre>\n\n<p>then for each table do:</p>\n\n<pre><code>create NewDatabaseName.tablename like OldDatabaseName.tablename;\ninsert into NewDataBaseName.tablename select * from OldDatabaseName.tablename;\n</code></pre>\n\n<p>then, if you want to,</p>\n\n<pre><code>drop database OldDatabaseName;\n</code></pre>\n\n<p>This approach would have the advantage of doing the entire transfer on server with near zero network traffic, so it will go a lot faster than a dump/restore.</p>\n\n<p>If you do have stored procedures/views/etc you might want to transfer them as well.</p>\n'}, {'answer_id': 46558670, 'author': 'Roee Gavirel', 'author_id': 672689, 'author_profile': 'https://Stackoverflow.com/users/672689', 'pm_score': 3, 'selected': False, 'text': "<p>For mac users, you can use <code>Sequel Pro</code> (free), which just provide the option to rename Databases. Though it doesn't delete the old DB.</p>\n\n<p>once open the relevant DB just click: <code>Database</code> --> <code>Rename database...</code></p>\n"}, {'answer_id': 48336870, 'author': 'Shubham Jain', 'author_id': 3190347, 'author_profile': 'https://Stackoverflow.com/users/3190347', 'pm_score': 4, 'selected': False, 'text': '<p>Steps :</p>\n\n<ol>\n<li>Hit <a href="http://localhost/phpmyadmin/" rel="noreferrer">http://localhost/phpmyadmin/</a></li>\n<li>Select your DB</li>\n<li>Click on Operations Tab</li>\n<li>There will be a tab as "Rename database to". Add new name and check Adjust privileges. </li>\n<li>Click on Go.</li>\n</ol>\n\n<p><a href="https://i.stack.imgur.com/0atGZ.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/0atGZ.jpg" alt="enter image description here"></a></p>\n'}, {'answer_id': 51368516, 'author': 'overals', 'author_id': 4066372, 'author_profile': 'https://Stackoverflow.com/users/4066372', 'pm_score': -1, 'selected': False, 'text': '<p>The simple way</p>\n\n<pre><code>ALTER DATABASE `oldName` MODIFY NAME = `newName`;\n</code></pre>\n\n<p>or you can use <a href="https://wtools.io/generate-sql-rename-database" rel="nofollow noreferrer">online sql generator</a></p>\n'}, {'answer_id': 62927756, 'author': 'nafischonchol', 'author_id': 10127054, 'author_profile': 'https://Stackoverflow.com/users/10127054', 'pm_score': 0, 'selected': False, 'text': "<pre><code>UPDATE `db`SET Db = 'new_db_name' where Db = 'old_db_name';\n</code></pre>\n"}, {'answer_id': 65234968, 'author': 'New Alexandria', 'author_id': 263858, 'author_profile': 'https://Stackoverflow.com/users/263858', 'pm_score': 3, 'selected': False, 'text': '<p>There is a reason you cannot do this. (despite all the attempted answers)</p>\n<ul>\n<li>Basic answers will work in many cases, and in others cause data corruptions.</li>\n<li>A strategy needs to be chosen based on heuristic analysis of your database.</li>\n<li><strong>That is the reason this feature was implemented, <a href="https://stackoverflow.com/questions/12190000/rename-mysql-database/21429268#comment21558791_12190318">and then removed</a>.</strong> [<a href="http://dev.mysql.com/doc/refman/5.1/en/rename-database.html" rel="noreferrer">doc</a>]</li>\n</ul>\n<p>You\'ll need to dump <em>all object types in</em> 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.</p>\n<p><em>If you want to see the commands that could do this, <a href="https://stackoverflow.com/a/21429268/263858">@satishD has the details</a>, which conveys some of the challenges around which you\'ll need to build a strategy that matches your target database.</em></p>\n'}, {'answer_id': 70784874, 'author': 'Aԃιƚყα Gυɾαʋ', 'author_id': 16821115, 'author_profile': 'https://Stackoverflow.com/users/16821115', 'pm_score': -1, 'selected': False, 'text': '<p>Quickest and simplest solution i can give is...in MySql Workbench right click on your schema -&gt; Click on create schema -&gt; Enter name for that schema.</p>\n<p>Drop your old schema with old name.</p>\n<p>You are ready to rock....</p>\n<p>NOTE :: For your local purpose only do this. Do not try at production database tables. Schema is created but there is no data in it. So be careful.</p>\n'}, {'answer_id': 70897059, 'author': 'ᴍᴇʜᴏᴠ', 'author_id': 722036, 'author_profile': 'https://Stackoverflow.com/users/722036', 'pm_score': 1, 'selected': False, 'text': '<p>If you prefer GUI tools and happen to have MySQL Workbench installed, you can use the built-in <a href="https://www.mysql.com/products/workbench/migrate/" rel="nofollow noreferrer">Migration Wizard</a></p>\n'}]
['https://Stackoverflow.com/questions/67093', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/556/']