简体   繁体   中英

C# Can't connect to MySQL server

I am having problems connecting to my SQL (online) server from my program.

I get the error

“A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.“

I think my connectionstring is correct...

    myConnection.ConnectionString =
    "SERVER=" + db_host + ";" +
    "DATABASE=" + db_name + ";" +
    "UID=" + db_username + ";" +
    "PASSWORD=" + db_password + ";";

My router has forwarded the port that the sql server uses. The db_host is the IP of the server. The db_username is a user for the sql server (not root@localhost)

I am not sure if I need to enable external connections to the DB? I heard they were open by default, and I can't find anywhere in phpmyadmin or cpanel to enable it.

Been trying various solutions for this specific error, but no luck. The program times out from trying to connect and throws the exception System.Data.SqlClient.SqlException (0x80131904)

EDIT Thanks for pointing me in the right direction. I am using MySQL .NET Connector class now, and I receive the error Authentication to host 'xxxx' for user 'myusername' using method 'mysql_native_password' failed with message: Access denied for user 'myusername'@'my_external_ip' (using password: YES) ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user 'myusername'@'my_external_ip' (using password: YES)

The IP xxxx is the MySQL server's IP. myusername is where my username is and my_external_ip is this PC's external IP. The password is correct. So does this mean external connections are disallowed by the host?

You can't use System.Data.SqlClient.SqlConnection to connect to MySql server

Try to use MySql.Data.MySqlClient.MySqlConnection

More info here

You can download the MySql Connector here

2nd Question

In MySql a user is a combination of the username and the host that user is connecting from. So you need to add a user to your server like user@xxxx

See more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM