1

I've been scouring the internet for guides on how to do this, and most seem to tell me to do exactly what I'm doing already. All I'm trying to do is connect to a MySQL database and retrieve a name from the database based on a parameter I pass. The problem is that I've been unable to connect to the database based on all the guides I've seen so far.

Here's one of the ways I've tried:

$connectionstring = "server=mysql.collegename.edu;uid=sconsdoc;pwd=password;database=namedatabase;"

try {
    $connection = New-Object MySql.Data.MySqlClient.MySqlConnection
    $connection.ConnectionString = $connectionString
    $connection.Open()
} catch {
    Write-Host "ERROR : Unable to run query : $query 'n$Error[0]"
} finally {
    $connection.Close()
}

I get the following error:

ERROR : Unable to run query : 'nException calling "Open" with "0" argument(s): "Unable to connect to any of the specified MySQL hosts." Exception calling "Open" with "0" argument(s): "Unable to connect to any of the specified MySQL hosts." Exception calling "Open" with "0" argument(s): "Unable to connect to any of the specified MySQL hosts."[0]

I'm pretty sure I've specified the correct host, and I know I have the correct login credentials (I can at least log in through phpmyadmin), so I'm not sure what's wrong. I've followed the format I've seen in several guides so I don't understand what's wrong here. If I'm supposed to pass arguments to open, what are they? Like I said this is the only format I've been able to find to use.

1 Answer 1

2

"Unable to connect" sounds like a blocked connection attempt due to a closed/filtered port. Check if you can actually access the port on the remote server:

telnet mysql.collegename.edu 3306

If the database is listening on a non-default port you need to add the port to the connection string:

server=mysql.collegename.edu;port=12345;uid=user;pwd=password;database=dbname;
Sign up to request clarification or add additional context in comments.

5 Comments

I could not create a connection using the following command. could that also be because I didn't specify login credentials, or does that definitely mean that the port is closed?
The command should have produced some output. What was that output?
Connecting To mysql.collegename.edu...Could not open connection to the host, on port 3306: connection failed
As suspected, either the port is blocked by some firewall, or the database server is listening on a non-default port. In case it's the latter (ask the server/DB administrator) add the port to the connection string (Port=12345;). Credentials can't be the issue here, because they can only be passed to the server after the connection was already established.
Same problem , did you got solution. please tell me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.