I want to connect to MySQL/MariaDB in the container from another Docker container

Asked 1 years ago, Updated 1 years ago, 68 views

I am learning container orchestration using docker-compose.

I would like to access the mariadb of the db container from the app container with the folder configuration below, but I got an error and it doesn't work.

.
├-- app
│   --Dockerfile
├-- db
│   --Dockerfile
└-- docker-compose.yml

app/Dockerfile

 FROM debian:buster

RUN set-x\
    &apt-get update\
    &apt-get install --no-install-recommends --no-install-suggests-y\
        mariadb-client\
        vim

CMD ["tail", "-f" ]

db/Dockerfile

 FROM debian:buster

RUN set-x\
    &apt-get update\
    &apt-get install --no-install-recommends --no-install-suggests-y\
        mariadb-server\
        mariadb-client\
        vim

CMD service mysql start\
    &tail-f/dev/null

docker-compose.yml

version: "3.9"

services:
  app:
    build:./app
    networks:
      - frontend

  db:
    build:./db
    expose:
      - 3306
    networks:
      - frontend

networks:
  frontend:
    driver —bridge

volumes:
  db_data:{}

I entered the app container and ran the following command, but got an error.

root@0e0ad0889639:/#mysql-hdb-uroot
ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)

## The connection seems to be working.
root@0e0ad0889639:/# ping db
PING db (192.168.128.3)56(84) bytes of data.
64 bytes from test_db_1.test_frontend (192.168.128.3)—icmp_seq=1ttl=64 time=0.104 ms
64 bytes from test_db_1.test_frontend (192.168.128.3)—icmp_seq=2ttl=64 time=0.142 ms

I would like to know the reason why the container cannot be connected.Thank you for your cooperation.

db container side

The db container is not initially set.
You can launch mariadb in the db container.

root@9d3b519e464f:/#service--status-all
 [?] hwclock.sh
 [+] mysql
 [-] rsync
MariaDB [mysql] > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema|
| mysql|
| performance_schema|
+--------------------+
3 rows in set (0.001sec)

MariaDB[mysql]>SELECT User, Host FROM mysql.user;
+------+-----------+
| User | Host |
+------+-----------+
| root | localhost |
+------+-----------+

Container side of app

If you return the container name, the error will change, so the communication itself seems to be working.

root@69ccb34f8a30:/#mariadb-u root-hdb
ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)

root@69ccb34f8a30:/#mariadb-u root-haa
ERROR 2005 (HY000): Unknown MySQL server host 'aa' (-2)
 ddocker-v        
Docker version 20.10.12, builde91ed57
ssw_vers
ProductName: macOS
ProductVersion: 12.3
BuildVersion: 21E230

mysql docker docker-compose mariadb

2022-09-29 22:06

1 Answers

It was resolved by changing the two settings.

my Settings for host acceptance within mysql

USE mysql;
GRANT ALL ON*.* TO 'root' @ '%' identified by 'pass' WITH GRANT OPTION;
GRANT ALL ON *.* TO 'root' @ 'localhost' identified by 'pass' WITH GRANT OPTION;
FLUSH PRIVILEGES;

myChange mysql config file

etc/mysql/mariadb.conf.d/50-server.cnf
I would like to add the following in .
bind-address=app


2022-09-29 22:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.