MongoDB is a popular NoSQL database system used for storing and managing unstructured data. Here are the steps to install MongoDB on Ubuntu and set up a database and user:

1. Import the MongoDB public key:

```
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
```

2. Create a MongoDB list file:

```
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
```

Replace "focal" with your Ubuntu version (e.g. "bionic" or "xenial").

3. Update the package index:

```
sudo apt update
```

4. Install MongoDB:

```
sudo apt install -y mongodb-org
```

5. Start the MongoDB service and enable it to start automatically on boot:

```
sudo systemctl start mongod
sudo systemctl enable mongod
```

6. Verify that MongoDB is running:

```
sudo systemctl status mongod
```

7. Create a MongoDB user and database:

```
mongo
```

This will open the MongoDB shell.

```
use admin
db.createUser({
user: "admin",
pwd: "password",
roles: [{ role: "root", db: "admin" }]
})
```

Replace "password" with a strong password of your choice.

```
use mydatabase
db.createUser({
user: "myuser",
pwd: "password",
roles: [{ role: "readWrite", db: "mydatabase" }]
})
```

Replace "mydatabase" and "myuser" with the names of your database and user, and "password" with a strong password of your choice.

8. Exit the MongoDB shell:

```
exit
```

9. Configure MongoDB to use authentication:

```
sudo nano /etc/mongod.conf
```

Uncomment the following line:

```
security:
authorization: enabled
```

Save the file and exit the text editor.

10. Restart the MongoDB service:

```
sudo systemctl restart mongod
```

You have successfully installed MongoDB on Ubuntu and set up a user and database with authentication. You can now connect to the database from your application using the MongoDB connection string:

```
mongodb://myuser:password@localhost:27017/mydatabase
```

Replace "myuser", "password", and "mydatabase" with the names of your user and database, and the password you chose.

Je li Vam ovaj odgovor pomogao? 14 Korisnici koji smatraju članak korisnim (53 Glasovi)