Access containerized mysql server from a container on a different host

I have container1 (10.1.0.2) on host1 (10.0.0.2) and container2 (10.2.0.3) on host2 (10.0.0.3). I’m trying to set up container2 to access a mysql server on container1. Containers are on bridged networks and cannot be directly accessed outside its respective host. In my attempt, I made the following steps:

  1. Modified /etc/mysql/conf.d/mysqld.cnf on container1 to read
port 3326
# ...
bind-address 0.0.0.0

restarted mysql service.
2. Added a proxy device to container1 on host 1:

bash $ lxc config device add container1 p3326 proxy listen=tcp:0.0.0.0:3326 connect=tcp:127.0.0.1:3326
  1. Allowed port 3326 on both host1 and container1:
bash $ ufw allow 3326
bash $ ufw reload
  1. Added remote user
mysql> create user 'user'@'container2.lxd' identified by 'password';;
mysql> grant all on *..* to 'user'@'container2.lxd';
mysql> flush privileges;
  1. Try to connect on host2:
bash $ mysql -u 'user' -p -h '10.0.0.2:3326'

And I get:

ERROR 2005 (HY000): Unknown MySQL server host '10.0.0.2:3326' (22)

I thought the proxy device would have routed all traffic on port 3326 to container1, what did I do wrong here?

I think that you’re not specifying the port correctly on the mysql client, the error suggests it is using the port as part of the host.

Please consult the manpage for the mysql command to see how to specify it properly.

D’oh! The mysql command to specify port is -p instead of the : notation. Should have paid more attention to documentation.

1 Like