简体   繁体   English

C# MySQL 无法连接到 ubuntu 服务器

[英]C# MySQL can't connect to ubuntu server

I'm currently developing ac# Login system, but the mysql part won't work.我目前正在开发 ac# 登录系统,但 mysql 部分不起作用。

already tried sudo ufw allow 3306/tcp, but still the error.已经尝试过 sudo ufw allow 3306/tcp,但仍然是错误。 Also tried with HeidiSql也尝试过 HeidiSql

"Can't connect to MySQL server on '5.230.22.130' (10061) “无法连接到‘5.230.22.130’上的 MySQL 服务器 (10061)

and yes, the user is %, not localhost!!是的,用户是 %,而不是本地主机!!

Error message:错误信息:

"SocketException: Unable to connect because the destination computer refused to connect Server 5.230.22.130" “SocketException:无法连接,因为目标计算机拒绝连接服务器 5.230.22.130”

class DB
{

    private MySqlConnection connection =
        new MySqlConnection("Datasource=5.230.22.130;Port=3306;Database=***;uid=sqlAdmin;pwd=***;");
    public void openConnection()
    {
        if (connection.State == System.Data.ConnectionState.Closed)
        {
            connection.Open();
        }
    }

    public void closeConnection()
    {
        if (connection.State == System.Data.ConnectionState.Open)
        {
            connection.Close();
        }
    }

    public MySqlConnection getConnection()
    {
        return connection;
    }
}

Ubuntu per default has an active firewall and unless you have disabled it or opened the port already, connections will be blocked.默认情况下,Ubuntu 有一个活动防火墙,除非您已禁用它或已打开端口,否则连接将被阻止。

You can try to open the firewall for mysql server via console on the ubuntu machine (requires root)您可以尝试在ubuntu机器上通过控制台打开mysql服务器的防火墙(需要root)

sudo ufw allow 3306/tcp

also make sure the user account you want to connect with allows "%" connection and not only "localhost"还要确保您要连接的用户帐户允许“%”连接,而不仅仅是“localhost”

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

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