These are the steps to set up a MySQL environment using Docker on an M2 MacBook Air.
- Create a
.env
file - Edit the Docker Compose file
- Start the MySQL container
- Test connection to the MySQL server
- Shutdown and cleanup
- Setting up
.gitignore
file
-
Create a
.env
file in the same directory as your Docker Compose file. -
Define environment variables such as MySQL root password in this file.
MYSQL_ROOT_PASSWORD=yourpassword MYSQL_DATABASE=mydatabase
-
Create or edit the
docker-compose.yml
file with the following content:version: '3.1' services: db: image: mysql:latest platform: linux/arm64 environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: ${MYSQL_DATABASE} ports: - "3306:3306" volumes: - /path/to/mysql/data:/var/lib/mysql
-
platform: linux/arm64
specifies the MySQL image optimized for ARM64 architecture.
-
Open the terminal and navigate to the directory where the
docker-compose.yml
file is located. -
Run the following command to start the MySQL container:
docker-compose up -d
-
To verify that MySQL has started correctly, connect to the MySQL server with the following command:
mysql -h 127.0.0.1 -P 3306 -u root -p
-
Enter the
MYSQL_ROOT_PASSWORD
value set in the.env
file when prompted for the password.
-
When finished, run the following command to stop the container and free up resources:
docker-compose down
-
Create a
.gitignore
file in the root directory of your project. -
Add
.env
to the.gitignore
file to prevent it from being included in the Git repository.# .gitignore .env
-
After making changes to the
.gitignore
file, run the following commands to reflect them in your Git repository:git add .gitignore git commit -m "Add .env to .gitignore" git push
This completes the steps to set up a MySQL environment using Docker on an M2 MacBook Air. Customize the .env
file and docker-compose.yml
file as needed.