第一步修改配置文件: xxx@xxx:~$ sudo gedit /etc/mongod.conf 将下面 security:  authorization: enabled 改为#security保存退出 xxx@xxx:~$ ps -ef | grep mongodb xxx@xxx:~$ sudo kill xxxx xxx@xxx:~$ sudo service mongod start xxx@xxx:~$ mongo MongoDB shell version: 3.0.7connecting to: test Server has startup warnings: 2015-10-27T17:01:07.973+0800 I CONTROL  [initandlisten] 2015-10-27T17:01:07.973+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2015-10-27T17:01:07.973+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'2015-10-27T17:01:07.973+0800 I CONTROL  [initandlisten] 2015-10-27T17:01:07.973+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-10-27T17:01:07.973+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'2015-10-27T17:01:07.973+0800 I CONTROL  [initandlisten] > show dbs admin  0.078GB local  0.078GB > use admin switched to db admin > db.createUser(... {... user:"admin",... pwd:"admin",... roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"}]... }... ) Successfully added user: {    "user" : "admin",    "roles" : [        {            "role" : "userAdminAnyDatabase",            "db" : "admin"        },        {            "role" : "readWriteAnyDatabase",            "db" : "admin"        }    ] } > db.system.users.find() { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "bUhMAodI9w6OPDt9GL5Auw==", "storedKey" : "jBhWrBoYjwz86PqlCUTMkSh3h2A=", "serverKey" : "DMq1OYN45RfPjWIi4jFKNW6BJ6k=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "readWriteAnyDatabase", "db" : "admin" } ] } > exit bye 接下来在配置文件中恢复认证 xxx@xxx:~$ sudo gedit /etc/mongod.conf 改#security:为security:  authorization: enabled 保存退出 接下来 xxx@xxx:~$ ps -ef | grep mongodb xxx@xxx:~$ sudo kill xxxx xxx@xxx:~$ sudo service mongod start 然后在mongo shell操作(注意1.该方式添加的用户不能用Robomongo 0.8.5连接,具体原因在下面;注意2.既然认证了就要先db.auth('xxx','xxx')返回1后再进行其他操作)如下操作 xxx@xxx:~$ mongo MongoDB shell version: 3.0.7connecting to: test > use admin switched to db admin > db.auth('admin','admin')1>show dbs admin  0.078GB local  0.078GB


转自:http://blog.csdn.net/zyb228/article/details/49453015


乐享:知识积累,快乐无限。