🤖 Ai回答: To set up a MySQL database using Docker Compose, you need to create a `docker-compose.yml` file that defines the necessary services、Below is an example of how to configure MySQL in a Docker Compose setup.
Example `docker-compose.yml`
yaml
version: '3.8'
services:
mysql:
image: mysql:8.0 Specify the version of MySQL you want to use
container_name: mysql_container
environment:
MYSQL_ROOT_PASSWORD: rootpassword Set the root password for MySQL
MYSQL_DATABASE: mydatabase Optional: Create a default database on startup
MYSQL_USER: myuser Optional: Create a user with specific privileges
MYSQL_PASSWORD: mypassword Password for the above user
ports:
3306:3306 Map MySQL port inside the container to the host machine
volumes:
./mysql_data:/var/lib/mysql Persist data outside the container
./init.sql:/docker-entrypoint-initdb.d/init.sql Optional: Run initialization scripts
restart: always Restart policy (optional)
Explanation:
1、Version: The `version: '3.8'` specifies the version of Docker Compose file format.
2、Service Name: We define a service named `mysql`.
3、Image: The `image: mysql:8.0` pulls the official MySQL Docker image (in this case, version 8.0).
4、Container Name: You can specify a custom name for the container (`mysql_container`), but it's optional.
5、Environment Variables:
`MYSQL_ROOT_PASSWORD`: This sets the root password for MySQL.
`MYSQL_DATABASE`: (Optional) Automatically creates a database when the container starts.
`MYSQL_USER` and `MYSQL_PASSWORD`: (Optional) Creates a new user with specific privileges.
6、Ports: Maps the MySQL port (`3306`) from inside the container to the host machine.
7、Volumes:
`./mysql_data:/var/lib/mysql`: Persists the MySQL data outside the container so that data is retained even if the container is removed.
`./init.sql:/docker-entrypoint-initdb.d/init.sql`: Optionally, you can run SQL scripts on container startup by placing them in the `/docker-entrypoint-initdb.d/` directory inside the container.
8、Restart Policy: The `restart: always` ensures that the container will automatically restart after a system reboot or crash.
Steps to Run:
1、Create the `docker-compose.yml` file in your project directory.
2、Create the `init.sql` file (optional):
If you want to initialize your database with some tables or data, you can create an `init.sql` file in the same directory as `docker-compose.yml`.
Example `init.sql`:
sql
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
email VARCHAR(100)
);
INSERT INTO users (username, email) VALUES ('john_doe', 'john@65.hk');
3、Run the following command in the terminal where the `docker-compose.yml` file resides:
bash
docker-compose up -d
This command will start the MySQL container in detached mode (`-d`).
4、Check if the container is running:
bash
docker ps
You should see the `mysql_container` listed.
5、Accessing MySQL:
You can connect to the MySQL server from the host machine using the MySQL client or any other tool like MySQL Workbench、Use the following connection details:
Host: `localhost`
Port: `3306`
User: `root` (or the user defined in `MYSQL_USER`)
Password: `rootpassword` (or the password defined in `MYSQL_PASSWORD`)
6、Stopping the Container:
To stop the MySQL container, run:
bash
docker-compose down
This will stop and remove the containers while keeping the data persisted in the `./mysql_data` volume.
Notes:
Data Persistence: By mounting the `./mysql_data` volume, your database files are stored outside the container, ensuring that they persist even if the container is destroyed or recreated.
Initialization Scripts: Any `.sql` or `.sh` scripts placed in the `/docker-entrypoint-initdb.d/` directory will be executed when the container first starts、This is useful for initializing databases or running migrations.
Security Considerations: Avoid hardcoding sensitive information such as passwords directly in the `docker-compose.yml`、Instead, consider using environment variables or Docker secrets for better security.
By following these steps, you should have a fully functional MySQL instance running via Docker Compose!