一、安装
Ubuntu 14.04.2
缺省的mongodb的版本是2.*版本
详细参照:https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
直接安装即可升级: apt-get install mongo.org
说明:这种升级后,在 /var/lib/mongodb中仍然保存的是原来版本的数据格式,也就是在使用缺省数据库文件的时候,是老版本的数据。可能也是为了兼容的问题。(似乎是这样)
二、运行环境规划,做好配置文件:
指定自己的data、log、绑定ip、端口、认证
这些都配置到自己的配置文件中:home/***/my_etc/mongodb.conf
storage:
dbPath: /home/***/mongodb/data
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /home/suanqi/mongodb/mongod.log
net:
port: ***
bindIp: ***
security:
keyFile: /home/suanqi/my_etc/mongodb-keyfile
authorization: enabled
其中黄色部分非常重要(为将来做cluster做准备,本次可以不需要);
keyFile的生成参见如下命令:
openssl rand -base64 741 > /home/***/mongodb/mongodb-keyfile
chmod 600 mongodb-keyfile
三、创建一个系统用户;
1、no auth方式启动 sudo mongod --dbpath /home/suanqi/mongodb/data #需要事前创建此目录
2、修改验证机理。3.0由MONGODB-CR 改为sha1,但是为什么远端还是使用mongodb-cr,估计是我的客户端没有升级导致。参见如下操作:
> use admin
switched to db admin
> var schema = db.system.version.findOne({"_id" : "authSchema"})
> schema
{ "_id" : "authSchema", "currentVersion" : 5 }
> schema.currentVersion=3
3
> db.system.version.save(schema)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
到这里,说明认证方式改变了。就可以使用 MONGODB-CR 验证机理了。
上述操作未解决如下错误:
Failed to authenticate woxi@admin with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentials missing in the user document
红色的代码不需要,但是也是费了比较大的功夫查到的解决错误的做法。在此做一个记录。
3、添加数据库系统管理员帐号:
use admin
db.createUser(
{
user: "***",
pwd: "***",
roles: [ { role: "root", db: "admin" } ]
}
)
然后以这个帐号启动mongod;sudo mongod --config /home/suanqi/my_etc/mongod.conf &
4、远端连接,并创建一个数据库用户,这是为了安全起见。
用刚才创建的帐号登录,
mongo -u ** -p ** --authenticationDatabase "admin" ip地址:mongodb端口号 #在mongodb.conf中
说明,mongdb3 ,在mongo客户端连接数据库时,需要指定“使用什么用户库”,缺省是admin。
use admin
db.createUser(
{
user: "##",
pwd: "##",
roles: [ { role: "dbOwner", db: "##" } ]
}
)
5、如果用户创建出问题,用户管理得到用户、删除用户、给用户赋权。
db.getUser("帐号")
db.dropUser("帐号")
db.grantRolesToUser(
"帐号","roles": [{"role": "dbOwner","db": "库名"}]
)
6、使用新创建的数据库用户与后台连接
mongo -u "woxi" -p "woxi2014"
--authenticationDatabase "admin"
101.200.191.85:27789/rails_api
四、rails的配置文件;按照说明配置好像总有问题。最后索性把 auth_source,roles都配置上,rails侧就算ok
参照如下配置
development:.....
# Configure available database clients. (required)
clients:
# Defines the default client. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: 库名
# Provides the hosts the default client can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- ip地址:端口
options:
# Change the default write concern. (default = { w: 1 })
# write:
# w: 1
# Change the default read preference. Valid options for mode are: :secondary,
# :secondary_preferred, :primary, :primary_preferred, :nearest
# (default: primary)
# read:
# mode: :secondary_preferred
# tag_sets:
# - use: web
# The name of the user for authentication.
user: '###'
# The password of the user for authentication.
password: '####'
# The user's database roles.
roles:
- 'dbOwner'
# Change the default authentication mechanism. Valid options are: :scram,
# :mongodb_cr, :mongodb_x509, and :plain. (default on 3.0 is :scram, default
# on 2.4 and 2.6 is :plain)
# auth_mech: :mongodb_cr
# The database or source to authenticate the user against. (default: admin)
auth_source: admin