Mac M series chip (M1/M2) Docker installs MySQL and persists data and configuration

This article is based on MySQL5.7.42 for explanation

Find MySQL images in the Docker registry

DockerHub mirror warehouse-mysql mirror

Pull the MySQL image 

Common commands (not applicable), prompt no matching manifest for linux/arm64/v8 in the manifest list entries

docker pull mysql:5.7.42

 Go to DockerHub to view the mirror image of MySQL5.7.42, you can see that there is no mirror image of linux/arm64/v8 version

 Therefore, we have to find another way to add the parameters of the specified architecture to pull

docker pull --platform linux/amd64 mysql:5.7.42

--platform linux/amd64: When Docker pulls the image, specify the version of the selected architecture to pull

view mirror

 In the image interface on the application side and when starting the image, Docker will detect that the architecture supported by the image is inconsistent with the CPU architecture of the computer, just ignore it (M1/M2 chips also support linux/amd64 version)

Start the mirror and view MySQL specific information

# 启动镜像并进入容器
docker run -it mysql:5.7.42 /bin/bash
# 查看MySQL版本
mysql --version
# 查找 Docker 内,MySQL 配置文件 my.cnf 的位置
mysql --help | grep my.cnf

View the contents of the MySQL configuration file my.cnf (as follows)

bash-4.2# cat my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

The default configuration does not specify a character set. At this time, the default character set is latin1 , which may cause some Chinese characters to be garbled, so we can modify the configuration file and add the default character set (see the next section)

Run the image and configure persistence

Prepare

1. The host computer creates a data storage directory (it can be other locations, /Users/leo is the user directory on the author's Mac, which is equivalent to the ~ directory, and the reader can adjust the directory by himself)

mkdir /Users/leo/docker/mysql
mkdir /Users/leo/docker/mysql/conf
mkdir /Users/leo/docker/mysql/data
mkdir /Users/leo/docker/mysql/logs
mkdir /Users/leo/docker/mysql/mysql-files

2. Create a new my.cnf configuration file and place it in the /Users/leo/docker/mysql/conf directory

my.cnf is configured as follows:

[client]
 
default_character_set=utf8
 
[mysql]
 
default_character_set=utf8
 
[mysqld]
 
character_set_server=utf8

run container

docker run --restart=always -p 3306:3306 --name mysql \
-e MYSQL_ROOT_PASSWORD="123456" \
-v /Users/leo/docker/mysql/conf:/etc/mysql/conf.d \
-v /Users/leo/docker/mysql/logs:/var/log/mysql \
-v /Users/leo/docker/mysql/data:/var/lib/mysql \
-v /Users/leo/docker/mysql/mysql-files:/var/lib/mysql-files \
-d mysql:5.7.42

Parameter Description:

  • docker run: run the Docker image
  • --restart=always: When docker restarts, the container will also restart
  • -p: Mapping port number, the host port is mapped to the internal port of the container (that is, accessing port 3306 of the host can access port 3306 of the container)
  • –name mysql: set the container name to mysql
  • -e MYSQL_ROOT_PASSWORD="123456": Specify the environment variable (  root the password of the initialized user is  123456)
  • -v /Users/leo/docker/mysql/conf:/etc/mysql/conf.d: Map the host's my.cnf file to the Docker container
  • -v /Users/leo/docker/mysql/logs:/var/log/mysql: Mount the /var/log/mysql directory of the container to the /Users/leo/docker/mysql/logs directory of the host
  • -v /Users/leo/docker/mysql/data:/var/lib/mysql:同上
  • -v /Users/leo/docker/mysql/mysql-files:/var/lib/mysql-files:同上
  • -d mysql:5.7.42: Select the running Docker image and specify the Tag (if not specified, the default is latest)

test

# 进入名为mysql的容器
docker exec -ti mysql bash
# 命令行连接MySQL
mysql -uroot -p'jY%kng8cc&'
# 查看字符集
show variables like 'char%';

reference article

1.  Install and start MySQL using Docker

2. DockerHub-MySQL

Guess you like

Origin blog.csdn.net/Tan_LC/article/details/130680474