From f5bce8ae801cc81f761721a455ed63b53923bd48 Mon Sep 17 00:00:00 2001 From: Fascinated Date: Wed, 28 Jun 2023 12:35:54 +0100 Subject: [PATCH] add user and db creation section --- guides/ubuntu/pages/mongodb-install.md | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/guides/ubuntu/pages/mongodb-install.md b/guides/ubuntu/pages/mongodb-install.md index 8ef0a5c..02db453 100644 --- a/guides/ubuntu/pages/mongodb-install.md +++ b/guides/ubuntu/pages/mongodb-install.md @@ -152,6 +152,42 @@ security: 7. Save the file and exit the editor. You can do this by pressing `CTRL + X`, then `Y`, then `ENTER`. +## Adding a user / database + +1. Connect to MongoDB, so that we can create a user. Run the following command: + +```bash +mongo -u admin -p --authenticationDatabase admin +``` + +This will ask you for the password that you set earlier in the Securing MongoDB section. + +2. Switch to the database that you want to add a user to eg: `forum`. Run the following command: + +```bash +use forum +``` + +3. Create a user. Make sure to change the `db` to match the database used above. Run the following command: + +```bash +db.createUser( + { + user: "forum", + pwd: "SET ME", # Generate a random string with: openssl rand -base64 32 | cut -c1-32 + roles: [ { role: "readWrite", db: "forum" } ] + } +) +``` + +4. Exit the mongo shell. Run the following command: + +```bash +exit +``` + +You have now created a user for the `forum` database. You can repeat this process for any other databases that you want to create users for. + ## Optional: Allowing remote connections 1. Open the MongoDB configuration file. Run the following command: