This article mainly introduces the installation scheme of MongoDB based on Docker containerization. With docker containerization technology, you can quickly create a MongoDB database service with just a few lines of commands. The following is the detailed process for your reference.
Docker install MongoDB
Taking Centos as an example, you need to install the docker environment first:
$ yum install docker
View docker version number:
$ docker -v
Docker version 20.10.17, build 100c701
It can output correctly, indicating that the docker installation is successful.
download mirror
Then come to dockerhub to view the mongo image:
At the time of writing, the latest latest version is 6.0.2:
Pull the image of mongo through docker
the command. If no tag is specified, the default is latest
, that is 6.0.2
:
$ docker pull mongo
View the downloaded image:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest b70536aeb250 13 days ago 695MB
create container
It is very simple to use docker. Start the image as a container to run a service .
The command to start the container is docker run
:
$ docker run
Before starting the mongo container, stop the mongod service of the host to prevent port occupation conflicts:
$ mongod --dbpath=/data/mongodb/data --shutdown
The data directory also needs to be specified when stopping.
Looking at the documentation of the mongo image, the easiest way to start a mongo container is:
$ docker run --name mongo -d -p 27017:27017 mongo
Parameter Description:
--name mongo
: The name of the created container, custom, generally has a corresponding relationship with the image name-d
: Start the container as a daemon-p 2701:27017
: The default port number of MongoDB is 27017. This parameter maps the port of the host to the port of the current mongo container, so that the database service of the container can be accessed through the networkmongo
: Which image to use to create the container. The complete writing is<image:tag>
. Since no tag is specified when downloading the mongo image, that is, the default tag is used, so there is no need to specify it here.
Through the above simple command, a mongo container is created, and the ID of the created container will be returned:
View running docker containers:
$ docker ps
You can see the container ID, name, creation time, port and other information:
At this point a mongo container is already running.
Stop the container from running
Use stop
the command, followed by the container name and container ID:
$ docker stop mongo
$ docker stop 1b5c
Only the first few digits of the ID are needed to identify a unique container, so when using the ID, it is usually abbreviated:
View all containers with the command:
$ docker ps -a
As you can see, the mongo container exited 58 seconds ago:
The host accesses the data service in the container
A container is a small virtual machine. Now, MongoDB is running inside the container. In order to access the data service, you need to use the Mongo client to connect. The Mongo client here refers to a wide range of clients, such as MongoDB Shell, Mongoose in Nodejs, and the graphical tool Robo 3T, etc., all belong to the MongoDB client, as long as they have credentials, they can connect to the running mongod service.
Above , we installed the MongoDB Shell separately in both the Windows system and the server (host) to connect to the MongoDB service. As a client tool, it can be accessed regardless of whether the MongoDB service is running locally, in a container, or on the network .
Since the container service created by default does not have a username and password set, only the address and port number are needed to successfully connect.
Using the host's mongosh
connection:
$ mongosh
It will connect by default mongodb://127.0.0.1:27017
, so the mongod service in the container is found:
External network access to MongoDB service
Open port 27017
Before accessing from the external network, you need to log in to the console of the server and open port 27017 in the security group:
shell tools
To use local mongosh
to connect, you need to specify the IP and port number of the remote host:
$ mongosh --host 101.200.218.201 --port 27017
Or can be shortened as:
$ mongosh mongodb://101.200.218.201:27017
Robo 3T Tools
Robo 3T is an excellent visual client tool that supports MongoDB database, click to enter the official website to download .
The installation process is very simple, so I won't demonstrate it any more.
After successful installation, open Robo 3T and create a new database connection:
In the pop-up window select create
:
Name
You can set a name for this connection in , enter the IP and port number of the server in , Address
and click Test
the button on the lower left to conduct a connectivity test. The following figure indicates a successful connection. Click Save
the button again to save the connection.
Then select this connection and click Connect
the button to connect to the MongoDB service running on the server:
Summarize
This article introduces in detail the process of downloading the mongo image through Docker in the Linux system and creating the mongo container. It also introduces the way the host machine accesses the database service in the container, and also introduces the way to remotely access the database service in the server locally. For the Robo 3T tool, it is very easy to use and is recommended.
Whether the database is running on the host machine or in the container, in fact, they all provide the same services to the outside world. We only need to focus on operating the database.
At present, the MongoDB we have installed has not set a username and password, which is very insecure. So before actually starting to operate the database, we will introduce MongoDB's security strategy in the next article.