懒得写config文件了,直接用命令行启动:
/path/to/mongod --dbpath=/path/to/db --fork --logpath=/path/to/log -port <port> --auth --replSet <path-to-keyfile> --keyFile <path-to-keyfile>
踩坑列表:
rs.initiate()->
errmsg: "replSetInitiate quorum check failed because not all proposed set members responded affirmatively:
IP:PORT failed with not authorized on admin to execute command"
鉴权失败,以为两台机起的时候都有账号密码验证,要填账号密码,结果也不行。
看了下官方文档
openssl rand -base64 756 > <path-to-keyfile>
chmod 400 <path-to-keyfile>
生成key,cp去两台机,启动时候指定--keyFile <path-to-keyfile>就解决了
rs.initiate()->
errmsg: "IP:PORT has data already, cannot initiate set"
用来做从那台机已有数据,所以不能直接覆盖,好吧,偷了个懒直接scp把主的东东全cp过来了。全部删掉,包括admin这个db。重启。终于可以初始化了。包括主那台机的账号密码也会重新同步到从。
不算是坑
之前的版本有个master/slave模式。启动配置有点不同。
指定的master启动的时候是--master,指定的slave启动时候是--slave --source masterIP:port
效果应该差不多,-h里面建议用replicate set相关参数
Master/slave options (old; use replica sets instead):
然后
对于要做读写分离来提升体验的,当然是选择SECONDARY_PREFERRED
其他几种模式就字面意思了。如果机器多到爆炸倒是可以试试NEAREST,不知道是随机还是有做ping来优先选择rtt小的。
client = MongoClient(uri,replicaSet='SetName',readPreference='secondaryPreferred')
* `PRIMARY`: Read from the primary. This is the default read preference, and provides the strongest consistency. If no primary is available, raise [`AutoReconnect`](http://api.mongodb.com/python/current/api/pymongo/errors.html#pymongo.errors.AutoReconnect "pymongo.errors.AutoReconnect").
* `PRIMARY_PREFERRED`: Read from the primary if available, otherwise read from a secondary.
* `SECONDARY`: Read from a secondary. If no matching secondary is available, raise [`AutoReconnect`](http://api.mongodb.com/python/current/api/pymongo/errors.html#pymongo.errors.AutoReconnect "pymongo.errors.AutoReconnect").
* `SECONDARY_PREFERRED`: (选它!!!)Read from a secondary if available, otherwise from the primary.
* `NEAREST`: Read from any available member.
网友评论