Software version 64-bit: $ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.0.tgz Mongodb-linux-x86_64-rhel62-3.2.0.tgz Server 3: 192.168.1.20(master[PRIMARY]) 192.168.1.21(slave[Secondary]) 192.168.1.22(Arbitration [Arbiter])Reference:Http://www.cnblogs.com/visionwang/p/3290435.html http://www.csdn.net/article/2014-04-09/2819221- build-high-avialable-mongodb-cluster-part-1$ TAR-ZXVFmongodb-linux-x86_64-rhel62-3.2.0.tgz/opt$ mvmongodb-linux-x86_64-rhel62-3.2.0 MongoDB$ mkdir-p/opt/mongodb/{conf,data,logs} #解压后创建目录conf data logs################### #mongodb配置文件 (/opt/mongodb/conf/mongodb.conf )#########存放的数据文件目录dbpath =/opt/mongodb/data# log file directory logpath=/opt/mongodb/logs/master.log# encrypted files, user cluster Connection verification #keyfile=/opt/ mongodb/mongodb.key# log append logappend=true# replica set name replset=repset# binding bind_ip=192.168.1.20# Port number port=27017# start user name password Authentication #auth =true# runs MongoDB as a daemon. Ensure its stability fork=true########################### #mongodb配置文件 ###################### #把配置好的mongodb复制到其他两台机器 $ scp-r/opt/mongodb [Email protected]:/opt/$ scp-r/opt/mongodb [email protected]:/opt/$ vim/opt/mondogd/conf/mongodb.conf #依次修改其他两台机器上配置文件绑定的ip$/opt/mongodb/bin/mongod-f/opt/mongodb/conf/mongodb.conf#启动mongodb服务 (performed on all 3 platforms)#如果是32位, the configuration file needs to be added,journal=trueStorageengine=mmapv1oplogsize=128$/opt/mongodb/bin/mongo 192.168.1.20:27017 #随便选择一台机器连接mongod> Use admin #切换到数据库admin (MySQL library equivalent to MySQL)#定义副本集配置, the primary node with a high priority value Repset must be the same value as the Replset defined in the configuration file _id.for the quorum point, be sure to add arbiteronly:true, specifying only the quorum node
> config = {_id: "Repset", Members: [{_id:0,host: ' 192.168.1.20:27017 ', Priority:2},{_id:1,host: ' 192.168.1.21:27017 ', Priority:1},{_id:2,host: ' 192.168.1.22:27017 ', Arbiteronly:true}]} > rs.initiate (config)#初始化副本集配置, some files are generated to the data directory,, cannot read and write. OK is 1 success> Rs.status ()#查看副本集集群状态,"Health": 1 is available on behalf of the server,"Statestr": "PRIMARY" table name own identity is master> Rs.ismaster ()#查看本机是否是master> rs.conf () #shows the health status, name, startup time, type of node, and so on for each node. View the configuration table for the current replica set#连接master写入数据, see if slave can find out, test slave can write data (not, will prompt no master)$/opt/mongodb/bin/mongo 192.168.1.20:27017repset:primary>Db.users.insert ({username: "Tangwan1", age:22}) Writeresult ({"ninserted": 1})repset:secondary>Show Collections[thread1] Error:listcollections failed: {"OK": 0, "errmsg": "Not Master and Slaveok=false", "Code": 1 3435}#mongodb默认是从主节点读写数据的, read is not allowed on the replica node and you need to set the replica node to be readable.repset:secondary>Rs.slaveok ()repset:secondary>Show Collectionsusersrepset:secondary>Db.users.find () {"_id": ObjectId ("572ed5c9d7ef239c6064117f"), "username": "Visionwang", "Age": #杀掉master, see if slave Switch to master$ kill-9 ' ps-ef|grep mongod|grep-v Grep|awk ' {print $} ' #此时再连接原来的slave (if already attached, direct enter ...) and see Repset: Secondary> into repset:primary>.repset:primary>Db.users.insert ({username: "tangwan2", age:22})repset:primary>Db.users.insert ({username: "tangwan2", age:22}) Writeresult ({"ninserted": 1})repset:primary>Db.users.find () {"_id": ObjectId ("572ed5c9d7ef239c6064117f"), "username": "Tangwan1", "Age":}} {"_id": ObjectId ("57 52973d814ab9878378c719 ")," username ":" tangwan2 "," Age ": #再启动刚杀掉的master, you can see it became slave $/opt/mongodb/bin/mongod-f /opt/mongodb/conf/mongodb.conf$/opt/mongodb/bin/mongo 192.168.1.21:27017# to check if the data was copied over the time it died.repset:secondary>Db.users.find () #用openssl生成密钥, for trust between the machines in the cluster, encrypt the heartbeat connection $ yum-y install openssl$ OpenSSL rand-base64 741 > Mongodb.key #给mong ODB Add user and password ERP; #数据库名erp_user # user name zgtx123.com# password # #创建用户前需要修改currentVersion, authentication Levelrepset:primary>Db.system.version.find ()repset:primary>Db.system.version.update ({' _id ': ' Authschema '},{$set: {' CurrentVersion ': 3}})repset:primary>Db.system.version.find () # #查看, create, delete userrepset:primary>Use adminrepset:primary>Db.system.users.find ()repset:primary>Db.system.users.remove ({User: "Erp_user"})repset:primary>Use of erpswitched to DB ERPrepset:primary>Db.createuser ({"User": "Erp_user", "pwd": "zgtx123.com", "roles": [{role: "ReadWrite", DB: "ERP"}]},{W: "Majority", WTIMEOUT:5000}) Successfully added User: {"user": "Erp_user", "roles": [{"Role": "ReadWrite", "DB": "ERP"}]}# #修改3个配置文件au Th=true restart mongodb$ kill-9 ' ps-ef|grep mongod|grep-v Grep|awk ' {print $} ' $/opt/mongodb/bin/mongod-f/opt/mongodb/con f/mongodb.conf$/opt/mongodb/bin/mongo 192.168.1.20:27017/erp-u erp_user-p# Enter password > db.auth ("Erp_user", " Zgtx123.com ")#也是一种认证方式
From for notes (Wiz)
22.mongodb Replica set cluster