Container cannot connect to the hosts' MySQL database

Hi all,
I am trying to get the docker observium container running on my Ubuntu machine. I already have the MySQL database installed and am trying to run the container like this:
docker run -d
–name observium
-p 8000:8000
-v /opt/observium/volumes/config:/config
-v /opt/observium/volumes/html:/opt/observium/html
-v /opt/observium/volumes/logs:/opt/observium/logs
-v /opt/observium/volumes/rrd:/opt/observium/rrd
yelp/observium

But then when I go to 0.0.0.0:8000 or localhost:8000 or 127.0.0.1:8000, I see the following error:
MySQL Error: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (13)

I made sure that the mysql.sock file does exist in the /var/run/mysqld/ directory of the host machine.
And this is the section in my.cnf file which everything looks correct to me:
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

And in the same file I have:
bind-address = 127.0.0.1

Can someone please shed some light on this? Am I doing something wrong or I should be able to run the docker command above and go to the localhost:8000 URL and see something?

Thanks

Hi,

The container part seems to be working for you because you are able to reach observium but observium is not able to connect to MySQL DB on your host.

As you know that containers have a different filesystem from host so observium running inside container won’t see the mysql.sock which is in the host filesystem. So you should make observium connect over network to MySQL.

For the above thing to work you should bind MySQL on the ports to which the container has access. Your MySQL is listening on 127.0.0.1 which is not enough.

I hope you understood the scenario. Please let me know if you need further help.

Regards

Hi,
Thanks for your helpful reply. I understood your scenario completely but just not sure how to implement it. I have amysql installed on the host and have observium container running on the same host. Can you please let me know how exactly I can get this to work with all the details?

Thanks in advance.

Hi,

As you are trying to use Docker I recommend you to use MySQL Docker image instead of using the Host MySQL.

Incase you still want to use the host mysql, you may need to do the following steps.

In your host mysql’s my.cnf set the bind address to 0.0.0.0 so that mysql listens on all network interfaces

bind-address = 0.0.0.0

The in your observium’s DB settings point your DB_HOST to your hosts IP and port as 3306.

You may also need to configure grants for the user account using which you are using to connect to mysql.

Regards

Thanks for your reply. I made the changes you suggested:

Inside my.cnf I added the bind-address = 0.0.0.0

Then inside the observium container I:
vi /opt/observium/config.php

And replaced the word localhost with my host’s IP address:

$config[‘db_host’] = ‘My local IP address’;
$config[‘db_user’] = ‘observium’;

But there is no place where I type the port number. And when I go to 0.0.0.0:8000 I still get an error:, it’s a different one but we’re getting close. Just liek you said we will give observium container permissions to access the database. Can you help me with that as well?

MySQL Error: Host ‘172.17.0.18’ is not allowed to connect to this MySQL server

Did I do anything wrong? Were you able to get that running on your machine? Any help would be much appreciated.

Thanks

Hi,

You should provide grant for the user to connect from the container.

In your mysql console execute the following command.

mysql> grant all privileges on *.* to 'observium'@'%' identified by 'somepassword';
mysql> flush privileges;

After this try to connect from observium.

Regards

Thanks much, that worked. I had to add the user to the database and also grant access to that user. Finally got the web GUI up but now I don’t have a way to login as there is no default username and password.
There is a file called adduser.php and I supposed to be able to run it like this: “./adduser.php username password 10” and that should create the user but that fails.

Did you or anyone else have luck with this part or know how to login to the GUI?

Thanks

Hi Ranjandas,

I am at the same point with terzain.

My host is ubuntu… I have created a java application docker image which is trying to connect localhost:3306.

And it is of course getting exception bec. mysql is not in the container. it is working on the host.

I have added the below lines to my.cnf file on my host…

#################
[mysql]
bind-address = 0.0.0.0
#################
and in the java application mysql url is still localhost…!

and i am still getting “Caused by: java.net.ConnectException: Connection refused” exception.

what i am asking is which ip must be written in the mysql url inside the java application configuration?

Below is my ipconfig -a result and i am running the docker image like this : docker run -i -t b576aa480ec3

Thanks in advance

root@max:/home/max# ifconfig -a
docker0 Link encap:Ethernet HWaddr 02:42:07:cf:d5:b8
inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:7ff:fecf:d5b8/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:125 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8416 (8.4 KB) TX bytes:546 (546.0 B)

enp3s0 Link encap:Ethernet HWaddr 20:6a:8a:1c:e0:a9
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:16

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:506707 errors:0 dropped:0 overruns:0 frame:0
TX packets:506707 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:30189867 (30.1 MB) TX bytes:30189867 (30.1 MB)

wlp5s0 Link encap:Ethernet HWaddr 18:f4:6a:78:1a:7d
inet addr:192.168.43.142 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::1af4:6aff:fe78:1a7d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:129126 errors:0 dropped:0 overruns:0 frame:2096706
TX packets:164323 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:61956994 (61.9 MB) TX bytes:145714100 (145.7 MB)
Interrupt:17

root@max:/home/max#

I’ve a similar problem, when i use two container connected to a docker bridge network (mynet): one container is a tomcat one in which is deployed a war, that try to connects via hibernate to the second container, a mysql one (named "mysql-phpmyadmin, image from https://hub.docker.com/r/grzesiekb/mysql-phpmyadmin/). When, in the hibernate.cfg.xml file, i point to "localhost:3306/AT_DB) i see “connection refused”… when i point to "mysql-phpmyadmin:3306/AT_DB) i have "17-Feb-2017 08:31:25.649 SEVERE [http-apr-8080-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [com.ATBoscoCastellano.servlets.UserSessionServlet] in context with path [/AtApplicazione1] threw exception [Servlet execution threw an e
xception] with root cause
java.sql.SQLException: null, message from server: “Host ‘tomcat8.mynet’ is not allowed to connect to this MySQL server”

My problem is explained more in details here

Although this topic is almost resolved you can review also this: