More comprehensive version: install Docker and start MySQL 8 in CentOS 8

1. Speed up

(1) Configure the image accelerator first to avoid pulling MySQL images too slowly

vim /etc/docker/daemon.json

(2) Add in the JSON file

{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}

2. Pull the image of MySQL 8

go Docker Hub Find the MySQL version you need. I found 8.0.25

docker pull mysql:8.0.25

3. Add data volume

(1) What is a data volume

  • A data volume is a directory or file (usually custom) in the host
  • When the container directory and the data volume directory are bound, the other party's modifications will be synchronized immediately

Translated here as:

  • A data volume is a directory or file in CentOS 8
  • After the container directory (in Docker) and the data volume directory (in CentOS 8) are bound, if the contents of the data volume directory in CentOS 8 are modified, the container directory in Docker will also change (even if it is one character in the file)
(2) Creating a data volume directory in CentOS 8
mkdir -p /usr/local/soft/docker-containers/mysql-8.0.25/conf
mkdir -p /usr/local/soft/docker-containers/mysql-8.0.25/logs
mkdir -p /usr/local/soft/docker-containers/mysql-8.0.25/data

PS: the data volume directory can be customized, but the container directory is not recommended

4. Copy profile

(1) Copy my. In MySQL container Content in CNF

There are two replication methods (the second is recommended):

  • 1. Enter my. In the MySQL container CNF, copy its contents
  • 2. Find a direct copy on the network (given directly below)

The first method (you need to create a temporary MySQL container):

cd /etc/mysql
  • Then enter my CNF (vim here must be installed if not installed, refer to the article mentioned above):
vim my.cnf

The second way (presumably more convenient):

# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
 
# Custom config should go here
!includedir /etc/mysql/conf.d/

(2) Paste the copied content into my.com of CentOS 8 On CNF

According to the directory you created:

cd /usr/local/soft/docker-containers/mysql-8.0.25/conf

Create my cnf:

vim my.cnf

Paste the copied content in

(3) Add the required MySQL configuration (omitted as needed)

The additional configurations that can be added are as follows (not comprehensive, welcome to add in the comment area), which can be selected as needed (added to the bottom of [mysqld]):

# Identify Chinese
character-set-server=utf8
# GRANT is not allowed to create a user with a blank password; If the storage engine is disabled or not compiled, automatic replacement of the storage engine can be prevented
sql_mode="NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION"
long_query_time=5
slow_query_log=1
slow_query_log_file=slow.log
# maximum connection
max_connections=3000
# Skip client domain name resolution
skip-name-resolve
# How many requests can be stored in the stack in a short time before MySQL temporarily stops responding to new requests
back_log=384
# Enable the binary log function and the binlog data location
log-bin=mysql-bin
# Mixed mode replication
binlog_format=mixed
#  Control the retention time of binlog log file, expire_logs_days=7, which means that the binlog logs of the last 7 days are retained, and the binlog logs of the previous 7 days will be automatically deleted
expire_logs_days=7
# The synchronization data of mysql contains the server ID, which is used to identify the server from which the statement was originally written
server-id=123

I only add utf8 that one

(4) Preserve

Don't write this

5. Create MySQL container

Create and mount

docker run --name=mysql-8.0.25 \
-p 3306:3306 \
-v /usr/local/soft/docker-containers/mysql-8.0.25/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/soft/docker-containers/mysql-8.0.25/logs:/var/log/mysql \
-v /usr/local/soft/docker-containers/mysql-8.0.25/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--privileged=true \
--restart=always \
-d mysql:8.0.25

6. Enter container

docker exec -it mysql-8.0.25 /bin/bash

7. Log in to MySQL

mysql -uroot -p

Added by harlequeen on Mon, 17 Jan 2022 01:21:46 +0200