Technology Sharing

Deploy MongoDB open source document database on Linux system and implement remote access without public IP

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina


💡 recommend

A few days ago, I found a great artificial intelligence learning website. It is easy to understand and humorous. I couldn't help but share it with you.Click to jump to the website

Preface

This article mainly introduces how to quickly deploy MongoDB on Linux Ubuntu system, and use ngrok intranet penetration tool to achieve remote access to local database from public network.

The MongoDB server can run on Linux, Windows, and MacOS platforms. It can store relatively complex data types and supports a very powerful query language that can implement almost all functions of single-table queries in relational databases. It can also index data.

It is more complicated to install MongoDB directly using the package manager. It is relatively simple and fast to install and deploy MongoDB using Docker. The following is a detailed operation process.

0920c95b762166d005b42f74d46e654

1. Install Docker

The operating environment of this tutorial is Linux Ubuntu system. Before starting, we need to install Docker.

Execute the following command in the terminal:

Add Docker source

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo 
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu 
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | 
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Install Docker package

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • 1

Verify that the Docker Engine installation was successful by running the image

sudo docker run hello-world
  • 1

2. Use Docker to pull the MongoDB image

sudo docker pull mongo
  • 1

image-20240221134922055

Then execute the command to view the image:

sudo docker images
  • 1

image-20240221135030232

You can see that the MongoDB image has been successfully pulled.

3. Create and start the MongoDB container

After successfully pulling the MongoDB image, we can use the image to create and run a MongoDB container.

First, create a storage directory /docker_mount/mongodb/data on the host to persist MongoDB data.

You can create it using the following command:

sudo mkdir -p /docker_mount/mongodb/data
  • 1

Then execute the following command in the terminal to run the container:

sudo docker run -d -p 27017:27017 --name mongodb -v /docker_mount/mongodb/data:/data/db -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=password mongo
  • 1

1708500820559

Parameter Description:

  • –name excalidraw: The container name in this example is mongodb, you can name it by yourself.
  • **-e MONGO_INITDB_ROOT_USERNAME=root: **Initialize a user root with root role
  • **-e MONGO_INITDB_ROOT_PASSWORD=password: **The password is password, you can set it yourself.
  • -p 27017:27017: Map the port and map the local port 27017 to the port 27017 inside the container.
  • **-d **: Set the container to run in the background all the time.

Then execute the following command to check whether the container is running:

sudo docker ps
  • 1

image-20240221153852667

You can see that the MongoDB container you just created is running.

4. Local connection test

OK, after the above process, the docker deployment of mongodb is completed. Now we can test the connection through Navicat.

The version of Navicat I use is 16. Create a new mongodb connection as shown below:

The host fills in the host ip, you can enter it in the terminal ip addrcommand to view it.

Port number: 27017

Verification fill in: admin

Fill in the username and password you set when starting the container: root and password

image-20240221154325747

Then click Test Connection, and you can see that the connection is successful!

5. Remotely access the local MongoDB container from the public network

However, we can only connect locally to the MongoDB service that we have just deployed using Docker. What if we are in a different location and want to remotely access the MongoDB container deployed locally but do not have a public IP?

We can use ngrok intranet penetration tool to achieve remote access requirements in an environment without public IP.

5.1 Intranet penetration tool installation

Here are the steps to install ngrok:

ngrok official website address: https://www.cpolar.com

  • Use one-click script installation command
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
  • 1
  • Adding services to the system
sudo systemctl enable cpolar
  • 1
  • Start the ngrok service
sudo systemctl start cpolar
  • 1

After ngrok is successfully installed, access Linux port 9200 on an external browser, that is, [http://server's LAN IP:9200], log in with your ngrok account, and you will see the ngrok web configuration interface after logging in. Then you can configure it in the web management interface.

image-20230831171159175

5.2 Create a remote connection public network address

After logging into the ngrok web UI, click Tunnel Management on the left dashboard - Create Tunnel:

  • Tunnel name: customizable. Be careful not to duplicate the existing tunnel name. This example uses: mongo1
  • Protocol: tcp
  • Local address: 27017
  • Domain type: Random domain
  • Region: Select China VIP

Click创建

image-20240221160200575

Then open the online tunnel list, check and copy the public network address of the TCP protocol,注意tcp://无需复制

image-20240221160228682

Open Navicat or other connection tools, enter the public network address created in ngrok above in the host option, and the port number corresponds to the public network port number generated after the colon in the address.

image-20240221160051207

Then click Test Connection, and you can see that the connection is successful!

summary

For the sake of demonstration, we used the TCP public network address tunnel generated by ngrok in the above operation, and its public network address was randomly generated.

The advantage of this random address is that it is quickly established and can be used immediately. However, its disadvantage is that the URL is randomly generated and this address will change randomly within 24 hours, making it more suitable for temporary use.

If you need to remotely access the local MongoDB service for a long time, but don't want to reconfigure the public network address every day, and you want the address to be nice and easy to remember, then I recommend that you use a fixed TCP address for remote access.

5.3 Remote access using a fixed TCP address

Log inngrok official website background, click Reserve on the left and find the reserved TCP address. Let's reserve a fixed TCP address for remote connection to the MongoDB database.

  • Region: Select China VIP
  • Description: Notes, customizable

Click保留

image-20240221160835462

After the address is successfully reserved, the system will generate a corresponding fixed public network address and copy it.

In this example, the address is: 6.tcp.vip.npolar.cn:13644

Then visit on your browserLinux局域网ip地址+:9200端口, log in to the ngrok web ui management interface.

Click Tunnel Management - Tunnel List on the left dashboard, find the remote database tunnel we created above, and click编辑

image-20240221161016132

Modify the tunnel information and configure the successfully retained fixed TCP address into the tunnel

  • Port type: Change to fixed TCP port
  • Reserved TCP address: fill in the address successfully reserved on the official website,

Click更新

image-20240221161130133

After the tunnel is updated successfully, click the status of the dashboard on the left - online tunnel list, and you can see that the public network address has been updated to a fixed TCP address.

image-20240221161207657

Open the connection tool, enter the fixed TCP address reserved by the official website, the port is the public network port number corresponding to the address, and click Test Connection.

image-20240221161332281
The connection success message indicates success. Now the public network address will no longer change randomly. As long as the tunnel is kept online, we can remotely connect to the MongoDB database in the local area network through this public network address.

The above is the complete process of how to use Docker to deploy MongoDB container in Ubuntu system and combine it with ngrok intranet penetration tool to achieve remote access to local services in intranet from public network. Thank you for watching.