Write the best Docker installation latest version of MySQL8 (mysql-8.0.31) tutorial (refer to Docker Hub and MySQL official documents)

I. Introduction

  MySQL official installation package download address:
  https://dev.mysql.com/downloads/mysql/

 
   Docker Hub official website:
  https://hub.docker.com/

 
  If you need to understand the installation and deployment of the latest version of MySQL5.7 under Centos7, you can refer to the tutorial [ The latest MySQL-5.7.40 is installed and deployed on the cloud server Centos7.9) ].
 
  This tutorial is written by the author referring to the official documents of Docker Hub and MySQL. After many operations and countless pitfalls, the content cannot be said to be the best in the whole network, but it is definitely not bad. At least it is much more detailed than many blog posts on the Internet. . For example, configuring the encoding set parameters to solve the problem of Chinese garbled characters, there are many ways to deal with it in this tutorial, and other blog posts may only write one. There are many details to pay attention to when installing MySQL8 container in Docker, which is much more troublesome than installing MySQL5.7!
 
  After all, the author has limited professional skills. If there are mistakes in some descriptions or operations in the article, you can comment or add attention to @大白点菜论坛, and everyone can learn from each other and make progress.
 
  本文由 @大白有点菜 原创,请勿盗用,转载请说明出处!如果觉得文章还不错,请点点赞,加关注,谢谢!
 

2. Installation and deployment

 

1. Search the keyword "mysql" in Docker Hub to view the latest stable version of MySQL, including MySQL8 and MySQL5.7 series. The corresponding MySQL page has a tutorial, and readers can also read it by themselves, but it is not very complete. The author's tutorial refers to the official website and has been changed.

(1) Search for "mysql" in Docker Hub .
 
Search "mysql" in Docker Hub
 
(2) Select the official "mysql". The page is an introduction to the version of MySQL in Docker. There is such a rule here: 8.0.31, 8.0, 8, latest, 8.0.31-oracle, 8.0-oracle, 8-oracle, oracle 其实都是同一个版本,它们的镜像ID都是相同, all displayed on the same line in the page are the same version, which the author has verified . There are also operation tutorials on the page, which are not very complete, and only the core operation steps are given.
 
  Introduction to MySQL in Docker Hub: https://hub.docker.com/_/mysql
 
Select the official "mysql" 
Introduction to the version of MySQL in Docker 
MySQL operation tutorial in Docker
 

2. Switch to the "Tags" page, the official list of the latest version and the corresponding command, for example docker pull mysql:latest, pull MySQL最新的版本. In fact docker pull mysql, shorthand, mysql is different from the version number, which is equivalent to the latest version, which will be verified in the following tutorials.

 
Pull the latest version of MySQL
 

3. Back to the topic, first create three directories. When creating a MySQL container, it will be mounted as a container volume (Volume), which is used to share files between Docker and the host (Centos), including configuration files, data files, and log files.

 
  What is a Volume ? docker -vThe " " in the command -vis the volume, and " -v" is just --volumethe abbreviation of " ".
 
  The meaning explained by the official Docker documentation : https://docs.docker.com/storage/volumes/
 
Use -pto create a multi-level directory, that is, mydatacreate mysqla directory under the directory, and create log 、data 、confthree directories under the mysql directory:

mkdir -p /mydata/mysql/log
mkdir -p /mydata/mysql/data
mkdir -p /mydata/mysql/conf

Create multi-level directories

 

4. Pull the MySQL image, you can see that the pulled tag is "latest".

docker pull mysql

Pull the MySQL image

 

5. Know some pre-knowledge points before creating a container to better understand some parameter configurations in the operation process.

(1) The official tutorial documentation of Docker Hub mentions two ways to run new containers: [ 使用自定义的 .cnf 配置文件] and [ 配置选项作为标志传递给mysqld,不用写xxx.cnf配置文件] .
 
Docker Hub official tutorial documents mention two ways to run new containers 
(2) [Important]了解必要的前置知识点,因为后面步骤配置用到的一些参数就以此有关 : In the MySQL8.0 container, both Server and Client have default character sets. If the client-related character set is not utf8mb4, connect to the server to view the data in the table, and the Chinese will be displayed as garbled characters.
 
The client connects to the server to view the data in the table, and the Chinese characters are displayed as garbled characters.
 
  1) Configuration related to Client : character_set_client, character_set_connection, character_set_results, collation_connection. It can be seen that the first three default character sets (character) are latin1, and the last collation (collation) is latin1_swedish_ci. Check the table data when connecting to the server, of course Chinese will display garbled characters.

