mongodb yum方式安装的,则可以直接用 :service mongod start/stop 来启动和停止;千万不用kill -9 PID的命令来停止,这样会损伤mongodb。
# mongod.conf
#where to log
logpath=/var/log/mongodb/mongod.log #日志文件位置
logappend=true
# fork and run in background #后台形式运行
fork=true
#port=27017
#数据文件位置
dbpath=/var/lib/mongo
# location of pidfile #PID文件位置
pidfilepath=/var/run/mongodb/mongod.pid
# Listen to local interface only. Comment out to listen on all interfaces.
#默认是本机IP,bind_ip=0.0.0.0则远程机器可以连接mongodb了
bind_ip=127.0.0.1
# Disables write-ahead journaling #写之前禁止记录日志
# nojournal=true
# Enables periodic logging of CPU utilization and I/O wait #启用定期记录CPU利用率和 I/O 等待
#cpu=true
# Turn on/off security. Off is currently the default #认证方式?
#noauth=true
#auth=true
# Verbose logging output. #冗长的日志记录
#verbose=true
# Inspect all client data for validity on receipt (useful for
# developing drivers) # 检查客户端输入数据的有效性检查
#objcheck=true
# Enable db quota management #开始数据库配额的管理,默认每个db可以有8个文件
#quota=true
# Set oplogging level where n is # 设置oplog记录等级
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#diaglog=0
# Ignore query hints #忽略查询提示
#nohints=true
# Enable the HTTP interface (Defaults to port 28017). #禁用http界面,默认为localhost:28017
#httpinterface=true
# Turns off server-side scripting. This will result in greatly limited
# functionality # 关闭服务器端脚本,这将极大的限制功能
#noscripting=true
# Turns off table scans. Any query that would do a table scan fails. # 关闭扫描表,任何查询将会是扫描失败
#notablescan=true
# Disable data file preallocation. # 关闭数据文件预分配
#noprealloc=true
# Specify .ns file size for new databases. # 为新数据库指定.ns文件的大小,单位:MB
# nssize=<size>
# Replication Options # 备份选项
# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile
以上是mongodb yum安装后在/etc/mongod.conf 中的默认内容;
给安装好了的mongodb 添加用户和密码:
首先,启动mongodb;
在任何目录下输入:mongo;
[root@i-E79D4B11 ~]# mongo
MongoDB shell version: 2.6.6
connecting to: test
>
出现以上信息,表示连接成功:
#use admin; #使用admin数据库;
#show collections; #查看集合;
#db.system.users.find(); #查看dmin数据库中的users中的记录
#db.addUser('changhongeb','changhongeb'); #为changhongeb 数据库添加 changhongeb 用户;3.0版本的mongodb 不能再使用addUser(),而是用db.createUser({user:"chbigdata",pwd:"chbigdata",roles:["readWrite","dbAdmin","dbOwner"]}) 来添加用户。
我添加成功后提示信息如下:
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "changhongeb", "roles" : [ "root" ] }
#vim /etc/mongodb.conf //将auth=true前面的注释拿掉
#service mongod restart //或者用下边的命令
# /etc/init.d/mongod restart //重启生效
再次连接 mongo;使用changhongeb数据库;
> use changhongeb;
switched to db changhongeb
> show dbs;
2014-12-14T02:37:45.592+0800 listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} at src/mongo/shell/mongo.js:47
>
以上信息显示显示dbs失败,因为我们开启权限认证,则没通过认证;
> use admin //切换到admin数据库
switched to db admin
> db.auth('changhongeb','changhongeb'); //将changhongeb数据库的changhongeb用户认证
1
> use changhongeb; //切换到changhongeb数据库
switched to db changhongeb
> show dbs;
admin 0.078GB
changhongeb (empty)
imgs (empty)
local 0.078GB
>
以上则表示认证成功;
http://21jhf.iteye.com/blog/2216103 mongodb3.0版本以后的认证方式。
接上,添加普通用户;
> db.addUser('changhongeb','changhongeb'); //给changhongeb数据库添加一个可以读写的普通用户
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "changhongeb", "roles" : [ "dbOwner" ] }
>
具体的mongodb命令参考:
http://www.111cn.net/database/MongoDB/55024.htm
集群配置mongodb