问题
Here is my connection string
mySqlCon.ConnectionString = "server=testserver.noip.me; port=3306; database=testserver1; uid=admin; pwd=admin"
I have created a vb.net application with TryCatch function to check MySql connection.
My server testserver.noip.me
is a hostname of my public ip (The hostname is an example, if you check the port online, it wont work). If I change the server to localhost/127.0.0.1 or my private ip address which is something like 192.168.0.xx, the result is connected just fine. Able to connect to database even on another PC but within the same wifi network.
Whenever I change it to my public ip address or my hostname, the application returns an error message :
"Unable to connect to any of the specified MySQL hosts."
Here is the code
Try
mySqlCon.Open()
If mySqlCon.State = ConnectionState.Open Then
Label5.Text = "Online"
Label5.ForeColor = Color.GreenYellow
LoginBtn.Enabled = True
Else
Label5.Text = "Offline"
Label5.ForeColor = Color.LightPink
LoginBtn.BackColor = Color.Red
End If
mySqlCon.Close()
Catch ex As MySqlException
If mySqlCon.State = ConnectionState.Open Then
mySqlCon.Close()
End If
Finally
mySqlCon.Dispose()
End Try
I have also done port forward in my router and the result is fine :
And the hostname port 3306 check is open as well:
Extra info : I have installed connectornet and is using MySQL workbench for this.
Everything is working fine on local network but not online. Any idea what I should do to make the application connect to the database successfully from another network online? No SSH or any secure connection guide please because this is just a test server.
回答1:
In your MYSQL (source machine) you have to enable the destination domain name for access rights. In the host table add your destination IP.
回答2:
It's Because You Did Not Forward Port.
Go to Your Router Settings, Find Port Forwarding Add A Service With HTTP, Enter Your Local IP Address, Then Specify 3306 Port As Ending Port.
This Worked For Me. Perhaps It Will For You Too.
来源:https://stackoverflow.com/questions/31739561/how-to-connect-to-mysql-database-from-another-pc-online-vb-net