Page 1 of 1

Connecting to MySQL from Container/Docker

Posted: Wed Aug 08, 2018 9:27 pm
by ColH
I have a functioning MySQL server running on my (QTS 4.3.3) system - phpmyadmin is working; and I have native apps writing to tables.
I need to get a (Grafana) Container/Docker able to access the server too, but it's connection attempts timeout. I've created [within phpmyadmin] a user on '%' hostname, with SELECT privileges. I'm setting the Container to connect to the system's actual IP address, port 3306, with this username and password. But, after a few minutes, the connection attempt times out.
Any tips please on what's missing?

(And, incidentally, where is the server's config file...?)

Re: Connecting to MySQL from Container/Docker

Posted: Wed Aug 08, 2018 11:15 pm
by Mousetick
- What is the network mode of your Grafana container: Bridge? Host?
- Is the MySQL server bound to a specific IP address or does it listen on all interfaces?
- Are you restricting network access to the NAS with firewall rules for specific IP subnets?
- Are you restricting network access to the SQL server from users with specific IP addresses/hostnames?
It seems that's what you have done. Try with a DB user that has no host restrictions.

Not sure what you mean by "system's actual IP address", which system are you talking about?
- Try using 172.17.0.1 which is the IP address of the host (the NAS in this case) in bridge network mode by default inside the container's virtual network.

Not sure what you mean by "server's config file", which server are you talking about?

When Docker runs in bridge network mode, it creates a virtual network on IP subnet 172.17.0.0/16 by default, inside the container(s), which is distinct from the real physical network to which the NAS is connected. Keep that in mind.

Re: Connecting to MySQL from Container/Docker

Posted: Wed Aug 08, 2018 11:32 pm
by ColH
Hi, thanks for replying - I'll try to accurately answer the questions.
- Looking at the Network Advanced Settings on the container, I believe it is in NAT mode ("Use default NAT network (command: --net=bridge)")
- I'm not sure about the MySQL server bindings - I couldn't spot a setting for this in phpMyAdmin; though as I say I created a user on "%". I am though aware of the bind-interface option, but haven't been able to pinpoint which is the config file being used by MySQL (/etc/config/my.cnf...?) - that's the file I meant by the "server's config file".
- "System's actual IP address" - I meant it's address on my LAN (192.168.x.x)
- I haven't consciously set any additional firewall rules.

From my suspicions, and your indications, it's perhaps an incorrect network mode and/or inadequate bind address on MySQL?

Re: Connecting to MySQL from Container/Docker

Posted: Thu Aug 09, 2018 12:00 am
by Mousetick
- Yes the configuration file is /etc/config/my.cnf. The SQL server is actually MariaDB, not MySQL.
- Ok so your container is configured for Bridge (NATed) network mode.
- Try 172.17.0.1 for the IP address as I suggested.
- Or change the container to Host network mode. The container will have the same IP address as the NAS, there will be no virtual network, no NATing. Then you should be able to use 192.168.x.x for the SQL server IP address, or 127.0.0.1.

See also https://stackoverflow.com/questions/243 ... f-the-mach

Re: Connecting to MySQL from Container/Docker

Posted: Thu Aug 09, 2018 12:23 am
by ColH
Aha. Got it connecting now: I've added "bind-address=0.0.0.0" to /etc/config/my.cnf , and restarted the SQL server from the Control Panel. The container is still in bridge mode.
Tried a few IP addresses, then a "cat /proc/net/arp" in a terminal of the container suggested that 10.0.3.1 might be the correct address. And yes, the Grafana container is now able to connect to the SQL server.
Thanks for the pointers!