Unknown
Unknown

Reputation: 919

Unable to connect to MySQL database in docker container

I'm honestly tired of this. I've tried every possible solution but it still refuses to connect.

Here is my docker-compose file:

version: '3'

services:
  # Database
  db:
    image: mysql:5.7
    container_name: db
    restart: always
    env_file: .env
    environment:
      - MYSQL_ROOT_HOST=%
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=$MYSQL_DATABASE
      - MYSQL_USER=$MYSQL_USER
      - MYSQL_PASSWORD=$MYSQL_PASSWORD
    networks:
      - backend
  # Wordpress
  wp:
    depends_on:
      - db
    image: wordpress:php7.3-fpm
    container_name: wordpress
    restart: always
    env_file: .env
    environment:
      - WORDPRESS_DB_HOST=db:3306
      - WORDPRESS_DB_USER=$MYSQL_USER
      - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
      - WORDPRESS_DB_NAME=$MYSQL_DATABASE
    volumes:
      - ./wordpress:/var/www/html
    networks:
      - backend
  # Nginx
  nginx:
    depends_on:
      - wp
    image: nginx
    restart: always
    ports:
      - "80:80"
    volumes:
      - ./wordpress:/var/www/html
      - ./nginx-conf:/etc/nginx/conf.d
    networks:
      - backend

networks:
  backend:
    driver: bridge

I've tried 127.0.0.1, 0.0.0.0, db, docker inspect db to get ip address of the container. All of them fail to connect. I've used Sequel Pro, MySQL Workbench and DataGrip.

The setup works completely fine. Its just that I cant connect to the database outside the container.

I even checked the mysql host privileges in the container and got:

% root

% wordpress (name of the user I created)

...

Am I missing something?

Upvotes: 2

Views: 7740

Answers (2)

Leela Mandaloju
Leela Mandaloju

Reputation: 1

MYSQL Changes

  • MY.ini changes in c:\programdata\mysql\mysql server 8.0 Add Bind_address=0.0.0.0

  • Save as Admin - Check timestam

  • Create one App user - Dont user mysql Root user for app

drop user 'username'@localhost;
FLUSH PRIVILEGES;
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
  • Restart the MYSQL Server
  • Restart Docker for Windows
  • Check Hosts files in C:\Windows\System32\drivers\etc if host.docker.internal variable is updated correctly
# Added by Docker Desktop
10.0.0.6 host.docker.internal
10.0.0.6 gateway.docker.internal

Application Changes:

User host.docker.internal in JDBC URL in app.properties

spring.datasource.url=jdbc:mysql://host.docker.internal:3306/test
spring.datasource.username=username
spring.datasource.password=password

maven install should work

docker run <repo/repo>:image should work fine. now

Upvotes: 0

takacsmark
takacsmark

Reputation: 4441

In order to reach your database from the host machine with an sql client you need to map the MYSQL database port to the host machine.

In your Compose file add the port mapping to your db service.

db: 
  ...
  ports:
    - "3306:3306"

I believe you are using the default port based on your Wordpress service.

Then configure your SQL client to 127.0.0.1 and port 3306.

Upvotes: 1

Related Questions