To Nha Notes | Aug. 14, 2024, 2:20 p.m.
First, pull the MySQL Docker image:
docker pull mysql:8.0
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
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
To ensure the database was created:
docker exec -it mysql-server mysql -uroot -proot -e 'SHOW DATABASES;'
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.