Step 1: Configure MongoDB for Authentication
1. Create a MongoDB Configuration File: Open a text editor and
create tiago_srv5.conf with the following content:
yaml
Copy code
storage:
dbPath: /path/to/your/db
net:
bindIp: 127.0.0.1
port: 27017
systemLog:
destination: file
path: /path/to/your/log/mongodb.log
processManagement:
fork: true
security:
authorization: enabled
Replace /path/to/your/db and /path/to/your/log with valid paths on your
system.
2. Save the configuration file.
Step 2: Start the MongoDB Server
1. Open the terminal and start MongoDB using the configuration file:
bash
Copy code
mongod --config /path/to/tiago_srv5.conf
Step 3: Set Up the Admin User
1. Open a new terminal and connect to MongoDB:
bash
Copy code
mongo
2. Switch to the admin database:
javascript
Copy code
use admin;
3. Create an admin user:
javascript
Copy code
db.createUser({
user: "tiago",
pwd: "q1.10-password",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
});
4. Exit the MongoDB shell:
bash
Copy code
exit
Step 4: Demonstrate Security Features
1. Access MongoDB Without Authentication:
o Connect to MongoDB without authentication:
bash
Copy code
mongo
o Try to view data (this should fail):
javascript
Copy code
use test;
db.test.find();
o Try to create data (this should fail):
javascript
Copy code
db.test.insertOne({ name: "Unauthorized Test" });
2. Access MongoDB With Authentication:
o Connect to MongoDB with authentication:
bash
Copy code
mongo -u "tiago" -p "q1.10-password" --authenticationDatabase "admin"
o Create a new database and collection:
javascript
Copy code
use myDatabase;
db.myCollection.insertMany([
{ name: "Document 1" },
{ name: "Document 2" },
{ name: "Document 3" }
]);
o View the data:
javascript
Copy code
db.myCollection.find();