如何使用 MySQL 命令行客户端安装 .sql 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8883438/
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 22:23:01  来源:igfitidea点击:

How to install a .sql file using MySQL Command line Client

mysqldatabaseimportinstallation

提问by Reflex84

I need to install or import a .sql file into an empty database that I've created. The .sql file will install all the tables etc.

我需要将 .sql 文件安装或导入到我创建的空数据库中。.sql 文件将安装所有表等。

When I click MySQL Command line Client prompt, I add my credentials and then...(to install / import the .sql file)?

当我单击 MySQL 命令行客户端提示时,我添加了我的凭据,然后...(安装/导入 .sql 文件)?

Thanks guys!!

谢谢你们!!

回答by Devart

Something like this:

像这样的东西:

shell> mysql db_name < script.sql

...specify your options: --user, --password, --host, --port...

...指定您的选项:--user、--password、--host、--port...

More information - mysql — The MySQL Command-Line Tool.

更多信息 - mysql — MySQL 命令行工具

回答by paulsm4

From the MySql prompt, type:

在 MySql 提示符下,键入:

\. file_name.sql 

回答by Yash

What worked for me was- on command prompt:

对我有用的是 - 在命令提示符下:

mysql -u root -p < filename.sql

mysql -u root -p <文件名.sql

I read somewhere that to make things easier I should keep the file in the same root directory as my command prompt was in. After running the above command, it asked me for the password. Typed the password, hit enter and I was back on my command prompt. Got pissed, opened mysql prompt, checked, the imported DB was present. Happy!

我在某处读到为了让事情更容易,我应该将文件保存在与命令提示符相同的根目录中。运行上述命令后,它要求我输入密码。输入密码,按回车键,我又回到了我的命令提示符。生气了,打开 mysql 提示,检查,导入的数据库存在。快乐的!

回答by Maximus

This is another way to do it in Linux

这是在 Linux 中执行此操作的另一种方法

mysql databaseName -uUSERNAME -pPASSWORD -e "source LOCATION_OF_FILE"

Example:

例子:

mysql studentDB -ustudent1 -pstuden1Pass -e "source /tmp/studentInserts.sql"

回答by Ashith

just thought of perfecting the great answers from wise users. before importing a .sql database

只是想完善来自明智用户的精彩答案。在导入 .sql 数据库之前

  1. Create a new database or use existing database.
  2. if your .sql file is saved in F as script.sql provide command as . F:\script.sql enjoy...
  1. 创建新数据库或使用现有数据库。
  2. 如果您的 .sql 文件保存在 F 中作为 script.sql 提供命令为 . F:\script.sql 享受...