1、使用mongod启动mongodb
2、使用mongo 进入shell
(1)使用某个数据库
> use test1 switched to db test1 >
(2)给test1数据库添加用户名
> db.addUser("nancr","123456") { "user" : "nancr", "readOnly" : false, "pwd" : "7d5e0b84b89bb8b81f13a118ce127216", "_id" : ObjectId("53f9ec3ee849e980b5388203") } >
(3)验证用户是否添加成功。1表示成功,0表示失败
> db.auth("nancr","123456") 1
3、使用用户名登陆访问数据库
(1)使用mongod --auth启动mongodb
(2)使用用户名访问test2数据库,进入shell
mongo test1 -u nancr -p MongoDB shell version: 2.4.10 Enter password: connecting to: test1 >