MYSQL/Mariadb using docker

I have homeAssistant running in docker on a nuc using an SSL offloading proxy and seperate mariadb image. (see screenshot from portainer showing the 3 containers running). HomeAssistant seems unable to connect to the database image based on the docker image name (which is ‘mariadb’ in my case. I have created a database and user and granted the user rights to the database. here is the prhrase of my config that define the connection in homeAssistant

dbconnection: mysql://hassuser:@mariadb/hass_db

When I start HomeAssistant, the log says:

2020-02-15 17:55:36 ERROR (Recorder) [homeassistant.components.recorder] Error during connection setup: (MySQLdb._exceptions.OperationalError) (2005, “Unknown MySQL server host ‘mariadb’ (-2)”)

Since I have set this up succesfully in the past I must be overlooking something stupid…

I have HA and mariadb running in docker via rancheros and have the following in my configuration.yaml file for the connection:

recorder:
  db_url: mysql://username:[email protected]/homeassistant?charset=utf8

Not sure if it will help but I haven’t tried it using the name of the docker container, always just used the IP address.

1 Like

The container name is not the server host name.
You can use the db ip 172.17.0.5 or set the.host name in portainer

Thanks for this insight. And it even works. How persistent is the docker assigned IP address?
And even more interesting: it seems possible to refer using a name end it even worked in the past with my setup. But for now this works!

Great! I use static reservations in DHCP on my router so the IP won’t change on me unless I change it for some reason.

If you update container it may change.

You can
Portainer>>db container>>duplicate/edit>>network>>set hostname

This will allow you use hostname for connection and you may keep that consistent between updates

1 Like

I’m having the same issues now. I moved mariadb from the windows based server to docker. Can access it fine with sqlworkbench but not from ha.

tried

db_url: mysql://username:[email protected]/homeassistant?charset=utf8

and 

db_url: mysql://username:password@host_IP/homeassistant?charset=utf8

and

db_url: mysql://username:password@hostname/homeassistant?charset=utf8


Possibly a firewall blocking it.

HA and MySQL using same docker host?
if HA has local network access the host_IP should have no issue. Are you accessing through correct port?
mysql://user:password@SERVER_IP:ALTERNATE_PORT/DB_NAME?charset=utf8mb4

Ha is on another server but same network. It was connected to the windows Maríadb server before (same host as new docker) and working fine. But just can’t get it to connect to the new instance in the docker. Port 3306 assigned.
Old Maríadb server shut down so no port conflict.

I’ll try a new port later.

All sorted.
The container had port binding 127.0.0.1:3306 > 3306
I removed the IP so it was just 3306>3306
and added on the mariadb connection HOST_IP:3306

BUT still mysql consuming much more cpu than the mariadb addon. I’ve killed that. 10%
I’ll try the light version of maridb.