Skip to main content

MongoDB

Install

# Support Ubuntu 20.04/18.04
# Get key of version
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

# Ubuntu 20.04:
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

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

# Check MongoDB 5.0/4.4 Release Signing Key
sudo apt-key list

# Install
sudo apt update && apt install -y mongodb-org

# If you wish to install a specific version of MongoDB, you can pass the version as shown below.
sudo apt-get install -y mongodb-org=5.0.2 mongodb-org-database=5.0.2 mongodb-org-server=5.0.2 mongodb-org-shell=5.0.2 mongodb-org-mongos=5.0.2 mongodb-org-tools=5.0.2

# Enable service
sudo systemctl enable --now mongod

# Check service
sudo systemctl status mongod

# Check default port
netstat -tunelp | grep 27017

# List port.
lsof -i | grep mongo

# test connect
mongo --eval 'db.runCommand({ connectionStatus: 1 })'

# You can confirm that everything is working fine from `"ok" : 1.`You can also try to create a test database and insert some dummy data.

sudo systemctl restart mongod

Old 3.6

# Expired from Ubuntu 18.04+.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

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

sudo apt update
sudo apt install -y mongodb-org

sudo systemctl enable mongod.service
sudo systemctl start mongod.service
sudo systemctl restart mongod.service
sudo systemctl status mongod.service
sudo systemctl stop mongod.service

Config

sudo vi /etc/mongod.conf
# Appand new section:
security:
authorization: enabled

# Enable Remote Access
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,<mongodb-server-ip>,<hostname>

# Change ram
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 0.05

Connect

# Local
mongosh

# Test
mongosh --eval 'db.runCommand({ connectionStatus: 1 })'

# With user, password, db
mongosh --host 127.0.0.1:27017 -u "user" -p "password" --authenticationDatabase "mydb"

Admin

# TRAP, must create administrator account with role: "userAdminAnyDatabase" at first with authorization enabled. And then create the other account using administrator.
use admin;

db.createUser(
{
user: "develop",
pwd: "passw0rd",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
);
exit;

# Must last one trancation for db to make instance, or db not exist
# Create access account for database
use dbname;

db.createUser( { user: "user2", pwd: "passw0rd2", roles: [ "readWrite" ] } );

SQL Command

# List db.
show dbs

# Using database.
use <database>;

# User & password
db.auth("username","password");

# Show users.
db.getUsers();

db.collection.stats();

db.getCollectionNames();

# Delete all data.
db.dropDatabase();

db.dropAllUsers();

# Inserts a document into a collection.
db.myCollection1.insertOne( { x : 1 } )

exit;

Maintain

# dbPath: /var/lib/mongodb
vi /etc/mongod.conf

# error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js
rm /var/lib/mongodb/mongod.lock

mongod.service: Main process exited

# Example : /tmp/mongodb-27017.sock
ls /tmp/mongodb-*
rm /tmp/mongodb-*

Windows

mongodump -h localhost -d testdb

mongorestore -h localhost -d testdb dump\testdb

GPG Error - KEYEXPIRED 1507497109

Update ubuntu meet KEYEXPIRED error.

Open SKS OpenPGP Key server http://keyserver.ubuntu.com/

Search=1507497109 Return=EA312927

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927