mongodb搭建教程

cooolr 于 2021-07-09 发布
$ mongo
# 创建管理员账号 2.6及一下版本用db.addUser
> use admin
> db.createUser({ user: "admin", pwd: "password", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
> db.createUser({user: "root",pwd: "password", roles: [ { role: "root", db: "admin" } ]})
# 创建用户自己的数据库message和管理角色message
> use message
> db.createUser({user: "message",pwd: "password",roles: [ { role: "dbOwner", db: "message" } ]})
# 2.6及一下用db.addUser
> db.addUser({ user: "message",  pwd: "wsfwl2000", roles: [ "dbOwner", "message" ]})
# 创建collection
> db.createCollection("message")

32位树莓派zero连接示例

pip3 install pymongo==2.6.3

import pymongo

mongoclient = "mongodb://message:wsfwl2000@127.0.0.1:27017/message"

client = pymongo.MongoClient(mongoclient)
db = client["message"]
col = db["message"]
print(col.find_one({}))
client.close()