2016-12-13 126 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的傳入連接,請確保從服務器端允許它。