Start MySQL using Docker

To Nha Notes | Aug. 14, 2024, 2:20 p.m.

1. Pull the MySQL Docker Image

First, pull the MySQL Docker image:

docker pull mysql:8.0

2. Run MySQL in a Docker Container

Run the MySQL container with the required settings:

docker run -d --name mysql-server -p 3306:3306 \
  -e MYSQL_ROOT_PASSWORD=root \
  -e MYSQL_DATABASE=ebdb \
  mysql:8.0

3. Wait for MySQL to be Ready

You can use the wait-for-it.sh script (or a similar script) to wait for MySQL to be fully initialized before proceeding:

/bin/bash scripts/wait-for-it.sh 127.0.0.1:3306 --timeout=60

4. Verify MySQL Database

To ensure the database was created:

docker exec -it mysql-server mysql -uroot -proot -e 'SHOW DATABASES;'

5. Create Additional Databases (if needed)

If you need to create additional databases:

docker exec -it mysql-server mysql -uroot -proot -e 'CREATE DATABASE IF NOT EXISTS another_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'

This setup will start MySQL in a Docker container and make it accessible on port 3306, allowing your application to interact with it just like any other MySQL instance.