MySQL: Cloning a MySQL database on the same MySql instance - mysql
I would like to write a script which copies my current database sitedb1 to sitedb2 on the same mysql database instance. I know I can dump the sitedb1 to a sql script:
mysqldump -u root -p sitedb1 >~/db_name.sql
and then import it to sitedb2.
Is there an easier way, without dumping the first database to a sql file?
As the manual says in Copying Databases you can pipe the dump directly into the mysql client:
mysqldump db_name | mysql new_db_name
If you're using MyISAM you could copy the files, but I wouldn't recommend it. It's a bit dodgy.
Integrated from various good other answers
Both mysqldump and mysql commands accept options for setting connection details (and much more), like:
mysqldump -u <user name> --password=<pwd> <original db> | mysql -u <user name> -p <new db>
Also, if the new database is not existing yet, you have to create it beforehand (e.g. with echo "create database new_db_name" | mysql -u <dbuser> -p).
Using MySQL Utilities
The MySQL Utilities contain the nice tool mysqldbcopy which by default copies a DB including all related objects (“tables, views, triggers, events, procedures, functions, and database-level grants”) and data from one DB server to the same or to another DB server. There are lots of options available to customize what is actually copied.
So, to answer the OP’s question:
mysqldbcopy \
--source=root:your_password#localhost \
--destination=root:your_password#localhost \
sitedb1:sitedb2
Best and easy way is to enter these commands in your terminal and set permissions to the root user. Works for me..!
:~$> mysqldump -u root -p db1 > dump.sql
:~$> mysqladmin -u root -p create db2
:~$> mysql -u root -p db2 < dump.sql
mysqladmin create DB_name -u DB_user --password=DB_pass && \
mysqldump -u DB_user --password=DB_pass DB_name | \
mysql -u DB_user --password=DB_pass -h DB_host DB_name
You could use (in pseudocode):
FOREACH tbl IN db_a:
CREATE TABLE db_b.tbl LIKE db_a.tbl;
INSERT INTO db_b.tbl SELECT * FROM db_a.tbl;
The reason I'm not using the CREATE TABLE ... SELECT ... syntax is to preserve indices. Of course this only copies tables. Views and procedures are not copied, although it can be done in the same manner.
See CREATE TABLE.
You need to run the command from terminal / command prompt.
mysqldump -u <user name> -p <pwd> <original db> | mysql -u <user name> <pwd> <new db>
e.g: mysqldump -u root test_db1 | mysql -u root test_db2
This copies test_db1 to test_db2 and grant the access to 'root'#'localhost'
First create the duplicate database:
CREATE DATABASE duplicateddb;
Make sure the permissions etc are all in place and:
mysqldump -u admin -p originaldb | mysql -u backup -p password duplicateddb;
A simple way to do so if you installed phpmyadmin:
Go to your database, select "operation" tab, and you can see the "copy database to" block. Use it and you can copy the database.
As mentioned in Greg's answer, mysqldump db_name | mysql new_db_name is the free, safe, and easy way to transfer data between databases. However, it's also really slow.
If you're looking to backup data, can't afford to lose data (in this or other databases), or are using tables other than innodb, then you should use mysqldump.
If you're looking for something for development, have all of your databases backed up elsewhere, and are comfortable purging and reinstalling mysql (possibly manually) when everything goes wrong, then I might just have the solution for you.
I couldn't find a good alternative, so I built a script to do it myself. I spent a lot of time getting this to work the first time and it honestly terrifies me a little to make changes to it now. Innodb databases were not meant to copied and pasted like this. Small changes cause this to fail in magnificent ways. I haven't had a problem since I finalized the code, but that doesn't mean you won't.
Systems tested on (but may still fail on):
Ubuntu 16.04, default mysql, innodb, separate files per table
Ubuntu 18.04, default mysql, innodb, separate files per table
We've since switched to docker and a simple copy of the entire mysql data folder, so this script is no longer maintained. Leaving it in case it's able to help anyone in the future.
What it does
Gets sudo privilege and verifies you have enough storage space to clone the database
Gets root mysql privileges
Creates a new database named after the current git branch
Clones structure to new database
Switches into recovery mode for innodb
Deletes default data in new database
Stops mysql
Clones data to new database
Starts mysql
Links imported data in new database
Switches out of recovery mode for innodb
Restarts mysql
Gives mysql user access to database
Cleans up temporary files
How it compares with mysqldump
On a 3gb database, using mysqldump and mysql would take 40-50 minutes on my machine. Using this method, the same process would only take ~8 minutes.
How we used it
We had our SQL changes saved alongside our code and the upgrade process is automated on both production and development, with each set of changes making a backup of the database to restore if there's errors. One problem we ran into was when we were working on a long term project with database changes, and had to switch branches in the middle of it to fix a bug or three.
In the past, we used a single database for all branches, and would have to rebuild the database whenever we switched to a branch that wasn't compatible with the new database changes. And when we switched back, we'd have to run the upgrades again.
We tried mysqldump to duplicate the database for different branches, but the wait time was too long (40-50 minutes), and we couldn't do anything else in the meantime.
This solution shortened the database clone time to 1/5 the time (think coffee and bathroom break instead of a long lunch).
Common tasks and their time
Switching between branches with incompatible database changes takes 50+ minutes on a single database, but no time at all after the initial setup time with mysqldump or this code. This code just happens to be ~5 times faster than mysqldump.
Here are some common tasks and roughly how long they would take with each method:
Create feature branch with database changes and merge immediately:
Single database: ~5 minutes
Clone with mysqldump: 50-60 minutes
Clone with this code: ~18 minutes
Create feature branch with database changes, switch to main for a bugfix, make an edit on the feature branch, and merge:
Single database: ~60 minutes
Clone with mysqldump: 50-60 minutes
Clone with this code: ~18 minutes
Create feature branch with database changes, switch to main for a bugfix 5 times while making edits on the feature branch inbetween, and merge:
Single database: ~4 hours, 40 minutes
Clone with mysqldump: 50-60 minutes
Clone with this code: ~18 minutes
The code
Do not use this unless you've read and understood everything above. It is no longer maintained, so it is more and more likely to be broken as time goes on.
#!/bin/bash
set -e
# This script taken from: https://stackoverflow.com/a/57528198/526741
function now {
date "+%H:%M:%S";
}
# Leading space sets messages off from step progress.
echosuccess () {
printf "\e[0;32m %s: %s\e[0m\n" "$(now)" "$1"
sleep .1
}
echowarn () {
printf "\e[0;33m %s: %s\e[0m\n" "$(now)" "$1"
sleep .1
}
echoerror () {
printf "\e[0;31m %s: %s\e[0m\n" "$(now)" "$1"
sleep .1
}
echonotice () {
printf "\e[0;94m %s: %s\e[0m\n" "$(now)" "$1"
sleep .1
}
echoinstructions () {
printf "\e[0;104m %s: %s\e[0m\n" "$(now)" "$1"
sleep .1
}
echostep () {
printf "\e[0;90mStep %s of 13:\e[0m\n" "$1"
sleep .1
}
MYSQL_CNF_PATH='/etc/mysql/mysql.conf.d/recovery.cnf'
OLD_DB='YOUR_DATABASE_NAME'
USER='YOUR_MYSQL_USER'
# You can change NEW_DB to whatever you like
# Right now, it will append the current git branch name to the existing database name
BRANCH=`git rev-parse --abbrev-ref HEAD`
NEW_DB="${OLD_DB}__$BRANCH"
THIS_DIR=./site/upgrades
DB_CREATED=false
tmp_file () {
printf "$THIS_DIR/$NEW_DB.%s" "$1"
}
sql_on_new_db () {
mysql $NEW_DB --unbuffered --skip-column-names -u root -p$PASS 2>> $(tmp_file 'errors.log')
}
general_cleanup () {
echoinstructions 'Leave this running while things are cleaned up...'
if [ -f $(tmp_file 'errors.log') ]; then
echowarn 'Additional warnings and errors:'
cat $(tmp_file 'errors.log')
fi
for f in $THIS_DIR/$NEW_DB.*; do
echonotice 'Deleting temporary files created for transfer...'
rm -f $THIS_DIR/$NEW_DB.*
break
done
echonotice 'Done!'
echoinstructions "You can close this now :)"
}
error_cleanup () {
exitcode=$?
# Just in case script was exited while in a prompt
echo
if [ "$exitcode" == "0" ]; then
echoerror "Script exited prematurely, but exit code was '0'."
fi
echoerror "The following command on line ${BASH_LINENO[0]} exited with code $exitcode:"
echo " $BASH_COMMAND"
if [ "$DB_CREATED" = true ]; then
echo
echonotice "Dropping database \`$NEW_DB\` if created..."
echo "DROP DATABASE \`$NEW_DB\`;" | sql_on_new_db || echoerror "Could not drop database \`$NEW_DB\` (see warnings)"
fi
general_cleanup
exit $exitcode
}
trap error_cleanup EXIT
mysql_path () {
printf "/var/lib/mysql/"
}
old_db_path () {
printf "%s%s/" "$(mysql_path)" "$OLD_DB"
}
new_db_path () {
printf "%s%s/" "$(mysql_path)" "$NEW_DB"
}
get_tables () {
(sudo find /var/lib/mysql/$OLD_DB -name "*.frm" -printf "%f\n") | cut -d'.' -f1 | sort
}
STEP=0
authenticate () {
printf "\e[0;104m"
sudo ls &> /dev/null
printf "\e[0m"
echonotice 'Authenticated.'
}
echostep $((++STEP))
authenticate
TABLE_COUNT=`get_tables | wc -l`
SPACE_AVAIL=`df -k --output=avail $(mysql_path) | tail -n1`
SPACE_NEEDED=(`sudo du -s $(old_db_path)`)
SPACE_ERR=`echo "$SPACE_AVAIL-$SPACE_NEEDED" | bc`
SPACE_WARN=`echo "$SPACE_AVAIL-$SPACE_NEEDED*3" | bc`
if [ $SPACE_ERR -lt 0 ]; then
echoerror 'There is not enough space to branch the database.'
echoerror 'Please free up some space and run this command again.'
SPACE_AVAIL_FORMATTED=`printf "%'d" $SPACE_AVAIL`
SPACE_NEEDED_FORMATTED=`printf "%'${#SPACE_AVAIL_FORMATTED}d" $SPACE_NEEDED`
echonotice "$SPACE_NEEDED_FORMATTED bytes needed to create database branch"
echonotice "$SPACE_AVAIL_FORMATTED bytes currently free"
exit 1
elif [ $SPACE_WARN -lt 0 ]; then
echowarn 'This action will use more than 1/3 of your available space.'
SPACE_AVAIL_FORMATTED=`printf "%'d" $SPACE_AVAIL`
SPACE_NEEDED_FORMATTED=`printf "%'${#SPACE_AVAIL_FORMATTED}d" $SPACE_NEEDED`
echonotice "$SPACE_NEEDED_FORMATTED bytes needed to create database branch"
echonotice "$SPACE_AVAIL_FORMATTED bytes currently free"
printf "\e[0;104m"
read -p " $(now): Do you still want to branch the database? [y/n] " -n 1 -r CONFIRM
printf "\e[0m"
echo
if [[ ! $CONFIRM =~ ^[Yy]$ ]]; then
echonotice 'Database was NOT branched'
exit 1
fi
fi
PASS='badpass'
connect_to_db () {
printf "\e[0;104m %s: MySQL root password: \e[0m" "$(now)"
read -s PASS
PASS=${PASS:-badpass}
echo
echonotice "Connecting to MySQL..."
}
create_db () {
echonotice 'Creating empty database...'
echo "CREATE DATABASE \`$NEW_DB\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" | mysql -u root -p$PASS 2>> $(tmp_file 'errors.log')
DB_CREATED=true
}
build_tables () {
echonotice 'Retrieving and building database structure...'
mysqldump $OLD_DB --skip-comments -d -u root -p$PASS 2>> $(tmp_file 'errors.log') | pv --width 80 --name " $(now)" > $(tmp_file 'dump.sql')
pv --width 80 --name " $(now)" $(tmp_file 'dump.sql') | sql_on_new_db
}
set_debug_1 () {
echonotice 'Switching into recovery mode for innodb...'
printf '[mysqld]\ninnodb_file_per_table = 1\ninnodb_force_recovery = 1\n' | sudo tee $MYSQL_CNF_PATH > /dev/null
}
set_debug_0 () {
echonotice 'Switching out of recovery mode for innodb...'
sudo rm -f $MYSQL_CNF_PATH
}
discard_tablespace () {
echonotice 'Unlinking default data...'
(
echo "USE \`$NEW_DB\`;"
echo "SET foreign_key_checks = 0;"
get_tables | while read -r line;
do echo "ALTER TABLE \`$line\` DISCARD TABLESPACE; SELECT 'Table \`$line\` imported.';";
done
echo "SET foreign_key_checks = 1;"
) > $(tmp_file 'discard_tablespace.sql')
cat $(tmp_file 'discard_tablespace.sql') | sql_on_new_db | pv --width 80 --line-mode --size $TABLE_COUNT --name " $(now)" > /dev/null
}
import_tablespace () {
echonotice 'Linking imported data...'
(
echo "USE \`$NEW_DB\`;"
echo "SET foreign_key_checks = 0;"
get_tables | while read -r line;
do echo "ALTER TABLE \`$line\` IMPORT TABLESPACE; SELECT 'Table \`$line\` imported.';";
done
echo "SET foreign_key_checks = 1;"
) > $(tmp_file 'import_tablespace.sql')
cat $(tmp_file 'import_tablespace.sql') | sql_on_new_db | pv --width 80 --line-mode --size $TABLE_COUNT --name " $(now)" > /dev/null
}
stop_mysql () {
echonotice 'Stopping MySQL...'
sudo /etc/init.d/mysql stop >> $(tmp_file 'log')
}
start_mysql () {
echonotice 'Starting MySQL...'
sudo /etc/init.d/mysql start >> $(tmp_file 'log')
}
restart_mysql () {
echonotice 'Restarting MySQL...'
sudo /etc/init.d/mysql restart >> $(tmp_file 'log')
}
copy_data () {
echonotice 'Copying data...'
sudo rm -f $(new_db_path)*.ibd
sudo rsync -ah --info=progress2 $(old_db_path) --include '*.ibd' --exclude '*' $(new_db_path)
}
give_access () {
echonotice "Giving MySQL user \`$USER\` access to database \`$NEW_DB\`"
echo "GRANT ALL PRIVILEGES ON \`$NEW_DB\`.* to $USER#localhost" | sql_on_new_db
}
echostep $((++STEP))
connect_to_db
EXISTING_TABLE=`echo "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$NEW_DB'" | mysql --skip-column-names -u root -p$PASS 2>> $(tmp_file 'errors.log')`
if [ "$EXISTING_TABLE" == "$NEW_DB" ]
then
echoerror "Database \`$NEW_DB\` already exists"
exit 1
fi
echoinstructions "The hamsters are working. Check back in 5-10 minutes."
sleep 5
echostep $((++STEP))
create_db
echostep $((++STEP))
build_tables
echostep $((++STEP))
set_debug_1
echostep $((++STEP))
discard_tablespace
echostep $((++STEP))
stop_mysql
echostep $((++STEP))
copy_data
echostep $((++STEP))
start_mysql
echostep $((++STEP))
import_tablespace
echostep $((++STEP))
set_debug_0
echostep $((++STEP))
restart_mysql
echostep $((++STEP))
give_access
echo
echosuccess "Database \`$NEW_DB\` is ready to use."
echo
trap general_cleanup EXIT
If everything goes smoothly, you should see something like:
You can do something like the following:
mysqldump -u[username] -p[password] database_name_for_clone
| mysql -u[username] -p[password] new_database_name
You can do:
CREATE DATABASE copy_of_db;
create table copy_of_db.table LIKE source_db.table;
If you want to copy data too:
INSERT INTO copy_of_db.table SELECT * FROM source_db.table;
Repeat for all tables, functions, procedures, etc
(mysqldump is the proper way, but this is a quick and dirty solution useful in many cases)
This statement was added in MySQL 5.1.7 but was found to be dangerous and was removed in MySQL 5.1.23. It was intended to enable upgrading pre-5.1 databases to use the encoding implemented in 5.1 for mapping database names to database directory names. However, use of this statement could result in loss of database contents, which is why it was removed. Do not use RENAME DATABASE in earlier versions in which it is present.
To perform the task of upgrading database names with the new encoding, use ALTER DATABASE db_name UPGRADE DATA DIRECTORY NAME instead: http://dev.mysql.com/doc/refman/5.1/en/alter-database.html
Using Mydumper
sudo apt install mydumper
Generate dump
mydumper --user=YOUR_USER --password=YOUR_PASSWORD -B YOUR_CURRENT_DB \
--triggers --routines --events --outputdir YOUR_OUTPUT_DIR
Load dump
myloader --user=YOUR_USER --password=YOUR_PASSWORD --database=YOUR_NEW_DB \
--directory=YOUR_OUTPUT_DIR
In addition to Greg's answer, this is the easiest and fastest way if the new_db_name doesn't yet exist:
echo "create database new_db_name" | mysql -u <user> -p <pwd>
mysqldump -u <user> -p <pwd> db_name | mysql -u <user> -p <pwd> new_db_name
If you have triggers in your original database, you can avoid the "Trigger already exists" error by piping a replacement before the import:
mysqldump -u olddbuser -p -d olddbname | sed "s/`olddbname`./`newdbname`./" | mysql -u newdbuser -p -D newdbname
Using MySQL Workbench you can use Database > Migration Wizard to copy database to the same or to the other server instance. I believe it works server-side so it should be a good solution for duplicating large databases.
Related
How can I detect if mysqldump fails in a bash script?
I have a small down and dirty script to dump one of the tables all of a client's databases nightly: #!/bin/bash DB_BACKUP="/backups/mysql_backup/`date +%Y-%m-%d`" DB_USER="dbuser" DB_PASSWD="dbpass" # Create the backup directory mkdir -p $DB_BACKUP # Remove backups older than 10 days find /backups/mysql_backup/ -maxdepth 1 -type d -mtime +10 -exec rm -rf {} \; # Backup each database on the system for db in $(mysql --user=$DB_USER --password=$DB_PASSWD -e 'show databases' -s --skip-column-names|grep -viE '(staging|performance_schema|information_schema)'); do echo "dumping $db-uploads"; mysqldump --user=$DB_USER --password=$DB_PASSWD --events --opt --single-transaction $db uploads > "$DB_BACKUP/mysqldump-$db-uploads-$(date +%Y-%m-%d).sql"; done Recently we've had some issues where some of the tables get corrupted, and mysqldump fails with the following message: mysqldump: Got error: 145: Table './myDBname/myTable1' is marked as crashed and should be repaired when using LOCK TABLES Is there a way for me to check if this happens in the bash script, and log the errors if so? Also, as written would such an error halt the script, or would it continue to backup the rest of the databases normally? If it would halt execution is there a way around that?
Every program has an exit status. The exit status of each program is assigned to the $? builtin bash variable. By convention, this is 0 if the command was successful, or some other value 1-255 if the command was not successful. The exact value depends on the code in that program. You can see the exit codes that mysqldump might issue here: https://github.com/mysql/mysql-server/blob/8.0/client/mysqldump.cc#L65-L72 You can check for this, and log it, output an error message of you choosing, exit the bash script, whatever you want. mysqldump ... if [[ $? != 0 ]] ; then ...do something... fi You can alternatively write this which does the same thing: mysqldump ... || { ...do something... } The || means to execute the following statement or code block if the exit status of the preceding command is nonzero. By default, commands that return errors do not cause the bash script to exit. You can optionally make that the behavior of the script by using this statement, and all following commands will cause the script to exit if they fail: set -e
Corrupt MySQL Database, Only Accessible With innodb_force_recovery=6
MySQL 5.7 on Ubuntu 16.04 LTS Database was corrupted after what I assume was an improper system shutdown, having the worst time trying to recover it. Previous system admin had no recent backups, somewhat necessary that I try everything I can to repair/recover the database. MySQL server only runs with: innodb_force_recovery=6 mysqldump gives this error: Couldn't execute 'SHOW VARIABLES LIKE 'gtid_mode'': Table 'performance_schema.session_variables' doesn't exist (1146) Can't run mysql_upgrade with innodb_force_recovery=6 If you have any relevant questions, please ask.
At this point the only option is to extract data and re-create InnoDB database from the dump. Quite often MySQL crashes when it hits a corruption in any of pages, so it's better to dump tables one by one. Here's a script to dump tables individually that you can tweak for your specific case. It will restart MySQL if it crashes, and save a list of "bad" tables in failed.txt. set -eux edir=export function ensure_mysqld() { mysql -e 'select 1' && return service mysql start timeout=300 while [[ $timeout -gt 0 ]] do mysql -e 'select 1' && return sleep 1 timeout=$(( $timeout - 1 )) done echo "failed to start MySQL" exit 1 } for d in $(cat databases) do mkdir -p $edir/$d set +e ensure_mysqld set -e for t in $(mysql -S $socket -NBe "select TABLE_NAME from information_schema.TABLES WHERE TABLE_SCHEMA='$d' AND ENGINE = 'InnoDB' AND TABLE_TYPE = 'BASE TABLE'") do ensure_mysqld mysqldump --skip-lock-tables $d $t > $edir/$d/$t.sql || echo "$d.$t" >> failed.txt done done
mysqldump - Dump multiple databases from separate mysql accounts to one file
The standard mysqldump command that I use is mysqldump --opt --databases $dbname --host=$dbhost --user=$dbuser --password=$dbpass | gzip > $filename To dump multiple databases mysqldump --opt --databases $dbname1 $dbname2 $dbname3 $dbname_etc --host=$dbhost --user=$dbuser --password=$dbpass | gzip > $filename My question is how do you dump multiple databases from different MySQL accounts into just one file? UPDATE: When I meant 1 file, I mean 1 gzipped file with the difference sql dumps for the different sites inside it.
Nobody seems to have clarified this, so I'm going to give my 2 cents. Going to note here, my experiences are in BASH, and may be exclusive to it, so variables and looping might work different in your environment. The best way to achieve an archive with separate files inside of it is to use either ZIP or TAR, i prefer to use tar due to its simplicity and availability. Tar itself doesn't do compression, but bundled with bzip2 or gzip it can provide excellent results. Since your example uses gzip I'll use that in my demonstration. First, let's attack the problem of MySQL dumps, the mysqldump command does not separate the files (to my knowledge anyway). So let's make a small workaround for creating 1 file per database. mysql -s -r -p$dbpass --user=$dbuser -e 'show databases' | while read db; do mysqldump -p$dbpass --user=$dbuser $db > ${db}.sql; done So now we have a string that will show databases per file, and export those databases out to where ever you need simply edit the part after the > symbol Next, let's add some look at the syntax for TAR tar -czf <output-file> <input-file-1> <input-file-2> because of this configuration it allows us to specify a great number of files to archive. The options are broken down as follows. c - Compress/Create Archive z - GZIP Compression f - Output to file j - bzip compression Our next problem is keeping a list of all the newly created files, we'll expand our while statement to append to a variable while running through each database found inside of MySQL. DBLIST=""; mysql -s -r -p$dbpass --user=$dbuser -e 'show databases' | while read db; do mysqldump p$dbpass --user=$dbuser $db > ${db}.sql; DBLIST="$DBLIST $DB"; done Now we have a DBLIST variable that we can use to have an output of all our files that will be created, we can then modify our 1 line statement to run the tar command after everything has been handled. DBLIST=""; mysql -s -r -p$dbpass --user=$dbuser -e 'show databases' | while read db; do mysqldump p$dbpass --user=$dbuser $db > ${db}.sql; DBLIST="$DBLIST $DB"; done && tar -czf $filename "$DBLIST" This is a very rough approach and doesn't allow you to manually specify databases, so to achieve that, using the following command will create you a TAR file that contains all of your specified databases. DBLIST=""; for db in "<database1-name> <database2-name>"; do mysqldump -p$dbpass --user=$dbuser $db > ${db}.sql; DBLIST="$DBLIST $DB.sql"; done && tar -czf $filename "$DBLIST" The looping through MySQL databases from the MySQL database comes from the following stackoverflow.com question "mysqldump with db in a separate file" which was simply modified in order to fit your needs. And to have the script automatically clean it up in a 1 liner simply add the following at the end of the command && rm "$DBLIST" making the command look like this DBLIST=""; for db in "<database1-name> <database2-name>"; do mysqldump -p$dbpass --user=$dbuser $db > ${db}.sql; DBLIST="$DBLIST $DB.sql"; done && tar -czf $filename "$DBLIST" && rm "$DBLIST"
For every MySQL server account, dump the databases into separate files For every dump file, execute this command: cat dump_user1.sql dump_user2.sql | gzip > super_dump.gz There is a similar post on Superuser.com website: https://superuser.com/questions/228878/how-can-i-concatenate-two-files-in-unix
just in case "multiple db" is literally "all db" for you mysqldump -u root -p --all-databases > all.sql
Dump tables from database in mysql?
How can I dump each table in database in separate file with that table name?
You may want to check out the shell script suggested in the following article: How do I dump all tables in a database into separate files? by Sunny Walia Script: #!/bin/bash db=$1 if [ "$db" = "" ]; then echo "Usage: $0 db_name" exit 1 fi mkdir $$ cd $$ clear for table in `mysql $db -e 'show tables' | egrep -v 'Tables_in_' `; do echo "Dumping $table" mysqldump --opt -Q $db $table > $table.sql done if [ "$table" = "" ]; then echo "No tables found in db: $db" fi
Here is a Linux command line to backup all tables in YOURDATABASENAME to separate files in a specific path: You will have to replace YOURDATABASENAME and YOURPATH with appropriate values. for I in $(mysql --database=YOURDATABASENAME -e 'show tables' -s --skip-column-names); do mysqldump YOURDATABASENAME $I | gzip > "/YOURPATH/YOURDATABASENAME/$I.sql.gz"; done I use this as a scheduled cron job to backup all tables daily. NOTE: if you are using this from a Linux command line, you will have to have to add a user with global privileges to the database for YOURUSERNAME#localhost with no password. Otherwise, you will have to add user and password options to the script as follows, but this will require a password for each table! for I in $(mysql -u MYSQLUSERNAME -p --database=YOURDATABASENAME -e 'show tables' -s --skip-column-names); do mysqldump -u MYSQLUSERNAME -p YOURDATABASENAME $I | gzip > "/YOURPATH/YOURDATABASENAME/$I.sql.gz"; done
How do I rename a MySQL database (change schema name)?
How do I quickly rename a MySQL database (change its schema name)? Usually I just dump a database and re-import it with a new name. This is not an option for very big databases. Apparently RENAME {DATABASE | SCHEMA} db_name TO new_db_name; does bad things, exists only in a handful of versions, and is a bad idea overall. This needs to work with InnoDB, which stores things very differently than MyISAM.
For InnoDB, the following seems to work: create the new empty database, then rename each table in turn into the new database: RENAME TABLE old_db.table TO new_db.table; You will need to adjust the permissions after that. For scripting in a shell, you can use either of the following: mysql -u username -ppassword old_db -sNe 'show tables' | while read table; \ do mysql -u username -ppassword -sNe "rename table old_db.$table to new_db.$table"; done OR 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; Notes: There is no space between the option -p and the password. If your database has no password, remove the -u username -ppassword part. If some table has a trigger, it cannot be moved to another database using above method (will result Trigger in wrong schema error). If that is the case, use a traditional way to clone a database and then drop the old one: mysqldump old_db | mysql new_db If you have stored procedures, you can copy them afterwards: mysqldump -R old_db | mysql new_db
Use these few simple commands: mysqldump -u username -p -v olddatabase > olddbdump.sql mysqladmin -u username -p create newdatabase mysql -u username -p newdatabase < olddbdump.sql Or to reduce I/O use the following as suggested by #Pablo Marin-Garcia: mysqladmin -u username -p create newdatabase mysqldump -u username -v olddatabase -p | mysql -u username -p -D newdatabase
I think the solution is simpler and was suggested by some developers. phpMyAdmin has an operation for this. 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. 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.
You can use SQL to generate an SQL script to transfer each table in your source database to the destination database. You must create the destination database before running the script generated from the command. You can use either of these two scripts (I originally suggested the former and someone "improved" my answer to use GROUP_CONCAT. Take your pick, but I prefer the original): SELECT CONCAT('RENAME TABLE $1.', table_name, ' TO $2.', table_name, '; ') FROM information_schema.TABLES WHERE table_schema='$1'; or SELECT GROUP_CONCAT('RENAME TABLE $1.', table_name, ' TO $2.', table_name SEPARATOR '; ') FROM information_schema.TABLES WHERE table_schema='$1'; ($1 and $2 are source and target respectively) This will generate a SQL command that you'll have to then run. Note that GROUP_CONCAT has a default length limit that may be exceeded for databases with a large number of tables. You can alter that limit by running SET SESSION group_concat_max_len = 100000000; (or some other large number).
Emulating the missing RENAME DATABASE command in MySQL: Create a new database Create the rename queries with: SELECT CONCAT('RENAME TABLE ',table_schema,'.`',table_name, '` TO ','new_schema.`',table_name,'`;') FROM information_schema.TABLES WHERE table_schema LIKE 'old_schema'; Run that output Delete old database It was taken from Emulating The Missing RENAME DATABASE Command in MySQL.
You may use this shell script: Reference: How to rename a MySQL database? #!/bin/bash set -e # terminate execution on command failure mysqlconn="mysql -u root -proot" olddb=$1 newdb=$2 $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" It's working: $ sh rename_database.sh oldname newname
Three options: 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. Create the new database, use CREATE TABLE ... LIKE statements, and then use INSERT ... SELECT * FROM statements. Use mysqldump and reload with that file.
The simple way Change to the database directory: cd /var/lib/mysql/ Shut down MySQL... This is important! /etc/init.d/mysql stop Okay, this way doesn't work for InnoDB or BDB-Databases. Rename database: mv old-name new-name ...or the table... cd database/ mv old-name.frm new-name.frm mv old-name.MYD new-name.MYD mv old-name.MYI new-name.MYI Restart MySQL /etc/init.d/mysql start Done... OK, this way doesn't work with InnoDB or BDB databases. In this case you have to dump the database and re-import it.
Simplest bullet-and-fool-proof way of doing a complete rename (including dropping the old database at the end so it's a rename rather than a copy): mysqladmin -uroot -pmypassword create newdbname mysqldump -uroot -pmypassword --routines olddbname | mysql -uroot -pmypassword newdbname mysqladmin -uroot -pmypassword drop olddbname Steps: Copy the lines into Notepad. Replace all references to "olddbname", "newdbname", "mypassword" (+ optionally "root") with your equivalents. Execute one by one on the command line (entering "y" when prompted).
I've only recently came across a very nice way to do it, works with MyISAM and InnoDB and is very fast: RENAME TABLE old_db.table TO new_db.table; I don't remember where I read it but credit goes to someone else not me.
This is what I use: $ mysqldump -u root -p olddb >~/olddb.sql $ mysql -u root -p mysql> create database newdb; mysql> use newdb mysql> source ~/olddb.sql mysql> drop database olddb;
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. Note: You must stop MySQL before you can rename the database 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.
Well there are 2 methods: Method 1: 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). From Shell mysqldump emp > emp.out mysql -e "CREATE DATABASE employees;" mysql employees < emp.out mysql -e "DROP DATABASE emp;" Although the above method is easy, it is time and space consuming. What if the schema is more than a 100GB? There are methods where you can pipe the above commands together to save on space, however it will not save time. To remedy such situations, there is another quick method to rename schemas, however, some care must be taken while doing it. Method 2: 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: Create the new database schema with the desired name. Rename the tables from old schema to new schema, using MySQL’s “RENAME TABLE” command. Drop the old database schema. If there are views, triggers, functions, stored procedures in the schema, those will need to be recreated too. MySQL’s “RENAME TABLE” fails if there are triggers exists on the tables. To remedy this we can do the following things : 1) Dump the triggers, events and stored routines in a separate file. 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. $ mysqldump <old_schema_name> -d -t -R -E > stored_routines_triggers_events.out 2) Generate a list of only “BASE” tables. These can be found using a query on information_schema.TABLES table. mysql> select TABLE_NAME from information_schema.tables where table_schema='<old_schema_name>' and TABLE_TYPE='BASE TABLE'; 3) Dump the views in an out file. Views can be found using a query on the same information_schema.TABLES table. mysql> select TABLE_NAME from information_schema.tables where table_schema='<old_schema_name>' and TABLE_TYPE='VIEW'; $ mysqldump <database> <view1> <view2> … > views.out 4) Drop the triggers on the current tables in the old_schema. mysql> DROP TRIGGER <trigger_name>; ... 5) Restore the above dump files once all the “Base” tables found in step #2 are renamed. mysql> RENAME TABLE <old_schema>.table_name TO <new_schema>.table_name; ... $ mysql <new_schema> < views.out $ mysql <new_schema> < stored_routines_triggers_events.out 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. The Percona Remote DBA team have written a script called “rename_db” that works in the following way : [root#dba~]# /tmp/rename_db rename_db <server> <database> <new_database> 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. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | emp | | mysql | | performance_schema | | test | +--------------------+ [root#dba ~]# time /tmp/rename_db localhost emp emp_test create database emp_test DEFAULT CHARACTER SET latin1 drop trigger salary_trigger rename table emp.__emp_new to emp_test.__emp_new rename table emp._emp_new to emp_test._emp_new rename table emp.departments to emp_test.departments rename table emp.dept to emp_test.dept rename table emp.dept_emp to emp_test.dept_emp rename table emp.dept_manager to emp_test.dept_manager rename table emp.emp to emp_test.emp rename table emp.employees to emp_test.employees rename table emp.salaries_temp to emp_test.salaries_temp rename table emp.titles to emp_test.titles loading views loading triggers, routines and events Dropping database emp real 0m0.643s user 0m0.053s sys 0m0.131s mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | emp_test | | mysql | | performance_schema | | test | +--------------------+ 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″. #!/bin/bash # Copyright 2013 Percona LLC and/or its affiliates set -e if [ -z "$3" ]; then echo "rename_db <server> <database> <new_database>" exit 1 fi db_exists=`mysql -h $1 -e "show databases like '$3'" -sss` if [ -n "$db_exists" ]; then echo "ERROR: New database already exists $3" exit 1 fi TIMESTAMP=`date +%s` character_set=`mysql -h $1 -e "show create database $2\G" -sss | grep ^Create | awk -F'CHARACTER SET ' '{print $2}' | awk '{print $1}'` TABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='BASE TABLE'" -sss` STATUS=$? if [ "$STATUS" != 0 ] || [ -z "$TABLES" ]; then echo "Error retrieving tables from $2" exit 1 fi echo "create database $3 DEFAULT CHARACTER SET $character_set" mysql -h $1 -e "create database $3 DEFAULT CHARACTER SET $character_set" TRIGGERS=`mysql -h $1 $2 -e "show triggers\G" | grep Trigger: | awk '{print $2}'` VIEWS=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='VIEW'" -sss` if [ -n "$VIEWS" ]; then mysqldump -h $1 $2 $VIEWS > /tmp/${2}_views${TIMESTAMP}.dump fi mysqldump -h $1 $2 -d -t -R -E > /tmp/${2}_triggers${TIMESTAMP}.dump for TRIGGER in $TRIGGERS; do echo "drop trigger $TRIGGER" mysql -h $1 $2 -e "drop trigger $TRIGGER" done for TABLE in $TABLES; do echo "rename table $2.$TABLE to $3.$TABLE" mysql -h $1 $2 -e "SET FOREIGN_KEY_CHECKS=0; rename table $2.$TABLE to $3.$TABLE" done if [ -n "$VIEWS" ]; then echo "loading views" mysql -h $1 $3 < /tmp/${2}_views${TIMESTAMP}.dump fi echo "loading triggers, routines and events" mysql -h $1 $3 < /tmp/${2}_triggers${TIMESTAMP}.dump TABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='BASE TABLE'" -sss` if [ -z "$TABLES" ]; then echo "Dropping database $2" mysql -h $1 $2 -e "drop database $2" fi if [ `mysql -h $1 -e "select count(*) from mysql.columns_priv where db='$2'" -sss` -gt 0 ]; then COLUMNS_PRIV=" UPDATE mysql.columns_priv set db='$3' WHERE db='$2';" fi if [ `mysql -h $1 -e "select count(*) from mysql.procs_priv where db='$2'" -sss` -gt 0 ]; then PROCS_PRIV=" UPDATE mysql.procs_priv set db='$3' WHERE db='$2';" fi if [ `mysql -h $1 -e "select count(*) from mysql.tables_priv where db='$2'" -sss` -gt 0 ]; then TABLES_PRIV=" UPDATE mysql.tables_priv set db='$3' WHERE db='$2';" fi if [ `mysql -h $1 -e "select count(*) from mysql.db where db='$2'" -sss` -gt 0 ]; then DB_PRIV=" UPDATE mysql.db set db='$3' WHERE db='$2';" fi if [ -n "$COLUMNS_PRIV" ] || [ -n "$PROCS_PRIV" ] || [ -n "$TABLES_PRIV" ] || [ -n "$DB_PRIV" ]; then echo "IF YOU WANT TO RENAME the GRANTS YOU NEED TO RUN ALL OUTPUT BELOW:" if [ -n "$COLUMNS_PRIV" ]; then echo "$COLUMNS_PRIV"; fi if [ -n "$PROCS_PRIV" ]; then echo "$PROCS_PRIV"; fi if [ -n "$TABLES_PRIV" ]; then echo "$TABLES_PRIV"; fi if [ -n "$DB_PRIV" ]; then echo "$DB_PRIV"; fi echo " flush privileges;" fi
Most of the answers here are wrong for one of two reasons: You cannot just use RENAME TABLE, because there might be views and triggers. If there are triggers, RENAME TABLE fails You cannot use mysqldump if you want to "quickly" (as requested in the question) rename a big database Percona has a blog post about how to do this well: https://www.percona.com/blog/2013/12/24/renaming-database-schema-mysql/ 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: https://gist.github.com/ryantm/76944318b0473ff25993ef2a7186213d Here is a copy of it: #!/bin/bash # Copyright 2013 Percona LLC and/or its affiliates # #see https://www.percona.com/blog/2013/12/24/renaming-database-schema-mysql/ set -e if [ -z "$3" ]; then echo "rename_db <server> <database> <new_database>" exit 1 fi db_exists=`mysql -h $1 -e "show databases like '$3'" -sss` if [ -n "$db_exists" ]; then echo "ERROR: New database already exists $3" exit 1 fi TIMESTAMP=`date +%s` character_set=`mysql -h $1 -e "SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = '$2'" -sss` TABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='BASE TABLE'" -sss` STATUS=$? if [ "$STATUS" != 0 ] || [ -z "$TABLES" ]; then echo "Error retrieving tables from $2" exit 1 fi echo "create database $3 DEFAULT CHARACTER SET $character_set" mysql -h $1 -e "create database $3 DEFAULT CHARACTER SET $character_set" TRIGGERS=`mysql -h $1 $2 -e "show triggers\G" | grep Trigger: | awk '{print $2}'` VIEWS=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='VIEW'" -sss` if [ -n "$VIEWS" ]; then mysqldump -h $1 $2 $VIEWS > /tmp/${2}_views${TIMESTAMP}.dump fi mysqldump -h $1 $2 -d -t -R -E > /tmp/${2}_triggers${TIMESTAMP}.dump for TRIGGER in $TRIGGERS; do echo "drop trigger $TRIGGER" mysql -h $1 $2 -e "drop trigger $TRIGGER" done for TABLE in $TABLES; do echo "rename table $2.$TABLE to $3.$TABLE" mysql -h $1 $2 -e "SET FOREIGN_KEY_CHECKS=0; rename table $2.$TABLE to $3.$TABLE" done if [ -n "$VIEWS" ]; then echo "loading views" mysql -h $1 $3 < /tmp/${2}_views${TIMESTAMP}.dump fi echo "loading triggers, routines and events" mysql -h $1 $3 < /tmp/${2}_triggers${TIMESTAMP}.dump TABLES=`mysql -h $1 -e "select TABLE_NAME from information_schema.tables where table_schema='$2' and TABLE_TYPE='BASE TABLE'" -sss` if [ -z "$TABLES" ]; then echo "Dropping database $2" mysql -h $1 $2 -e "drop database $2" fi if [ `mysql -h $1 -e "select count(*) from mysql.columns_priv where db='$2'" -sss` -gt 0 ]; then COLUMNS_PRIV=" UPDATE mysql.columns_priv set db='$3' WHERE db='$2';" fi if [ `mysql -h $1 -e "select count(*) from mysql.procs_priv where db='$2'" -sss` -gt 0 ]; then PROCS_PRIV=" UPDATE mysql.procs_priv set db='$3' WHERE db='$2';" fi if [ `mysql -h $1 -e "select count(*) from mysql.tables_priv where db='$2'" -sss` -gt 0 ]; then TABLES_PRIV=" UPDATE mysql.tables_priv set db='$3' WHERE db='$2';" fi if [ `mysql -h $1 -e "select count(*) from mysql.db where db='$2'" -sss` -gt 0 ]; then DB_PRIV=" UPDATE mysql.db set db='$3' WHERE db='$2';" fi if [ -n "$COLUMNS_PRIV" ] || [ -n "$PROCS_PRIV" ] || [ -n "$TABLES_PRIV" ] || [ -n "$DB_PRIV" ]; then echo "IF YOU WANT TO RENAME the GRANTS YOU NEED TO RUN ALL OUTPUT BELOW:" if [ -n "$COLUMNS_PRIV" ]; then echo "$COLUMNS_PRIV"; fi if [ -n "$PROCS_PRIV" ]; then echo "$PROCS_PRIV"; fi if [ -n "$TABLES_PRIV" ]; then echo "$TABLES_PRIV"; fi if [ -n "$DB_PRIV" ]; then echo "$DB_PRIV"; fi echo " flush privileges;" fi Save it to a file called rename_db and make the script executable with chmod +x rename_db then use it like ./rename_db localhost old_db new_db
For those who are Mac users, Sequel Pro has a Rename Database option in the Database menu. http://www.sequelpro.com/
Seems noone mentioned this but here is another way: create database NewDatabaseName like OldDatabaseName; then for each table do: create NewDatabaseName.tablename like OldDatabaseName.tablename; insert into NewDataBaseName.tablename select * from OldDatabaseName.tablename; then, if you want to, drop database OldDatabaseName; 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. If you do have stored procedures/views/etc you might want to transfer them as well.
For mac users, you can use Sequel Pro (free), which just provide the option to rename Databases. Though it doesn't delete the old DB. once open the relevant DB just click: Database --> Rename database...
It is possible to rename all tables within a database to be under another database without having to do a full dump and restore. DROP PROCEDURE IF EXISTS mysql.rename_db; DELIMITER || CREATE PROCEDURE mysql.rename_db(IN old_db VARCHAR(100), IN new_db VARCHAR(100)) BEGIN SELECT CONCAT('CREATE DATABASE ', new_db, ';') `# create new database`; SELECT CONCAT('RENAME TABLE `', old_db, '`.`', table_name, '` TO `', new_db, '`.`', table_name, '`;') `# alter table` FROM information_schema.tables WHERE table_schema = old_db; SELECT CONCAT('DROP DATABASE `', old_db, '`;') `# drop old database`; END|| DELIMITER ; $ time mysql -uroot -e "call mysql.rename_db('db1', 'db2');" | mysql -uroot However any triggers in the target db will not be happy. You'll need to drop them first then recreate them after the rename. mysql -uroot -e "call mysql.rename_db('test', 'blah2');" | mysql -uroot ERROR 1435 (HY000) at line 4: Trigger in wrong schema
Here is a batch file I wrote to automate it from the command line, but it for Windows/MS-DOS. Syntax is rename_mysqldb database newdatabase -u [user] -p[password] :: *************************************************************************** :: FILE: RENAME_MYSQLDB.BAT :: *************************************************************************** :: DESCRIPTION :: This is a Windows /MS-DOS batch file that automates renaming a MySQL database :: by using MySQLDump, MySQLAdmin, and MySQL to perform the required tasks. :: The MySQL\bin folder needs to be in your environment path or the working directory. :: :: WARNING: The script will delete the original database, but only if it successfully :: created the new copy. However, read the disclaimer below before using. :: :: DISCLAIMER :: This script is provided without any express or implied warranties whatsoever. :: The user must assume the risk of using the script. :: :: You are free to use, modify, and distribute this script without exception. :: *************************************************************************** :INITIALIZE #ECHO OFF IF [%2]==[] GOTO HELP IF [%3]==[] (SET RDB_ARGS=--user=root) ELSE (SET RDB_ARGS=%3 %4 %5 %6 %7 %8 %9) SET RDB_OLDDB=%1 SET RDB_NEWDB=%2 SET RDB_DUMPFILE=%RDB_OLDDB%_dump.sql GOTO START :START SET RDB_STEP=1 ECHO Dumping "%RDB_OLDDB%"... mysqldump %RDB_ARGS% %RDB_OLDDB% > %RDB_DUMPFILE% IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT SET RDB_STEP=2 ECHO Creating database "%RDB_NEWDB%"... mysqladmin %RDB_ARGS% create %RDB_NEWDB% IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT SET RDB_STEP=3 ECHO Loading dump into "%RDB_NEWDB%"... mysql %RDB_ARGS% %RDB_NEWDB% < %RDB_DUMPFILE% IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT SET RDB_STEP=4 ECHO Dropping database "%RDB_OLDDB%"... mysqladmin %RDB_ARGS% drop %RDB_OLDDB% --force IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT SET RDB_STEP=5 ECHO Deleting dump... DEL %RDB_DUMPFILE% IF %ERRORLEVEL% NEQ 0 GOTO ERROR_ABORT ECHO Renamed database "%RDB_OLDDB%" to "%RDB_NEWDB%". GOTO END :ERROR_ABORT IF %RDB_STEP% GEQ 3 mysqladmin %RDB_ARGS% drop %NEWDB% --force IF %RDB_STEP% GEQ 1 IF EXIST %RDB_DUMPFILE% DEL %RDB_DUMPFILE% ECHO Unable to rename database "%RDB_OLDDB%" to "%RDB_NEWDB%". GOTO END :HELP ECHO Renames a MySQL database. ECHO Usage: %0 database new_database [OPTIONS] ECHO Options: Any valid options shared by MySQL, MySQLAdmin and MySQLDump. ECHO --user=root is used if no options are specified. GOTO END :END SET RDB_OLDDB= SET RDB_NEWDB= SET RDB_ARGS= SET RDB_DUMP= SET RDB_STEP=
For your convenience, below is a small shellscript that has to be executed with two parameters: db-name and new db-name. 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. #!/usr/bin/env bash mysql -e "CREATE DATABASE $2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;" for i in $(mysql -Ns $1 -e "show tables");do echo "$1.$i -> $2.$i" mysql -e "rename TABLE $1.$i to $2.$i" done mysql -e "DROP DATABASE $1"
The simplest method is to use HeidiSQL software. It's free and open source. It runs on Windows and on any Linux with Wine (run Windows applications on Linux, BSD, Solaris and Mac OS X). To download HeidiSQL, goto http://www.heidisql.com/download.php. To download Wine, goto http://www.winehq.org/. To rename a database in HeidiSQL, just right click on the database name and select 'Edit'. Then enter a new name and press 'OK'. It is so simple.
TodoInTX's stored procedure didn't quite work for me. Here's my stab at it: -- stored procedure rename_db: Rename a database my means of table copying. -- Caveats: -- Will clobber any existing database with the same name as the 'new' database name. -- ONLY copies tables; stored procedures and other database objects are not copied. -- Tomer Altman (taltman#ai.sri.com) delimiter // DROP PROCEDURE IF EXISTS rename_db; CREATE PROCEDURE rename_db(IN old_db VARCHAR(100), IN new_db VARCHAR(100)) BEGIN DECLARE current_table VARCHAR(100); DECLARE done INT DEFAULT 0; DECLARE old_tables CURSOR FOR select table_name from information_schema.tables where table_schema = old_db; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; SET #output = CONCAT('DROP SCHEMA IF EXISTS ', new_db, ';'); PREPARE stmt FROM #output; EXECUTE stmt; SET #output = CONCAT('CREATE SCHEMA IF NOT EXISTS ', new_db, ';'); PREPARE stmt FROM #output; EXECUTE stmt; OPEN old_tables; REPEAT FETCH old_tables INTO current_table; IF NOT done THEN SET #output = CONCAT('alter table ', old_db, '.', current_table, ' rename ', new_db, '.', current_table, ';'); PREPARE stmt FROM #output; EXECUTE stmt; END IF; UNTIL done END REPEAT; CLOSE old_tables; END// delimiter ;
There is a reason you cannot do this. (despite all the attempted answers) 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.
In MySQL Administrator do the following: Under Catalogs, create a new database schema. Go to Backup and create a backup of the old schema. Execute backup. Go to Restore and open the file created in step 3. Select 'Another Schema' under Target Schema and select the new database schema. Start Restore. Verify new schema and, if it looks good, delete the old one.
in phpmyadmin you can easily rename the database select database goto operations tab in that rename Database to : type your new database name and click go ask to drop old table and reload table data click OK in both Your database is renamed
Here is a quick way to generate renaming sql script, if you have many tables to move. SELECT DISTINCT CONCAT('RENAME TABLE ', t.table_schema,'.', t.table_name, ' TO ', t.table_schema, "_archive", '.', t.table_name, ';' ) as Rename_SQL FROM information_schema.tables t WHERE table_schema='your_db_name' ;
Here is a one-line Bash snippet to move all tables from one schema to another: history -d $((HISTCMD-1)) && mysql -udb_user -p'db_password' -Dold_schema -ABNnqre'SHOW TABLES;' | sed -e's/.*/RENAME TABLE old_schema.`&` TO new_schema.`&`;/' | mysql -udb_user -p'db_password' -Dnew_schema The history command at the start simply ensures that the MySQL commands containing passwords aren't saved to the shell history. Make sure that db_user has read/write/drop permissions on the old schema, and read/write/create permissions on the new schema.
ALTER DATABASE is the proposed way around this by MySQL and RENAME DATABASE is dropped. From 13.1.32 RENAME DATABASE Syntax: RENAME {DATABASE | SCHEMA} db_name TO new_db_name; This statement was added in MySQL 5.1.7, but it was found to be dangerous and was removed in MySQL 5.1.23.
I did it this way: Take backup of your existing database. It will give you a db.zip.tmp and then in command prompt write following "C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost -u root -p[password] [new db name] < "C:\Backups\db.zip.tmp"
This works for all databases and works by renaming each table with maatkit mysql toolkit Use mk-find to print and rename each table. The man page has many more options and examples mk-find --dblike OLD_DATABASE --print --exec "RENAME TABLE %D.%N TO NEW_DATABASE.%N" If you have maatkit installed (which is very easy), then this is the simplest way to do it.