【问题标题】:unable to connect to other machine's mysql server using C#?无法使用 C# 连接到其他机器的 mysql 服务器?
【发布时间】:2015-02-23 13:41:35
【问题描述】:

我需要使用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 地址,我可以通过 url 栏中的 192.168.1.5/phpmyadmin 进行连接,但是通过 C# 编码连接时显示,

Host 'pc1' is not allowed to connect to this MySQL server

PC 1 是我正在编写代码的电脑。

请帮帮我。

【问题讨论】:

  • 允许 mysql 访问此用户的 '%' ips。
  • 我允许了,但什么也没发生
  • 能否请您告诉我该怎么做而不是参考其他解决方案..?
  • 这与您的 mysql 服务器不允许 root 用户从本地以外的其他 ips 连接有关。解决方案是在 mysql 服务器上为该用户提供对您的 ip 的访问权限...和我刚刚为您提供了如何做到这一点的链接...顺便说一句tell me what to do 不是开发人员的态度!

标签: c# mysql visual-studio-2010


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 2020-03-18
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2021-08-04
    • 2020-09-13
    • 2016-05-18
    相关资源
    最近更新 更多