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

I have a C # application that will access the MySQL server on another computer. I am trying to do this 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 that I will connect it via the Internet or a Wi-Fi connection to which both computers are connected. Then I accessed the database through SQL strings encoded in C #. Now I could also connect them through the LAN, but I do not know how to do this.

I get this exception in my code

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

Any ideas on how I can access the server through the network?

+6
source share
3 answers

The problem is on the server side of MySQL, the root user does not have permission to connect remotely. To give root user permissions a remote connection, simply enter this at the mysql command line:

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

Just change "rootPass" to the current root password if root does not have a password. MySQL will not allow you to connect, so you will need to define it for it.

One-way note: As a best security practice, you should define a user other than root to access mysql databases from client applications.

Hope this helps,

+6
source

One addition to this topic:

In some cases, when two computers are connected to the same network, the above connection string may still not work, despite the configuration of the source computer and the provision of privileges for the user from the client computer.

In such cases, please consider (before moving on and taking more serious actions on the source computer, for example, changing the Mysql configuration file, etc.) to "use the computer name as the server name instead of the IP address".

i.e.

  • first grant privileges to the user as described above,
  • but instead of the connection string suggested above, try the following connection string:

so the connection string will look like this:

 server = "pc-name-whateveritis"; database = "restaurantdb"; uid = "root"; password = ""; string connectionString; connectionString = "SERVER=" + server + "; PORT = 3306 ;" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; mycon = new MySqlConnection(connectionString); 

The name of the source computer can work in many cases when the IP address of the source computer returns an error.

Good luck

0
source

Switching from root to another username and setting a password works well for remote access, since root only has access rights when you are on the local host. An error message is displayed on the LAN using the IP address, so use the computer name.

0
source

All Articles