How to Set Up MySQL Database with Docker (2024)

  Рет қаралды 1,838

ProgrammingKnowledge

ProgrammingKnowledge

Ай бұрын

How to Set Up MySQL Database with Docker (Using Docker Command and Docker-Compose)
Setting up a MySQL database using Docker simplifies the process of database management, allowing you to create, configure, and manage your databases in isolated containers. This guide will walk you through the steps to set up a MySQL database using Docker commands and Docker-Compose.
*Requirements:*
- Docker installed on your system
- Basic knowledge of command-line interface
*Step-by-Step Guide:*
Using Docker Command
Step 1: Pull the MySQL Docker Image
First, you need to pull the official MySQL image from the Docker Hub repository.
```bash
docker pull mysql:latest
```
Step 2: Run a MySQL Container
Run a container using the MySQL image with environment variables for the root password and other configurations.
```bash
docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=rootpassword -e MYSQL_DATABASE=mydatabase -e MYSQL_USER=myuser -e MYSQL_PASSWORD=mypassword -p 3306:3306 -d mysql:latest
```
- `--name mysql-container`: Names the container `mysql-container`.
- `-e MYSQL_ROOT_PASSWORD=rootpassword`: Sets the root password.
- `-e MYSQL_DATABASE=mydatabase`: Creates a database named `mydatabase`.
- `-e MYSQL_USER=myuser`: Creates a user named `myuser`.
- `-e MYSQL_PASSWORD=mypassword`: Sets the password for `myuser`.
- `-p 3306:3306`: Maps port 3306 of the container to port 3306 of the host.
- `-d`: Runs the container in detached mode.
- `mysql:latest`: Uses the latest MySQL image.
Step 3: Verify the MySQL Container
Check if the MySQL container is running.
```bash
docker ps
```
You should see your `mysql-container` listed and running.
Step 4: Connect to the MySQL Database
You can now connect to the MySQL database using the MySQL client or any other database management tool.
```bash
docker exec -it mysql-container mysql -u root -p
```
Enter the root password when prompted.
Using Docker-Compose
Step 1: Create a Docker-Compose File
Create a `docker-compose.yml` file in your project directory.
```yaml
version: '3.1'
services:
db:
image: mysql:latest
container_name: mysql-compose-container
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: mydatabase
MYSQL_USER: myuser
MYSQL_PASSWORD: mypassword
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
```
Step 2: Run Docker-Compose
Navigate to the directory containing the `docker-compose.yml` file and run:
```bash
docker-compose up -d
```
- `up`: Creates and starts the containers.
- `-d`: Runs the containers in detached mode.
Step 3: Verify the MySQL Container
Check if the MySQL container is running using:
```bash
docker-compose ps
```
Step 4: Connect to the MySQL Database
Similar to the Docker command method, connect to the MySQL database using the MySQL client:
```bash
docker-compose exec db mysql -u root -p
```
Enter the root password when prompted.
*Conclusion:*
By following these steps, you have successfully set up a MySQL database using Docker commands and Docker-Compose. This setup allows for easy deployment and management of your MySQL databases in isolated containers, making your development and testing processes more efficient.
Don't forget to like, share, and subscribe for more tech tutorials and tips!
#MySQL #Docker #DockerCompose #Database #TechTutorial #HowTo #DatabaseSetup #DockerMySQL #Containerization #DevOps #DatabaseManagement

Пікірлер: 4
@COOKINGBIRD
@COOKINGBIRD Ай бұрын
Thank you
@AdityaRaj-vy9bv
@AdityaRaj-vy9bv 29 күн бұрын
@NewEraTech very informative😍
@dr.chiranjeevikola
@dr.chiranjeevikola 22 күн бұрын
Sir please make a video on how to install matlab on mac m1 air? I am waiting for this video, please upload it as much as possible.
@meowsdummy2734
@meowsdummy2734 22 күн бұрын
funny bots i almost fell for it imagine L
Using docker in unusual ways
12:58
Dreams of Code
Рет қаралды 422 М.
How to create a great dev environment with Docker
29:55
Patrick Loeber
Рет қаралды 256 М.
Best KFC Homemade For My Son #cooking #shorts
00:58
BANKII
Рет қаралды 62 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 9 МЛН
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 11 МЛН
Build a Full Stack Web App in PHP and MySQL with Docker from scratch!
26:09
How to Run MongoDB in Docker
11:44
ScriptBytes
Рет қаралды 15 М.
100+ Docker Concepts you Need to Know
8:28
Fireship
Рет қаралды 877 М.
Nextcloud All In One On Docker On Windows Using Dockge
31:46
KL Tech Videos
Рет қаралды 9 М.
Learn SQL Basics in Just 15 Minutes!
16:57
Kenji Explains
Рет қаралды 130 М.
How to Set Up MySQL Database with Docker
16:46
Abstract programmer
Рет қаралды 39 М.
Docker and PostgreSQL in [10 Minutes]
21:06
Amigoscode
Рет қаралды 267 М.
Docker Tutorial for Beginners [FULL COURSE in 3 Hours]
2:46:15
TechWorld with Nana
Рет қаралды 5 МЛН
The Linux Experience
31:00
Bog
Рет қаралды 645 М.
Best KFC Homemade For My Son #cooking #shorts
00:58
BANKII
Рет қаралды 62 МЛН