如何从终端导入带有 MySQL 的数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4546778/
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 can I import a database with MySQL from terminal?
提问by aneuryzm
How can I import a database with mysql from terminal?
如何从终端使用 mysql 导入数据库?
I cannot find the exact syntax.
我找不到确切的语法。
回答by
Assuming you're on a Linux or Windows console:
假设您使用的是 Linux 或 Windows 控制台:
Prompt for password:
提示输入密码:
mysql -u <username> -p <databasename> < <filename.sql>
Enter password directly (not secure):
直接输入密码(不安全):
mysql -u <username> -p<PlainPassword> <databasename> < <filename.sql>
Example:
例子:
mysql -u root -p wp_users < wp_users.sql
mysql -u root -pPassword123 wp_users < wp_users.sql
See also:
也可以看看:
4.5.1.5. Executing SQL Statements from a Text File
Note: If you are on windows then you will have to cd
(change directory) to your MySQL/bin directory inside the CMD before executing the command.
注意:如果您使用的是 Windows,那么cd
在执行命令之前,您必须(更改目录)到 CMD 内的 MySQL/bin 目录。
回答by Sri Harsha Kappala
Preferable way for windows:
窗户的首选方式:
Open the console and start the interactive MySQL mode
use <name_of_your_database>;
source <path_of_your_.sql>
打开控制台,启动交互式 MySQL 模式
use <name_of_your_database>;
source <path_of_your_.sql>
回答by Mukesh Singh Rathaur
mysql -u <USERNAME> -p <DB NAME> < <dump file path>
mysql -u <USERNAME> -p <DB NAME> < <dump file path>
-u
- for Username
-u
- 用户名
-p
- to prompt the Password
-p
- 提示密码
Eg.mysql -u root -p mydb < /home/db_backup.sql
例如。mysql -u root -p mydb < /home/db_backup.sql
You can also provide password preceded by -p but for the security reasons it is not suggestible. The password will appear on the command itself rather masked.
您还可以提供以 -p 开头的密码,但出于安全原因,不建议这样做。密码将出现在命令本身上,而被屏蔽。
回答by Lokesh Das
Directly from var/www/html
直接来自 var/www/html
mysql -u username -p database_name < /path/to/file.sql
From within mysql:
在 mysql 中:
mysql> use db_name;
mysql> source backup-file.sql
回答by Mohammad Anini
From Terminal:
从终端:
mysql -uroot -p --default-character-set=utf8 database_name </database_path/database.sql
回答by Patrick Mutwiri
in the terminal type
在终端类型
mysql -uroot -p1234; use databasename; source /path/filename.sql
回答by Lano
I usually use this command to load my SQL data when divided in files with names : 000-tableA.sql, 001-tableB.sql, 002-tableC.sql.
当我将 SQL 数据分成名称为 000-tableA.sql、001-tableB.sql、002-tableC.sql 的文件时,我通常使用此命令来加载我的 SQL 数据。
for anyvar in *.sql; do <path to your bin>/mysql -u<username> -p<password> <database name> < $anyvar; done
Works well on OSX shell.
在 OSX shell 上运行良好。
回答by Deepak Chaube
Below command is working on ubuntu 16.04, I am not sure it is working or not other Linux platforms.
下面的命令在 ubuntu 16.04 上运行,我不确定它是否在其他 Linux 平台上运行。
Export SQL file:
导出 SQL 文件:
$ mysqldump -u [user_name] -p [database_name] < [database_name.sql]
Example : mysqldump -u root -p max_development > max_development.sql
示例:mysqldump -u root -p max_development > max_development.sql
Import SQL file:
导入 SQL 文件:
$ mysqldump -u [user_name] -p [database_name] > [file_name.sql]
Example: mysqldump -u root -p max_production < max_development.sql
Note SQL file should exist same directory
注意 SQL 文件应该存在于同一目录中
回答by user42826
Explanation:
解释:
First create a database or use an existing database. In my case, I am using an existing database
Load the database by giving
<name of database> = ClassicModels
in my case and using the operator<
give the path to thedatabase = sakila-data.sql
By running show tables, I get the list of tables as you can see.
首先创建一个数据库或使用现有的数据库。就我而言,我使用的是现有数据库
通过
<name of database> = ClassicModels
在我的情况下给出并使用操作符<
给出路径来加载数据库database = sakila-data.sql
通过运行 show tables,我得到了你所看到的表列表。
Note : In my case I got an error 1062, because I am trying to load the same thing again.
注意:就我而言,我收到错误 1062,因为我试图再次加载相同的内容。
回答by vaibhav kulkarni
mysql -u username -ppassword dbname < /path/file-name.sql
example
例子
mysql -u root -proot product < /home/myPC/Downloads/tbl_product.sql
Use this from terminal
从终端使用它