【问题标题】:Cannot get MySQL assembly to load in powershell无法在 powershell 中加载 MySQL 程序集
【发布时间】:2020-04-21 19:25:23
【问题描述】:

我无法连接到在我的本地机器上运行 ubuntu 的 linux mysql 容器。我正在使用 powershell 创建一个 mysql 连接。刚接触数据库和输入数据,所以真的不知道我在做什么。

在容器中,我创建了一个数据库

mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.04 sec)

mysql> CREATE TABLE test (id INT, text varchar(50), date_val date, date_time datetime);
mysql> USE mydb;
Database changed
mysql> CREATE TABLE test (id INT, text varchar(50), date_val date, date_time datetime);
Query OK, 0 rows affected (0.12 sec)

从我的本地主机测试与容器配置端口的连接

PS C:\Users\me> Test-NetConnection 127.0.0.1 -Port 3306



ComputerName     : 127.0.0.1
RemoteAddress    : 127.0.0.1
RemotePort       : 3306
InterfaceAlias   : Loopback Pseudo-Interface 1
SourceAddress    : 127.0.0.1
TcpTestSucceeded : True

这是我的 powershell 脚本

[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$constr = "server=127.0.0.1:3306;port=3306;database=mydb;user id=root;password=P@ssw0rd"
$con = New-Object MySql.Data.MySqlclient.MySqlConnection
$con.ConnectionString = $constr
$con.Open()
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.CommandText = "INSERT INTO test VALUES(@id, @text, @date, @datetime)"
$cmd.Connection = $con
$cmd.Prepare()
$today = get-date
$cmd.Parameters("@id").Value = 1
$cmd.Parameters("@text").Value = "Text Value"
$cmd.Parameters("@date").Value = $today
$cmd.Parameters("@datetime").Value = $today
$cmd.ExecuteNonQuery()
$con.close()

这是我得到的错误

Exception calling "Open" with "0" argument(s): "Unable to connect to any of the specified MySQL hosts."
At \\file\Tech\Dietrich\powershell scripts\VMware\Horizonview_session_count.ps1:24 char:1
+ $con.Open()
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : MySqlException

Exception calling "Prepare" with "0" argument(s): "The connection is not open."
At \\file\Tech\me\powershell scripts\VMware\Horizonview_session_count.ps1:28 char:1
+ $cmd.Prepare()
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

Method invocation failed because [MySql.Data.MySqlClient.MySqlCommand] does not contain a method named 'Parameters'.
At \\file\Tech\me\powershell scripts\VMware\Horizonview_session_count.ps1:30 char:1
+ $cmd.Parameters("@id").Value = 1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [MySql.Data.MySqlClient.MySqlCommand] does not contain a method named 'Parameters'.
At \\file\Tech\me\powershell scripts\VMware\Horizonview_session_count.ps1:31 char:1
+ $cmd.Parameters("@text").Value = "Text Value"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [MySql.Data.MySqlClient.MySqlCommand] does not contain a method named 'Parameters'.
At \\file\Tech\me\powershell scripts\VMware\Horizonview_session_count.ps1:32 char:1
+ $cmd.Parameters("@date").Value = $today
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [MySql.Data.MySqlClient.MySqlCommand] does not contain a method named 'Parameters'.
At \\file\Tech\me\powershell scripts\VMware\Horizonview_session_count.ps1:33 char:1
+ $cmd.Parameters("@datetime").Value = $today
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Exception calling "ExecuteNonQuery" with "0" argument(s): "Connection must be valid and open."
At \\file\Tech\me\powershell scripts\VMware\Horizonview_session_count.ps1:34 char:1
+ $cmd.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

【问题讨论】:

  • 连接字符串中的服务器错误? $constr = "server=127.0.0.1;port=3306;database=…"
  • 这一切都在我的本地机器上,在 docker compose yaml 文件中我有一个端口 3306:3306 的条目
  • 对于Parameters 消息,您需要方括号[] 而不是括号()。其他的还不确定。
  • @JoelCoehoorn 那些方括号需要放在哪里??

标签: mysql powershell containers


【解决方案1】:

这是我的语法。这有效(尽管我仍然收到此错误...表格填充)

Method invocation failed because [MySql.Data.MySqlClient.MySqlCommand] does not contain a method named 'Parameters'.
At \\ae-file\Tech\Dietrich\powershell scripts\VMware\Horizonview_session_count.ps1:28 char:1
+ $cmd.Parameters("@id").Value = 1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$constr = "server=127.0.0.1;port=3306;database=mydb;user id=root;password=P@ssw0rd"
$con = New-Object MySql.Data.MySqlclient.MySqlConnection
$con.ConnectionString = $constr
$con.Open()
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.CommandText = "INSERT INTO test VALUES(@id, @text, @date, @datetime)"
$cmd.Connection = $con
$cmd.Prepare()
$today = get-date
$cmd.Parameters("@id").Value = 1
$cmd.Parameters.AddWithValue("@id", 1)
$cmd.Parameters.AddWithValue("@text", $totalSessions.count)
$cmd.Parameters.AddWithValue("@date", $today)
$cmd.Parameters.AddWithValue("@datetime", $today)
$cmd.ExecuteNonQuery()
$con.close()

结果

SourceVersion           : Default
ParameterName           : @id
Direction               : Input
IsNullable              : False
MySqlDbType             : Int32
Precision               : 0
Scale                   : 0
Size                    : 0
Value                   : 1
PossibleValues          :
SourceColumn            :
SourceColumnNullMapping : False
DbType                  : Int32


SourceVersion           : Default
ParameterName           : @text
Direction               : Input
IsNullable              : False
MySqlDbType             : Int32
Precision               : 0
Scale                   : 0
Size                    : 0
Value                   : 241
PossibleValues          :
SourceColumn            :
SourceColumnNullMapping : False
DbType                  : Int32


SourceVersion           : Default
ParameterName           : @date
Direction               : Input
IsNullable              : False
MySqlDbType             : DateTime
Precision               : 0
Scale                   : 0
Size                    : 0
Value                   : 4/21/2020 4:34:40 PM
PossibleValues          :
SourceColumn            :
SourceColumnNullMapping : False
DbType                  : DateTime


SourceVersion           : Default
ParameterName           : @datetime
Direction               : Input
IsNullable              : False
MySqlDbType             : DateTime
Precision               : 0
Scale                   : 0
Size                    : 0
Value                   : 4/21/2020 4:34:40 PM
PossibleValues          :
SourceColumn            :
SourceColumnNullMapping : False
DbType                  : DateTime

1

表格

mysql> select * from test;
+------+------------+------------+---------------------+
| id   | text       | date_val   | date_time           |
+------+------------+------------+---------------------+
|    1 | Text Value | 2020-04-21 | 2020-04-21 15:54:54 |
|    1 | Text Value | 2020-04-21 | 2020-04-21 15:57:05 |
|    1 | Text Value | 2020-04-21 | 2020-04-21 16:02:12 |
+------+------------+------------+---------------------+
3 rows in set (0.00 sec)

【讨论】:

    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多