mysql export import_mysql - 如何从命令行导出和导入.sql文件?

mysql - 如何从命令行导出和导入.sql文件?

这个问题在这里已有答案:

从命令行下载MySQL转储                                     10个答案

如何在MySQL中使用命令行导入SQL文件?                                     40个答案

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

是否有任何命令在MySQL中导出.sql文件? 那我该如何导入呢?

执行导入时,可能存在启用/禁用外键检查或仅导出表结构等约束。

我们可以用mysqldump设置这些选项吗?

8个解决方案

48 votes

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

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

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

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

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

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

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

mysqldump -u username -p databasename > filename.sql

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

Frankline answered 2019-02-05T13:27:21Z

22 votes

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

use databasename;

source data.sql;

David Kennedy answered 2019-02-05T13:27:44Z

13 votes

尝试

mysqldump databaseExample > file.sql

Youcha answered 2019-02-05T13:28:02Z

12 votes

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

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

PodTech.io answered 2019-02-05T13:28:25Z

6 votes

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

mysqldump --database --user = root --password your_db_name&gt;  export_into_db.sql

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

现在使用命令登录mysql,

mysql -u [用户名] -p

然后使用“source”命令和文件路径。

您可以找到更多信息:导出导出MySQL数据库

请享用 :)

Umesh Patil answered 2019-02-05T13:29:26Z

4 votes

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

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

Buzz answered 2019-02-05T13:29:48Z

3 votes

因为我没有足够的声誉在最高职位后发表评论,所以我在这里添加。

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

thx @Hariboo,添加事件/触发器/路径参数

mysqldump -x -u [uname] -p[pass] -C --databases db_name --events --triggers --routines | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/ ' | awk '{ if (index($0,"GTID_PURGED")) { getline; while (length($0) > 0) { getline; } } else { print $0 } }' | grep -iv 'set @@' | trickle -u 10240 mysql -u username -p -h localhost DATA-BASE-NAME

一些问题/提示:

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

# --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";

ERROR 2006(HY000)第866行:MySQL服务器已经消失mysqldump:写错了32

# 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

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

# set SET GLOBAL log_bin_trust_function_creators = 1;

# or use super user import data

第138行的错误1227(42000):拒绝访问; 您需要(至少一个)此操作的SUPER权限mysqldump:写错了32

# add sed/awk to avoid some privilege issues

希望这个帮助!

Howard.TH answered 2019-02-05T13:31:06Z

0 votes

您可以使用此脚本从此链接提供的终端导出或导入任何数据库:[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

Ridhwan Luthra answered 2019-02-05T13:31:29Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值