This documentation outlines the steps to retrieve your database credentials and verify that the MongoDB service is running correctly on your server.
1. Retrieving Database Credentials
Your database username and password are stored securely on the server. To view them, follow these steps:
-
Access your server via SSH.
-
Run the following command:
Bashcat /root/credential.txt -
Record the Username and Password provided in the output. These are required for all database connections.
2. Verifying MongoDB Status
To confirm that MongoDB is installed and functioning correctly, you can use the diagnostic script below. This script checks the service status, network port availability, and authentication configuration.
Command:
echo "---- MongoDB FINAL STATUS ----"; \
(mongod --version | head -n 1) || echo "MongoDB: NOT INSTALLED"; \
echo -n "Service: " && systemctl is-active mongod; \
echo -n "Port: " && (ss -tulnp | grep -q 27017 && echo "OPEN" || echo "CLOSED"); \
echo -n "Bind IP: " && ss -tulnp | grep 27017 | awk '{print $5}'; \
echo -n "Auth Enabled: " && (grep -q "authorization: enabled" /etc/mongod.conf && echo "YES" || echo "NO"); \
echo -n "Connection Test: " && (mongosh --eval "db.runCommand({ ping: 1 })" >/dev/null 2>&1 && echo "OK" || echo "FAIL"); \
echo "--------------------------------"
Interpreting the Results:
-
Service: Should display
active. -
Port: Should display
OPEN(typically on port 27017). -
Auth Enabled: Should display
YES -
Connection Test: Should display
OK.
3. Connecting to the Database
Once you have verified the status and retrieved your credentials, you can connect to the MongoDB shell (mongosh) using the following command:
mongosh -u [USERNAME] -p [PASSWORD] --authenticationDatabase admin
Replace [USERNAME] and [PASSWORD] with the credentials retrieved from /root/credential.txt.
Troubleshooting:
-
If the Connection Test returns
FAIL, ensure the MongoDB service is active by runningsystemctl start mongod. -
If you are connecting from a remote computer, ensure your server firewall allows traffic on port
27017. -
Do not share the
credential.txtfile with unauthorized users, as it provides full administrative access to your database.