开发者

MySQL Dump All Databases and Create (or Recreate) them on Import?

开发者 https://www.devze.com 2023-02-06 06:50 出处:网络
I was wondering how I can get the following file, mysqldump -h server -u root -p --all-databases > all_dbs.sql

I was wondering how I can get the following file,

mysqldump -h server -u root -p --all-databases > all_dbs.sql

to automatically create any databases that are not yet created, when imported. Also, if a database is already present, it 开发者_StackOverflowshould recreate it (overwriting the old one).

Is this possible? Thanks!


Export: mysqldump -u root -p --all-databases > all_dbs.sql
Import: mysql -u root -p < all_dbs.sql


Export:

mysqldump -uroot -p --all-databases > alldb.sql

Look up the documentation for mysqldump. You may want to use some of the options mentioned in comments:

mysqldump -uroot -p --opt --all-databases > alldb.sql
mysqldump -uroot -p --all-databases --skip-lock-tables> alldb.sql

Import:

mysql -u root -p < alldb.sql

I Just found a new solution:

Create a bash script. It backs up each database into a different file

#!/bin/bash

USER="zend"
PASSWORD=""
#OUTPUT="/Users/rabino/DBs"

#rm "$OUTPUTDIR/*gz" > /dev/null 2>&1

databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump -u $USER -p$PASSWORD --databases $db > `date +%Y%m%d`.$db.sql
       # gzip $OUTPUT/`date +%Y%m%d`.$db.sql
    fi
done


do not use "mysql" command to export data. Please use "mysqldump" instead.

I have to administrate a server that saves only:

\n

Exiting...

after executing "mysql --user=username --password=passord > somefile.sql"


/applications/MAMP/library/bin/mysqldump -u root -p --all-databases > all_dbs.sql

0

精彩评论

暂无评论...
验证码 换一张
取 消