我需要使用C#编码连接到另一台机器的mysql服务器,下面是我的编码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace c_mysql
{
class Program
{
static void Main(string[] args)
{
string connection = "SERVER=192.168.1.5; Database=b2b; Uid=root;";
MySqlConnection con = new MySqlConnection(connection);
con.Open();
MySqlCommand cmd = new MySqlCommand("select * from area",con);
MySqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Console.WriteLine("connection successfull");
}
else
{
Console.WriteLine("not connected...");
}
}
}
}
IP地址192.168.1.5是局域网中其他计算机的IP地址,我可以通过网址栏中的192.168.1.5/phpmyadmin进行连接,但是当通过C#编码进行连接时,它说,
Host 'pc1' is not allowed to connect to this MySQL server
PC 1 是我正在编码的电脑。
请帮助我。
最佳答案
启用对 MySql 的远程根访问。 click here for more details
摘自上面的片段:
为root创建一个新的HOST并允许root从任何地方登录。
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> exit
关于c# - 无法使用C#连接到其他机器的mysql服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27653821/