show variables like 'char%';
show variables like 'collation%';

 
The encoding value corresponding to the Client-related configuration

 
  2) Configuration related to Server : character_set_database, character_set_server, collation_database, collation_server. It can be seen that the values ​​of the first two default character sets (character) are utf8mb4, and the values ​​of the last two collations (collation) are by default utf8mb4_0900_ai_ci, that is to say, the Server configuration in MySQL8 默认的字符集就是 utf8mb4.

show variables like 'char%';
show variables like 'collation%';

 
Please add a picture description
 
  3) " utf8_unicode_ci" and " utf8mb4_general_ci" are more in contact, but utf8mb4_0900_ai_ciwhat is " "?
 
  All three are the default collation rules of MySQL's utf8 encoding. According to the MySQL official website documentation: The default collation rules utf8mb4 between MySQL 5.7 and 8.0 are different ( 5.7版是 utf8mb4_general_ci, 8.0版是 utf8mb4_0900_ai_ci).

  • utf8 encoding: MySQL5.7the default collation isutf8_unicode_ci
  • utf8mb4 encoding: MySQL5.7the default collation is utf8mb4_general_ci, MySQL8.0the default collation isutf8mb4_0900_ai_ci

 
  [The default collation utf8mb4 is different between MySQL 5.7 and 8.0, with a Google translation screenshot attached]:
  https://dev.mysql.com/doc/refman/8.0/en/charset-connection.html#charset-connection-system- variables

 
Screenshot source-1, with Google Translate screenshot 1 
The default collation utf8mb4 is different between MySQL 5.7 and 8.0, with a screenshot of Google Translate
 
 
   [MySQL8.0 server character set and sorting rules, with a screenshot of Google Translate]:
  https://dev.mysql.com/doc/refman/8.0/en/charset-server.html

 
MySQL8.0 server character set and collation, with Google Translate screenshot 1 
MySQL8.0 server character set and collation, with Google Translate screenshot 2
 
   [MySQL5.7 server character set and sorting Rules, with a screenshot of Google Translate]:
  https://dev.mysql.com/doc/refman/5.7/en/charset-server.html

 
MySQL5.7 server character set and collation, with Google Translate screenshot 1
 
MySQL5.7 server character set and collation, with Google Translate screenshot 2
 
   [Character sets and default sorting rules supported by MySQL8.0, 移除了 utf8,新增了 utf8mb3,保留 utf8mb4, with a screenshot of Google Translate] :
  https://dev.mysql.com/doc/refman/8.0/en/charset-charsets.html

 
The character set and default collation supported by MySQL8.0, utf8 is removed, utf8mb3 is added, utf8mb4 is reserved, and a screenshot of Google translation is attached
 
   [Character sets and default collations supported by MySQL 5.7, 有 utf8 和 utf8mb4 , with a screenshot of Google Translate]:
  https://dev. mysql.com/doc/refman/5.7/en/charset-charsets.html

 
The character sets and default collations supported by MySQL5.7 include utf8 and utf8mb4, with a screenshot of Google Translate
 

6. 方法一创建容器: Pass configuration options to mysqld as flags, without writing the xxx.cnf configuration file.

 
Execute the following command to create a mysql container and specify a directory to mount as a container volume, and set the client character set:

docker run --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/data:/var/lib/mysql -v /mydata/mysql/conf:/etc/mysql/conf.d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql --init-connect="SET collation_connection=utf8mb4_0900_ai_ci" --init-connect="SET NAMES utf8mb4" --skip-character-set-client-handshake

Execute the command, create a mysql container and specify the directory to mount as the volume of the container, and set the client character set
 
The character set has been changed to utf8mb4
 
What do the parameters of the above command mean? Use docker run --helpto view the meaning of each parameter:

