使用 VB.NET 2010 远程访问网站的 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21849647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:48:04  来源:igfitidea点击:

Remote Access to MySQL database of a website using VB.NET 2010

mysqlvb.netremote-access

提问by japhfortin

Im trying to connect a vb.net2010 project to a database of a website. I have already added % as host in the remote mysql access of cpanel, and turned off my firewall.

我正在尝试将 vb.net2010 项目连接到网站的数据库。我已经在 cpanel 的远程 mysql 访问中添加了 % 作为主机,并关闭了我的防火墙。

Here is the code I used:

这是我使用的代码:

    Dim MySQLconn As New MySqlConnection
    MySQLconn = New MySqlConnection("server=www.mysite.net; User Id=myusername; pwd=mypassword; database=mydatabase; port=3306;")
            Try
                MySQLconn.Open()
                MsgBox("Sucess", vbOKOnly)
                MySQLconn.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

An error is caught which says "Unable to connect to any of the specified MySQL hosts"

捕获错误,显示“无法连接到任何指定的 MySQL 主机”

I hope I can get help or suggestions.

我希望我能得到帮助或建议。

Thank you.

谢谢你。

回答by Jürgen Steinblock

Please verify that the MySQL server listens on external interfaces.

请验证 MySQL 服务器是否侦听外部接口。

In most *nix Environments it defaults to localhost, that means you can't connect from a remote machine, even if you disable a firewall.

在大多数 *nix 环境中,它默认为 localhost,这意味着您无法从远程计算机连接,即使您禁用了防火墙。

This is either achived via skip-networkingor more common via bind-address localhostin the my.cnf file.

这是通过my.cnf 文件中的通过skip-networking或更常见的实现bind-address localhost

Try setting

尝试设置

bind-address x.x.x.x 

where x.x.x.x is the IP adress of www.mysite.net and restart your mysql server.

其中 xxxx 是 www.mysite.net 的 IP 地址并重新启动您的 mysql 服务器。

Update

更新

As you are using cpanel, have you whitelistet your ip adress as mentioned here? http://kb.liquidweb.com/enable-remote-mysql-connections-in-cpanel/http://www.yourhowto.net/how-to-make-a-remote-mysql-connection/

当您使用 cpanel 时,您是否已将您的 IP 地址列入白名单? http://kb.liquidweb.com/enable-remote-mysql-connections-in-cpanel/ http://www.yourhowto.net/how-to-make-a-remote-mysql-connection/

Basically there are three things to do:

基本上要做三件事:

  1. Whitelist your IP or % as a wild card (you already have done this)
  2. Add Firewall rules (not sure if that is required for cpanel, you could verify that with a port scanner
  3. Your user needs permission to access the database (GRANT ... ON ... TO user@%) from a remote host. If you run this command from a command prompt you should get a password prompt >mysql -h www.mysite.net -u user -p, try to log in. If you get an access denied for user@% you need to grant permissions for user@your-ip-address-or-hostname.
  1. 将您的 IP 或 % 列入白名单作为通配符(您已经这样做了)
  2. 添加防火墙规则(不确定 cpanel 是否需要,您可以使用端口扫描器进行验证
  3. 您的用户需要GRANT ... ON ... TO user@%从远程主机访问数据库 ( ) 的权限。如果您从命令提示符运行此命令,您应该会收到密码提示>mysql -h www.mysite.net -u user -p,请尝试登录。如果您的 user@% 访问被拒绝,您需要授予 user@your-ip-address-or-hostname 的权限。