Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

mysql

I have a C# Application which would access a MySQL server 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);

Now the idea is, I would connect it through the Internet or Wi-Fi connection on which both of the computers are connected to. I would then access the database through SQL Strings coded on C#. Now I could also connect them through LAN networking but I don't know how.

I am getting this exception in my code

{"Access denied for user 'root'@'Crave-PC.lan' (using password: NO)"}   System.Exception {MySql.Data.MySqlClient.MySqlException}

Any ideas how I can access the server through networking?

like image 220
Paul Anthony Macachor Avatar asked Feb 26 '13 16:02

Paul Anthony Macachor


People also ask

Can we connect MySQL with C?

MySQL database is available on most important OS platforms. It runs on BSD Unix, Linux, Windows, or Mac OS. To be able to compile C examples, we need to install the MySQL C development libraries. The above line shows how we can do it on Debian based Linux.

How do I access a remote database?

via cPanel To add your computer as an Access Host: Log in to cPanel. Under the Databases section, click on the Remote MySQL® icon. On the Remote MySQL® page, enter the connecting IP address, then click Add Host.


1 Answers

The problem is at MySQL Server side, root user doesn't have permissions to connect remotelly. To give root user permisions to connect remotelly just type this on a mysql command line:

GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'rootPass';

Just change 'rootPass' for your current root password, if root doesnt have password MySQL wont let you connect so you will have to define one for it.

One side note: As a security best practice you should define a different user other than root to access your mysql databases from client applications.

Hope it helps,

like image 121
echavez Avatar answered Oct 04 '22 10:10

echavez