如何在 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
How do I import an SQL file using the command line in MySQL?
提问by Jaylen
I have a .sql
file with an export from phpMyAdmin
. I want to import it into a different server using the command line.
我有一个.sql
从phpMyAdmin
. 我想使用命令行将其导入不同的服务器。
I have a Windows Server 2008R2 installation. I placed the .sql
file 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 -R
and --triggers
to 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-db
or -n
option), 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 root
and password is password
. And you have a database name as bank
and 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 Desktop
folder/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 Project
directory and your SQL file is in the Desktop
directory. If you want to access it from the Project
directory 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 dump
or the sql
file:
如果您已经拥有该数据库,请使用以下命令导入该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 MySQL
console 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 sql
or the dump
file to the database from
然后将文件sql
或dump
文件导入到数据库中
mysql> source pathToYourSQLFile;
Note: if your terminal is not in the location where the dump
or sql
file exists, use the relative path in above.
注意:如果您的终端不在dump
orsql
文件所在的位置,请使用上面的相对路径。
回答by user4412947
- Open the MySQL command line
- Type the path of your mysql bin directory and press Enter
- Paste your SQL file inside the
bin
folder of mysql server. - Create a database in MySQL.
- Use that particular database where you want to import the SQL file.
- Type
source databasefilename.sql
and Enter - Your SQL file upload successfully.
- 打开 MySQL 命令行
- 键入您的 mysql bin 目录的路径,然后按 Enter
- 将您的 SQL 文件粘贴
bin
到 mysql 服务器的文件夹中。 - 在 MySQL 中创建一个数据库。
- 使用要导入 SQL 文件的特定数据库。
- 类型
source databasefilename.sql
和Enter - 您的 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