Article directory
- foreword
- 1. Configure Mongodb source
- 2. Install the MongoDB database
- 3. LAN connection test
- 4. Install cpolar intranet penetration
- 5. Configure the public network access address
- 6. Public network remote connection
- 7. Fixed connection public network address
- 8. Use a fixed public network address to connect
foreword
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.d
directory
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
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
Check out where it's installed
whereis mongod
You can see the directory of the mongodconf configuration file
Modify mongod.conf
the configuration file
vim /etc/mongod.conf
Change bindIp: 127.0.0.1
to bindIp: 0.0.0.0
, then save
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
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
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
- 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创建
Then open the online tunnel list, view and copy the public network address of the tcp protocol,注意tcp://无需复制
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
Click to test the connection, if the connection is successful, it means success
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保留
After the address is successfully reserved, the system will generate a corresponding fixed public network address and copy it down
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编辑
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更新
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.
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.
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.