Solved

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

Posted on 2016-10-27
9
43 Views
Last Modified: 2016-11-03
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 ...
0
Comment
Question by:SniperCode Sheva
  • 3
  • 3
  • 3
9 Comments
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 41862291
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
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41862326
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.
0
 

Author Comment

by:SniperCode Sheva
ID: 41862338
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...
0
 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 41862355
Hi!

Check and make sure that no firewall is blocking the 3306 port.

Regards,
    Tomas Helgi
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41862385
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/
0
 

Author Comment

by:SniperCode Sheva
ID: 41862400
I install WorkBench in the server right ? or on the remote ?
0
 

Author Comment

by:SniperCode Sheva
ID: 41862407
Thank you it was a firewall problem, the port 3306 was blocked.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41862421
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.
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 41862619
Glad to help.
Don't forget to close the question and award points to those that helped solving your problem.  :)

Regards,
    Tomas Helgi
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Creating and Managing Databases with phpMyAdmin in cPanel.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now