2016-12-13 112 views
-1

如何连接到MySQL(C#)如何连接到MySQL?

服务器:通过UNIX套接字

本地主机

我的代码:

public void Connect() 
{ 
    server = "91.196.48.243"; 
    database = "w554_users"; 
    port = "3306"; 
    uid = "name"; 
    password = "password"; 

    myConnectionString = "Server=" + server + ";" + "Port=" + port + ";" + "Database=" + 
    database + ";" + "Uid=" + uid + ";" + "Pwd=" + password + ";"; 
    conn = new MySqlConnection(myConnectionString); 

    conn.Open(); 
} 

并给我这个错误:

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

Additional information: Host '188.47.8.212' is not allowed to connect to this MySQL server

============================================== ===========================

如果我登录phpmyadmin的

Ip: 91.196.48.243

===== ============

我改变端口(2222)

错误:

An unhandled exception of type 'System.TimeoutException' occurred in MySql.Data.dll

Additional information: Timeout in IO operation

+0

在MySQL中,你需要允许访问该特定的IP,用户名和密码来远程访问DB –

+0

这是一个了NamedPipes/ip问题ping的声音,你需要确保你的数据库配置为基于ipaddress访问也可以从目前的位置ping服务器..我也会考虑使用.config文件来设置您的数据库连接等等.. – MethodMan

回答

2

它看起来像你的IP没有权限访问MySQL服务器。

Additional information: Host '188.47.8.212' is not allowed to connect to this MySQL server

尝试在你的DrirectAdmin此解决方案:

If you have a MySQL database with your hosting account and need to connect to it from your home computer, or another web server, you'll need to add a remote "Access Host" to your database to allow the connection in. Go to:

User Level -> MySQL Management -> databasename -> Add Access Host 

You can either add the IP of the remote connecting box, or just use: %

to allow any IP (if you're unsure of the remote IP, or if it may change)

Note that the correct login/password is still required, the Access Host is just another layer of security.

Also make sure that port 3306 is open in your firewall on the DirectAdmin box, so the remote box can connect.

Connect to your MySQL database from a remote connection

0

您的MYSQL服务器不允许来自该IP的传入连接,请确保从服务器端允许它。