本文翻译自:How to export and import a .sql file from command line with options? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- Downloading MySQL dump from command line 10 answers 从命令行 10回答 下载MySQL转储
- How do I import an SQL file using the command line in MySQL? 如何在MySQL中使用命令行导入SQL文件? 45 answers 45个答案
Not Duplicate! 不重复! looking for some feature have phpmyadmin during export in command line 在命令行导出期间寻找某些功能有phpmyadmin
I want to export and import a .sql file to and from a MySQL database from command line . 我想从命令行导出和导入MySQL数据库的.sql文件。
Is there any command to export .sql file in MySQL? 是否有任何命令在MySQL中导出.sql文件? Then how do I import it? 那我该如何导入呢?
When doing the export/import, there may be constraints like enable/disable foreign key check or export only table structure . 执行导出/导入时,可能存在启用/禁用外键检查或仅导出表结构等约束 。
Can we set those options with mysqldump
? 我们可以用mysqldump
设置这些选项吗?
some example of Options 选项的一些例子
#1楼
参考:https://stackoom.com/question/LRzv/如何从命令行导出和导入带有选项的-sql文件-重复
#2楼
将整个数据库转储到文件:
mysqldump -u USERNAME -p password DATABASENAME > FILENAME.sql
#3楼
Type the following command to import sql data file: 键入以下命令以导入sql数据文件:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
In this example, import 'data.sql' file into 'blog' database using vivek as username: 在此示例中,使用vivek作为用户名将'data.sql'文件导入'blog'数据库:
$ mysql -u vivek -p -h localhost blog < data.sql
If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows: 如果您有专用数据库服务器,请将localhost主机名替换为实际服务器名称或IP地址,如下所示:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
To export a database, use the following: 要导出数据库,请使用以下命令:
mysqldump -u username -p databasename > filename.sql
Note the <
and >
symbols in each case. 请注意每种情况下的<
和>
符号。
#4楼
尝试
mysqldump databaseExample > file.sql
#5楼
If you're already running the SQL shell, you can use the source
command to import data: 如果您已经在运行SQL shell,则可以使用source
命令导入数据:
use databasename;
source data.sql;
#6楼
Well you can use below command to export, 那你可以使用下面的命令导出,
mysqldump --database --user=root --password your_db_name > export_into_db.sql mysqldump --database --user = root --password your_db_name> export_into_db.sql
and the generated file will be available in the same directory where you had ran this command. 生成的文件将在您运行此命令的同一目录中可用。
Now login to mysql using command, 现在使用命令登录mysql,
mysql -u[username] -p mysql -u [用户名] -p
then use "source" command with the file path. 然后使用“source”命令和文件路径。