Docker case analysis: Building a MySQL database service

Docker case analysis: Building a MySQL database service

Use the official MySQL image to build the database service. The current latest version corresponds to version 8.0.19. To prevent compatibility issues in the following steps after the official version is updated, you can specify version 8.0.19 when using the image.

1 Create configuration and data directories

Create a directory on this machine to store MySQL configuration and data.

  • Configuration file directory: ~/docker/conf/mysql
  • Data file directory: ~/docker/data/mysql
docker pull mysql

3 Copy the configuration file from the image to the host

The internal configuration files of the image are stored in the /etc/mysql directory. A temporary container is created to copy the configuration file directory to the local ~/docker/conf/ directory.

# Create a container named mysql docker run --name mysql -e MYSQL_ROOT_PASSWORD=crane -d mysql

# Copy the configuration file in the docker container to the local ~/docker/conf/mysql directory docker cp mysql:/etc/mysql /Users/crane/docker/conf/

# Stop and delete the temporary container docker stop mysql
docker rm mysql

4 Create and run the mysql container

Mount the host configuration and data file directories into the Docker container and execute the following commands in the terminal:

# ~/docker/conf/mysql contains the mysql configuration file # ~/docker/data/mysql contains the mysql data file # The local port 3306 and 33060 are mapped to the 3306 and 33060 ports in docker respectively # The database root password is crane
# -d Run docker in the background
docker run --name mysql -v ~/docker/conf/mysql:/etc/mysql/conf.d -v ~/docker/data/mysql:/var/lib/mysql -p 3306:3306 -p 33060:33060 -e MYSQL_ROOT_PASSWORD=crane -d mysql

5 Host connects to database

5.1 Terminal command connection

After the container is started successfully in the previous step, execute the following command in the host terminal to connect to the database

# Enter the following command and press Enter, enter the database password crane
mysql -uroot -p --protocol=tcp

insert image description here

The above command specifies to use TCP to connect to the database. If you do not add --protocol=tcp, the default connection will be socket, and the following error will be reported

insert image description here

5.2 Database tool connection

You can also connect using database tools, such as DataGrip, to perform related data operations.

insert image description here

insert image description here

6 View mysql log

During the normal operation of the container, you can use the following command to view the log

docker logs -f mysql

Use externally mounted configurations and data when building services to facilitate data backup and migration. If you start a container on another host using the same version of the Docker image and mount the backed-up configuration and data, you will get exactly the same database configuration and data.

Refer to the official MySQL image

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of deploying MySQL on Docker
  • Install and run a MySQL instance on Docker
  • Example of how to build a Mysql cluster with docker
  • Methods for deploying MySQL services in Docker and the pitfalls encountered

<<:  Solution to the conflict between two tabs navigation in HTML

>>:  How to use async and await in JS

Recommend

Installation process of CentOS8 Linux 8.0.1905 (illustration)

As of now, the latest version of CentOS is CentOS...

How to solve the problem that MySQL cannot start because it cannot create PID

Problem Description The MySQL startup error messa...

Using Docker Enterprise Edition to build your own private registry server

Docker is really cool, especially because it'...

Web Design Experience: Efficiently Writing Web Code

Originally, this seventh chapter should be a deep ...

Solve the error problem caused by modifying mysql data_dir

Today, I set up a newly purchased Alibaba Cloud E...

Example of implementing login effect with vue ElementUI's from form

Table of contents 1. Build basic styles through E...

js implements the algorithm for specifying the order and amount of red envelopes

This article shares the specific code of js to im...

Solution to MySql Error 1698 (28000)

1. Problem description: MysqlERROR1698 (28000) so...

JavaScript destructuring assignment detailed explanation

Table of contents concept Array Destructuring Dec...

How to add sudo permissions to a user in Linux environment

sudo configuration file The default configuration...

How to configure jdk environment under Linux

1. Go to the official website to download the jdk...

Detailed tutorial on installing MySQL 8.0.19 in zip version on win10

Table of contents 1. After downloading, unzip it ...

Basic commands for MySQL database operations

1. Create a database: create data data _name; Two...