Невозможно получить MySQL сборку для загрузки в powershell - PullRequest
1 голос
/ 21 апреля 2020

Я не могу подключиться к linux mysql контейнеру с Ubuntu на моей локальной машине. Я использую 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

1 Ответ

0 голосов
/ 22 апреля 2020

это был мой синтаксис. Это сработало (хотя я все еще получаю эту ошибку ... таблица заполняется)

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...