Install MySQL (including utf8) using Docker on Windows/Mac

Install MySQL (including utf8) using Docker on Windows/Mac

1. Docker installation on Mac

Visit the Docker official website: https://www.docker.com/get-started to download the Mac version. When downloading, we need to log in to the Docker account, so we need to register a Docker account first. This account can also be used to log in when starting the application later.

Log in to your Docker account to download. After downloading, you will get a Docker.dmg file (523.1M). Double-click it to install it. After installation, click the icon to start Docker, as shown below (whaly should be a naughty whale).

After the next step is completed, you can see this icon above , when the container stops jumping, the startup is complete.

In order to pull images more quickly, you can configure it as a domestic image source. Click the icon, select Preferences --> Daemon, and fill in the domestic Docker container proxy address in Registry mirrors. Alibaba Cloud is recommended here, and you can also use NetEase Cloud. Here is a demonstration of how to obtain your own Alibaba Cloud container image service.

Visit Alibaba Cloud's official website https://www.aliyun.com/ and log in to your account. Go to the management console --> Products and Services --> Elastic Compute --> Container Mirroring Service --> Mirror Accelerator, copy your own accelerator address to Registry mirrors, and restart Docker.

2. Docker installation under Win 10 system

Visit the Docker official website: https://www.docker.com. If you have a Docker account, it is best to log in first and click , then click (If you have not logged in before, a login page will pop up here.) Click Get start with Docker Desktop on the pop-up page to start downloading.

Remember the middle option Download the Windows version. The download may be slow here. If the download is slow, you can download it from http://get.daocloud.io/#install-docker-for-mac-windows

Here, the Win 10 system can turn on Hyper-V , and then confirm to restart

Turn Windows features on or off

However, if Hyper-V is enabled, VMware Workstation will prompt the following error when restarting the image and will fail to start. If you want to use it normally, you can cancel this option, and then execute bcdedit /set hypervisorlaunchtype off in PowerShell (Start -> Right click -> Windows PowerShell Administrator) for safety. Restart the system to use VMware Workstation normally.

bcdedit /set hypervisorlaunchtype off 

Finally, double-click the downloaded Docker for Windows Installer.exe to install it.

After the installation is complete, you will be prompted to restart the system. A small Docker icon will appear in the system taskbar. You can follow the prompts to enter in PowerShell (Shift + right click to select):

docker info
docker version
docker ps
docker images
docker run hello-world 

3. Install MySQL

You can also search for the official mysql in the Alibaba Cloud console image search

Select the first one, which contains very detailed installation instructions. You can also see that the supported version (label) is 8.0.12. We will install this version this time.

Before installation, it is recommended to read the description of the "Where to Store Data" section in the document to help us understand why the database data is mapped to the local files of your system by default when installing MySQL in Docker.

There are many ways to install it. Here we just use the following command to install it automatically.

docker run --restart=always --name mysql8.0 -p 3306:3306 -v ~/soft/mysql/datadir:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=**** -d mysql:8.0.12 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

Parameter explanation: For more detailed parameter description, please refer to the official website's description of docker run (docker run Description)

--restart=always Set up automatic container startup after Docker
--name Set the name of this container
-p 3306:3306 The front part is the exposed port, and the back part is the service port inside the container
-v local directory: container path It is best to specify this parameter to explicitly map the container's mount point to a local folder, which will store the container's data, including MySQL data.
-e MYSQL_ROOT_PASSWORD Set the root user's password
-d mysql:tag Specify the version of MySQL to be installed. The tag can select the optional version provided by the official documentation.

View mysql container information

$ docker ps 

Use the tool (Navicat) to connect

The connection is successful. Check the version, which is also the 8.0.12 version we just installed.

[Note] One thing to note here is that the official MySQL Dockerfile uses the Debian system, which does not support language and utf8 character set by default, as shown below:

If you don't use the mysql cli command in docker, you can directly use the official image, which is completely fine. However, if you need to use the mysql cli command in docker, the official one will not be able to input Chinese characters, and the displayed Chinese is garbled. At this time, we visit the Dockerfile.debian officially provided by MySQL (https://github.com/docker-library/mysql), download version 8.0 to the local computer (Dockerfile.debian, docker-entrypoint.sh, and config under version 8.0 need to be downloaded), and modify the Dockerfile as follows. The key point is to add support for utf8 encoding and set the local language environment to en_US.utf8 (this is enough at the beginning).

#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
# The system uses the buster-slim image of Debian FROM debian: buster-slim
 
# Set up uft8 environment RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8
 
# Add the mysql user and group RUN groupadd -r mysql && useradd -r -g mysql mysql
 
RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*
 
