【发布时间】: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