How do I import an SQL file using the command line in MySQL?

后端 未结 30 2223
不知归路
不知归路 2020-11-22 06:48

I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.

I have a Windows Ser

相关标签:
30条回答
  • 2020-11-22 07:22

    Import into the database:

    mysql -u username -p database_name < /file path/file_name.sql

    Export from the database:

    mysqldump -u username -p database_name > /file path/file_name.sql

    After these commands, a prompt will ask for your MySQL password.

    0 讨论(0)
  • 2020-11-22 07:24

    While most answers here just mention the simple command

    mysql -u database_user -p [db_name] < database_file.sql

    today it's quite common that databases and tables have utf8-collation where this command is not sufficient. Having utf8-collation in the exported tables it's required to use this command:

    mysql -u database_user -p --default-character-set=utf8 [db_name] < database_file.sql

    Surley this works for other charsets too, how to show the right notation can be seen here:

    https://dev.mysql.com/doc/refman/5.7/en/show-collation.html

    One comment mentioned also that if a database never exists an empty database had to be created first. This might be right in some cases, but depends on the export file. If the exported file includes already the command to create the database then the database never has to be created in a separated step, which even could cause an error on import. So on import it's advisable to have a look first in the file to know which commands are included there, on export it's advisable note the settings, especially if the file is very large and hard to read in an editor.

    There are still more parameters for the command which are listed and explained here:

    https://dev.mysql.com/doc/refman/5.7/en/mysql-command-options.html

    If you use another database-version consider searching for the corresponding version of the manual too. The mentioned links refer to MySQL version 5.7.

    0 讨论(0)
  • 2020-11-22 07:26

    Among all the answers, for the problem above, this is the best one:

     mysql> use db_name;
     mysql> source file_name.sql;
    
    0 讨论(0)
  • 2020-11-22 07:27

    Regarding the time taken for importing huge files: most importantly, it takes more time because the default setting of MySQL is autocommit = true. You must set that off before importing your file and then check how import works like a gem.

    You just need to do the following thing:

    mysql> use db_name;
    
    mysql> SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;
    
    0 讨论(0)
  • 2020-11-22 07:29

    A solution that worked for me is below:

    Use your_database_name;
    SOURCE path_to_db_sql_file_on_your_local;
    
    0 讨论(0)
  • 2020-11-22 07:29

    Use:

    mysql -u root -p password -D database_name << import.sql
    

    Use the MySQL help for details - mysql --help.

    I think these will be useful options in our context:

    [~]$ mysql --help
    mysql  Ver 14.14 Distrib 5.7.20, for osx10.12 (x86_64) using  EditLine wrapper
    Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
    Usage: mysql [OPTIONS] [database]
      -?, --help          Display this help and exit.
      -I, --help          Synonym for -?
      --bind-address=name IP address to bind to.
      -D, --database=name Database to use.
      --delimiter=name    Delimiter to be used.
      --default-character-set=name Set the default character set.
      -f, --force         Continue even if we get an SQL error.
      -p, --password[=name] Password to use when connecting to server.
      -h, --host=name     Connect to host.
      -P, --port=#        Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).
      --protocol=name     The protocol to use for connection (tcp, socket, pipe,
      -s, --silent        Be more silent. Print results with a tab as separator, each row on new line.
      -v, --verbose       Write more. (-v -v -v gives the table output format).
      -V, --version       Output version information and exit.
      -w, --wait          Wait and retry if connection is down.
    

    What is fun, if we are importing a large database and not having a progress bar. Use Pipe Viewer and see the data transfer through the pipe

    For Mac, brew install pv

    For Debian/Ubuntu, apt-get install pv.

    For others, refer to pv - Pipe Viewer

    pv import.sql | mysql -u root -p password -D database_name
    
    1.45GiB 1:50:07 [339.0KiB/s]   [=============>      ] 14% ETA 11:09:36
    1.46GiB 1:50:14 [ 246KiB/s]     [=============>      ] 14% ETA 11:09:15
    1.47GiB 1:53:00 [ 385KiB/s]     [=============>      ] 14% ETA 11:05:36
    
    0 讨论(0)
提交回复
热议问题