How to run MySQL using docker-compose

How to run MySQL using docker-compose

Directory Structure

.
│ .env
│ docker-compose.yml
│
└─mysql
 ├─config
 │ my.cnf
 │
 └─data

The data directory under the mysql directory is the data directory, where the mysql data tables and binary log files are located. The .env file contains some variables that can be referenced in the docker-compose.yml file using ${variable_name} .

Of course, you can also put the mysql directory somewhere else, but here, for convenience, I put it directly in the same directory as the yml file.

.env File

MYSQL_ROOT_PASSWORD=root
MYSQL_ROOT_HOST=%
MYSQL_DIR=./mysql

MySQL configuration file my.cnf

[mysqld]
character-set-server=utf8mb4
default-time-zone='+8:00'
innodb_rollback_on_timeout='ON'
max_connections=500
innodb_lock_wait_timeout=500

If using the default configuration, this file can be omitted.

docker-compose.yml

version: '3'

services:

 mysql-db:
 container_name: mysql-docker # Specify the name of the container image: mysql:8.0 # Specify the image and version ports:
 - "3306:3306"
 environment:
 MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
 MYSQL_ROOT_HOST: ${MYSQL_ROOT_HOST}
 volumes:
 - "${MYSQL_DIR}/data:/var/lib/mysql" #Mount data directory - "${MYSQL_DIR}/config:/etc/mysql/conf.d" #Mount configuration file directory

Environment variables

  • MYSQL_ROOT_PASSWORD: This is self-explanatory, the password of the root user.
  • MYSQL_USER, MYSQL_PASSWORD: These two variables are optional and create a new user with superuser privileges on the database specified by the MYSQL_DATABASE variable.
  • MYSQL_DATABASE : Specifies a database to be created when the container starts.
  • MYSQL_ALLOW_EMPTY_PASSWORD : Set to yes to allow the root user to have an empty password. (Not recommended)
  • MYSQL_RANDOM_ROOT_PASSWORD: Set to yes to generate a random password for the root user when the container starts, and the password will be displayed to the standard output stream (GENERATED ROOT PASSWORD:......).
  • MYSQL_ONETIME_PASSWORD: literally means a one-time password, set for the root user, the password must be changed after the first login (only supported in versions 5.6 and above).

Running the container

Execute in the docker-compose.yml directory:

> docker-compose up

To run in the background, use docker-compose up -d .

Stop the container:

> docker-compose down

If it is running in the foreground, use: Ctrl + C to stop. Both methods will delete the container after stopping, and the up command must be used to start it next time.

Stop but do not delete the container:

> docker-compose stop

After stopping with the stop command, restart with the start command.

Summarize

The above is the method of running MySQL using docker-compose introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Docker compose deploys SpringBoot project to connect to MySQL and the pitfalls encountered
  • Implementation of docker-compose deployment project based on MySQL8
  • Use dockercompose to build springboot-mysql-nginx application
  • A brief analysis of the problem of mysql being inaccessible when deployed with docker-compose
  • How to build an elk system using docker compose
  • Detailed process of building mongodb and mysql with docker-compose

<<:  How to change the database data storage directory in MySQL

>>:  vue-cropper component realizes image cutting and uploading

Recommend

Detailed explanation of Vue filter implementation and application scenarios

1. Brief Introduction Vue.js allows you to define...

Solve the Docker x509 insecure registry problem

After installing Docker, I encountered the x509 p...

How to configure static network connection in Linux

Configuring network connectivity for Linux system...

Differences and comparisons of storage engines in MySQL

MyISAM storage engine MyISAM is based on the ISAM...

Packetdrill's concise user guide

1. Packetdrill compilation and installation Sourc...

Solution to forgetting mysql database password

You may have set a MySQL password just now, but f...

A brief discussion on VUE uni-app conditional coding and page layout

Table of contents Conditional compilation Page La...

How to use vue-cli to create a project and package it with webpack

1. Prepare the environment (download nodejs and s...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

How to use React slots

Table of contents need Core Idea Two ways to impl...

VMware Workstation Pro installs Win10 pure version operating system

This article describes the steps to install the p...

WeChat applet to save albums and pictures to albums

I am currently developing a video and tool app, s...

Vue computed properties

Table of contents 1. Basic Examples 2. Computed p...

Solution to the problem of mysql master-slave switch canal

After configuring VIP, the error message that app...

Example code for implementing bottom alignment in multiple ways with CSS

Due to the company's business requirements, t...