简体   繁体   English

如何在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 .我有一个从phpMyAdmin导出的.sql文件。 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?我需要先创建一个数据库吗?

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如果它不存在并且导出的 SQL 不包含CREATE DATABASE (使用--no-create-db-n选项导出),则您可能必须从 mysql 创建(空)数据库,然后再执行可以导入。

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;

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 ;

Among all the answers, for the problem above, this is the best one:在所有答案中,对于上面的问题,这是最好的一个:

 mysql> use db_name;
 mysql> source file_name.sql;

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;

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

If you already have the database, use the following to import the dump or the sql file:如果您已经有了数据库,请使用以下命令导入dumpsql文件:

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 中创建相关数据库(空),首先通过在终端或 cmd 中运行以下命令登录到MySQL控制台

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.注意:如果您的终端不在dumpsql文件所在的位置,请使用上面的相对路径。

  1. Open the MySQL command line打开 MySQL 命令行
  2. Type the path of your mysql bin directory and press Enter键入您的 mysql bin 目录的路径,然后按Enter
  3. Paste your SQL file inside the bin folder of mysql server.将您的 SQL 文件粘贴到 mysql 服务器的bin文件夹中。
  4. Create a database in MySQL.在 MySQL 中创建一个数据库。
  5. Use that particular database where you want to import the SQL file.使用要导入 SQL 文件的特定数据库。
  6. Type source databasefilename.sql and Enter输入source databasefilename.sql回车
  7. Your SQL file upload successfully.您的 SQL 文件上传成功。

A solution that worked for me is below:对我有用的解决方案如下:

Use your_database_name;
SOURCE path_to_db_sql_file_on_your_local;

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

Go to the directory where you have the MySQL executable.转到您拥有 MySQL 可执行文件的目录。 -u for username and -p to prompt for the password: -u用户名和-p提示输入密码:

C:\xampp\mysql\bin>mysql -u username -ppassword databasename < C:\file.sql

我认为值得一提的是,您还可以使用zcat加载gzipped(压缩)文件,如下所示:

zcat database_file.sql.gz | mysql -u username -p -h localhost database_name

To import a single database, use the following command.要导入单个数据库,请使用以下命令。

mysql -u username -p password dbname < dump.sql

To import multiple database dumps, use the following command.要导入多个数据库转储,请使用以下命令。

mysql -u username -p password < dump.sql
mysql --user=[user] --password=[password] [database] < news_ml_all.sql

For exporting a database:导出数据库:

mysqldump -u username -p database_name > file.sql

For importing a database:导入数据库:

mysql -u username -p database_name < file.sql

For importing multiple SQL files at one time, use this:要一次导入多个 SQL 文件,请使用以下命令:

# Unix-based solution
for i in *.sql;do mysql -u root -pPassword DataBase < $i;done

For simple importing:对于简单的导入:

# Unix-based solution
mysql -u root -pPassword DataBase < data.sql

For WAMP :对于WAMP

#mysqlVersion replace with your own version
C:\wamp\bin\mysql\mysqlVersion\bin\mysql.exe -u root -pPassword DataBase < data.sql

For XAMPP:对于 XAMPP:

C:\xampp\mysql\bin\mysql -u root -pPassword DataBase < data.sql

You do not need to specify the name of the database on the command line if the .sql file contains CREATE DATABASE IF NOT EXISTS db_name and USE db_name statements.如果 .sql 文件包含CREATE DATABASE IF NOT EXISTS db_nameUSE db_name语句,则不需要在命令行上指定数据库的名称。

Just make sure you are connecting with a user that has the permissions to create the database, if the database mentioned in the .sql file does not exist.如果 .sql 文件中提到的数据库不存在,请确保您正在连接具有创建数据库权限的用户。