docker run --help

 
The --name parameter of docker run --help

 
The -v parameter of docker run --help

 
The -p parameter of docker run --help

 
The -e parameter of docker run --help

 
The -d parameter of docker run --help
 

  • --name
       Container name.

  • -v
       The parameter -v is shorthand for --volume list, which mounts the specified folder as the volume of the container (Volume), which is used to share files (log files, configuration files, data files).

  • /mydata/mysql/log
       log directory.

  • /mydata/mysql/data
       data directory.

  • /mydata/mysql/conf
       Configuration file directory.

  • -p 3306:3306
       The parameter -p is shorthand for --publish list, which maps port 3306 to port 3306 of the container and provides the port externally. If multiple mysql containers are started at the same time, the external port numbers can be different, and there will be no conflict between services.

  • -e MYSQL_ROOT_PASSWORD=123456
       Parameter -e is the abbreviation of --env list, set the environment variable, and set the password variable (MYSQL_ROOT_PASSWORD) of the root user to 123456.

  • -d mysql
       The parameter -d is shorthand for --detach, which means that the container runs in the background and prints the container ID. The following mysql can add a version number, such as mysql:latest, mysql:8.0.31 and so on.

  • --init-connect="SET collation_connection=utf8mb4_0900_ai_ci"
       When the Client initializes the connection to the Server, it willcollation_connectionThe collation value is set toutf8mb4_0900_ai_ciand passed as a flag to mysqld. It is equivalent to adding under the [mysqld] location in the my.cnf configuration fileinit-connect=“SET collation_connection=utf8mb4_0900_ai_ci”parameter.

  • --init-connect="SET NAMES utf8mb4"
       When the Client initializes the connection to the Server, set the value of the system variable NAMES to utf8mb4 and pass it to mysqld as a flag. In the official MySQL documentation introduction, setNAMESThe character set is given to the three session system variablescharacter_set_clientcharacter_set_connectioncharacter_set_resultsSet the same character set. It is equivalent to adding under the [mysqld] location in the my.cnf configuration fileinit-connect=“SET NAMES utf8mb4”parameter.

  • --skip-character-set-client-handshake
       Equivalent to the my.cnf configuration file, add under the [mysqld] locationskip-character-set-client-handshakeparameter. A switch for --character-set-client-handshake to ignore client information and use default server characters. As described in the official documentation, in MySQL version 4.0, both the server and the client have a "global" character set, and the server administrator decides which characters to use. But after MySQL4.1, when the client connects to the server, there is a handshake (handshake) [ official document address: connection character set and collation ].
       For versions after MySQL 4.1, when the client connects, it wants to send the specified character set to the server to set the three system variables character_set_client, character_set_connection, and character_set_results. When mysqld starts with–character-set-server=utf8When this configuration is started, it is impossible to control the client character set setting, but MySQL4.0 can do this. In order to preserve the behavior of MySQL4.0, the --character-set-client-handshake switch was born.

 
  [Set the character set of NAMES, with a screenshot of Google Translate]:
  https://dev.mysql.com/doc/refman/8.0/en/set-names.html

 
Set the character set of NAMES, with a screenshot of Google Translate 1
 
Set the character set of NAMES, with Google Translate screenshot 2
 
   [“–skip-character-set-client-handshake” official Document introduction, with Google translation screenshot]:
  https://dev.mysql.com/doc/refman/8.0/en/faqs-cjk.html
  https://dev.mysql.com/doc/refman/8.0/en/ server-options.html#option_mysqld_character-set-client-handshake

 
"--skip-character-set-client-handshake" official document introduction 1
 
"--skip-character-set-client-handshake" official document introduction 2
 
"--skip-character-set-client-handshake" official document introduction 3
 
"--skip-character-set-client-handshake" official document introduction 4
 

7. 方法二创建容器: Use a custom custom.cnf configuration file.

 
(1) /mydata/mysql/conf/Create a custom custom.cnfconfiguration file under the directory. The file name is arbitrary, 文件格式必须为 .cnf.

vim /mydata/mysql/conf/custom.cnf

Create a custom custom.cnf configuration file

 
/etc/my.cnfThere is such a line at the end of   the MySQL default configuration file : !includedir /etc/mysql/conf.d/, which means that new creation in the /etc/mysql/conf.d/ directory 自定义的配置文件 custom.cnfwill also be read, and it is still 优先读取true (the MySQL tutorial document in Docker Hub mentions it).
 
MySQL default configuration file /etc/my.cnf
 
MySQL uses custom configuration file 1 in Docker Hub

 
MySQL uses custom configuration file 2 in Docker Hub

 
(2) Add configuration parameters for container running.

[mysqld]
init-connect="SET collation_connection=utf8mb4_0900_ai_ci"
init_connect="SET NAMES utf8mb4"
skip-character-set-client-handshake

Add configuration parameters for container running

 
(3) Create a container and run it. Some parameters are described in [Method 1 Create Container], so I won’t go into details here.

