After the MongoDB database is started, the default is to be used locally only, and the port is 27017, so how can I set it to remote access?
1. Modify the configuration file mongodb.conf
Command:vim/etc/mongodb.conf
bind_ip=127.0.0.1 this line commented out or modified into bind_ip=0.0.0.0, preferably directly commented out, because under the CentOS, using bind_ip=0.0.0.0 will be an error.
Note: After setting up remote access, the password starts auth authorized access, otherwise it is very insecure, the database is vulnerable to attack and is modified in the configuration file as follows:
# Turn on/off security. OFF is currently the default
#noauth = True
Auth = True
--------------------------------------------------------------------------------------------------------------- --------------------------
2. Create a new user name and password in the MONGO database:
Start the database in non-authoritative mode and use MONGO to connect to the database, such as the user and password for which we want to create a new meeting database:
Use meeting
Db.createuser ({User: "Kimkat", pwd: "123456", roles:[{ Role: "Readwriteanydatabase", DB: "Meeting"}]})
Note: If you are adding users to the meeting database, use the meeting first, or you will not be able to connect even if you add it successfully.
How to add a user to MongoDB: https://docs.mongodb.com/manual/reference/method/db.createUser/index.html
MongoDB built-in user role : https://docs.mongodb.com/manual/core/security-built-in-roles/index.html
--------------------------------------------------------------------------------------------------------------- --------------------------------
3. Restart the MongoDB service
Command:/etc/init.d/mongodb restart
2.1 Firewall Open 27017 port
Command:iptables-a input-p tcp-m State--state new-m TCP--dport 27017-j ACCEPT
Note: If it is a cloud server, it is generally not necessary to open the port in the system, as long as the server management interface to open the port.
--------------------------------------------------------------------------------------------------------------- -------------------------------------
4. Remote connection
The ip:116.85.12.51 to be connected
Command:MONGO 116.85.12.51:27017/meeting-u "Kimkat"-P "123456"
This will allow you to connect to 116.85.12.51 's mongodb/meeting database.
It is recommended to use Robo3t,mongovue, or MONGO such as compass, where the official MONGO compass can be exported directly to a CSV file.
The author tries to connect to the database on the cloud server using Mongo.exe in Windows, resulting in a failure because the shell and server do not match.
Setting up remote connections to access the MONGODB database