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 disabling 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 MongoDB is successfully installed on the server.
Next, set a login account for the database.
As you can see, when we connected to the MongoDB server just now, 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. If others know your IP, they will have all the permissions of MongoDB. 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 following prompt 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 you just created
password: The password you just created
your_server_ip: Your server's IP address
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.