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

本文翻译自:How do I import an SQL file using the command line in MySQL?

I have a .sql file with an export from phpMyAdmin . 我有一个.sql文件,从phpMyAdmin导出。 I want to import it into a different server using the command line. 我想使用命令行将其导入到其他服务器中。

I have a Windows Server 2008 R2 installation. 我有Windows Server 2008 R2安装。 I placed the .sql file on the C drive , and I tried this command 我将.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? 我需要先创建一个数据库吗?

#1楼

参考:https://stackoom.com/question/1C7nV/如何在MySQL中使用命令行导入SQL文件


#2楼

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. 注2:使用-R--triggers保留原始数据库的例程和触发器。 They are not copied by default. 默认情况下不会复制它们。

Note-3 You 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 CREATE DATABASE可以导入它。


#3楼

A common use of mysqldump is 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 Windows command 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;

#4楼

Go to the directory where you have MySQL. 转到您拥有MySQL的目录。

 c:\mysql\bin\> mysql -u username -p password database_name <
 filename.sql

Also to dump all databases, use the -all-databases option, and no databases' name needs to be specified anymore. 同样,要转储所有数据库,请使用-all-databases选项,并且不再需要指定数据库名称。

mysqldump -u username -ppassword –all-databases > dump.sql

Or you can use some GUI clients like SQLyog to do this. 或者,您可以使用SQLyog之类的GUI客户端来执行此操作。


#5楼

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 . 例如,如果用户名是root ,密码是password And you have a database name as bank and the SQL file is bank.sql . 并且您有一个数据库名称为bank ,而SQL文件为bank.sql Then, simply do like this: 然后,只需执行以下操作:

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

Remember where your SQL file is. 记住您的SQL文件在哪里。 If your SQL file is in the Desktop folder/directory then go the desktop directory and enter the command like this: 如果您的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. 并且如果您位于Project目录中,而您的SQL文件位于Desktop目录中。 If you want to access it from the Project directory then you can do like this: 如果要从Project目录访问它,则可以执行以下操作:

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

#6楼

Regarding the time taken for importing huge files: most importantly, it takes more time because the default setting of MySQL is autocommit = true . 关于导入大文件所花费的时间:最重要的是,它花费了更多时间,因为MySQL的默认设置为autocommit = true You must set that off before importing your file and then check how import works like a gem. 在导入文件之前,必须先将其关闭,然后检查导入的工作方式像gem。

You just need to do the following thing: 您只需要执行以下操作:

mysql> use db_name;

mysql> SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;
发布了0 篇原创文章 · 获赞 72 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105192247
今日推荐