如何将 vb.net 连接到 ONLINE MySQL 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16244466/
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
How to connect vb.net to ONLINE MySQL database?
提问by Mark Kevz
Anyone had an idea how to connect vb.net windows form application program into online / intranet database?
任何人都知道如何将 vb.net windows 窗体应用程序连接到在线/内联网数据库中?
I tried using localhost, yes it connect but if i use IP in the HOST i cant access the database.
我尝试使用本地主机,是的,它可以连接,但是如果我在主机中使用 IP,则无法访问数据库。
Here is my connection:
这是我的连接:
Dim conStr As String = "server=192.168.1.18;Uid=root;Pwd='';Database=hrms"
Dim con As New MySqlConnection(conStr)
Try
MessageBox.Show("Connecting to mysql database..................")
con.Open()
Dim sqlStr As String = "SELECT * FROM accounts"
Dim cmd As MySqlCommand = New MySqlCommand(sqlStr, con)
Dim rd As MySqlDataReader = cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
MessageBox.Show(rd(0) & " " & rd(1) & " Naa koi na fetch")
Exit Sub
Else
MessageBox.Show("Could not find something")
Exit Sub
End If
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
回答by Brad
Yes, you can connect to a MySQL server over a network. Make sure that you are using the right IP address, and that MySQL is actually listening on that interface/port. Also, check your firewall. A few things to note:
是的,您可以通过网络连接到 MySQL 服务器。确保您使用的是正确的 IP 地址,并且 MySQL 实际上正在侦听该接口/端口。另外,请检查您的防火墙。需要注意的几点:
- This is generally a slow way of doing things
- The MySQL wire protocol is notsecure. You should be doing this over SSL or a VPN.
- It doesn't hurt to hide your server on some random port number up high. This doesn't protect you, but will generally keep the hordes of bots from hammering your server with default passwords constantly. If you only allow MySQL to listen on your VPN interface (recommended), then this doesn't matter.
- 这通常是一种缓慢的做事方式
- MySQL的有线协议是不是安全。您应该通过 SSL 或 VPN 执行此操作。
- 将您的服务器隐藏在某个随机端口号上并没有什么坏处。这并不能保护您,但通常可以防止成群的机器人不断使用默认密码攻击您的服务器。如果您只允许 MySQL 侦听您的 VPN 接口(推荐),那么这无关紧要。
回答by Kamil
You may need to get familiar with few things.
您可能需要熟悉一些事情。
Difference between "localhost" and 127.0.0.1 in MySQL
MySQL中“localhost”和127.0.0.1之间的区别
When you connect with localhost using IP address - it connects via TCP protocol.
当您使用 IP 地址与 localhost 连接时 - 它通过 TCP 协议连接。
When you connect with localhost using "localhost" name - it will try to connect via shared memory (or socket under linux).
当您使用“localhost”名称与 localhost 连接时 - 它会尝试通过共享内存(或 linux 下的套接字)进行连接。
In second case - you will not connect with localhost via "localhost", if your server has "shared memory" turned off. You can turn on that protocol on your local server, or use 127.0.0.0 to force TCP connection.
在第二种情况下 - 如果您的服务器关闭了“共享内存”,您将不会通过“本地主机”连接到本地主机。您可以在本地服务器上打开该协议,或使用 127.0.0.0 来强制 TCP 连接。
MySQL permissions to connect from other hosts
从其他主机连接的 MySQL 权限
In MySQL user by default has permissions to connect only from localhost. You may need to add permission to connect to specyfic user from other host.
在 MySQL 中,用户默认只有从本地主机连接的权限。您可能需要添加权限才能从其他主机连接到特定用户。
Read this: grant remote access of MySQL database from any IP address

