Running multiple MySQL instances on different ports (mapped by docker port)

Time:2020-10-26

requirement analysis

In the team’s project, part of the3306As the MySQL port, the other part is to use the3307

In general, MySQL installed directly can only serve one port.

In this way, if you encounter different projects, you can either change the MySQL service or the database port of the project. However, neither method is convenient

Running multiple MySQL instances on different ports (mapped by docker port)

Docker can solve this problem perfectly.

Using docker can achieve: running multiple MySQL instances on one device at the same time, and these instances will not interfere with each other.

Running multiple MySQL instances on different ports (mapped by docker port)

Let’s look at it from two aspects: Theory and operation.

What is docker?

You don’t need to understand virtualization technology, just use itvirtual machineYou can understand docker.

virtual machine

First of all, what is a virtual machine

In the existing system, simulate a group of “virtual” hardware, and then install a set of operating system on this hardware.

The system in the virtual machine and the host system areIsolated from each otherYes.

Running multiple MySQL instances on different ports (mapped by docker port)

exceptThe hardware of virtual machine is simulatedIn addition, other aspects,There is no difference between a virtual machine and a real system

Virtual machines have their ownfile system, ownthread , ownnetwork card, ownIP, ownport… it has everything.

For example, the 3306 port in the physical machine will not affect the 3306 port of the virtual machine;
For example, if the HTTP service is enabled on port 80 of the virtual machine, it has nothing to do with port 80 of the host

But if you want tovirtual machinebyHost computerHow to provide service ports?

  • Port mapping.

Port mapping

You don’t have to understand the concept. Just give me an example.

Suppose the host name of the physical machine islocalhost, IP of virtual machine is 192.168.1.100

Normally, if we passlocalhostDomain name, can only accessHost computerCan not be directly accessedvirtual machineThe port of the.

Running multiple MySQL instances on different ports (mapped by docker port)

But what happens if you map 3306 of a virtual machine to 3307 of a physical machine?

When we type againlocalhost:3307It is equivalent to visiting192.168.1.100:3306

Running multiple MySQL instances on different ports (mapped by docker port)

We can access the virtual machine directly through the port that we don’t know.

Docker

If we put“Virtual system”This word is replaced by“container
The virtual machine’s“Installation disk”Change to“image
Well, this is it“virtual machine”That isDocker

Running multiple MySQL instances on different ports (mapped by docker port)

DockerIt’s not a virtual machineHowever, docker has most of the features of virtual machines.

So we don’t have to studyWhat’s the difference between themBut through researchThe similarities between themCome ontransferWhat we have.

After reading the introduction of virtual machine just now, we can not only use our feet to think, but also understand:Docker also has independent ports

Running multiple MySQL instances on different ports (mapped by docker port)

Realize MySQL “double open”

With the above theory, it is easy to create multiple MySQL running on different ports.

Running multiple MySQL instances on different ports (mapped by docker port)

We use the docker container to create unlimitedAs like as two peasMysql.

Then, port mapping is used to map to different ports of host to achieve the effect of “double opening”.

(it should be noted that these containers are independent, and changing container 1 does not affect container 2)

operation

Install docker

Whether you are Mac, Linux or windows, you can install docker quickly through the terminal.

(it is recommended to use the simplest way to install docker. It does not need to waste too much time during installation. After installation, you can not log in.)

Pull image

What is mirror image?

As mentioned just now, it is equivalent to installing the system“CD”。 We can install many on many computers through this CDAs like as two peasSystem.

And the mirror image? With this image, you can create manyAs like as two peasThe container, also equivalent to the installation of manyAs like as two peasSystem.

Enter in the terminal:

//Pull mysql5.7 image
docker pull mysql:5.7

//Or pull the latest MySQL image
docker pull mysql

Wait for the reading bar to complete. This MySQL“Installation disk”It was downloaded to our computer.

Build container

Just now, a container is equivalent to a virtual machine system. So, to create a container is to create a container in the“Installation system”Since you are installing the system, you need to use the “installation disk” just now.

Terminal input:

//Replace various attributes with your own!
Docker run - ID -- name = [container name] - P [host port]: [container port] - e MySQL\_ ROOT\_ Password = [MySQL password] [image name]

//Sample code
docker run -id --name=mysql3307 -p 3307:3306 -e MYSQL\_ROOT\_PASSWORD=123456 mysql:5.7

It is worth noting that 3307:3306 in the command is “port mapping”, that is to map the 3306 of the container to the 3307 of the local computer. Be careful not to write it backwards!!

Modify MySQL login password

Many test environments log in with a null root password, so we change the MySQL in the container to an empty password.

Since the container is an independent environment, it has its own terminal, so we need to switch from the local terminal to the container terminal.

Input in the terminal and enter the container:

//Change the container name to its own
Docker exec - it [container name] Bash

//Sample code
docker exec -it mysql3307 bash

After input, you can find that the prefix of the terminal has changed
Running multiple MySQL instances on different ports (mapped by docker port)

Indicates that it has entered the terminal of the container.

Next, enter the MySQL environment

//Old rules, change to your own
MySQL - u [user name] - P [password]
//Instance code
mysql -uroot -p123456

We find that the prefix of the terminal has changed again
Running multiple MySQL instances on different ports (mapped by docker port)

In this case, enter:

//Run root to log in with a blank password
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;

The following figure shows that the setting is successful, and your database is ready for useRoot empty passwordvisitlocalhost:3307Yes.
Running multiple MySQL instances on different ports (mapped by docker port)

Enter exit to exit the container and return to the local terminal:
Running multiple MySQL instances on different ports (mapped by docker port)

Open the second container

Port 3307 was activated just now. So if we want to open another 3306 port, how to operate? ——Just repeat it.

//Build container
docker run -id --name=mysql3306 -p 3306:3306 -e MYSQL\_ROOT\_PASSWORD=123456 mysql:5.7

//Enter the container
docker exec -it mysql3306 bash

//Enter MySQL
mysql -uroot -p123456

//Allow root to log in from any location
GRANT ALL PRIVILEGES ON \*.\* TO 'root'@'%' IDENTIFIED BY '' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;

After one operation, as like as two peas, we got two Mysql, except for ports.

extend

After that, you can start MySQL on different ports dynamically according to your own requirements.

For example, in the sample code, you can usedocker start mysql3306Start the MySQL of 3306 and use thedocker start mysql3307Start MySQL of 3307.

When you don’t use it, you candocker stop mysql3307Turn it off. It’s very flexible.

summary

Docker is a good thing!

Author: Liu Yuxuan, mengyunzhi development team, Hebei University of Technology

Recommended Today

Layout of angular material (2): layout container

Layout container Layout and container Using thelayoutDirective to specify the layout direction for its child elements: arrange horizontally(layout=”row”)Or vertically(layout=”column”)。 Note that if thelayoutInstruction has no value, thenrowIs the default layout direction. row: items arranged horizontally.max-height = 100%andmax-widthIs the width of the item in the container. column: items arranged vertically.max-width = 100%andmax-heightIs the height of the […]