banner
小鱼

小鱼's Blog

Getting Started with MongoDB

# Installing MongoDB and Mongosh#

## Deploying MongoDB on Ubuntu#

### Download the tar package from the official website#

Copy the download link of the tar package from the official website


image

### Installation on Ubuntu#

Execute the command to download the tar package

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.7.tgz

Unzip the package

tar -zxvf mongodb-linux-x86_64-ubuntu2204-6.0.7.tgz

Rename the extracted files and move them to /usr/local/mongodb

mv mongodb-linux-x86_64-ubuntu2204-6.0.7/ mongodb/

### Configuration#

Create the logs and data folders in the directory

mkdir logs
mkdir data

Execute the following command in the logs folder

touch mongodb.log

Then go to the bin directory
Create a new configuration file called mongodb.conf

# Directory for data files
dbpath = /usr/local/mongodb/data
# Directory for log files
logpath = /usr/local/mongodb/logs/mongodb.log
logappend=true
# Port
port = 27017
# Enable as a daemon, running in the background
fork = false
# Authentication mode
auth=true
# Remote connection
bind_ip=0.0.0.0

### Starting in the bin directory#

Execute the startup command

./mongod -f mongodb.conf

To start in the background, add --fork

./mongod --fork -f mongodb.conf


image

## Installing MongoDB Shell#

Download the compressed package

wget https://downloads.mongodb.com/compass/mongosh-1.6.0-linux-x64.tgz

Unzip the package

tar -zxvf mongosh-1.6.0-linux-x64.tgz

Rename the extracted files

mv mongosh-1.6.0-linux-x64 mongosh

The mongosh/bin directory provides the mongosh command to connect to the MongoDB database service.


image

## Adding the bin directory to the PATH environment variable#

Currently, the mongod command is located in the mongodb/bin directory, and the mongosh command is located in the mongosh/bin directory. These two paths need to be added to the Path environment variable.

Execute the command to edit the environment variable

vi ~/.bashrc

Add the following line at the end of the file

PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin:/usr/local/mongosh/bin

Save the file and execute the command to make the configuration effective

source ~/.bashrc

## Starting#

MongoDB can be started using

mongodb --fork -f mongodb.conf

To start mongosh directly

mongosh

mongosh connects to mongodb:localhost:27017 by default


image

## Troubleshooting#

### Permission issues#

However, when inserting data as a regular user (or even as a root user), an error occurs. This is because in the previous configuration file, the auth verification was set to true.


image

To resolve this, auth needs to be changed to false first, and then restart the process.

ps -ef | grep mongodb

kill -9 port_number

After starting, create a super administrator account in the admin collection. Once created, change the auth setting in the configuration file to true and restart the mongo process.

#### Creating a super user#

>use admin

>db.createUser({user:"root",pwd:"XXXXXX",roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"}]})

After creating the user, restart MongoDB and reconnect with mongosh.


image

# Users and Roles#

## Roles#

Built-In Roles

Database User Roles: read, readWrite;

Database Management Roles: dbAdmin, dbOwner, userAdmin;

Cluster Administration Roles: clusterAdmin, clusterManager, clusterMonitor, hostManager;

Backup and Restoration Roles: backup, restore;

All Database Roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase;

Superuser Role: root  
// Several roles indirectly or directly provide access to the system superuser (dbOwner, userAdmin, userAdminAnyDatabase)

Internal Role: __system

Functionality of Specific Roles

Read: Allows users to read from a specified database

readWrite: Allows users to read and write to a specified database

dbAdmin: Allows users to perform administrative functions in a specified database, such as creating and deleting indexes, viewing statistics, or accessing system.profile

userAdmin: Allows users to write to the system.users collection, enabling them to create, delete, and manage users in a specified database

clusterAdmin: Only available in the admin database, grants users administrative privileges for all sharding and replication-related functions

readAnyDatabase: Only available in the admin database, grants users read access to all databases

readWriteAnyDatabase: Only available in the admin database, grants users read and write access to all databases

userAdminAnyDatabase: Only available in the admin database, grants users userAdmin privileges for all databases

dbAdminAnyDatabase: Only available in the admin database, grants users dbAdmin privileges for all databases

root: Only available in the admin database, superuser account with superuser privileges

## Users#

### Creating a Regular User#

// Switch to the admin database
use admin

// Perform auth authentication, returns 1 if successful
db.auth("root","password")

// Switch or create a database, e.g. test
use test

db.createUser({user:"user",pwd:"123456",roles:[{role:"readWrite",db:"test"}]})


image

db.createUser({user:"testadmin",pwd:"123456",roles:[{role:"dbAdmin",db:"test_data"},{role:"readWrite",db:"test_data"},{role:"userAdmin",db:"test_data"}]})


image

### Viewing and Deleting Users#

# Switch to the admin database
use admin

# View all users
db.system.users.find()

# Delete a user. Switch to the database where the account is located before deleting.
db.system.users.remove({user:"testadmin"})


image

The super administrator may not have the permission to delete. Check if the role includes the permission to delete the user.


image

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.