Import a database导入数据库

  1. Go to drive:开车去:

     command: d:
  2. MySQL login登录

     command: c:\\xampp\\mysql\\bin\\mysql -u root -p
  3. It will ask for pwd.它会要求输入密码。 Enter it:输入它:

     pwd
  4. Select the database选择数据库

     use DbName;
  5. Provide the file name提供文件名

     \\.DbName.sql

To import a database, use the following command.要导入数据库,请使用以下命令。

  mysql> create new_database;
  mysql> use new_database;
  mysql> source (Here you need to import the path of the sql file);
 eg:
  mysql> source E:/test/dump.sql;

You need to use forward slashes (/) even on Windows, eg E:/test/dump.sql instead of E:\\test\\dump.sql即使在 Windows 上也需要使用正斜杠 (/),例如 E:/test/dump.sql 而不是 E:\\test\\dump.sql

Or double backslashes (\\\\) because of escaping, ie E:\\\\test\\\\dump.sql或者双反斜杠(\\\\)因为转义,即 E:\\\\test\\\\dump.sql

Use:用:

mysql -u root -p password -D database_name << import.sql

Use the MySQL help for details - mysql --help .有关详细信息,请使用 MySQL 帮助 - mysql --help

I think these will be useful options in our context:我认为这些在我们的背景下将是有用的选择:

