官网下载地址:http://www.mongodb.org/downloads
tar xf mongodb-linux-x86_64-rhel62-3.4.6.tgz
mv mongodb-linux-x86_64-rhel62-3.4.2 /usr/local/mongodb
#vi /etc/profile
export PATH=$PATH:/usr/local/mongodb/bin
加载/etc/profile使定义生效
#. /etc/profile
查看mongodb的bin文件
#which mongo
/usr/local/mongodb/bin/mongo
查看mongodb的版本号
#mongo --version
创建数据目录 mkdir -p /usr/local/mongodb/data
根据提示设置ulimit和/sys/kernel参数:
#echo never > /sys/kernel/mm/transparent_hugepage/enabled
#echo never> /sys/kernel/mm/transparent_hugepage/defrag
#vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
* soft nproc 32768
* hard nproc 32768
mongodb soft nofile 65535
mongodb hard nofile 65535
mongodb soft nproc 32768
mongodb hard nproc 32768
useradd -r -m -s /bin/bash mongodb
chown mongodb.mongodb /usr/local/mongodb/data/ -R
mkdir /usr/local/mongodb/log
chown mongodb.mongodb /usr/local/mongodb/log
创建配置文件
#vi /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /usr/local/mongodb/log/mongod.log
logRotate: rename
# Where and how to store data.
storage:
dbPath: /usr/local/mongodb/data
journal:
enabled: true
engine: wiredTiger
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /usr/local/mongodb/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
# bindIp: 10.0.18.149 # Listen to local interface only, comment to listen on all interfaces.
http:
enabled: false
RESTInterfaceEnabled: false
#setParameter:
# enableLocalhostAuthBypass: true
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
保存退出,以上配置文件开启了authorization功能!
启动命令:
#su mongodb -c "/usr/local/mongodb/bin/mongod -f /etc/mongod.conf"
主从复制
主节点
然后重新启动:
#su mongodb -c "/usr/local/mongodb/bin/mongod --master -f /etc/mongod.conf"
从节点
#su mongodb -c "/usr/local/mongodb/bin/mongod --slave --source 192.168.153.137:27017 -f /etc/mongod.conf"
从节点需要执行
rs.slaveOk();
然后可以执行show dbs;
关闭mongodb
use admin
db.shutdownServer()
复制集
1、修改配置文件
基本配置如下:
systemLog:
destination: file
logAppend: true
path: /usr/local/mongodb/log/mongod.log
logRotate: rename
storage:
dbPath: /usr/local/mongodb/data
journal:
enabled: true
engine: wiredTiger
processManagement:
fork: true # fork and run in background
pidFilePath: /usr/local/mongodb/mongod.pid # location of pidfile
net:
port: 27017
# bindIp: x.x.x.x # Listen to local interface only, comment to listen on all interfaces.
http:
enabled: false
RESTInterfaceEnabled: false
#security:
# authorization: enabled
在基本配置的基础上对三台mongodb的配置文件统一添加如下:
replication:
oplogSizeMB: 20
replSetName: Myrepl
注:oplogSizeMB指oplog大小,即复制操作日志的最大大小(以兆字节为单位
replSetName 指副本集名称,注意格式是前面空两格,冒号后空一格。
然后重启三台MongoDB的mongod进程!
#killall mongod
#su mongod -c "/usr/local/mongodb/bin/mongod -f /etc/mongod.conf" #以普通用户启动
后面的步骤参照
http://linuxg.blog.51cto.com/4410110/1897727
config={_id:"Myrepl",members:[{_id:0,host:"192.168.153.135:27017"},{_id:1,host:"192.168.153.136:27017"},{_id:2,host:"192.168.153.137:27017"}]}
rs.initiate(config)
如果之前有数据,会报错,要先改回原来的配置,执行db.dropDatabase() 再把复制集的添加上。