docker run --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/data:/var/lib/mysql -v /mydata/mysql/conf:/etc/mysql/conf.d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql

Create a container and run 
The character set has been changed to utf8mb4
 

8. Disable the root account from being connected by external tools.

 
  Enter the container, connect to mysql, and delete the record of user="root" and host="%" in the user table of the mysql database. Because this piece of data will allow the root account to be allowed to connect with external tools (such as Navicat or SQLyog), in fact, this should be prohibited. The correct way is to only allow the root account to connect locally. If you want the root account to continue to be connected by external tools, then set the root password to be more complicated, too simple and unsafe!
 
Allow the root account to be allowed to connect with external tools (such as Navicat or SQLyog)
 
Switch to mysql database:

use mysql;

Delete the record with user="root" and host="%" in the user table of the mysql database:

delete user from mysql.user where user='root' and host='%';

Refresh permissions:

flush privileges;

Delete the record with user="root" and host="%" in the user table of the mysql database
 
Prohibit the root account from being allowed to connect with external tools (such as Navicat or SQLyog) 

9. Create a new account for external tools to connect. You can refer to the article [ How to elegantly create a MySQL account for external tools to connect ].

(1) Use , for example, in CREATE 创建账户the corresponding table, it is zyt, it is %, it is dbydc&666, and " " represents any host. use . mysql.user字段user字段host账号密码%GRANT 授予账户特定权限
 
  1) Create a user and password

CREATE USER 'zyt'@'%' IDENTIFIED BY 'dbydc&666';

or

create user 'zyt'@'%' identified by 'dbydc&666';

  2) Grant account-specific permissions. ALL 和 ALL PRIVILEGES 是一样的, which can be abbreviated as ALL.

GRANT ALL ON *.* TO 'zyt'@'%' WITH GRANT OPTION;

or

grant all on *.* to 'zyt'@'%' with grant option;

 
(2) Refresh account permissions.

FLUSH PRIVILEGES;

or

flush privileges;

 
(3) Use the Navicat tool to test the connection, and the account "zyt" is connected normally.
 
Use the Navicat tool to test the connection, the account "zyt" is connected normally

 

10. Set the container to start automatically.

docker update --restart=always mysql

Set the container to start automatically 

11. Start, stop, delete, restart and check the running status of the container.

(1) Start the container

docker start mysql

or

docker start 容器ID

(2) Stop the container

docker stop mysql

or

docker stop 容器ID

(3) Delete the container

docker rm mysql

or

docker rm 容器ID

(4) Restart the container

docker restart mysql

or

docker restart 容器ID

(5) Check the running status of the container
 
To check the running status of all containers 包括运行的和停止的:

docker ps -a

To view the status of all running containers, 不包括停止的:

docker ps

 

12. Create the database and verify functionality.

(1) Create dbydc database, the data tables are tb_account and tb_user.

-- MySQL dump 10.13  Distrib 8.0.31, for Linux (x86_64)
--
-- Host: localhost    Database: dbydc
-- ------------------------------------------------------
-- Server version	8.0.31

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Current Database: `dbydc`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `dbydc` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;

USE `dbydc`;

--
-- Table structure for table `tb_account`
--

DROP TABLE IF EXISTS `tb_account`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `tb_account` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '账户名称',
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `tb_account`
--

LOCK TABLES `tb_account` WRITE;
/*!40000 ALTER TABLE `tb_account` DISABLE KEYS */;
INSERT INTO `tb_account` VALUES (1,'zyt','123456'),(2,'君莫笑','654321');
/*!40000 ALTER TABLE `tb_account` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `tb_user`
--

DROP TABLE IF EXISTS `tb_user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `tb_user` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `tb_user`
--

LOCK TABLES `tb_user` WRITE;
/*!40000 ALTER TABLE `tb_user` DISABLE KEYS */;
INSERT INTO `tb_user` VALUES (1,'大白有点菜',18),(2,'CSDN新人',20);
/*!40000 ALTER TABLE `tb_user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2022-11-18  4:02:16

 
(2) Create dbydc2 database, the data table is tb_user.

-- MySQL dump 10.13  Distrib 8.0.31, for Linux (x86_64)
--
-- Host: localhost    Database: dbydc2
-- ------------------------------------------------------
-- Server version	8.0.31

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Current Database: `dbydc2`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `dbydc2` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;

USE `dbydc2`;

--
-- Table structure for table `tb_user`
--

DROP TABLE IF EXISTS `tb_user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `tb_user` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `tb_user`
--

LOCK TABLES `tb_user` WRITE;
/*!40000 ALTER TABLE `tb_user` DISABLE KEYS */;
INSERT INTO `tb_user` VALUES (1,'大白有点菜666',20),(2,'CSDN新人666',22);
/*!40000 ALTER TABLE `tb_user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2022-11-19  2:33:53

 
(3) Query the table data of the database without entering the container.
 
  Run mysql through the shell, add the command to be executed after the -e parameter, which is equivalent to the command entered after the mysql connection. Here, first switch to the dbydc database, and then query all the data in the two data tables tb_account and tb_user.

docker exec -i mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "use dbydc;" -e "select * from tb_account;" -e "select * from tb_user;"'

Query the table data of the database dbydc without entering the container 1 
  There is another way of writing, that is, you don't need to execute the "use dbydc;" command to switch databases, and you can directly add the dbydc database after "select * from tb_account;".

docker exec -i mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "select * from tb_account;" dbydc -e "select * from tb_user;"'

Query the table data of the database dbydc without entering the container 2 
  How is the syntax for inserting data written? There are double quotation marks in the SQL statement insert into tb_user(name,age) values("西门吃鸡", 36);, you need to use slashes " \" 转义.

docker exec -i mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "use dbydc;" -e "insert into tb_user(name,age) values(\"西门吃鸡\", 36);" -e "select * from tb_user;"'

insert data 

13. MySQL database backup.

 
  MySQL official document backup database:
  https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html#option_mysqldump_databases

 
(1) Create a data backup directorydb_backup

mkdir -p /mydata/db_backup

(2) Back up all databases. The parameter --all-databasesrepresents all databases. The name of the backup sql file is arbitrary, such as all-databases.sql.

docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /mydata/db_backup/all-databases.sql

(3) Back up multiple specified databases at the same time. After the parameter, --databasesyou can specify the database name, and one or more databases are supported. The name of the backup sql file is arbitrary, such as multi-databases.sql.

docker exec mysql sh -c 'exec mysqldump --databases dbydc dbydc2 -uroot -p"$MYSQL_ROOT_PASSWORD"' > /mydata/db_backup/multi-databases.sql

(4) Backup a database. There are two ways here, however,   1) --databases , which can be restored, is best used this way.只有加了参数 --databases 才能被正确地还原!
有参数

docker exec mysql sh -c 'exec mysqldump --databases dbydc -uroot -p"$MYSQL_ROOT_PASSWORD"' > /mydata/db_backup/dbydc.sql

  2) 无参数--databases , cannot be restored, do not use this method. The MySQL official website has written that this can be done, but after the author verified and checked the official website information, this method cannot achieve the restoration effect. The author's professional skills are limited, and it may support restoration, but I can't write such an instruction for the time being.

docker exec mysql sh -c 'exec mysqldump dbydc -uroot -p"$MYSQL_ROOT_PASSWORD"' > /mydata/db_backup/dbydc.sql

The author tried the following command to restore the data, but found that it didn't work at all:

docker exec -i mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "use dbydc;"' < /mydata/db_backup/dbydc.sql

(5) Back up the specified data table. This method has been verified by the author and cannot be restored.

docker exec mysql sh -c 'exec mysqldump dbydc tb_user -uroot -p"$MYSQL_ROOT_PASSWORD"' > /mydata/db_backup/tb_user.sql

 

14. MySQL database restore.

(1) Restore all databases.

docker exec -i mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /mydata/db_backup/all-databases.sql

(2) Restore the collection's database.

docker exec -i mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /mydata/db_backup/multi-databases.sql

(3) Restore a database. Note here, 必须是前面加了 --databases 参数 mysqldump 出来的sql文件才会被正常还原!in fact, using the mysql client to restore the sql file, the sql file must have CREATE DATABASEand USEThese two statements, only the parameters --all-databasesand --databaseswill be loaded normally.

docker exec -i mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /mydata/db_backup/dbydc.sql

 
  [Load the backup sql format file, with Google translation]:
  https://dev.mysql.com/doc/refman/8.0/en/reloading-sql-format-dumps.html

 
Load the backup sql format file 1 
Load the backup sql format file 2 
Load the backup sql format file 3 

Guess you like

Origin blog.csdn.net/u014282578/article/details/127920419