From 67e0af133bef8ef73214ee404ef8beecf4a30503 Mon Sep 17 00:00:00 2001 From: Fascinated Date: Wed, 28 Jun 2023 12:26:25 +0100 Subject: [PATCH] add mongodb --- guides/ubuntu/home.md | 1 + guides/ubuntu/pages/mongodb-install.md | 181 +++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 guides/ubuntu/pages/mongodb-install.md diff --git a/guides/ubuntu/home.md b/guides/ubuntu/home.md index 6fe9566..755d9a2 100644 --- a/guides/ubuntu/home.md +++ b/guides/ubuntu/home.md @@ -11,3 +11,4 @@ dateCreated: 2023-06-27T11:35:59.640Z # Pages - [Expanding an LVM](/guides/ubuntu/pages/expanding-an-lvm) +- [Installing MongoDB](/guides/ubuntu/pages/mongodb-install) diff --git a/guides/ubuntu/pages/mongodb-install.md b/guides/ubuntu/pages/mongodb-install.md new file mode 100644 index 0000000..8ef2ab1 --- /dev/null +++ b/guides/ubuntu/pages/mongodb-install.md @@ -0,0 +1,181 @@ +--- +title: Installing MongoDB +description: How to install MongoDB on Ubuntu 22.04 +published: true +date: 2023-06-28T12:09:18.672Z +tags: linux, server, ubuntu, mongodb +editor: markdown +dateCreated: 2023-06-28T12:09:18.672Z +--- + +# Installing MongoDB + +This guide provides instructions on how to install MongoDB on Ubuntu 22.04. Installing mongo is a fairly simple process. + +## Updating the System + +- We will start by updating the apt package list. Run the following command: + +```bash +sudo apt update +``` + +This will update the apt package list so that we ensure that the latest versions of packages are available. + +- Next, we will upgrade the packages. Run the following command: + +```bash +sudo apt upgrade +``` + +This will upgrade the packages to the latest versions. + +## Installing required packages + +- Now, we will install some packages that are required. Run the following command: + +```bash +sudo apt install gnupg2 wget +``` + +## Installing MongoDB + +1. Import the MongoDB GPG key. Run the following command: + +```bash +wget -nc https://www.mongodb.org/static/pgp/server-6.0.asc +cat server-6.0.asc | gpg --dearmor | sudo tee /etc/apt/keyrings/mongodb.gpg >/dev/null +``` + +These commands will download the GPG key and import it, which will allow us to verify the packages. + +2. Add MongoDB to the apt sources list. Run the following command: + +```bash +sudo sh -c 'echo "deb [ arch=amd64,arm64 signed-by=/etc/apt/keyrings/mongodb.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" >> /etc/apt/sources.list.d/mongo.list' +``` + +This command will add the MongoDB apt repository to the sources list. + +3. Update the apt package list. Run the following command: + +```bash +sudo apt update +``` + +This will update the apt package list so that we ensure that the latest versions of packages are available. + +4. Install MongoDB. Run the following command: + +```bash +sudo apt install mongodb-org +``` + +This will install the latest stable version of MongoDB. + +5. Start MongoDB. Run the following command: + +```bash +sudo systemctl start mongod +``` + +6. Enable MongoDB so that it will automatically start on system boot. Run the following command: + +```bash +sudo systemctl enable mongod +``` + +7. Verify that MongoDB is running. Run the following command: + +```bash +sudo systemctl status mongod +``` + +You should see something like this: + +```bash +● mongod.service - MongoDB Database Server + Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) + Active: active (running) since Tue 2023-06-27 08:00:00 UTC; 1min 30s ago + Docs: https://docs.mongodb.org/manual + Main PID: 1234 (mongod) + Tasks: 33 (limit: 4915) + Memory: 1.2G + CGroup: /system.slice/mongod.service + └─1234 /usr/bin/mongod --config /etc/mongod.conf +``` + +## Securing MongoDB + +1. Connect to MongoDB, so that we can create an admin user. Run the following command: + +```bash +mongo +``` + +2. Switch to the `admin` database. Run the following command: + +```bash +use admin +``` + +3. Create an admin user. Run the following command: + +```bash +db.createUser( + { + user: "admin", + pwd: "SET ME", # Generate a random string with: openssl rand -base64 32 | cut -c1-32 + roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] + } +) +``` + +4. Exit the mongo shell. Run the following command: + +```bash +exit +``` + +5. Now, we need to secure enable authentication, this will secure the server and prevent unauthorized access. Run the following command: + +```bash +sudo nano /etc/mongod.conf +``` + +6. Find the `security` section. We need to uncomment it, and add the following line below it, it should look like this: + +```bash +security: + authorization: enabled +``` + +7. Save the file and exit the editor. You can do this by pressing `CTRL + X`, then `Y`, then `ENTER`. + +## Optional: Allowing remote connections + +1. Open the MongoDB configuration file. Run the following command: + +```bash +sudo nano /etc/mongod.conf +``` + +2. Find the `net` section and replace the `bindIp` line with the following: + +```bash +bindIp: 0.0.0.0 +``` + +3. Save the file and exit the editor. You can do this by pressing `CTRL + X`, then `Y`, then `ENTER`. + +4. Restart MongoDB. Run the following command: + +```bash +sudo systemctl restart mongod +``` + +You should now be able to connect to MongoDB from a remote machine. + +## Finished + +You have successfully installed MongoDB on Ubuntu 22.04. You can now connect to MongoDB and start using it.