Home > database >  Connecting to MySql database in Docker
Connecting to MySql database in Docker

Time:10-08

I'm trying to bring up 2 docker containers

  • app container - asp.net core api
  • db container - MySql database for api

the api can't connect to the db. The docker-compose file is

services:
  db:
    image: mysql/mysql-server
    container_name: db
    ports:
      - "49301:3306
    environment:
      MYSQL_ROOT_PASSWORD: Password1!
  app:
    image: mycontainner/api:1.0
    container_name: app
    build:
      context: .
      dockerfile: MyProject.Api\Dockerfile
    ports:
      - "49501:80"
    environment:
      - "ConnectionStrings:MySql=Server=db;Port=3306;Database=MyDb;Uid=root;Pwd=Password1!;SslMode=None;ConnectionReset=false;connect timeout=3600"     
    depends_on:
      - db
networks:
  default:
     external: true
     name: nat

I can ping between the 2 servers so the network is fine and they can see each other. I can browser to test api endpoints that don't connect to the database

The error says

An error occurred using the connection to database '' on server 'db'.

so the MySql host appears incorrect but for some reason it doesn't appear to pick up the database it's trying to connect to be it is specified in the connection string

If I launch a shell on the database then query the status of the MySql instance then I get

mysql  Ver 8.0.26 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          27
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.26 MySQL Community Server - GPL
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /var/lib/mysql/mysql.sock
Binary data as:         Hexadecimal

So the server seems to be on localhost which looks right.

Anyone any ideas what I'm doing wrong?

The containers are windows but if I try linux containers then I still can't connect (the docker-compose file is slightly different but the network is still available)

Also - the database isn't actually there but entity framework migrations should create it on start up. I've also put up a temporary API endpoint which also runs the migrations put if I run that after all the containers are up then I still get connection errors. It all works fine outside of containers

Many Thanks

CodePudding user response:

Ref :How to connect locally hosted MySQL database with the docker container

  1. bring to the container the same networking as your host, in order to share the localhost, so the container will find mysql there. Use network mode as "host":

network_mode: "host"

  1. Find the host machine ip in the docker network. If you use docker-compose.yml version: "3" it's probably that that IP is: 172.18.0.1, but confirm it searching for the "Gateway" of your container (your host): docker inspect | grep Gateway

    "Gateway": "", "IPv6Gateway": "", "Gateway": "172.18.0.1", "IPv6Gateway": "",

  2. Allow permission using mysqld.cnf

    sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf bind-address = 0.0.0.0 save your file and restart mysql server

    sudo systemctl restart mysql.service

CodePudding user response:

Could you share the log of the error? "can't connect to the Server db" is not enough info for troubleshooting.

  • Related