Link to home
Start Free TrialLog in
Avatar of SniperCode Sheva
SniperCode Sheva

asked on

How to connect to a remote mysql server using C#?

I have a C# Application which would access a MySQL server using WAMP on another computer. I am trying to do it via IP. Here is my Connection String :
server = "192.168.10.221";
database = "restaurantdb";
uid = "root";
password = "";
string connectionString;
connectionString = "SERVER=" + server + "; PORT = 3306 ;" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
mycon = new MySqlConnection(connectionString);

Open in new window


I have another computer in which we are in the same network, but when I try to connect to the Mysql I am not able to connect but locally it works...
What do you think ?

What I have tried:

I published my C# application and then launch it in the other computer so that I will try to connect to mysql but nothing works. But I have to mention that when I put the ip adress in the other computer I can have access to wampserver and phpmyadmin ...
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Hi!

If you have access to mysql locally and as root/superuser then do a select on mysql.user table like this.

select user,host from mysql.users where user = 'your_username';

Open in new window


To be able to access mysql from a host with your_username you should see a record with the host/ipaddress and the user.

If not you would need to grant access to the database for that user on that host. Note that yourusername with host % means that you can access mysql from any host.

Granting access to a user
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'your_hosts_ipaddress'

Open in new window


http://dev.mysql.com/doc/refman/5.7/en/account-management-sql.html

Regards,
    Tomas Helgi
On the computer that runs the C# program, you must have either the MySQL ODBC connector or the MySQL .NET connector.  These are basically drivers that can connect from your program to the MySQL server.  https://www.mysql.com/products/connector/

On the MySQL server, you must have a user that is allowed to connect remotely.  MySQL users are setup with username, password, and host that they are allowed to connect from.  That's shown above in Tomas's post.
Avatar of SniperCode Sheva
SniperCode Sheva

ASKER

When I try to connect I got this error : MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
I did what Thomas told me but nothing I got the same error...
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I suggest you install MySQL Workbench and use it to establish the correct connection info.  Then you can use that in your program.  http://www.mysql.com/products/workbench/
I install WorkBench in the server right ? or on the remote ?
Thank you it was a firewall problem, the port 3306 was blocked.
MySQL Workbench goes on the client where you were having a problem.  You can put it on the server but the privileges are different on the server because it does not require remote access.

Glad you got it fixed.
Glad to help.
Don't forget to close the question and award points to those that helped solving your problem.  :)

Regards,
    Tomas Helgi