1../mongod --config /home/soft/mongoserver/config/mongo.conf


2、添加管理用户(mongoDB 没有无敌用户root,只有能管理用户的用户 userAdminAnyDatabase)


利用mongo命令连接mongoDB服务器端:

db.createUser( {user: "test_mongo",pwd: "testdcadmin",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]});


3、添加完管理用户后,关闭MongoDB,并使用权限方式再次开启MongoDB,这里注意不要使用kill直接去杀掉mongodb进程,(如果这样做了,请去data/db目录下删除mongo.lock文件),可以使用db.shutdownServer()关闭.


4、使用权限方式启动MongoDB


在配置文件中添加:auth=true , 然后启动


5、进入mongo shell,使用admin数据库并进行验证,如果不验证,是做不了任何操作的。 

> use admin
> db.auth("test_mongo","testdcadmin")   #认证,返回1表示成功

6、创建业务库及用户


切换数据库到业务库

use dcenter
db.createUser({user: "dcenter", pwd: "test123456", roles: [{ role: "dbOwner", db: "dcenter" }]})
db.auth("dcenter","test123456")


问题记录:


非常规停止运行解决方法

ERROR: child process failed, exited with error number 100


1.删除 data中的mongod.lock

2.然后以repair的模式启动


./mongod --config /home/soft/mongodbserver/etc/mongodb.conf --repair

3.然后在启动一次

./mongod --config /home/soft/mongodbserver/etc/mongodb.conf


db.shutdownServer()关闭. 权限不足问题。先授权,在执行。

1. use admin
2. db.auth("admin","admin")
3. db.grantRolesToUser( "admin" , [ { role: "hostManager", db: "admin" } ])
4. db.shutdownServer()

参考:http://www.itpub.net/forum.php?mod=viewthread&tid=1937988


我找到了 是缺少hostManger的这个角色 这个角色具有shutdown的权限。

Cluster Administration Roles

clusterAdmin   角色:clusterManager, clusterMonitor, hostManager


1.clusterManager

AddShard,ApplicationMessage,CleanupOrphaned,FlushRouterConfig,

ListShards,RemoveShard,ReplSetConfigure,ReplSetGetStatus,

ReplSetStateChange,Resync,


EnableSharding,MoveChunk,SplitChunk,splitVector


2.clusterMonitor

connPoolStats,cursorInfo,getCmdLineOpts,getLog,getParameter,

getShardMap,hostInfo,inprog,listDatabases,listShards,netstat,

replSetGetStatus,serverStatus,shardingState,top


collStats,dbStats,getShardVersion


3.hostManager

applicationMessage,closeAllDatabases,connPoolSync,cpuProfiler,

diagLogging,flushRouterConfig,fsync,invalidateUserCache,killop,

logRotate,resync,setParameter,shutdown,touch,unlock

> db.grantRolesToUser( "admin" , [ { role: "hostManager", db: "admin" } ])
> db.getName()
admin
> show tables;
system.indexes
system.users
system.version
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "MONGODB-CR" : "9f3121efccbe3fef09a799d5e63077c2" }, "roles" : [ { "role" : "readWriteAnyDatabase", "db" : "admin" }, { "role" : "readAnyDatabase", "db" : "admin" }, { "role" : "hostManager", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "test.test", "user" : "test", "db" : "test", "credentials" : { "MONGODB-CR" : "e78333b96cbdc20a67432095f4741222" }, "roles" : [ { "role" : "dbOwner", "db" : "test" } ] }
> db.shutdownServer()
2016-01-26T10:18:30.064+0800 DBClientCursor::init call() failed
server should be down...
2016-01-26T10:18:30.066+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-01-26T10:18:30.067+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-01-26T10:18:30.067+0800 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
>



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