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

后端 未结 30 2785
不知归路
不知归路 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: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.

提交回复
热议问题