-1

I have set up an php laravel sql platform with docker compose with the following docker compose.yml. I only have one database(with production data) but I also want to test out somethings with dummy data, therefore I need to set up another database without going back and forth. What is the most simple and effective way I can do this. I am using Dbeaver for DB management.

services:
  web:
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: web
    working_dir: /var/www/html
    volumes:
      - ./src:/var/www/html
      - ./config/php:/usr/local/etc
    ports:
      - 8080:80
    networks:
      - mfund-network
    depends_on:
      db:
        condition: service_healthy
  db:
    image: mysql:5.7.33
    container_name: db
    environment:
      MYSQL_DATABASE: production
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ALLOW_EMPTY_PASSWORD: yes
    volumes:
      - dbdata:/var/lib/mysql
    ports:
      - 3306:3306
    networks:
      - mfund-network
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h db -P 3306 -u root -p$$MYSQL_ROOT_PASSWORD"]
      interval: 5s
      timeout: 20s
      retries: 30
networks:
  my-network:
    driver: bridge
volumes:
  dbdata:

I have tried creating two database with one connection in Dbeaver with init.sql but I can't seem to switch between the two unless I use docker compose down -v which completely wipes out all the volume and I have to import the production data and set up again. If possible, I don't want to restart the container, I simply want to switch between the databases I will be working on.

-- Create the test database if it doesn't exist
CREATE DATABASE IF NOT EXISTS test;

I want two databases: production and test

1 Answer 1

0

If you want to create two databases, you can put a SQL script in /docker-entrypoint-initdb.d folder of container. Script will be executed the first time the container is started.

Script can be:

-- Create the production database if it doesn't exist
CREATE DATABASE IF NOT EXISTS production;

-- Create the test database if it doesn't exist
CREATE DATABASE IF NOT EXISTS test;

and in your docker-compose.yml, add a volume for above script so it is linked to the /docker-entrypoint-initdb.d folder:

    volumes:
      - dbdata:/var/lib/mysql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

You may have to adapt SQL script in order to create user accounts, and give them right permissions.

Not related to your question: upgrade your MySQL version to 8.4 (has support until 2032), as version 5.7 is not supported since almost 2 years.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.