创建用户
db.createUser({ user: 'testname', // 用户名 pwd: 'testpwd', // 密码 roles: [ { role: 'readWrite', // 读写权限 db: 'test' // 所属数据库 } ] })需要验证启动mongodb
mongod --dbpath mongoData --auth show dbs // 报错,因为没有验证 use test // 切换到test数据库 db.auth('testname', 'testpwd') // 1 show collections // 可以查看表 use local // 切换到local数据 show collections // 报错 db.auth('testname', 'testpwd') // 报错,因为testname是test数据的用户修改密码
use test // 切换到test数据库 db.auth('testname', 'testpwd') // 验证 db.changeUserPassword('testname', 'newtestpwd') // 修改密码