DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Envoy Proxy’s PostgreSQL and TCP Filters to Collect Yugabyte SQL Statistics
  • How to Run a MySQL Database in a Docker Container
  • How to Repair Corrupt MySQL Database Tables Step-by-Step
  • Harnessing the Power of AWS Aurora for Scalable and Reliable Databases

Trending

  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • Enhancing Avro With Semantic Metadata Using Logical Types
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • The Role of Retrieval Augmented Generation (RAG) in Development of AI-Infused Enterprise Applications
  1. DZone
  2. Data Engineering
  3. Databases
  4. Multiple MySQL Databases With One MySQL Container

Multiple MySQL Databases With One MySQL Container

If you've ever tried to create two MySQL databases inside a single container, you know it can be a pain. Let's see how to get this up and running.

By 
Ravi Isnab user avatar
Ravi Isnab
·
Updated Sep. 27, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
31.3K Views

Join the DZone community and get the full member experience.

Join For Free

Problem Statement

I want to create 2 databases inside one MySQL container and give the user of the first database full access to the 2nd database. With the official MySQL image one can easily create a database and allow a user access to that database. However, creating a 2nd database is not easily provisioned.

Solution

Docker images work on the concept of layers. Each new command, so to speak, creates a new layer, and herein lies our solution.

Simply create a SQL file with the following commands:

# Create second_db database if it doesn't exist
CREATE DATABASE IF NOT EXISTS second_db;
# Grant all privilidges on second_db to org_user
GRANT ALL PRIVILEGES ON second_db.* TO 'org_user' identified by 'org_user_password';

Let's say you created a file called create_second_db.sql in your project folder (or wherever you really want it to be). Then you can mount this file into docker /docker-entrypoint-initdb.d folder. Files in this folder are loaded in alphabetical order at startup time under root user.

Below is the corresponding Dockerfile (that would mount the create_second_db.sql into the /docker-entrypoint-initdb.d) folder:

# Use base mysql image with tag 5.7
FROM mysql:5.7
# Copy our custom SQL file to /docker-entrypoint-initdb.d folder
COPY ./create_second_db.sql /docker-entrypoint-initdb.d/create_second_db.sql

NOTE: You can mount as many SQL files as you would like into that folder.

Once you have created the image (via docker build), when you start the container, that container would have 2 databases created.

Build the image:

docker build -t custom_mysql -f Dockerfile .</code>

Start the container:

docker run --name custom_mysql \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-e MYSQL_USER=org_user \
-e MYSQL_PASSWORD=org_user_password \
-e MYSQL_DATABASE=first_db \
-d custom_mysql

Result:

# rhasija ~/tmp/docker
$ docker exec -it 0fdbf85f6218 /bin/bash
root@0fdbf85f6218:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| first_db           |
| mysql              |
| performance_schema |
| second_db          |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql>

Notice first_db and second_db above.

Boom! The job is done. Let's open the champagne!

If you have any questions, feel free to ask in the comments section.

Docker (software) Database MySQL

Published at DZone with permission of Ravi Isnab, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using Envoy Proxy’s PostgreSQL and TCP Filters to Collect Yugabyte SQL Statistics
  • How to Run a MySQL Database in a Docker Container
  • How to Repair Corrupt MySQL Database Tables Step-by-Step
  • Harnessing the Power of AWS Aurora for Scalable and Reliable Databases

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!