Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 857
John Beasley Avatar asked May 01 '19 03:05

John Beasley


People also ask

How to connect MySQL Docker from DBeaver?

To download visit DBeaver Download page. 5. After install finish open DBeaver and connect your docker MySQL server which is running in “localhost” port “3306”, use your root user and password you have defined on previous terminal command. 6.

How do I connect to a MySQL Docker container?

Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

Can DBeaver connect to MySQL?

DBeaver can be used to access any database or cloud application that has an ODBC or JDBC driver, such as Oracle, SQL Server, MySQl, Salesforce, or Mailchimp. Devart DBeaver provides you with the most important features you'd need when working with a database in a GUI tool, such as: SQL queries execution.

Why is Docker useful?

Docker is a tool designed to make it easier for developers to develop, ship, and run applications by using containers. Containers allow devs to package an application with all of its requirements and configurations, such as libraries and other dependencies and deploy it as a single package.


2 Answers

For those who are running DB on different machine, you can do the following:

First, from where the container is, run docker ps to get the containers details including the Container ID

[root@test-001 ~]# docker ps 
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
1b02333fb3b9        tutum/nginx              "/usr/sbin/nginx"        6 weeks ago         Up 7 days           0.0.0.0:80->80/tcp       docker_nginx_1
8c1d234a3731        mariadb                  "docker-entrypoint.s…"   6 weeks ago         Up 7 days           0.0.0.0:3306->3306/tcp   docker_mysql_1

After you get the ID of your database container, run docker inspect CONTAINER_ID to get the related IP address of that container.

[root@test-001 ~]# docker inspect 8c1d234a3731 | grep -i IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.23.0.3",

On Dbeaver, after selecting the DB type on adding new connection window, Go to network settings (SSH, ...) then put your docker machine details. and from the main page, in the Server Host: add the IP you got from the docker inspect command, then your credentials.

This should work

like image 177
Eng7 Avatar answered Sep 29 '22 02:09

Eng7


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.

like image 23
vivekyad4v Avatar answered Sep 29 '22 04:09

vivekyad4v