Most computer science students need to learn database principles, whether it’s MySQL, Oracle, or SQL Server, which requires a lot of installation time and may even fail. If you want to learn the difference between different versions of a database, it’s almost impossible. In many cases, these skills don’t need to be mastered, which is what we call low marginal utility. In addition to database software, in a Web project, you may need other application services such as Redis, FTP services, etc. Generally speaking, it is not only time-consuming to download and install one by one, but also difficult to reuse the environment. That’s the first thing I’m going to teach you.

Learn the basics of Docker

What is Docker? In short, it is very similar to virtual machine, but the principle is different. Different from VMware, Docker occupies much lower resources, running efficiency and other aspects. So what can we do with it? We can use it to deploy services such as MySQL.

Before we can use Docker, we need to install it firstwww.docker.com/products/do…Take a Docker package, install it, and start it. Type on the command linedocker infoIs displayed, the installation is successful.

Install mysql using Docker

First we open the command line, CMD on Windows, Terminal on Mac. Enter on the command linedocker pull mysqlDownload mysql, as shown below, if you want to specify the version (tag), enter itdocker pull mysql:8For the specific version number, seehub.docker.com/_/mysql, do not write the default islatest Then performdocker image ls -aView all the images, you can seemysql:latest Now we’re going to startmysqlTo create a container, use this method. Containers evolved from images, and one image can create multiple containers. With this feature, it is easy to simulate distributed systems.

docker run --name mysql_latest -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d --restart=always mysql

Create a mysql container named 
      
        and map port 3306 to port 3306 on the host, while running in the background
      
# docker run --name <container-name> -p <local-port>:<container-port> -e MYSQL_ROOT_PASSWORD=<root-password> -d mysql:<tag>

# parameter parsing:
# 
      
        Specifies the name of the container
      
# 
      
        Specifies the local port number
      
# 
      
        Specifies the port number of the container
      
# 
      
        Specifies the password of user root
      
# 
      
        specifies the version not written. Default is' latest '
      
# --name Specifies the container name
The -d container is running in the background
# --restart=always Containers will always start as docker starts
For more parameters, see https://hub.docker.com/_/mysql
Copy the code

So far, you have created a mysql service. Now try to connect, using Navicat in this case.If you want to connect from the command line, typedocker exec -it mysql_latest bashTo enter the command line inside the container. This step does not mean you are entering the mysql service. Currently, it is inside the container of Linux, you can typeuname -aTo view, the current identity can be enteredwhoamiCommand to view. To get inside mysql, you need to typemysql -u root -pMysql > enter the mysql internal command line.At this point, you can use mysql from the command line. If you don’t want the container anymore, type it indocker stop mysql_latestTo stop the container and then enterdocker rm mysql_latestTo delete the container.

summary

You’ll find it easy to create mysql using Docker with almost no errors, and you can add or remove containers at will. In addition to mysql, you can install almost any service you want, such as Oracle, SQL Server, mongoDB, Redis, etc. During the learning process, you can not specify –restart=always to ensure that the service is not automatically started, which will reduce the system resource usage, which is more difficult to handle on the physical machine. In addition to database services, you can also install common services such as WordPress. In the past, to install WordPress, you had to install a bunch of dependencies, including a certain version of PHP above, a certain database, and then create an account. In Docker, only one line of Docker pull wordpress is needed to download, and one line of Docker run –name some-wordpress –network some-network-d wordpress is needed to start the service. Some basic configuration is done on the command line. You can download Nextcloud to build your own web disk, download an FTP service to transfer data between different servers, create a search engine ElasticSearch, or even create a virtual operating system like Ubuntu /centos.

You can even use its advanced use, Docker-compose, which helps you build complex apps. You can also build your app into an image to help you quickly deploy to a cloud server without installing any responsive environment other than Docker itself.