To create a MySQL database using Docker Compose, you can follow these steps:
1) Create a new directory for your project and navigate into it.
2) Create a new file called "docker-compose.yml" and open it in your preferred text editor.
3) Add the following content to the "docker-compose.yml" file:
version: '3.8'
services:
db:
image: mysql:latest
container_name: mysql_db
restart: always
environment:
MYSQL_ROOT_PASSWORD: example_password
MYSQL_DATABASE: example_database
ports:
- "3306:3306"
This creates a new Docker Compose project with a single service called "db" that uses the latest version of the MySQL image. It sets the root password to "example" and creates a new database named "mydb" with the user "myuser" and the password "mypassword". It also frees port 3306 so that you can connect to the database from your host computer.
4) Save the file and exit the text editor.
5) Open a terminal window and navigate to the directory where the docker-compose.yml file is located.
6) Run the following command to start the MySQL container:
docker-compose up -d
This command will start the MySQL container in detached mode, which means it will run in the background.
Verify that the container is running by running the following command:
docker ps
You should see a container with the name "mysql_db" and the status "Up".
You have now created a MySQL database using Docker Compose. To connect to the database, you can use any MySQL client and connect to the host "localhost" on port "3306".
That's it You now have a running MySQL database in a Docker container that you can use in your applications.