Solved

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

Posted on 2016-10-27
9
74 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
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
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

810 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