如何连接到使用C#远程MySQL服务器? [英] How to connect to a remote MySQL server using C#?

查看:215
本文介绍了如何连接到使用C#远程MySQL服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这将访问另一台计算机上MySQL服务器的C#应用​​程序。我试图通过IP来做到这一点。这里是我的连接字符串:

 服务器=192.168.10.221; 
数据库=restaurantdb;
UID =根;
密码=;
字符串的connectionString;
的connectionString =服务器=+服务器+; PORT = 3306; +DATABASE =+数据库+; +UID =+ UID +; +PASSWORD =+密码+;;
mycon =新的MySqlConnection(的connectionString);

现在的想法是,我将它连接通过互联网或Wi-Fi连接,其中两个将计算机连接到。然后,我将通过SQL字符串编码在C#访问数据库。现在,我也可以通过局域网连接它们的网络,但我不知道怎么办。



我在我的代码


$ B获得此异常$ b

  {访问被拒绝的用户root'@'Crave-PC.lan'(使用密码:NO)} {System.Exception的MySql.Data.MySqlClient.MySqlException } 

任何想法如何,我可以访问通过网络服务器?


< DIV CLASS =h2_lin>解决方案

现在的问题是在MySQL服务器端,root用户没有权限来remotelly连接。
为了让root用户permisions到remotelly连接只需键入这个mysql命令行:

  GRANT ALL PRIVILEGES ON * *以root @%IDENTIFIED BY'rootPass'; 



只要改变'rootPass对于当前的root密码,如果根可是没有密码的MySQL不会让你连接。所以你必须定义一个用于它



一个侧面说明:作为安全性的最佳实践,你应该定义不是root访问你的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?

解决方案

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,

这篇关于如何连接到使用C#远程MySQL服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