在从Powershell获取MySQL查询时遇到一些问题,这是我的代码:
function Invoke-MySQL {
Param(
[Parameter(
Mandatory = $true,
ParameterSetName = '',
ValueFromPipeline = $true)]
[string]$Query
)
$MySQLAdminUserName = 'USER'
$MySQLAdminPassword = 'PASSWORD'
$MySQLDatabase = 'DATABASE'
$MySQLHost = 'MYSQLSERVER.mydomain.local'
$ConnectionString = "server=" + $MySQLHost + "; port=3306; uid=" + $MySQLAdminUserName + "; pwd=" + $MySQLAdminPassword + "; database="+$MySQLDatabase
Try {
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$Connection.ConnectionString = $ConnectionString
$Connection.Open()
$Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Connection)
$DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command)
$DataSet = New-Object System.Data.DataSet
$RecordCount = $dataAdapter.Fill($dataSet, "data")
$DataSet.Tables[0]
}
Catch {
throw "ERROR : Unable to run query : $query `n$Error[0]"
}
Finally {
$Connection.Close()
}
}
Invoke-MySQL -Query "select * from ImaginaryTable"
$ Connection.Open()命令上出现此问题,并出现以下错误:
ERROR : Unable to run query : select * from ImaginaryTable
Exception calling "Open" with "0" argument(s): "Unable to connect to any of the specified MySQL h
osts."[0]
有人可以帮助我吗?:)
脚本中没有错误,我认为您的服务器在端口3306上不可访问。
请确保您没有阻止该端口的防火墙规则。
也许您的mysql服务器仅从本地ip地址监听。
假设您在linux mysql服务器上,请按照以下步骤操作:
打开此文件/etc/mysql/my.cnf,并将bind-address
value从更改127.0.0.1
为网卡的相应IP地址(例如:192.168.0.50或外部ip(如果服务器位于外部)。
然后重启你的mysql服务器
/etc/init.d/mysql restart
mysql重启后执行以下查询:
GRANT ALL PRIVILEGES ON *.* TO 'USER'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
现在,您将可以通过Powershell查询数据库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句