Article directory
Reprinted from Cpolar Lisa article: Linux server installation and deployment of MongoDB database - remote connection without public network IP "intranet penetration"
foreword
MongoDB is a product between relational databases and non-relational databases. It is the most functional among non-relational databases and most similar to relational databases. The data structure it supports is very loose, which is a bson format similar to json, so it can store more complex data types. The biggest feature of Mongo is that the query language it supports is very powerful. Its syntax is somewhat similar to object-oriented query language. It can almost realize most of the functions similar to single-table query of relational database, and it also supports indexing of data.
Let's install the mongoDB database on Linux centos in a few simple steps, and at the same time do intranet penetration, use cpolar intranet penetration to map the local port to the public network, so that the MongoDB database can also be accessed remotely in the public network environment.
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 MongoDB
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
edit a bitmongod.conf配置文件
vim /etc/mongod.conf
Change bindIp: 127.0.0.1
to bindIp: 0.0.0.0
and 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
the TCP protocol, which 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 installed the cpolar internal network penetration tool. Through the TCP public network address of cpolar, we can easily access the MongoDB database remotely without public network IP or Set up your router. Here are the steps to install cpolar.
cpolar official website address: 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
5. Configure the public network access address
After successfully starting the cpolar service, we visit the Linux LAN ip address + port 9200 on the browser, and log in to the cpolar web UI management interface.
After successful login, click Tunnel Management on the left dashboard - Create Tunnel, and create an http 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, pay attention无需复制tcp://
6. Remote connection via public network
Open navicat or other connection tools, enter the public network address we created above in cpolar, the port number corresponds to the port 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, which 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.
- Reserve a fixed tcp address
Log in to the background of the cpolar official website, click Reserved on the left, and find the reserved tcp address. Let's reserve a fixed tcp address for the remote mongoDB:
- 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 the Linux LAN ip address +: 9200 port on the browser, log in to the cpolar web ui management interface, click the tunnel management on the left dashboard - tunnel list, find the tcp 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 address to connect
Open the connection tool, enter the fixed tcp address we just configured successfully, the port is the corresponding address behind the address, and click Test Connection
If the connection is successful, it means success. Now this address will not change randomly. As long as the tunnel is kept online, we can remotely connect to the mongoDB database of the intranet linux in the external public network environment.