简体   繁体   English

如何使用C#连接到远程MySQL服务器?

[英]How to connect to a remote MySQL server using C#?

I have a C# Application which would access a MySQL server on another computer. 我有一个C#应用程序,可以访问另一台计算机上的MySQL服务器。 I am trying to do it via IP. 我试图通过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. 现在我的想法是,我将通过互联网连接到两台计算机的Wi-Fi连接。 I would then access the database through SQL Strings coded on C#. 然后,我将通过在C#上编码的SQL字符串访问数据库。 Now I could also connect them through LAN networking but I don't know how. 现在我也可以通过LAN网络连接它们,但我不知道如何。

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? 有关如何通过网络访问服务器的任何想法?

The problem is at MySQL Server side, root user doesn't have permissions to connect remotelly. 问题出在MySQL Server端,root用户没有连接remotelly的权限。 To give root user permisions to connect remotelly just type this on a mysql command line: 要给root用户permisions连接remotelly,只需在mysql命令行输入:

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. 只需更改当前root密码的'rootPass',如果root没有密码MySQL不会让你连接,所以你必须为它定义一个。

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. 一方面注意: 作为安全性最佳实践,您应该定义除root之外的其他用户,以从客户端应用程序访问您的mysql数据库。

Hope it helps, 希望能帮助到你,

One addition to this thread: 这个帖子的一个补充:

In some cases where two computers are connected to the same network, the connection string defined above might still not work despite the configuration of the source computer and grant of the privileges for the user from the client computer. 在两台计算机连接到同一网络的某些情况下,尽管配置了源计算机并且从客户端计算机授予用户权限,但上面定义的连接字符串可能仍然不起作用。

In such cases, please consider (before moving further and taking more serious action on source computer such as the modification of Mysql config file etc.) to "use the computer name as the server name instead of the IP address". 在这种情况下,请考虑(在进一步采取行动并在源计算机上采取更严肃的行动,例如修改Mysql配置文件等)以“使用计算机名称作为服务器名称而不是IP地址”。

ie

  • first, grant the privileges to the user as described above, 首先,如上所述向用户授予权限,
  • but instead of the connection string proposed above, try the following connection string: 但不是上面提出的连接字符串,请尝试以下连接字符串:

so, the connection string would look like: 所以,连接字符串看起来像:

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 might work in many cases the IP address of the source computer returns error. 源计算机的名称可能在许多情况下工作,源计算机的IP地址返回错误。

Good luck! 祝好运!

Changing from root to a different username and set the password works well for remote access because root only have full access privileges when you are on localhost. 从root用户名更改为其他用户名并设置密码非常适合远程访问,因为root用户在localhost上只具有完全访问权限。 On a local area network using an IP address gives an error so use the computer name. 在使用IP地址的局域网上出现错误,因此请使用计算机名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM