1

Anyone had an idea how to connect vb.net windows form application program into online / intranet database?

I tried using localhost, yes it connect but if i use IP in the HOST i cant access the database.

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 
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
Mark Kevz
  • 31
  • 1
  • 1
  • 5
  • Possible duplicate of: http://stackoverflow.com/questions/2269237/connecting-to-a-online-mysql-database-using-vb-net?rq=1 – Kenneth Apr 26 '13 at 20:14
  • can u answer my question, what if i will use IP address in my connection? my code posted is incorrect. can u teach me sir? – Mark Kevz Apr 26 '13 at 20:15
  • Please read the answers in the other question. They should cover all cases. Anyway, I don't directly see an error in your code, it has to be some infrastructure problem (firewall, ports, wrong IP, wrong username, ...) – Kenneth Apr 26 '13 at 20:17
  • OK tnx for the information anyways! – Mark Kevz Apr 26 '13 at 20:19
  • @Kenneth, The question you linked to is a very poor question, and this is not a duplicate of it. – Brad Apr 26 '13 at 20:21

2 Answers2

0

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:

  • This is generally a slow way of doing things
  • The MySQL wire protocol is not secure. 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.
Brad
  • 159,648
  • 54
  • 349
  • 530
0

You may need to get familiar with few things.

Difference between "localhost" and 127.0.0.1 in MySQL

When you connect with localhost using IP address - it connects via TCP protocol.

When you connect with localhost using "localhost" name - it will try to connect via shared memory (or socket under 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.

MySQL permissions to connect from other hosts

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.

Read this: grant remote access of MySQL database from any IP address

Community
  • 1
  • 1
Kamil
  • 13,363
  • 24
  • 88
  • 183