如何导出和导入一个.sql文件从命令行选项?

不重复!寻找一些功能有phpmyadmin在命令行导出

我想从命令行中导出和导入一个.sql文件到MySQL数据库。

MySQL中有导出。sql文件的命令吗?那我怎么导入呢?

当进行导出/导入时,可能会有约束,例如enable/disable 外键检查仅导出表结构

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

选项的一些例子

enter image description here

675203 次浏览

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

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

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

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

在本例中,导入'data。Sql '文件到'博客'数据库使用vivek作为用户名:

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

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

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

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

mysqldump -u username -p databasename > filename.sql

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

试一试

mysqldump databaseExample > file.sql

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

use databasename;
source data.sql;

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

mysqldump——databases——user=root——password your_db_name > . export_into_db.sql < / p >

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

现在使用命令登录mysql,

Mysql -u[username] -p

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

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

mysqldump -uuser -p db_name --events --triggers --routines > db_name.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

因为我在最高的帖子后没有足够的声誉来评论,所以我在这里添加。

Linux平台使用'|'节省磁盘空间。

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

错误2006 (HY000)在第866行:MySQL服务器已经离开 mysqldump: get errno 32 on write

# 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

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

# set SET GLOBAL log_bin_trust_function_creators = 1;
# or use super user import data

ERROR 1227 (42000) at line 138: Access denied;您需要(至少一个)SUPER特权来执行此操作 mysqldump: get errno 32 on write

# add sed/awk to avoid some privilege issues

希望这对你有所帮助!