Search code examples
mysqldockerdocker-composedbeaverdbconnection

Docker MySQL connection DBeaver


I just finished following this Docker tutorial on youtube:

https://www.youtube.com/watch?v=SXY0MLHP3hA&lc=Ugzp3vKtSOp0rn2lYyd4AaABAg

I was able to create a couple of Docker containers for PHP and MySQL. The file structure is as follows:

>Docker_PHP_MySQL
 >DB
   -Dockerfile
 >src
   -index.php
 >www
   -Dockerfile
 development.env
 docker-compose.yml

The DB Dockerfile:

FROM mysql:8.0

index.php:

<?php
$mysqli = new mysqli('tut07-db', getenv('MYSQL_USER'), getenv('MYSQL_PASSWORD'), 'information_schema');
if($mysqli->connect_error) 
{
  echo 'Connection Error [', $mysqli->connect_errno, ']: ', $mysqli->connect_error;
} 
else 
{
  echo 'MySQLi Connected Successfully!';
}
?>

The www Dockerfile:

FROM php:7.2-apache

RUN docker-php-ext-install mysqli
RUN docker-php-ext-enable mysqli

Here is the development.env file:

MYSQL_USER=sys_admin
MYSQL_PASSWORD=sys_password
MYSQL_ROOT_PASSWORD=root_password

And then finally, the docker-compose.yml file:

version: "3"

networks:
  tut07-frontend:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.10.1.0/24
  tut07-backend:
    driver: bridge
    ipam: 
      driver: default
      config:
        - subnet: 172.10.2.0/23

services:
  tut07-db:
    build: ./db
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - 3306:3306
    networks:
      tut07-backend:
        ipv4_address: 172.10.3.2
    env_file:
      - ./development.env
  tut07-www:
    build: ./www
    ports:
      - 8080:80
    volumes:
      - ./src:/var/www/html/
    networks:
      tut07-backend:
        ipv4_address: 172.10.2.2
      tut07-frontend:
        ipv4_address: 172.10.1.2
    depends_on:
      - tut07-db
    env_file:
      - ./development.env

Now here's where I know I'm going in completely blind...

In dbeaver, I'm trying to establish a connection:

enter image description here

But when I test the connection, I get the following response:

enter image description here

How do I fix this problem?


Solution

  • Instead of giving localhost_compliant-db as the Server Host in dbeaver, try giving it localhost.

    3306 port is bind on the host machine as well hence localhost:3306 from your host machine should work.

    PS - I presume dbeaver and docker compose stack both are on the same machine. If not, you need to map localhost_compliant-db to certain IP which your host machine can understand.