Search code examples
mysqlubuntutcpserverubuntu-server

port fowarding for mysql server ubuntu remote connection


I have a ubuntu 16.04 on which I run 3 servers from virtualbox (a webserver, a home file server and a mysql server).

In this mysql server I naturally have an internal ip (192.168....) and an external one.

However when I try to connect remotely to this mysql server from mysql shell, workbench or .NET connector (eg. mysql -u root -p -h 172.241.442.12) using the external IP of this virtual machine I always get ERROR: Not connected.

My question is whether to connect I have to enable port forwarding in my router (3306), just as I would do enabling 80/8080 in a webserver to make it accessible from the internet.

I never did it before and haven't find any clear tutorial either. My main purpose is to connect to this db in a vb.net application.

Thanks!


Solution

  • Yes, if you intend to access it in this manner you will need to forward a port. You will also need to grant access to MySQL for a user that is allowed remote access.

    GRANT ALL ON somedb.* TO `user`@`123.123.123.123` IDENTIFIED BY 'somePassword'
    

    The IP address can be a hostname, or % to match everything.

    That said, unless you really need a permanant external connection to MySQL you should not expose it like this, the better option would be to tunnel the port through SSH.

    ssh -N user@dbserver -L12345:localhost:3306 &
    mysql -u root -h localhost -P 12345