0

I have a mysql database and an app that is trying to connect to it. When I run docker-compose I get an error like this:

[error] failed to initialize database, got error dial tcp 192.168.128.3:3306: connect: connection refused
2023/01/14 19:52:53 Error when connecting to database: dial tcp 192.168.128.3:3306: connect: connection refused

It starts to work when I add line user: root to docker-compose, but I've read that's something I should avoid.

I tried running docker exec -it usermysql /bin/bash to try to connect to database from inside the container but it doesn't work as well (with user: root works fine).

I have no name!@usermysql:/$ mysql -u Slimo300 -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/bitnami/mysql/tmp/mysql.sock' (2)

Why it behaves like that? How can I run mysql container as a nonroot?

Services definitions:

 usermysql:
    image: bitnami/mysql
    container_name: usermysql
    hostname: usermysql
    user: root #<------- root
    networks:
      - chatnet
    volumes:
      - usermysql_data:/bitnami/mysql/data
    environment:
      - MYSQL_DATABASE=dbusers
      - MYSQL_ROOT_PASSWORD=password123
      - MYSQL_USER=Slimo300
      - MYSQL_PASSWORD=password123
  userservice:
    build:
      context: .
      dockerfile: ./backend/user-service/Dockerfile
    container_name: userservice
    hostname: userservice
    networks: 
      - chatnet
    ports:
      - "8083:8080"
    depends_on:
      - usermysql
    restart: on-failure
Tuturutu
  • 23
  • 1
  • 5
  • Did you have a look here : https://stackoverflow.com/questions/5376427/cant-connect-to-local-mysql-server-through-socket-var-mysql-mysql-sock-38 – SelVazi Jan 14 '23 at 21:14
  • @SelVazi I just did, but I don't think there is an answer for me, my database runs in container environment and if I run is as nonroot (what I am trying to do) I am not able to even read files there. – Tuturutu Jan 14 '23 at 21:55

0 Answers0