如何使用 C# 连接到远程 MySQL 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15094819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to a remote MySQL server using C#?
提问by Paul Anthony Macachor
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 :
我有一个 C# 应用程序,它可以访问另一台计算机上的 MySQL 服务器。我正在尝试通过 IP 来实现。这是我的连接字符串:
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.
现在的想法是,我会通过两台计算机都连接到的 Internet 或 Wi-Fi 连接来连接它。然后我将通过在 C# 上编码的 SQL 字符串访问数据库。现在我也可以通过 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?
任何想法如何通过网络访问服务器?
采纳答案by echavez
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:
问题出在 MySQL 服务器端,root 用户没有远程连接的权限。要授予 root 用户远程连接权限,只需在 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.
只需将“rootPass”更改为您当前的 root 密码,如果 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,
希望能帮助到你,
回答by Aykut Saribiyik
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 地址”。
i.e.
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!
祝你好运!
回答by Newton Ray Mubanga
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. On a local area network using an IP address gives an error so use the computer name.
从 root 更改为不同的用户名并设置密码对于远程访问很有效,因为 root 只有在本地主机上时才有完全访问权限。在使用 IP 地址的局域网上会出现错误,因此请使用计算机名称。