如何从命令行导出和导入带有选项的.sql文件?

时间:2012-07-10 05:51:16

标签: mysql command-line command-line-arguments sqlcommand

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

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

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

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

8 个答案:

答案 0 :(得分:68)

键入以下命令以导入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

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

答案 1 :(得分:25)

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

use databasename;
source data.sql;

答案 2 :(得分:17)

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

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

答案 3 :(得分:14)

尝试

mysqldump databaseExample > file.sql

答案 4 :(得分:13)

您可以使用以下命令导出

  

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

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

现在使用命令

登录mysql
  

mysql -u [用户名] -p

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

答案 5 :(得分:6)

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

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

答案 6 :(得分:3)

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

使用&#39; |&#39;在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

ERROR 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
希望这有帮助!

答案 7 :(得分:0)

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