Docker Compose and MySQL DB in a Flash
This article shows you how to get a MySQL DB running in 5 minutes and only 4 easy steps — i.e. In A Flash. This article has nothing to do with with Barry Allen.
Im assuming the following:
- You have Docker installed
- You have Docker Compose installed
If you do not have these installed, check out these articles:
Step 1: Create Directories
Run the following commands to create necessary directories
mkdir -m 777 -p ./data/db ./data/mysql ./data/mounts/
Step 2: (Optional) Additional Configuration
I had a character encoding issue when importing data into the database so I had to have additional config mounted into my countainer.
Copy and run the following command:
echo "[mysqld]
character-set-server = utf8
collation-server = utf8_unicode_ci
skip-character-set-client-handshake" >> ./data/mounts/mycustom.cnf
This should create a file called mycustom.cnf in the ./data/mounts/ directory. It will also echo the above text into the file.
Step 3: The Compose
Create a file called docker-compose.yml and copy and paste the following piece of code.
Run the following command to bring up the containers
docker-compose up -d
You should see the following:
Note:
This compose file includes an additional container called adminer to help connect and view contents of the database
The compose file forwards the default MySQL port (3306) to our own custom port (3308) because we have a lot of MySQL databases
Step 4: Connect to the Database
- Navigate to: http://localhost:8088/
- Login with the database details as specified in the compose file:
- Once you login, you should see contents of your database as shown below, we got nothing.
This article is part of a series called:
“Devil’s in the Database: A Short Story of Docker and Databases”.
This series looks at how to use docker-compose to set up different database.