MySQL 转储所有数据库并在导入时创建(或重新创建)它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4749448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:23:44  来源:igfitidea点击:

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

mysqldatabasemysqldump

提问by Garrett

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 should recreate it (overwriting the old one).

在导入时自动创建任何尚未创建的数据库。此外,如果一个数据库已经存在,它应该重新创建它(覆盖旧的)。

Is this possible? Thanks!

这可能吗?谢谢!

回答by Garrett

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

回答by Suneel Kumar

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 的文档。您可能想使用评论中提到的一些选项:

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

创建一个 bash 脚本。它将每个数据库备份到不同的文件中

#!/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

回答by user3895544

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

不要使用“mysql”命令导出数据。请改用“mysqldump”。

I have to administrate a server that saves only:

我必须管理一个只保存的服务器:

\n

Exiting...

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

执行“mysql --user=username --password=passord > somefile.sql”后

回答by M6R7M

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

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