Open the application store of the Baota panel and search for mongodb#
-
Install mongodb and wait for the installation to complete
-
After the installation is complete, configuration is required. The bindip needs to be set to 0.0.0.0, otherwise, you can only connect locally on the server and cannot remotely connect to the database.
-
After the installation is complete, you need to disable the firewall of the Baota panel or allow the corresponding 27017 port for mongodb.
-
After closing the firewall of Baota, you also need to allow the corresponding 27017 port in the Alibaba Cloud or Tencent Cloud backend. You can search for tutorials on how to allow the port.
Test Connection
mongo mongodb://your_public_ip:27017
You need to have mongodb environment locally
If the following prompt appears, it means that the connection is successful, indicating that the mongodb installation on the server is successful.
Next, set a login account for the database.
As you can see, when we just connected to the mongodb server, we did not enter a username and password. You only need to know your public IP to connect because the mongodb database generally uses port 27017. Others only need to know your IP to have all mongodb permissions. Therefore, we need to set a username and password for mongodb and enable login authentication.
Step 1: Open the command line of the server and connect to mongodb#
Step 2: Enter the admin database#
Enter the command "use admin" to enter the admin database
Step 3: Create an admin user#
Command to create a user:
db.createUser({user: "root",pwd: "12345678", roles: [ { role: "root", db: "admin" } ]})
db.createUser({user: "admin",pwd: "12345678", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
Create a role for a specific database
use mydata
db.createUser({user:'username',pwd:'123456',roles:['readWrite']})
Copy after logging in
Verification
db.auth('username', '123456')
user: The username you need to use to connect to the database
password: The password you need to use to connect to the database
Step 4: Create a root user#
Step 5: Check if the creation is successful, these two users must be created#
Enter the command:
show users
If the prompt below appears, it means that the creation is successful
Step 6: Enable user authentication#
Go back to the Baota panel, open the mongodb settings, and modify the configuration file to enable user authentication.
Now the configuration is complete
Connection path to the database:
mongo mongodb://username:password@your_server_ip:27017/your_database?authSource=admin
username: The username that was just created
password: The password that was just created
your_server_ip: Your server IP
your_database: The database you want to connect to
?authSource=admin: Verify the database and specify which database to authenticate the user from. Since we created it in the admin table, it should be admin.