Skip to content

mongodb配置

配置文件:mongod.cfg

text
# 配置端口和可访问ip
net:
  port: 27017
  bindIp: 0.0.0.0
# 开启验证  
security:
  authorization: enabled

账号操作

shell
# 使用adin数据
use admin;

# 创建root角色账号
db.createUser({
  user: "user",
  pwd: "pwd",
  roles: [
    { role: "root", db: "admin" }
  ]
})

# 创建dbOwner角色账号
db.createUser({
  user: "user",
  pwd: "pwd",
  roles: [
    { role: "dbOwner", db: "db" }
  ]
})

# 查询数据库用户
db.getUsers();

# 删除所有用户
db.dropUser("user");

常用语句

shell
# 获取系统中所有的用户
db.system.users.find();
# 查询数据库版本
db.version();

db.yourCollection.find().sort({ yourField: 1 }).limit(10);