一、Mongdb3.0安装
官网下载最新稳定版本的mongodb软体
https://www.mongodb.org/downloads
解压软体
[[email protected] service]# tar zxvf mongodb-linux-x86_64-rhel62-3.0.3.tgz mongodb-linux-x86_64-rhel62-3.0.3/README mongodb-linux-x86_64-rhel62-3.0.3/THIRD-PARTY-NOTICES mongodb-linux-x86_64-rhel62-3.0.3/GNU-AGPL-3.0 mongodb-linux-x86_64-rhel62-3.0.3/bin/mongodump mongodb-linux-x86_64-rhel62-3.0.3/bin/mongorestore mongodb-linux-x86_64-rhel62-3.0.3/bin/mongoexport mongodb-linux-x86_64-rhel62-3.0.3/bin/mongoimport mongodb-linux-x86_64-rhel62-3.0.3/bin/mongostat mongodb-linux-x86_64-rhel62-3.0.3/bin/mongotop mongodb-linux-x86_64-rhel62-3.0.3/bin/bsondump mongodb-linux-x86_64-rhel62-3.0.3/bin/mongofiles mongodb-linux-x86_64-rhel62-3.0.3/bin/mongooplog mongodb-linux-x86_64-rhel62-3.0.3/bin/mongoperf mongodb-linux-x86_64-rhel62-3.0.3/bin/mongod mongodb-linux-x86_64-rhel62-3.0.3/bin/mongos mongodb-linux-x86_64-rhel62-3.0.3/bin/mongo
创建mongdb数据存放路径及日志存放路径
[[email protected] service]# mkdir -p /date/{mongodbdata,mongodb_logs}
建立mongod账号,授权数据目录
[[email protected] date]# useradd mongod [[email protected] date]# chown -R mongod.mongod /date/mongodbdata/
创建mongod.conf配置文件,只需要以下几个选项即可
[[email protected] date]#cat /etc/mongod.conf dbpath = /data/mongodata #mongodb数据存放路径 logpath = /data/mongodb_logs/mongodb.log #mongodb日志存放路径 httpinterface = true #mongodb管理端口默认+1000:280 fork = true #后台启动
二、启动Mongodb初始化数据
(1)第一种直接在命令行中指定路径和日志
/usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongod --dbpath=/date/mongodbdate/ --logpath=/date/mongodb_logs/mongodb.log --fork about to fork child process, waiting until server is ready for connections. forked process: 5238 child process started successfully, parent exiting 端口已经起来了: [[email protected] mongodb_logs]# lsof -i :27017 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mongod 5238 root 6u IPv4 41939 0t0 TCP *:27017 (LISTEN) 初始化由于要同步数据,建立本地的一些数据文件。会启动比较慢。
(2)第二种方式启动:通过加载配置文件启动(推荐)
[[email protected] mongodbdate]# /usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongod -f /etc/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 5382 child process started successfully, parent exiting
进入终端查看数据库
[[email protected] mongodb_logs]# /usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongo MongoDB shell version: 3.0.3 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2015-06-30T17:37:53.882+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2015-06-30T17:37:53.882+0800 I CONTROL [initandlisten] 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘. 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘ 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘. 2015-06-30T17:37:53.906+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘ 2015-06-30T17:37:53.906+0800 I CONTROL [initandlisten] > show dbs; local 0.078GB
启动报错会出现以下:
[[email protected] mongodbdate]# /usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongod -f /etc/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 5298
检查原因:
第一:由于之前没有正常的退出Mongodb服务,需要删除mongod.lock文件,再次启动恢复正常。
第二:配置文件有问题,需要检查配置文件是否有特殊字符,导致无法解析
三、mongodb副本集配置
配置mongodb的副本集务必要做好以下几点:
1、各个节点能够正常访问mongodb的socket端口,iptables要放行
2、SELINUX必须要关闭掉,以免禁止端口复制
3、各个服务器节点要做好解析/etc/hosts
4、由于副本集是通过Heartbeat心跳线连接到,务必同步好各个节点的时间
小注:本人由于之前没有注意到这些,被坑了好久。哈哈!!!
mongodb复制过程及概念
(1)主节点将数据修改操作保存至oplog,oplog:大小固定的文件,存储在local数据库
(2)初始同步(inital sync)
节点没有任何数据
节点丢失副本复制历史
:克隆所有的数据库
:应用数据集的所有改变:复制oplog并应用于本地
:为所有collection的构建索引
(3)回滚后追赶(post-rollback catch-up)
(4)切分块迁移(shareding chunk migrations)
local:存放了副本集的所有元素和oplog;用于存储oplog的是一个名为oplog.rs的collection;
oplog.rs的大小依赖于OS及文件系统;但可以自定义其大小oplogsize
环境试验