3台机子 192.168.2.187,192.168.2.188,192.168.2.189
cd /home/jboss/ftp tar -xvf mongodb-linux-x86_64-ubuntu1404-3.0.4.gz cd /opt/ cp -R /home/jboss/ftp/mongodb-linux-x86_64-ubuntu1404-3.0.4 mongodb vi /etc/profile export PATH=/opt/mongodb/bin:$PATH
cd /opt/mongodb mkdir data mkdir log chmod -R 777 /opt/mongodb/data chown -R jboss /opt/mongodb
#设定主服务器
vi /opt/mongodb/master-mongo.conf fork = true pidfilepath = /opt/mongodb/master-mongodb.pid logpath = /opt/mongodb/log/master-mongo.log dbpath = /opt/mongodb/data/ directoryperdb = true logappend = true replSet=kingmed oplogSize=1000 journal = true
#启动服务
/opt/mongodb/bin/mongod -f /opt/mongodb/master-mongo.conf
#停止服务
/opt/mongodb/bin/mongod --shutdown -f /opt/mongodb/master-mongo.conf
进入主服务器
mongo config={_id : 'kingmed',members : [{_id : 1, host : '192.168.2.187:27017'},{_id : 2, host : '192.168.2.188:27017'},{_id : 3, host : '192.168.2.189:27017'}]} rs.initiate(config);
完成
#设定从服务器
vi /opt/mongodb/slave-mongo.conf fork = true pidfilepath = /opt/mongodb/slave-mongodb.pid logpath = /opt/mongodb/log/slave-mongo.log dbpath = /opt/mongodb/data/ directoryperdb = true logappend = true replSet=kingmed oplogSize=1000 journal = true
#启动服务
/opt/mongodb/bin/mongod -f /opt/mongodb/slave-mongo.conf /opt/mongodb/bin/mongod --shutdown -f /opt/mongodb/slave-mongo.conf
Spring中的设置
<!-- spring data-mongo --> <mongo:mongo id="mongo" replica-set="${mongo.host.1}:${mongo.port},${mongo.host.2}:${mongo.port},${mongo.host.3}:${mongo.port}"> <!-- 一些连接属性的设置 --> <mongo:options connections-per-host="${mongo.connectionsPerHost}" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" write-number="1" write-timeout="0" slave-ok="${mongo.slaveOk}" write-fsync="true"/> </mongo:mongo>
#mongodb settings
mongo.host.1=192.168.2.187 mongo.host.2=192.168.2.188 mongo.host.3=192.168.2.189 mongo.port=27017 mongo.connectionsPerHost=8 mongo.threadsAllowedToBlockForConnectionMultiplier=4 mongo.connectTimeout=1000 mongo.maxWaitTime=1500 mongo.autoConnectRetry=true mongo.socketKeepAlive=true mongo.socketTimeout=1500 mongo.slaveOk=true(读写分离)