Connect to remote MySQL database using VB.
NET 2010
Ask Question
Asked 7 years, 2 months ago
Active 3 years, 7 months ago
Viewed 31k times
2
2
My connection string as follows.
conn.ConnectionString = "Server=192.248.***.***; Port=3036; User id=admin; password=***; Database=abc; Connect Timeout=60;"
But it returns error "Error connecting to database: Unable to connect to any of the specified MySQL hosts."
But i can log in to the same using PHPMyAdmin.
//192.248.***.***/phpmyadmin
Also i can log in to local MySQL database using:
conn.ConnectionString = "server=" & "localhost" & ";" & "user id=" & "admin" & ";" & "password=" & "" & ";" & "database=abc"
What is the wrong in this code. I want to connect to the remote database since it is the requirement of the system. Any help please.
Update: In immediate window it shows:
PassbookPrinter.vshost.exe Error: 0 : Unable to connect to any of the specified MySQL hosts.
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
mysql vb.net
shareimprove this question
edited Dec 10 '12 at 9:45
asked Dec 10 '12 at 9:15
nirosha rathnayaka
18811 gold badge55 silver badges1818 bronze badges
post your full stack trace error – Ami Dec 10 '12 at 9:18
maybe you have typo in your port .. the default port is 3306 not 3036. unless you changed it. – kiLLua Feb 3 '17 at 5:33
add a comment
5 Answers
activeoldestvotes
4
Connect to remote MySQL Database Using VB.Net
To connect vb.net to remote MySql database ; No matter what VB.Net version you might be using, just go throw the following steps.
1) Download Mysql Connector/Net from the url (https://dev.mysql.com/downloads/connector/net/)
2) Install the connector; by default the connector will be installed in the path (C:\Program Files\MySQL\Connector Net 6.9.6) that's the
version i have installed.
3) Open VB.Net IDE and start the new project.
4) Add the "Mysql.Data.dll" as a reference to your project, which you can find it in the path (C:\Program Files\MySQL\Connector Net
6.9.6\Assemblies\v4.5);
5) Prepare your connection form as shown in this image;
6) Create the class named "Database" and write in the following code.
Database class code
Imports MySql.Data.MySqlClient
Public Class Database
Private _connection As New MySqlConnection
Private _errormessge As String
Private _servername As String
Private _databasename As String
Private _userid As String
Private _password As String
Public WriteOnly Property ServerName() As String
Set(ByVal value As String)
_servername = value
End Set
End Property
Public WriteOnly Property DatabaseName() As String
Set(ByVal value As String)
_databasename = value
End Set
End Property
Public WriteOnly Property UserID() As String
Set(ByVal value As String)
_userid = value
End Set
End Property
Public WriteOnly Property Password() As String
Set(ByVal value As String)
_password = value
End Set
End Property
Public ReadOnly Property ErrorMessage() As String
Get
Return _errormessge
End Get
End Property
Public Function Connection() As Boolean
Try
_connection.ConnectionString = "Server=" & _servername & ";Port=3306;Database=" & _databasename & ";User ID=" & _userid & ";Password=" &
_password & ""
_connection.Open()
If _connection.State = ConnectionState.Open Then
_connection.Close()
Return True
End If
Catch ex As Exception
_errormessge = ex.Message
Return False
End Try
End Function
End Class
Form Class Code
Public Class Frm_Main Private Sub btn_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn_connect.Click
'Object declaration and instantiation
Dim data As New Database
With data
'Assing the object property values
.ServerName = txt_server.Text
.DatabaseName = txt_database.Text
.UserID = txt_uid.Text
.Password = txt_pwd.Text
'Connection testing
If .Connection Then
MessageBox.Show("Database Conneted.")
Else
MessageBox.Show(.ErrorMessage)
End If
End With
End Sub
Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
Close()
End Sub
End Class
7) Run the project and try the connection; if the connection is successfully then your luck; and if the connection is not successfully with the
following error message worry not just keep reading more;
8) Note the ip address on error message after @ (thats your ip) and add it to your domain cpanel "remote mysql access" the image bellow
illustrates how the remote mysql access looks like(they are the same bu they may defer in colors); Don't forget to press "add hosts" button.
This settings can work daily for those who are in static
ip.
See the message of success after the above steps;
But if the error message persists try to leave the password text blank and connect again if you had no password in your remote database; if
the error comes again except YES is changed to be NO then you have to check if your in DHCP;
9) If your in DHCP which means the ip is changing in every new Internet connection. If your using modem probably your in DHCP. If your
in dynamic ips then check what is changing in the ip's 4 blocks. If the first ip was 197.250.3.201 in the first connection and the next ip is
197.250.60.70 and the next next ip is 197.250.80.24; you have to add 197.250.% in your cpanel access hosts for your connection to be
stable.
10) Note: As the percent symbol (wild card) flows to the left side of the ip address the more the door of security becomes open. On new
error please contact your domain provider there might be some other security issues in the domain. Thanks!
shareimprove this answer
edited Jun 4 '15 at 14:42
answered Jun 4 '15 at 14:22
Simbo
4133 bronze badges
This class can be replaced by dev.mysql.com/doc/dev/connector-net/6.10/html/… – Mary Jul 17 '19 at 20:43
add a comment
1
just try this , it will work
con.ConnectionString ="Persist Security Info=False;datasource=site.com;port=3306;username=username;password=password;database=database name"
You must add
Persist Security
in your code . to access your mysql database of your server of site . go cpanel > Remote MySQL , and add your ip address
see this image >>> https://www.dropbox.com/s/ytsz57spanwdpkz/cpanel.PNG
now enjoy :p
shareimprove this answer
answered Aug 5 '14 at 9:31
Prappo Prince
1111 bronze badge
add a comment
0
The default port for MySQL is 3306. If you can connect from phpmyadmin using the default settings it sounds like a typo and you should
have:
conn.ConnectionString = "Server=192.248.***.***; Port=3306; User id=admin; password=***; Database=abc; Connect Timeout=60;"
Additionally remote access to MySQL Server is disabled by default. For example if you're running MySQL on a Debian Linux distro you
might need to take additional steps such as the following to allow access from another machine:
http://www.debianhelp.co.uk/remotemysql.htm
For a Windows host the following may prove useful:
http://techminded.net/blog/allow-remote-connections-for-mysql-on-windows.html
shareimprove this answer
edited Dec 10 '12 at 10:10
answered Dec 10 '12 at 9:37
PeterJ
2,8871212 gold badges4242 silver badges5353 bronze badges
Also try Uid=admin; Pwd=*** - I seem to remember the MySQL connector doesn't allow some aliases that SQL/Server does – PeterJ Dec 10
'12 at 9:51
Nope. Still the same error. Is there something should I have to do since it is secured. – nirosha rathnayaka Dec 10 '12 at 9:57
Also noticed that phpadmin is on the other machine, maybe a firewall problem on the machine and port 3306 isn't open for remote
connections? Maybe install MySQL Workbench and see if you can connect remotely to isolate the problem. – PeterJ Dec 10 '12 at 9:57
Further comment, if you can't connect from MySQL Workbench then we'll need to know the operating system MySQL is running on to
solve the problem. Just about to update my answer. – PeterJ Dec 10 '12 at 10:06
Thanks Peter, I tried using MySQL WB. It says cannot connect. It shows 192.248.***.*** (10060). Is it port 10060? The OS is Win 2000
server. – nirosha rathnayaka Dec 10 '12 at 10:14
show 3 more comments
0
Iv previously had the same issue, i was the same as it was running on a different server.
Try to ping the server from where the application is running and see if you can connect. that's how i found out and needed a firewall change.
shareimprove this answer
answered Dec 10 '12 at 10:13
Gerry85
6122 silver badges99 bronze badges
add a comment
-1
try changing port to 3307 on .ini config of mysql then bind-address=server's ip or hostname on .ini config of mysql also