# Add gosu to make it easy to downgrade from root, see https://github.com/tianon/gosu/releases for more details
ENV GOSU_VERSION 1.12
RUN set -eux; \
	savedAptMark="$(apt-mark showmanual)"; \
	apt-get update; \
	apt-get install -y --no-install-recommends ca-certificates wget; \
	rm -rf /var/lib/apt/lists/*; \
	dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
	wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
	wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
	export GNUPGHOME="$(mktemp -d)"; \
	gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
	gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
	gpgconf --kill all; \
	rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \
	apt-mark auto '.*' > /dev/null; \
	[ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
	apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
	chmod +x /usr/local/bin/gosu; \
	gosu --version; \
	gosu nobody true
 
RUN mkdir /docker-entrypoint-initdb.d
 
RUN apt-get update && apt-get install -y --no-install-recommends \
# for MYSQL_RANDOM_ROOT_PASSWORD
		pwgen \
# for mysql_ssl_rsa_setup
		openssl \
# FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:
# File::Basename
# File::Copy
# Sys::Hostname
# Data::Dumper
		perl \
# install "xz-utils" for .sql.xz docker-entrypoint-initdb.d files
		xz-utils \
	&& rm -rf /var/lib/apt/lists/*
 
RUN set -ex; \
# gpg: key 5072E1F5: public key "MySQL Release Engineering <[email protected]>" imported
	key='A4A9406876FCBD3C456770C88C718D3B5072E1F5'; \
	export GNUPGHOME="$(mktemp -d)"; \
	gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
	gpg --batch --export "$key" > /etc/apt/trusted.gpg.d/mysql.gpg; \
	gpgconf --kill all; \
	rm -rf "$GNUPGHOME"; \
	apt-key list > /dev/null
 
# Add mysql environment variable ENV MYSQL_MAJOR 8.0
ENV MYSQL_VERSION 8.0.22-1debian10
 
RUN echo 'deb http://repo.mysql.com/apt/debian/ buster mysql-8.0' > /etc/apt/sources.list.d/mysql.list
 
# the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql)
# also, we set debconf keys to make APT a little quieter
RUN { \
		echo mysql-community-server mysql-community-server/data-dir select ''; \
		echo mysql-community-server mysql-community-server/root-pass password ''; \
		echo mysql-community-server mysql-community-server/re-root-pass password ''; \
		echo mysql-community-server mysql-community-server/remove-test-db select false; \
	} | debconf-set-selections \
	&& apt-get update \
	&& apt-get install -y \
		mysql-community-client="${MYSQL_VERSION}" \
		mysql-community-server-core="${MYSQL_VERSION}" \
	&& rm -rf /var/lib/apt/lists/* \
	&& rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \
	&& chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \
# ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime
	&& chmod 1777 /var/run/mysqld /var/lib/mysql
 
#Specify the mount point as /var/lib/mysql, or run docker run -v host directory: mount point in container VOLUME /var/lib/mysql
 
# Config files
COPY config/ /etc/mysql/
COPY docker-entrypoint.sh /usr/local/bin/
# backwards compat
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh
# The function is similar to CMD, but more flexible and complex. After use, the content of CDM will be passed as a parameter to the ENTRYPOINT instruction ENTRYPOINT ["docker-entrypoint.sh"]
 
# Declare the ports exposed at runtime (port1 port2 …). However, this configured port is not exposed by default at runtime and still needs to be specified when running.
EXPOSE 3306 33060
CMD ["mysqld"]
 

Execute the following command to start building the image. Here, the -f parameter is used to specify the Dockerfile file as Dockerfile.debian. If you use the default file name, you do not need to specify it.

docker build -t mysql-utf8:8.0.22 -f ./Dockerfile.debian .

After the build is successful, you can view the mysql image you just built through docker images.

Finally, we use this image to start a container with the following command

docker run --name mysql8.0 -p 33307:3306 -v /u01/docker-mysql/datadir:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root user password -d mysql-utf8:8.0.22 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

After entering the container, I found that utf8 is already supported

If you are installing mysql5.X or 8.X version directly under Linux system, please refer to my other blog Offline installation of mysql 5.7 / mysql 8.0 under Centos7 environment

Reference: https://hub.docker.com/r/_/mysql/

This is the end of this article about installing MySQL (including utf8) using Docker on Windows/Mac. For more information about installing MySQL using Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on group by in MySQL
  • A brief discussion on what situations in MySQL will cause index failure
  • The leftmost matching principle of MySQL database index
  • MySQL joint index effective conditions and index invalid conditions
  • A brief discussion on the invalidation or implicit conversion of MySQL integer and string indexes
  • Installation of mysql5.7 and implementation process of long-term free use of Navicate
  • Detailed explanation of practical examples of implementing simple Restful style API with Gin and MySQL
  • Details on using order by in MySQL
  • Details on using regular expressions in MySQL

<<:  Embed codes for several older players

>>:  Detailed explanation of the group by statement in MySQL database group query

Recommend

Summary of uncommon operators and operators in js

Summary of common operators and operators in java...

Essential conditional query statements for MySQL database

Table of contents 1. Basic grammar 2. Filter by c...

Nginx anti-crawler strategy to prevent UA from crawling websites

Added anti-crawler policy file: vim /usr/www/serv...

What is JavaScript anti-shake and throttling

Table of contents 1. Function debounce 1. What is...

Design Theory: Hierarchy in Design

<br />Original text: http://andymao.com/andy...

Linux kernel device driver Linux kernel basic notes summary

1. Linux kernel driver module mechanism Static lo...

Solution to MySQL unable to read table error (MySQL 1018 error)

1. Error reproduction I can access the MySQL data...

Implementation of nginx flow control and access control

nginx traffic control Rate-limiting is a very use...

How to connect to MySQL remotely through Navicat

Using Navicat directly to connect via IP will rep...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

Detailed explanation of the execution process of MySQL query statements

Table of contents 1. Communication method between...

mysql installer community 8.0.12.0 installation graphic tutorial

This tutorial shares the installation of mysql in...