[~]$ mysql --help
mysql  Ver 14.14 Distrib 5.7.20, for osx10.12 (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --bind-address=name IP address to bind to.
  -D, --database=name Database to use.
  --delimiter=name    Delimiter to be used.
  --default-character-set=name Set the default character set.
  -f, --force         Continue even if we get an SQL error.
  -p, --password[=name] Password to use when connecting to server.
  -h, --host=name     Connect to host.
  -P, --port=#        Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).
  --protocol=name     The protocol to use for connection (tcp, socket, pipe,
  -s, --silent        Be more silent. Print results with a tab as separator, each row on new line.
  -v, --verbose       Write more. (-v -v -v gives the table output format).
  -V, --version       Output version information and exit.
  -w, --wait          Wait and retry if connection is down.

What is fun, if we are importing a large database and not having a progress bar.如果我们正在导入一个大型数据库并且没有进度条,那有什么好玩的。 Use Pipe Viewer and see the data transfer through the pipe使用管道查看器查看通过管道的数据传输

For Mac, brew install pv对于 Mac, brew install pv

For Debian/Ubuntu, apt-get install pv .对于 Debian/Ubuntu, apt-get install pv

For others, refer to pv - Pipe Viewer其他请参考pv - Pipe Viewer

pv import.sql | mysql -u root -p password -D database_name

1.45GiB 1:50:07 [339.0KiB/s]   [=============>      ] 14% ETA 11:09:36
1.46GiB 1:50:14 [ 246KiB/s]     [=============>      ] 14% ETA 11:09:15
1.47GiB 1:53:00 [ 385KiB/s]     [=============>      ] 14% ETA 11:05:36

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.或者您可以使用一些 GUI 客户端(如 SQLyog)来执行此操作。

While most answers here just mention the simple command虽然这里的大多数答案只是提到了简单的命令

mysql -u database_user -p [db_name] < database_file.sql mysql -u database_user -p [db_name] < database_file.sql

today it's quite common that databases and tables have utf8-collation where this command is not sufficient.今天,数据库和表具有 utf8 排序规则是很常见的,而这个命令是不够的。 Having utf8-collation in the exported tables it's required to use this command:在导出的表中有 utf8-collat​​ion 需要使用这个命令:

mysql -u database_user -p --default-character-set=utf8 [db_name] < database_file.sql mysql -u database_user -p --default-character-set=utf8 [db_name] < database_file.sql

Surley this works for other charsets too, how to show the right notation can be seen here: Surley 这也适用于其他字符集,如何显示正确的符号可以在这里看到:

https://dev.mysql.com/doc/refman/5.7/en/show-collation.html https://dev.mysql.com/doc/refman/5.7/en/show-collat​​ion.html

One comment mentioned also that if a database never exists an empty database had to be created first.一个评论还提到,如果数据库从不存在,则必须首先创建一个空数据库。 This might be right in some cases, but depends on the export file.这在某些情况下可能是正确的,但取决于导出文件。 If the exported file includes already the command to create the database then the database never has to be created in a separated step, which even could cause an error on import.如果导出的文件已经包含创建数据库的命令,则永远不必在单独的步骤中创建数据库,这甚至可能导致导入错误。 So on import it's advisable to have a look first in the file to know which commands are included there, on export it's advisable note the settings, especially if the file is very large and hard to read in an editor.因此,在导入时,建议先查看文件以了解其中包含哪些命令,在导出时,建议注意设置,尤其是当文件非常大且难以在编辑器中读取时。

There are still more parameters for the command which are listed and explained here:此处列出并解释了该命令的更多参数:

https://dev.mysql.com/doc/refman/5.7/en/mysql-command-options.html https://dev.mysql.com/doc/refman/5.7/en/mysql-command-options.html

If you use another database-version consider searching for the corresponding version of the manual too.如果您使用其他数据库版本,请考虑搜索相应版本的手册。 The mentioned links refer to MySQL version 5.7.提到的链接是指 MySQL 5.7 版。

EDIT:编辑:
The same parameters are working for mysqldump too.相同的参数也适用于mysqldump So while the commands for export and import are different, the mentioned parameters are not.因此,虽然导出和导入的命令不同,但提到的参数却不同。

添加--force选项

mysql -u username -p database_name --force < file.sql

You can try this query.你可以试试这个查询。

Export:出口:

mysqldump -u username –-password=your_password database_name > file.sql

Import:进口:

mysql -u username –-password=your_password database_name < file.sql

and detail following this link:并在此链接后提供详细信息:

https://chartio.com/resources/tutorials/importing-from-and-exporting-to-files-using-the-mysql-command-line/ https://chartio.com/resources/tutorials/importing-from-and-exporting-to-files-using-the-mysql-command-line/

以下命令适用于WAMP上 Windows 7 上的命令行 (cmd)。

d:/wamp/bin/mysql/mysql5.6.17/bin/mysql.exe -u root -p db_name < database.sql

Try this code.试试这个代码。

mysql -u username -p database_name < file.sql

For more information: How to import an SQL file using the command line in MySQL更多信息: 如何在 MySQL 中使用命令行导入 SQL 文件

I thought it could be useful for those who are using Mac OS X :我认为这对使用Mac OS X 的人可能有用:

/Applications/xampp/xamppfiles/bin/mysql -u root -p database < database.sql

Replace xampp with mamp or other web servers.mamp或其他 Web 服务器替换xampp

For information, I just had the default root + without password.有关信息,我只有默认的 root + 没有密码。 It didn't work with all previous answers.它不适用于以前的所有答案。

  • I created a new user with all privileges and a password.我创建了一个拥有所有权限和密码的新用户。 It worked.有效。

  • -ppassword WITHOUT SPACE. -ppassword 没有空格。

Providing credentials on the command line is not a good idea.在命令行上提供凭据不是一个好主意。 The above answers are great, but neglect to mention上面的答案很好,但忽略了提及

mysql --defaults-extra-file=etc/myhost.cnf database_name < file.sql

Where etc/myhost.cnf is a file that contains host, user, password, and you avoid exposing the password on the command line.其中 etc/myhost.cnf 是一个包含主机、用户、密码的文件,并且避免在命令行中暴露密码。 Here is a sample,这是一个样本,

[client]
host=hostname.domainname
user=dbusername
password=dbpassword

Similarly to vladkras's answer to How do import an SQL file using the command line in MySQL?类似于vladkras 对如何使用 MySQL 中的命令行导入 SQL 文件的回答 . .

Key differences for me:对我来说的主要区别:

  1. The database has to exist first数据库必须首先存在
  2. No space between -p and the password -p和密码之间没有空格

shell> mysql -u root -ppassword #note: no space between -p and password
mysql> CREATE DATABASE databasename;
mysql> using databasename;
mysql> source /path/to/backup.sql

I am running Fedora 26 with MariaDB.我正在使用 MariaDB 运行 Fedora 26。

Import into the database:导入数据库:

mysql -u username -p database_name < /file path/file_name.sql mysql -u 用户名 -p 数据库名 < /文件路径/file_name.sql

Export from the database:从数据库导出:

mysqldump -u username -p database_name > /file path/file_name.sql mysqldump -u 用户名 -p 数据库名称 > /file path/file_name.sql

After these commands, a prompt will ask for your MySQL password.在这些命令之后,提示将询问您的 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?我需要首先创建一个数据库吗?

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?我需要首先创建一个数据库吗?

The following steps help to upload file.sql to the MySQL database. 以下步骤有助于将file.sql上传到MySQL数据库。

Step 1: Upload file.sql.zip to any directory and unzip there 步骤1:将file.sql.zip上传到任何目录并在其中解压缩
Note : sudo apt-get install unzip : sudo apt-get unzip file.sql.zip 注意sudo apt-get install unzipsudo apt-get unzip file.sql.zip
Step 2: Now navigate to that directory. 步骤2:现在导航到该目录。 Example: cd /var/www/html 范例: cd /var/www/html

Step 3: mysql -u username -p database-name < file.sql 步骤3: mysql -u username -p database-name < file.sql
Enter the password and wait till uploading is completed. 输入密码,然后等待上传完成。

I kept running into the problem where the database wasn't created. 我一直遇到未创建数据库的问题。

I fixed it like this 我这样固定

mysql -u root -e "CREATE DATABASE db_name"
mysql db_name --force < import_script.sql

I'm using Windows 10 with PowerShell 5 and I found almost all "Unix-like" solutions not working for me.我正在使用 Windows 10 和 PowerShell 5,我发现几乎所有“类 Unix”解决方案都不适合我。

> mysql -u[username] [database-name] < my-database.sql
At line:1 char:31
+ mysql -u[username] [database-name] < my-database.sql
+                               ~
The '<' operator is reserved for future use.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported

I ends up using this command:我最终使用了这个命令:

> type my-database.sql | mysql -u[username] -h[localhost] -p [database-name]

And it works perfectly, and hopefully it helps.它工作得很好,希望它有所帮助。

Thanks to @ Francesco Casula 's answer , BTW.感谢@ Francesco Casula回答,顺便说一句。

To import a database via the terminal通过终端导入数据库

Navigate to folder where the .sql file is located导航到 .sql 文件所在的文件夹

Then run the below command:然后运行以下命令:

mysql -u database_user_name -p database_name < sql_file_name.sql

It will ask for a password.它会要求输入密码。 Enter the database password.输入数据库密码。 It will take a few seconds to import the data into the database.将数据导入数据库需要几秒钟的时间。

This line imports the dump file in the local database, under Linux.此行在 Linux 下导入本地数据库中的转储文件。

mysql -u dbuser -p'password including spaces' dbname < path/to/dump_file.sql

This line imports the dump file in the remote database, under Linux.此行在 Linux 下导入远程数据库中的转储文件。 Note: -P is for the port and is required if the MySQL port is different than the default.注意-P用于端口,如果 MySQL 端口与默认端口不同,则需要此选项。

mysql -h dbhost -u dbuser -p'password including spaces' -P 3306 dbname < path/to/dump_file.sql

Note: the password includes spaces and this is the reason of the single quotes.注意:密码包含空格,这是单引号的原因。 Just change the path style for using the command under Windows ( C:\\windows\\path\\dump_file.sql ).只需更改在 Windows 下使用命令的路径样式( C:\\windows\\path\\dump_file.sql )。

If importing data into a Docker container use the following command.如果将数据导入 Docker 容器,请使用以下命令。 Adjust user(-u), database(-D), port(-P) and host(-h) to fit your configuration.调整用户(-u)、数据库(-D)、端口(-P)和主机(-h)以适合您的配置。

mysql -u root -D database_name -P 4406 -h localhost --protocol=tcp -p < sample_dump.sql

Simple.简单的。 Just use this command in cmd:只需在 cmd 中使用此命令:

use databasename
\. C:/test/data.mysql

使用 MySQL 安全外壳:

mysqlsh -u <username> -p -h <host> -D <database name> -f dump.sql

If you are using MAMP on Mac OS X, this may be helpful:如果您在 Mac OS X 上使用MAMP ,这可能会有所帮助:

/applications/MAMP/library/bin/mysql -u MYSQL_USER -p DATABASE_NAME < path/to/database_sql/FILE.sql

MYSQL_USER is root by default. MYSQL_USER 默认是 root。

If your folder has multiple SQL files, and you've installed Git Bash you can use this command to import multiple files:如果您的文件夹有多个 SQL 文件,并且您已经安装了Git Bash ,则可以使用此命令导入多个文件:

cd /my-project/data

cat *.sql | /c/xampp/mysql/bin/mysql -u root -p 1234 myProjectDbName

Export particular databases:导出特定数据库:

mysqldump --user=root --host=localhost --port=3306 --password=test -B CCR KIT > ccr_kit_local.sql

This will export CCR and KIT databases...这将导出 CCR 和 KIT 数据库...

Import all exported databases to a particular MySQL instance (you have to be where your dump file is):将所有导出的数据库导入特定的 MySQL 实例(您必须在您的转储文件所在的位置):

mysql --user=root --host=localhost --port=3306 --password=test < ccr_kit_local.sql

If you use XAMPP on the windows, first, you must manually create the database and then run the following commands:如果在 windows 上使用XAMPP ,首先必须手动创建数据库,然后运行以下命令:

cd C:\xampp\mysql\bin && 
mysql -u YOUR_USERNAME -p YOUR_DATABASE_NAME < PATH_TO_YOUR_SQL_FILE\YOUR_SQL_FILE.sql

And then enter the password然后输入密码

For Windows OS, you can use the below command to import data from an SQL dump.对于 Windows 操作系统,您可以使用以下命令从 SQL 转储导入数据。

C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin>mysql -u<> -p<> DBName < filelocation\\query.sql C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin>mysql -u<> -p<> DBName < filelocation\\query.sql

Where -u is the username, and -p is the MySQL password.其中-u是用户名, -p是 MySQL 密码。 Then enter your password and wait for data to import.然后输入您的密码并等待数据导入。

You can use:您可以使用:

mysql -u<user> -p<pass> <db> < db.sql

Example:例子:

mysql -uroot -proot db < db.sql

In Ubuntu在 Ubuntu 中

 mysql -u root -p
 CREATE database dbname;
 use dbname;
 source /home/computername/Downloads/merchantapp.sql
 exit;

In Windows在 Windows 中

Download the SQL file and save it in C:\\xampp\\mysql\\bin .下载 SQL 文件并将其保存在C:\\xampp\\mysql\\bin

After that, open the command prompt with C:\\xampp\\mysql\\bin :之后,使用C:\\xampp\\mysql\\bin打开命令提示符:

 C:\xampp\mysql\bin> mysql -u username -p database_name < file.sql
  • Create a database in MySQL.在 MySQL 中创建一个数据库。

  • Then go to your computer directory C:\\xampp\\mysql\\bin , write cmd in the address bar, and hit Enter .然后进入你的电脑目录C:\\xampp\\mysql\\bin ,在地址栏中输入cmd ,然后按 Enter

  • Unzip your SQL file解压缩您的 SQL 文件

  • Then write: mysql -u root -p dbname and press Enter .然后写: mysql -u root -p dbname并按Enter

  • Write: source sql.file.写入:源 sql.file。 Like Source C:\\xampp\\htdocs\\amarbazarltd\\st1159.sql喜欢Source C:\\xampp\\htdocs\\amarbazarltd\\st1159.sql

  • Done完毕

If you are importing to your local database server, you can do the following:如果要导入到本地数据库服务器,则可以执行以下操作:

mysql -u database_user -p < database_file.sql

For a remote database server do the follwing:对于远程数据库服务器,请执行以下操作:

mysql -u database_user -p -h remote_server_url < database_file.sql

If you are using XAMPP then go to folder xapppmysqlbin .如果您使用的是XAMPP,则转到文件夹xapppmysqlbin Open cmd here and paste this:在此处打开 cmd 并粘贴:

mysql -u root -p dbname < dbfilename.sql

You can use these steps as easily.您可以轻松地使用这些步骤。

  1. Download the SQL file into your "mysql/bin" folder.将 SQL 文件下载到“mysql/bin”文件夹中。

  2. Open the "mysql/bin" folder using CMD.使用 CMD 打开“mysql/bin”文件夹。

  3. If not exists required database, then create the database first.如果需要的数据库不存在,则先创建数据库。

  4. Type this in the CMD and run:在 CMD 中输入并运行:

    mysql -u <user> -p<password> <dbname> < file.sql

    "file.sql" is an SQL file that you want to insert into the target database. “file.sql”是您要插入到目标数据库中的 SQL 文件。 examples: If your "password" is "1234", "user" is "root", and "dbname" is "test":示例:如果您的“密码”是“1234”,“用户”是“root”,而“dbname”是“test”:

     mysql -u root -p1234 test < file.sql

    If your "password" is null & "user" is "root" & "dbname" is "test"如果您的“密码”为空且“用户”为“root”且“dbname”为“test”

     mysql -u root test < file.sql
  5. Check the target data successfully uploaded or not.检查目标数据是否成功上传。

This method can be used to upload the large size data using SQL files in the CMD.该方法可用于在CMD中使用SQL文件上传大尺寸数据。

Make sure in step 4, if you use that password, insert "-p" as a single word without any spaces.确保在第 4 步中,如果您使用该密码,请将“-p”作为单个单词插入,没有任何空格。

Most of the answers include > or < characters which is not a proper method for all the cases.大多数答案都包含 > 或 < 字符,这不是适用于所有情况的正确方法。 I recommend using mysqlimport while you may make the dump file using mysqldump .我建议使用mysqlimport ,而您可以使用mysqldump制作转储文件。

These tools will be installed with the mysql service and both are available for backup and restore in a database or multiple databases in MySQL.这些工具将随 mysql 服务一起安装,都可用于 MySQL 中一个数据库或多个数据库的备份和恢复。

Here is the way you could leverage it for importing to the mysql这是您可以利用它导入到 mysql 的方法

mysqlimport -u database_admin -p database_name ~/path/to/dump_file.sql mysqlimport -u database_admin -p database_name ~/path/to/dump_file.sql

In case you do not have it, please install it via:如果您没有,请通过以下方式安装:

sudo apt update sudo apt install mysql-client sudo apt update sudo apt 安装 mysql-client

In the same way, you make a backup to a dump file as follows :同样,您可以按如下方式备份到转储文件:

mysqldump [options] --result-file=dump_file.sql mysqldump [选项] --result-file=dump_file.sql

Try this:试试这个:

cd C:\xampp\mysql\bin
mysql -u root -p database_name --force < C:\file.sql
  1. Go to your wamp or xampp directory转到您的wampxampp目录

    Example例子

    cd d:/wamp/bin/mysql/mysql5.7.24/bin
  2. mysql -u root -p DATABASENAME < PATHYOUDATABASE_FILE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM