如何导出和导入从选项命令行.sql文件? [重复](How to export and impo

2019-06-14 19:02发布

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

  • 在命令行下载MySQL的转储 10个回答
  • 如何使用在MySQL命令行导入SQL文件? 41个回答

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

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

在做进口,有可能会像启用/禁用外键检查出口仅表结构的约束。

我们可以设置这些选项 mysqldump

Answer 1:

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

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

在这个例子中,进口“data.sql”文件到使用维韦克作为用户名“博客”数据库:

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

如果你有一个专用的数据库服务器,与实际的服务器名称或如下的IP地址替换本地主机的主机名:

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

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

mysqldump -u username -p databasename > filename.sql

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



Answer 2:

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

use databasename;
source data.sql;


Answer 3:

mysqldump的将不转储数据库事件,触发器和程序,除非明确指出倾销个人数据库时;

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


Answer 4:

尝试

mysqldump databaseExample > file.sql


Answer 5:

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

mysqldump的--database --user =根--password your_db_name> export_into_db.sql

并且生成的文件会在您运行了此命令的同一目录可用。

立即登录使用命令到MySQL,

MySQL的-u [用户名] -p

然后用“源”命令与该文件的路径。



Answer 6:

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

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


Answer 7:

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

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

THX @Hariboo,添加事件/触发器/ routints参数

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

在线路866 ERROR 2006年(HY000):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

在线路32730 ERROR 1419(HY000):你没有SUPER权限和二进制日志启用(您可能需要使用不太安全log_bin_trust_function_creators变量)

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

在管线138 ERROR 1227(42000):访问被拒绝; 你需要(至少一个),该操作的mysqldump SUPER权限(S):得到错误号32上写

# add sed/awk to avoid some privilege issues

希望这帮助!



Answer 8:

你可以使用这个脚本来导出或从这个链接给终端导入任何数据库: 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


文章来源: How to export and import a .sql file from command line with options? [duplicate]