When trying to connect to a mariadb container either manually or via shell script using
mysql -h'127.0.0.1:8003 -uusername -ppassword -ndatabasename
(It does not matter weather I use 127.0.0.1 or localhost.)
I get the following error
ERROR 2005 (HY000): Unknown MySQL server host '127.0.0.1:8003'
When I try to connect to the same mariadb server using MysqlWorkbench it works just fine, though.
I am using the mariadb container as part of a docker compose environment (running on MInt)
The mariadb container is setup as follows:
veto_db:
image: mariadb:latest
volumes:
- veto_data:/var/lib/mysql
- ./db:/docker-entrypoint-initdb.d
restart: always
ports:
- '8003:3306'
environment:
MYSQL_RANDOM_ROOT_PASSWORD: '1'
MYSQL_DATABASE: databasename
MYSQL_USER: username
MYSQL_PASSWORD: password
container_name: veto_db
My /etc/hosts file entry concerning the localhost
127.0.0.1 localhost
127.0.0.1 FQDN of dev site 1
127.0.0.1 FQDN of dev site 2
etc...
I need these DNS entries since I am using a nginx reverse proxy to have my development sites accessible via https and their FQDN. No idea if that is really relevant, though.
for -h the ip_address:port format you're using is invalid, you want to use a -P option for specifying the port (for localhost this isn't necessary).
And the way Docker remaps the port such as with your 8003:3306 is <host_port>:<container_port> so the container doesn't undergo any changes, but the host will forward anything on 8003 to the container on 3306. But for other containers on the same Docker network they will all still use the standard ports for their respective apps when communicating with eachother
$ docker-compose up -d
Creating veto_db ... done
$ docker exec -it veto_db mysql -h127.0.0.1 -P3306 -uusername -ppassword -Ddatabasename
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.14-MariaDB-1:10.3.14+maria~bionic mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [databasename]>
though I am a bit ashamed both about the invalid usage of the "-h" flag and not simply using the docker "exec" command ... I am still very thankful. You helped me out a lot.
Most helpful comment
for
-htheip_address:portformat you're using is invalid, you want to use a-Poption for specifying the port (for localhost this isn't necessary).And the way Docker remaps the port such as with your
8003:3306is<host_port>:<container_port>so the container doesn't undergo any changes, but the host will forward anything on 8003 to the container on 3306. But for other containers on the same Docker network they will all still use the standard ports for their respective apps when communicating with eachother