我有一个.sql文件,其中包含phpMyAdmin的导出。我想使用命令行将其导入到其他服务器。
我安装了Windows Server 2008 R2。我将.sql文件放在C驱动器上,并尝试了以下命令
database_name < file.sql
它不起作用。我遇到语法错误。
我如何才能毫无问题地导入此文件?我需要先创建数据库吗?
我有一个.sql文件,其中包含phpMyAdmin的导出。我想使用命令行将其导入到其他服务器。
我安装了Windows Server 2008 R2。我将.sql文件放在C驱动器上,并尝试了以下命令
database_name < file.sql
它不起作用。我遇到语法错误。
我如何才能毫无问题地导入此文件?我需要先创建数据库吗?
当前回答
如果.sql文件包含CREATE database if not EXISTS db_name和USE db_name语句,则不需要在命令行上指定数据库的名称。
如果.sql文件中提到的数据库不存在,请确保您正在与具有创建数据库权限的用户进行连接。
其他回答
有时定义的端口以及该数据库的服务器IP地址也很重要。。。
mysql -u user -p user -h <Server IP address> -P<port> (DBNAME) < DB.sql
试试看:
cd C:\xampp\mysql\bin
mysql -u root -p database_name --force < C:\file.sql
我正在使用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
我最终使用以下命令:
> type my-database.sql | mysql -u[username] -h[localhost] -p [database-name]
它工作得很好,希望能有所帮助。
感谢@Francesco Casula的回答,BTW。
要导入数据库,请使用以下命令。
mysql> create new_database;
mysql> use new_database;
mysql> source (Here you need to import the path of the SQL file);
E.g.:
mysql> source E:/test/dump.sql;
即使在Windows上,也需要使用正斜杠(/),例如e:/test/dump.sql而不是e:\test\dump.sql
或由于转义而使用双反斜杠(\\),即e:\\test\\dump.sql
在Ubuntu中
mysql -u root -p
CREATE database dbname;
use dbname;
source /home/computername/Downloads/merchantapp.sql
exit;
在Windows中
下载SQL文件并将其保存在C:\xamp\mysql\bin中。
之后,使用C:\xamp\mysql\bin打开命令提示符:
C:\xampp\mysql\bin> mysql -u username -p database_name < file.sql