我有一个相当大的数据库,所以我想用命令提示符导出它,但我不知道如何。

我正在使用WAMP。


当前回答

语法

(mysqldump.exe full path) -u (user name) -p (password) (database name) > (export database file full path)

例子

c:>d:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump.exe -u root -p mydbname > d:\mydb.sql

其中d:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump.exe将是您实际的mysqldump.exe路径,mydbname是您要导出的数据库的名称,d:\mydb. exe将是您要导出的数据库的名称。SQL是要存储导出数据库的路径。

其他回答

找到你的mysql实例:

which mysql

如果这是正确的,然后用以下方法导出(否则导航到bin中的mamp文件夹中的mysql实例):

mysqldump -u [username] -p [password] [dbname] > filename.sql

如果你想在同一时间压缩它:

mysqldump -u [username] -p [password] [db] | gzip > filename.sql.gz

然后你可以在服务器之间移动这个文件:

scp user@xxx.xxx.xxx.xxx:/path_to_your_dump/filename.sql.gz your_detination_path/

(其中xxx.xxx.xxx.xxx为服务器IP地址)

然后导入:

gunzip filename.sql.gz | mysql -u [user] -p [password] [database]

你可以使用下面的命令,

Mysqldump——databases——user=root——password your_db_name > export_into_db.sql

生成的文件将在运行此命令的同一目录中可用。

你可以找到更多关于mysqldump的官方参考:Import Export MySQL DB

注意:使用——databases代替——database,因为不再支持最后一个。

享受:)

所有这些解决方案(使用>重定向器字符)的问题是您从stdout写入转储,这可能会破坏数据库中某些字符的编码。

如果你有字符编码问题。例如:

错误1064(42000):你有一个错误的SQL语法;检查手册,对应于您的MySQL服务器版本的正确语法使用近…

然后,你必须使用-r选项来写入文件。

MySQL

mysqldump -u user -pyour-password-without-space-between-letter-p-and-your-password --default-character-set=utf8 --host $HOST database-name -r dump.sql

使用码头工人

docker exec --rm -v $pwd:dump -it mysql:5:7 mysqldump -u user -pyour-password-without-space-between-letter-p-and-your-password --default-character-set=utf8 --host $HOST database-name -r dump/dump.sql

注意:这会将当前路径挂载为实例中的转储。

我们在这里找到了答案

相反,不要使用<将转储文件导入数据库,同样,您的非utf8字符可能不会被传递;但更喜欢源选项。

mysql -u user -pYourPasswordYouNowKnowHow --default-character-set=utf8 your-database
mysql> SET names 'utf8'
mysql> SOURCE dump.sql
mysqldump --no-tablespaces -u username -p pass database_name > db_backup_file.sql

您可以使用此脚本从终端导出或导入任何数据库 网址:https://github.com/Ridhwanluthra/mysql_import_export_script/blob/master/mysql_import_export_script.sh

echo -e "Welcome to the import/export database utility\n"
echo -e "the default location of mysqldump file is: /opt/lampp/bin/mysqldump\n"
echo -e "the default location of mysql file is: /opt/lampp/bin/mysql\n"
read -p 'Would like you like to change the default location [y/n]: ' location_change
read -p "Please enter your username: " u_name
read -p 'Would you like to import or export a database: [import/export]: ' action
echo

mysqldump_location=/opt/lampp/bin/mysqldump
mysql_location=/opt/lampp/bin/mysql

if [ "$action" == "export" ]; then
    if [ "$location_change" == "y" ]; then
        read -p 'Give the location of mysqldump that you want to use: ' mysqldump_location
        echo
    else
        echo -e "Using default location of mysqldump\n"
    fi
    read -p 'Give the name of database in which you would like to export: ' db_name
    read -p 'Give the complete path of the .sql file in which you would like to export the database: ' sql_file
    $mysqldump_location -u $u_name -p $db_name > $sql_file
elif [ "$action" == "import" ]; then
    if [ "$location_change" == "y" ]; then
        read -p 'Give the location of mysql that you want to use: ' mysql_location
        echo
    else
        echo -e "Using default location of mysql\n"
    fi
    read -p 'Give the complete path of the .sql file you would like to import: ' sql_file
    read -p 'Give the name of database in which to import this file: ' db_name
    $mysql_location -u $u_name -p $db_name < $sql_file
else
    echo "please select a valid command"
fi