Mongodb in Ubuntu 20.04 Installation - working as on Jan 2023
-
# wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb # dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb # wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - # echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list # apt-get update # apt-get install -y mongodb-org # service mongod status
Ref: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
-
# mongo > use admin > db.createUser( { user: "admin", pwd: "1234567890", roles: [ { role: "root", db: "admin" } ] } ) > use nodebb > db.createUser( { user: "nodebb", pwd: "1234567890", roles: [ { role: "readWrite", db: "nodebb" }, { role: "clusterMonitor", db: "admin" } ] } ) > db.createUser( { user: "root", pwd: "1234567890", roles: [ { role: "readWrite", db: "nodebb" }, { role: "db.root", db: "admin" } ] } ) > quit()
Enable security in MongoDB conf file.
File: /etc/mongod.confsecurity: authorization: enabled
Example connection string for node
const { MongoClient } = require('mongodb') const url = 'mongodb://127.0.0.1:27017'; const dbClient = new MongoClient(url); dbClient.connect() const db = dbClient.db('somedb')