mysql - 如何使用PowerShell脚本将.sql文件导入MySQL?

标签 mysql powershell

我正在尝试从 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( 连接到服务器并执行基本的 SELECT 查询) 方法,但收到此特定 SOURCE 命令的错误:

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/

相关文章:

php - 将所有列值插入变量mysql

php - 使用嵌套无序列表构建树

powershell - 设置内容,然后获取内容以某种方式更改内容

powershell 导入模块失败

PowerShell 7;字符串到日期时间?

powershell - 启 Action 业结果为 "The background process reported an error with the following message: ."

powershell - 在Powershell输出的文件大小中包含unc路径

php - 使用 PHP 从数据库中检索数据

javascript - 使用 Spring Boot 在多个端口上调用 REST API

mysql - : before a value in sql mean? 是什么意思