首页 文章

如何从命令行导出和导入.sql文件? [重复]

提问于
浏览
266

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

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

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

执行导入时,可能存在启用/禁用 foreign key checkexport only table structure 等限制 .

我们可以用 mysqldump 设置那些 options 吗?

8 回答

  • 22

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

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

  • 51

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

    use databasename;
    source data.sql;
    
  • 6

    尝试

    mysqldump databaseExample > file.sql
    
  • 13

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

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

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

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

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

    现在使用命令登录mysql,

    mysql -u [用户名] -p

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

    你可以找到更多:Import Export MySQL DB

    请享用 :)

  • 3

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

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

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

    使用'|'在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):拒绝访问;你需要(至少一个)这个操作mysqldump的SUPER权限:写错了32

    # add sed/awk to avoid some privilege issues
    

    希望这个帮助!

  • 0

    您可以在此链接中使用此终端的 script to export or import 任何数据库: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
    

相关问题