简体   繁体   English

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

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

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! 谢谢!

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. 查找有关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. 创建一个bash脚本。 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. 不要使用“ mysql”命令导出数据。 Please use "mysqldump" instead. 请改为使用“ 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”后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM