Detailed process of deploying MySQL with docker (common applications deployed with docker)

Detailed process of deploying MySQL with docker (common applications deployed with docker)

I have introduced it to you before: docker (deploy common applications): docker deploy nginx

Docker deploys mysql:5.7.26

# Download the image docker pull mysql:5.7.26

# View the image docker images|grep mysql

# Start the container image. It is recommended to execute the following docker run command under /usr/local/workspace/mysql: docker run -p 13306:3306 --name my-mysql -v $PWD/conf:/etc/mysql -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.26

   # It is recommended to hard-code the path docker run -p 13306:3306 --name my-mysql -v /usr/local/workspace/mysql/conf:/etc/mysql -v /usr/local/workspace/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.26

Command Explanation:

-p 13306:3306 maps the container's port 3306 to the host's port 3306

--name my-mysql The container is named my-mysql after startup

-v $PWD/conf:/etc/mysql mounts the conf/ directory in the current directory of the host to the /etc/mysql of the container (the conf directory is the configuration file of mysql, it is okay if it is not mounted)

-v $PWD/logs:/logs mounts the logs directory under the current directory of the host to the /logs of the container (the logs directory is the log directory of MySQL, and it has no effect if it is not mounted)

-v $PWD/data:/var/lib/mysql mounts the data directory in the current directory of the host to the /var/lib/mysql of the container (the data directory is the storage path of the data files configured for mysql. It is recommended to mount it to store data. If the container is shut down, the data can be mounted again.)

-e MYSQL_ROOT_PASSWORD=123456 Initialize the root user's password

Check the container startup status

[xxx@xxx-xx-xxx mysql]# docker ps|grep mysql
5291ed3fe987 mysql:5.7.26 "docker-entrypoint.s?? 5 minutes ago Up 5 minutes 33060/tcp, 0.0.0.0:13306->3306/tcp my-mysql

Enter the mysql container

# Login container [root@cbov10-sso55-xxx ~]# docker exec -it my-mysql bash
root@5291ed3fe987:/# ls
bin dev entrypoint.sh home lib64 media opt root sbin sys usr
boot docker-entrypoint-initdb.d etc lib logs mnt proc run srv tmp var
# Login mysqlroot@5291ed3fe987:/# mysql -uroot -p --default-character-set=utf8
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
|mysql |
| performance_schema |
|sys|
+--------------------+
4 rows in set (0.00 sec)

Set up remote login to mysql

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
| localhost | test |
+-----------+---------------+
5 rows in set (0.00 sec)

# Set the root user to log in remotely from anywhere and have all operation permissions for all libraries. (The company must not do this because the exposed attack surface is too large.) This is just for testing.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

# Refresh permissionsmysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

# Exit mysql 
mysql> exit
Bye

Regarding MySQL authorization, you can search Baidu by yourself. Here is the recommendation

mysql permission management

Mysql permission control

Remote login test

Use the database connection tool to connect to the host IP + the port exposed by the host.

When we started the container earlier, -p13306:3306 indicated that the host port 13306 was mapped to the container's port 3306. If our host, that is, the server's IP address is 10.10.10.11

The database connected to 10.10.10.11:13306 should be connected. It is recommended to create a new test database for testing, as follows

Docker, MySQL restart problem (will the data be lost?)

# View container [root@cbov10-sso55-113 mysql]# docker ps | grep mysql
5291ed3fe987 mysql:5.7.26 "docker-entrypoint.s?? 4 hours ago Up 4 hours 33060/tcp, 0.0.0.0:13306->3306/tcp my-mysql

# Stop the container ( 5291ed3fe987 is the mysql container ID)
[root@cbov10-sso55-113 mysql]# docker stop 5291ed3fe987
5291ed3fe987


# Delete container [root@cbov10-sso55-113 mysql]# docker rm 5291ed3fe987
5291ed3fe987

Go to our original mount directory to check

The mount host directory is /usr/local/workspace/mysql,

[root@cbov10-sso55-xxx mysql]# cd data/
[root@cbov10-sso55-xxx data]# ls
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 performance_schema public_key.pem server-key.pem test
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 mysql private_key.pem server-cert.pem sys

The data files are still there! Let's re-execute

# Note that the directory must be consistent with the mounted host directory. The command executed in /usr/local/workspace/mysql for the first time should also be in the same directory this time. # Of course, there will be no problem if you write a fixed path [root@cbov10-sso55-xxx mysql]# docker run -p 13306:3306 --name my-mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.26
74c91431533ebb9bbfd3a1123b3f910f54770a08ad08c3c37cbbb996d29e0428

# Here we can see that the container id has changed [root@cbov10-sso55-xxx mysql]# docker ps |grep mysql
74c91431533e mysql:5.7.26 "docker-entrypoint.s?? 16 seconds ago Up 15 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp my-mysql

# Enter the container [root@cbov10-sso55-xxx mysql]# docker exec -it bash 74c91431533e
Error: No such container: bash
[root@cbov10-sso55-xxx mysql]# docker exec -it 74c91431533e bash
root@74c91431533e:/#mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
|mysql |
| performance_schema |
|sys|
| test |
+--------------------+
5 rows in set (0.00 sec)

Found that the test database is also there! The size of the data file can also be verified before and after the MySQL container is deleted. Readers can try it by themselves.

If the host file is saved properly, the data will not be lost.

illustrate:

In fact, production is much more complicated than this test. It is really difficult to solve the problems of MySQL cluster, master-slave, data synchronization, network, etc. with Docker.

The management of MySQL containers or the management of stateful applications requires a more sophisticated tool, the famous Kubernetes project.

recommend

Docker official website

Kubernetes official website

This is the end of this article about docker deploying mysql (docker deploying common applications). For more related docker deploying mysql content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Deploy .Net6 project to docker
  • Detailed steps for deploying Microsoft Sql Server with Docker
  • Docker deployment MySQL8 cluster (one master and two slaves) implementation steps
  • Process analysis of deploying ASP.NET Core applications on Linux system Docker
  • Example of deploying MySQL on Docker
  • Deploy Asp.Net Core (.Net6) in Docker under Linux CentOS
  • Complete steps for deploying Asp.net core applications with docker
  • Docker deploys Mysql, .Net6, Sqlserver and other containers

<<:  A brief introduction to bionic design in Internet web design

>>:  Understanding and example code of Vue default slot

Recommend

Summary of flex layout compatibility issues

1. W3C versions of flex 2009 version Flag: displa...

Detailed explanation of two quick ways to write console.log in vscode

(I) Method 1: Define it in advance directly in th...

How to load the camera in HTML

Effect diagram: Overall effect: Video loading: Ph...

Causes and solutions for MySQL deadlock

The database, like the operating system, is a sha...

Implementation of CSS scroll bar style settings

webkit scrollbar style reset 1. The scrollbar con...

Solve the problem of yum installation error Protected multilib versions

Today, when installing nginx on the cloud server,...

UDP DUP timeout UPD port status detection code example

I have written an example before, a simple UDP se...

Detailed explanation of the underlying encapsulation of Java connection to MySQL

This article shares the Java connection MySQL und...

KTL tool realizes the method of synchronizing data from MySQL to MySQL

Use ktl tool to synchronize data from mysql to my...

Interpretation of syslogd and syslog.conf files under Linux

1: Introduction to syslog.conf For different type...

In-depth analysis of Nginx virtual host

Table of contents 1. Virtual Host 1.1 Virtual Hos...

A simple tutorial on how to use the mysql log system

Table of contents Preface 1. Error log 2. Binary ...