我正在尝试从 Windows 环境将转储文件(包含 CREATE/DROP/INSERT 命令的 .sql 文件)导入到远程 MySQL 服务器中。我已经有了这个 PowerShell 脚本,可以使用我的环境中安装的“MySQL shell 实用程序”功能来完成这项工作
Set-ExecutionPolicy RemoteSigned
Add-Type –Path 'C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.11\Assemblies\v4.5.2\MySql.Data.dll'
$Connection = [MySql.Data.MySqlClient.MySqlConnection]@{ConnectionString='server=192.168.100.100;Encrypt=false;uid=myUser;pwd=myPass;database=myDB;'}
$Connection.Open()
$sql = New-Object MySql.Data.MySqlClient.MySqlCommand
$sql.Connection = $Connection
$sql.CommandText = "SOURCE D:/MySQL_Dumps/myFile.sql"
$sql.ExecuteNonQuery()
$Reader.Close()
$Connection.Close()
我可以使用 ExecuteNonQuery()
/ExecuteScalar()
/ExecuteReader( 连接到服务器并执行基本的
方法,但收到此特定 SOURCE 命令的错误:SELECT
查询)
Exception calling "ExecuteNonQuery" with "0" argument(s): "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SOURCE D:/MySQL_Dumps/myFile.sql' at line 1" At line:8 char:1 + $sql.ExecuteNonQuery() + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : MySqlException
我尝试了几种输入路径的替代方法,例如使用双斜杠或双反斜杠,但遇到了相同的错误。有人可以帮忙吗?
最佳答案
./mysql -u root -p DatabaseName -e "source C:\path\to\my_sql_file.sql"
关于mysql - 如何使用PowerShell脚本将.sql文件导入MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51356167/