[Intranet penetration] Realize remote connection to Linux server without public network IP and install and deploy MongoDB database

[Intranet penetration] Realize remote connection to Linux server without public network IP and install and deploy MongoDB database

insert image description here

​ Longing Blog Home Page
In the eyes of the strong, there is no best, only better.
High-quality creator in the field of full-stack development, Alibaba Cloud expert blogger

foreword

insert image description here

MongoDB is a database based on distributed file storage. Written in C++ language, it aims to provide scalable high-performance data storage solutions for WEB applications. Its biggest feature is that the query language it supports is very powerful, and its syntax is somewhat similar to object-oriented query language, which can almost realize most of the functions similar to single-table query of relational database, and also supports indexing of data.

In this tutorial, we will install and deploy the Mongodb database on the Linux centos server, and at the same time use cpolar to penetrate the intranet, so that we can remotely connect and access the Mongodb database even when we are outside.

1. Configure Mongodb source

enter yum.repos.ddirectory

cd  /etc/yum.repos.d 

Create a new download source file

vim  mongodb-org-4.0.repo 

Add the following content to the file and save it

[mngodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.0/x86_64/
gpgcheck=0
enabled=1

image-20230418154404590

2. Install the MongoDB database

Execute the command to install, wait for the installation to complete after execution

yum -y install mongodb-org

Completed appears to indicate that the installation is complete

image-20230418155030584

Check out where it's installed

whereis mongod

You can see the directory of the mongodconf configuration file

image-20230418155146455

Modify mongod.confthe configuration file

vim /etc/mongod.conf

Change bindIp: 127.0.0.1to bindIp: 0.0.0.0, then save

image-20230418155448996

Then start mongoDB, the following series are operation commands

## 启动mongodb :systemctl start mongod.service
## 停止mongodb :systemctl stop mongod.service
## 重启mongodb :systemctl restart mongod.service
## 查看状态mongodb :systemctl status mongod.service
## 设置mongodb开机自启动 :systemctl enable mongod.service

Let's check the status after startup

image-20230418160038472

The default mongodb port is 27017, TCP protocol, as can be seen in our configuration file above. If you are using FirewallD for your firewall, you can use the following command:

sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent

Restart the firewall after setting

sudo firewall-cmd --reload

3. LAN connection test

We use a graphical tool to connect, here we use navicat to test the connection, create a new MongoDB connection in navicat, because we did not enter the account password during installation, so we directly enter the ip address and port, click the test connection and the connection is successful

image-20230418163403956

4. Install cpolar intranet penetration

Above, we installed the mongodb database on local Linux and the LAN test was ok. Next, we realized the remote connection to the MongoDB database in the external public network environment.

cpolar is a great 跨平台tool 内网穿透工具for debugging and development. You can put it in your regular toolbox as a standing tool. It can expose a local server behind a NAT or firewall to the public Internet through a secure tunnel. Support http/https/tcp/tls/ftp protocols, no public network IP is required, use compiled traffic, no need to register domain names to purchase cloud servers. The following are the steps to install cpolar intranet penetration.

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

  • Install commands using one-click scripts
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
  • token authentication

Log in to the background of the cpolar official website, click the verification on the left to view your own authentication token, and then paste the token in the command line:

cpolar authtoken xxxxxxx

20230206171248

  • Add a service to the system
sudo systemctl enable cpolar
  • Start the cpolar service
sudo systemctl start cpolar

Linux centos installs MongoDB database, public network remote connection - cpolar 极点云

5. Configure the public network access address

After successfully starting the cpolar service, we access and log in to the cpolar web UI management interface on the browser, Linux局域网ip地址+9200端口.

After successful login, click Tunnel Management on the left dashboard - Create Tunnel, and create a tcp protocol tunnel pointing to port 27017 of mongoDB:

  • Tunnel name: can be customized, be careful not to duplicate the existing tunnel name
  • protocol: tcp
  • Local address: 27017
  • Domain name type: choose a random domain name for free
  • Region: Select China VIP

click创建

image-20230418164119075

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

image-20230418170956762

6. Public network remote connection

Open navicat or other connection tools, enter the public network address we created above in cpolar, and the port number corresponds to the public network port number behind the address

image-20230418171050519

Click to test the connection, if the connection is successful, it means success

image-20230418171126365

7. Fixed connection public network address

It should be noted that the above steps use a random temporary tcp port address, and the generated public network address is a random temporary address. Although it can be used permanently for free, the public network address will change randomly within 24 hours. Next, we configure a fixed TCP port address for it, which will not change, and there is no need to modify the address repeatedly every day after setting.

To configure a fixed tcp port address, you need to upgrade cpolar to a professional package or above.

Log in to the background of the cpolar official website , 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: It is a note, which can be customized

click保留

image-20230418165620238

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

image-20230418165733635

Configure a fixed tcp address, access it on a browser Linux局域网ip地址+:9200端口, and log in to the cpolar web ui management interface.

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

image-20230418165851431

Modify the tunnel information and configure the successfully reserved fixed tcp address into the tunnel

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

click更新

image-20230418165922827

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

image-20230418165947193

8. Use a fixed public network address to connect

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

image-20230418170227498

If the connection is successful, it means success. Now the public network address will not change randomly. As long as the tunnel is kept online, we can remotely connect to the MongoDB database in the LAN through this public network address.

image-20230418170305053

Reprinted from the cpolar pole cloud article: [Linux server installation and deployment of MongoDB database - no public network IP remote connection "intranet penetration"] (Linux server installation and deployment of MongoDB database - no public network IP remote connection "intranet penetration" - cpolar pole cloud )

insert image description here

Guess you like

Origin blog.csdn.net/qq_24694139/article/details/132215854