—
1. Check if MongoDB is Installed
To verify if MongoDB is installed on your server:
mongod --version
If MongoDB is installed, this command will return the installed version. If it’s not installed, you’ll see an error indicating that the command is not found.
You can also check if the MongoDB service is running:
sudo systemctl status mongod
If MongoDB is running, you will see “active (running)” in the output.
—
2. Install MongoDB (if not installed)
If MongoDB is not installed and you need it, follow these steps:
Install MongoDB on Debian-based systems (YunoHost is based on Debian):
Import the MongoDB repository key:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Add the MongoDB repository:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/debian $(lsb_release -cs)/mongodb-org/6.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
Update and install MongoDB:
sudo apt update
sudo apt install -y mongodb-org
Start and enable MongoDB:
sudo systemctl start mongod
sudo systemctl enable mongod
—
3. Access MongoDB CLI
If MongoDB is installed, you can access the MongoDB shell using:
mongosh
If mongosh
is unavailable, use the older mongo
command (though it’s deprecated):
mongo
—
4. Set Up a Database via CLI
Once in the MongoDB shell (mongosh
or mongo
):
Create or switch to a database:
use mydatabase
This command switches to a database named mydatabase
. If it doesn’t exist, it will be created when you add data.
Add a collection and insert data:
db.mycollection.insertOne({ name: "example", value: 42 })
This creates a collection called mycollection
and inserts a document into it.
List all databases:
show dbs
List all collections in the current database:
show collections
—
5. Configure MongoDB for Security
Create an Admin User:
In the admin
database, create a user with administrative privileges:
use admin
db.createUser({
user: "admin",
pwd: "yourpassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
Enable Authentication:
Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf
Look for the security
section and enable authorization:
security:
authorization: enabled
Restart MongoDB:
sudo systemctl restart mongod
Log in with Authentication:
When connecting to MongoDB, provide the admin user credentials:
mongosh -u admin -p yourpassword --authenticationDatabase admin
—
Let me know if you need additional help configuring MongoDB for specific YunoHost apps or securing the setup!