跳到主要内容
版本:0.17.0+

mongodb-howto

install mongodb in Ubuntu

reference:

In KeJie's ubuntu operating system, I saw that it's Ubuntu 18.04 (Bionic) when I use the command lsb_release -dc, and I had to use this command echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list as it suggested in the official reference.

Then, when I used sudo apt-get install -y mongodb-org to install mongodb, it's sad to find that the downloading speed is rather slow.

picture 18

Hence, I turned for help of a Chinese mongodb mirror which leads me to the Tsinghua mirror website at mongodb | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror, then it booms~

picture 19

configure mongodb remote connection via 0.0.0.0

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock

reference:

Mongodb create superuser

db.createUser({user:"admin", pwd:"admin",roles:[{role:"root","db":"admin"}]})
# db.createUser({user:"admin", pwd:"admin",roles:[{role:"root","db":"admin"}],authenticationRestrictions:[{clientSource:["127.0.0.1"]}]})

Mongodb Auth JS

const {MongoClient} = require('mongodb');

const user = 'admin';
const pswd = 'admin';
const auth_db_name = 'admin';
const db_name = 'hjxh_express_match';
const coll_items_name = 'items';
const mongo_uri = '101.43.125.199:27017';

const uri = `mongodb://${user}:${pswd}@${mongo_uri}`;
const client = new MongoClient(uri);

// Function to connect to the server
async function run() {
try {
// Connect the client to the server
await client.connect();
// Establish and verify connection
await client.db(db_name).command({ping: 1});
console.log('Connected successfully to server');
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);

reference:

Mongodb Auth PY

approach 1

from pymongo import MongoClient
uri = "mongodb://{user}:{pswd}@{host}/?authSource={auth_db}"
client = MongoClient(uri)

approach 2

import urllib.parse
username = urllib.parse.quote_plus('user')
password = urllib.parse.quote_plus('pass/words')
MongoClient('mongodb://%s:%s@127.0.0.1' % (username, password))

approach 3

client = MongoClient(
'example.com',
username='user',
password='password',
authSource='the_database',
)