?
Solved

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

Posted on 2016-10-27
9
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 1

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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 25

Accepted Solution

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

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

Regards,
    Tomas Helgi
0
 
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
 
LVL 1

Author Comment

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

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

801 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