MySQL 如何使用选项从命令行导出和导入 .sql 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11407349/
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 14:06:42  来源:igfitidea点击:

How to export and import a .sql file from command line with options?

mysqlcommand-linecommand-line-argumentssqlcommand

提问by AZinkey

Not Duplicate!looking for some feature have phpmyadmin during export in command line

不重复!在命令行导出期间寻找具有 phpmyadmin 的某些功能

I want to export and import a .sql file to and from a MySQL database from command line.

我想从命令行向 MySQL 数据库导出和导入 .sql 文件。

Is there any command to export .sql file in MySQL? Then how do I import it?

是否有任何命令可以在 MySQL 中导出 .sql 文件?那我怎么导入呢?

When doing the export/import, there may be constraintslike enable/disable foreign key checkor export only table structure.

进行导出/导入时,可能存在诸如启用/禁用外键检查仅导出表结构之类的约束

Can we set those optionswith mysqldump?

我们可以设置这些选项mysqldump

some example of Options

选项的一些例子

enter image description here

在此处输入图片说明

回答by Frankline

Type the following command to import sql data file:

输入以下命令导入sql数据文件:

$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

In this example, import 'data.sql' file into 'blog' database using vivek as username:

在此示例中,使用 vivek 作为用户名将“data.sql”文件导入“blog”数据库:

$ mysql -u vivek -p -h localhost blog < data.sql

If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:

如果您有专用的数据库服务器,请将 localhost 主机名替换为实际的服务器名称或 IP 地址,如下所示:

$ mysql -u username -p -h 202.54.1.10 databasename < data.sql

To export a database, use the following:

要导出数据库,请使用以下命令:

mysqldump -u username -p databasename > filename.sql

Note the <and >symbols in each case.

请注意每种情况下的<>符号。

回答by Big McLargeHuge

If you're already running the SQL shell, you can use the sourcecommand to import data:

如果您已经在运行 SQL shell,则可以使用以下source命令导入数据:

use databasename;
source data.sql;

回答by PodTech.io

mysqldump will not dump database events, triggers and routines unless explicitly stated when dumping individual databases;

除非在转储单个数据库时明确说明,否则 mysqldump 不会转储数据库事件、触发器和例程;

mysqldump -uuser -p db_name --events --triggers --routines > db_name.sql

回答by Umesh Patil

Well you can use below command to export,

那么你可以使用下面的命令导出,

mysqldump --database --user=root --password your_db_name > export_into_db.sql

mysqldump --database --user=root --password your_db_name > export_into_db.sql

and the generated file will be available in the same directory where you had ran this command.

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

Now login to mysql using command,

现在使用命令登录到mysql,

mysql -u[username] -p

mysql -u[用户名] -p

then use "source" command with the file path.

然后将“source”命令与文件路径一起使用。

回答by Youcha

Try

尝试

mysqldump databaseExample > file.sql

回答by Buzz

Dump an entire database to a file:

将整个数据库转储到文件中:

mysqldump -u USERNAME -p password DATABASENAME > FILENAME.sql

回答by Howard.TH

since I have no enough reputation to comment after the highest post, so I add here.

由于我没有足够的声誉在最高帖子后发表评论,所以我在这里添加。

use '|' on linux platform to save disk space.

使用“|” 在linux平台上节省磁盘空间。

thx @Hariboo, add events/triggers/routints parameters

谢谢@Hariboo,添加事件/触发器/路由参数

mysqldump -x -u [uname] -p[pass]  -C --databases db_name  --events --triggers --routines | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/ '  | awk '{ if (index(
# --lock-all-tables,-x , this parameter is to keep data consistency because some transaction may still be working like schedule.
# also you need check and confirm: grant all privileges on *.* to root@"%" identified by "Passwd";
,"GTID_PURGED")) { getline; while (length(
# set this values big enough on destination mysql server, like: max_allowed_packet=1024*1024*20
# use compress parameter '-C'
# use trickle to limit network bandwidth while write data to destination server
) > 0) { getline; } } else { print
# set SET GLOBAL log_bin_trust_function_creators = 1;
# or use super user import data
} }' | grep -iv 'set @@' | trickle -u 10240 mysql -u username -p -h localhost DATA-BASE-NAME

some issues/tips:

一些问题/提示:

Error: ......not exist when using LOCK TABLES

错误:......使用 LOCK TABLES 时不存在

# add sed/awk to avoid some privilege issues

ERROR 2006 (HY000) at line 866: MySQL server has gone away mysqldump: Got errno 32 on write

第 866 行的 ERROR 2006 (HY000):MySQL 服务器已消失 mysqldump:写入时出现 errno 32

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

ERROR 1419 (HY000) at line 32730: You do not have the SUPER privilege and binary logging is enabled (you mightwant to use the less safe log_bin_trust_function_creators variable)

第 32730 行的 ERROR 1419 (HY000):您没有 SUPER 权限并且启用了二进制日志记录(您可能想要使用安全性较低的 log_bin_trust_function_creators 变量)

##代码##

ERROR 1227 (42000) at line 138: Access denied; you need (at least one of) the SUPER privilege(s) for this operation mysqldump: Got errno 32 on write

第 138 行的 ERROR 1227 (42000):访问被拒绝;您需要(至少其中之一)此操作 mysqldump 的 SUPER 权限:Got errno 32 on write

##代码##

hope this help!

希望这有帮助!

回答by Ridhwan Luthra

You can use this script to export or importany database from terminal given at this link: https://github.com/Ridhwanluthra/mysql_import_export_script/blob/master/mysql_import_export_script.sh

您可以使用此脚本从以下链接中给出的终端导出或导入任何数据库:https: //github.com/Ridhwanluthra/mysql_import_export_script/blob/master/mysql_import_export_script.sh

##代码##