Solved

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

Posted on 2016-10-27
9
57 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 25

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 83

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 25

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 83

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 83

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 25

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

16 Experts available now in Live!

Get 1:1 Help Now