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:

  1. Access your server via SSH.

  2. Run the following command:

    Bash
     
    cat /root/credential.txt
    
  3. 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:

Bash
 
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:

Bash
 
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 running systemctl start mongod.

  • If you are connecting from a remote computer, ensure your server firewall allows traffic on port 27017.

  • Do not share the credential.txt file with unauthorized users, as it provides full administrative access to your database.

這篇文章有幫助嗎? 0 用戶發現這個有用 (0 投票)