No data in MySQL after using Docker

Viewed 38

My task was to use docker to wrap my application from the database into containers. phpMyAdmin was used to monitor the database. Final stack: MySQL + Docker + phpMyAdmin + Java + Hibernate.

My docker-compose.yml file:

version: "3"
services:
 db:
  image: mysql:8.0.18
  restart: always
  environment:
    MYSQL_USER: root
    MYSQL_PASSWORD: root
    MYSQL_ROOT_PASSWORD: root
    MYSQL_DATABASE: usersDB
  ports:
    - '3306:3306'
  volumes:
    - mysql-data:/var/lib/mysql
 phpmyadmin:
   image: phpmyadmin:latest
   restart: always
   ports:
     - '8080:80'
   environment:
     - PMA_ARBITRARY=1
volumes:
    mysql-data:

hibernate.cfg.xml file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/usersDB?createDatabaseIfNotExist=true</property>
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

After running the program and checking the operation of the database, it turns out that the data is written locally in MySQL Workbench, and not on phpMyAdmin. Absolutely empty.

MySQL Workbench

phpMyAdmin

docker ps

1 Answers

Seems the hibernate project sits on your local computer. In the hibernate.cfg.xml file file above I see the database URL specified as jdbc:mysql://localhost:3306/usersDB?createDatabaseIfNotExist=true. This will definitely save data to the local MySQL database. First, you need to change the host port in the line 3306:3306 of the docker-compose.yml file to to something like 3307:3306 if the local MySQL instance is already using port 3306. Next, change the database URL to point to the docker database instance jdbc:mysql://localhost:3307/usersDB?createDatabaseIfNotExist=true

Related