如何在 MySQL 中使用命令行导入 SQL 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17666249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:10:23  来源:igfitidea点击:

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

mysqlsqlcommand-lineimport

提问by Jaylen

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

我有一个.sqlphpMyAdmin. 我想使用命令行将其导入不同的服务器。

I have a Windows Server 2008R2 installation. I placed the .sqlfile on the C drive, and I tried this command

我有一个Windows Server 2008R2 安装。我把.sql文件放在C 盘上,我尝试了这个命令

database_name < file.sql

It is not working. I get syntax errors.

它不工作。我收到语法错误。

  • How can I import this file without a problem?
  • Do I need to create a database first?
  • 我怎样才能毫无问题地导入这个文件?
  • 我需要先创建一个数据库吗?

回答by bansi

Try:

尝试:

mysql -u username -p database_name < file.sql

Check MySQL Options.

检查MySQL 选项

Note-1:It is better to use the full path of the SQL file file.sql.

注 1:最好使用 SQL 文件的完整路径file.sql

Note-2:Use -Rand --triggersto keep the routines and triggers of original database. They are not copied by default.

Note-2:使用-R--triggers来保持原数据库的例程和触发器。默认情况下不会复制它们。

Note-3You may have to create the (empty) database from mysql if it doesn't exist already and the exported SQL don't contain CREATE DATABASE(exported with --no-create-dbor -noption), before you can import it.

注3如果mysql 不存在并且导出的SQL 不包含CREATE DATABASE(使用--no-create-db-n选项导出),则您可能必须从mysql 创建(空)数据库,然后才能导入它。

回答by vladkras

A common use of mysqldumpis for making a backup of an entire database:

mysqldump 的一个常见用途是备份整个数据库:

shell> mysqldump db_name > backup-file.sql

You can load the dump file back into the server like this:

您可以像这样将转储文件加载回服务器:

UNIX

UNIX

shell> mysql db_name < backup-file.sql

The same in Windowscommand prompt:

Windows命令提示符中相同:

mysql -p -u [user] [database] < backup-file.sql

PowerShell

电源外壳

C:\> cmd.exe /c "mysql -u root -p db_name < backup-file.sql"

MySQL command line

MySQL命令行

mysql> use db_name;
mysql> source backup-file.sql;

回答by Paresh Behede

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.

关于导入大文件所需的时间:最重要的是,它需要更多的时间,因为 MySQL 的默认设置是autocommit = true. 您必须在导入文件之前将其关闭,然后检查导入如何像 gem 一样工作。

You just need to do the following thing:

你只需要做以下事情:

mysql> use db_name;

mysql> SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;

回答by Manoj Kumar

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

在所有答案中,对于上面的问题,这是最好的一个:

 mysql> use db_name;
 mysql> source file_name.sql;

回答by Amrit Dhungana

We can use this command to import SQL from command line:

我们可以使用这个命令从命令行导入 SQL:

mysql -u username -p password db_name < file.sql

For example, if the username is rootand password is password. And you have a database name as bankand the SQL file is bank.sql. Then, simply do like this:

例如,如果用户名是root,密码是password。并且您有一个数据库名称,bank而 SQL 文件是bank.sql. 然后,只需这样做:

mysql -u root -p password bank < bank.sql

Remember where your SQL file is. If your SQL file is in the Desktopfolder/directory then go the desktop directory and enter the command like this:

记住您的 SQL 文件在哪里。如果您的 SQL 文件在Desktop文件夹/目录中,则转到桌面目录并输入如下命令:

~ ? cd Desktop
~/Desktop ? mysql -u root -p password bank < bank.sql

And if your are in the Projectdirectory and your SQL file is in the Desktopdirectory. If you want to access it from the Projectdirectory then you can do like this:

如果您在Project目录中并且您的 SQL 文件在Desktop目录中。如果你想从Project目录访问它,那么你可以这样做:

~/Project ? mysql -u root -p password bank < ~/Desktop/bank.sql

回答by Ammad

Easiest way to import into your schema:

导入架构的最简单方法:

Login to mysql and issue below mention commands.

登录到 mysql 并发出下面提到的命令。

mysql> use your_db_name;

mysql> source /opt/file.sql;

回答by Kasun Siyambalapitiya

If you already have the database, use the following to import the dumpor the sqlfile:

如果您已经拥有该数据库,请使用以下命令导入该dump或该sql文件:

mysql -u username -p database_name < file.sql

if you don't you need to create the relevant database(empty) in MySQL, for that first log on to the MySQLconsole by running the following command in terminal or in cmd

如果你不需要在 MySQL 中创建相关的数据库(空),首先MySQL通过在终端或 cmd 中运行以下命令登录到控制台

mysql -u userName -p;

And when prompted provide the password.

并在提示时提供密码。

Next, create a database and use it:

接下来,创建一个数据库并使用它:

mysql>create database yourDatabaseName;
mysql>use yourDatabaseName;

Then import the sqlor the dumpfile to the database from

然后将文件sqldump文件导入到数据库中

mysql> source pathToYourSQLFile;

Note: if your terminal is not in the location where the dumpor sqlfile exists, use the relative path in above.

注意:如果您的终端不在dumporsql文件所在的位置,请使用上面的相对路径。

回答by user4412947

  1. Open the MySQL command line
  2. Type the path of your mysql bin directory and press Enter
  3. Paste your SQL file inside the binfolder of mysql server.
  4. Create a database in MySQL.
  5. Use that particular database where you want to import the SQL file.
  6. Type source databasefilename.sqland Enter
  7. Your SQL file upload successfully.
  1. 打开 MySQL 命令行
  2. 键入您的 mysql bin 目录的路径,然后按 Enter
  3. 将您的 SQL 文件粘贴bin到 mysql 服务器的文件夹中。
  4. 在 MySQL 中创建一个数据库。
  5. 使用要导入 SQL 文件的特定数据库。
  6. 类型source databasefilename.sqlEnter
  7. 您的 SQL 文件上传成功。

回答by Shiks

A solution that worked for me is below:

对我有用的解决方案如下:

Use your_database_name;
SOURCE path_to_db_sql_file_on_your_local;

回答by Adeleke Akinade

To dump a database into an SQL file use the following command.

要将数据库转储到 SQL 文件中,请使用以下命令。

mysqldump -u username -p database_name > database_name.sql

To import an SQL file into a database (make sure you are in the same directory as the SQL file or supply the full path to the file), do:

要将 SQL 文件导入数据库(确保您与 SQL 文件位于同一目录中或提供文件的完整路径),请执行以下操作:

mysql -u username -p database_name < database_name.sql