MongoDB settings replica set cluster and use PHP to connect

Source: Internet
Author: User
Tags vps

MongoDB has been the master from the mode, and then the official recommendation with Replica Set (RS) to replace the principal and subordinate, mainly when the primary node failure, the RS cluster will automatically vote to elect primary node mechanism, automatically select a new primary node, This way, the application does not need to be concerned with master-slave switching. To configure a simple RS, you need at least two machines. I used VMware to run the two Vps,ip in Nat: 192.168.33.112 and 192.168.33.119, note two VPS access issues (Iptables network, etc.)


1.SSH Login to 112 that machine, first download MongoDB The latest version is now 3.0.5 (3.x version of a lot of awesome, whether from read-write performance, or data compression capabilities)

Cd/usr/local/binwget HTTP://FASTDL.MONGODB.ORG/LINUX/MONGODB-LINUX-X86_64-3.0.5.TGZTAR-ZXVF  MONGODB-LINUX-X86_64-3.0.5.TARMV Mongodb-linux-x86_64-3.0.5/mongodb

2. Create some necessary folders for MongoDB

Mkdir-p/mnt/mongodb/rs/datamkdir-p/mnt/mongodb/rs/logsmkdir-p/mnt/mongodb/rs/config

3. Create a configuration file

Vi/mnt/mongodb/rs/config/mongod.conf

Insert the following content:

Dbpath=/mnt/mongodb/rs/data   #数据存放目录logpath =/mnt/mongodb/rs/logs/mongod.log  #日志文件目录pidfilepath =/mnt/ Mongodb/rs/mongod.pid  #pid端口文件port =27017   #mongodb端口logappend =true   #追加方式写日志文件fork =true        # Background run journal=true     #启用日志选项, MONGODB data operations will be written to the Journal folder file oplogsize=2048   #同步操作记录文件大小 (MB) smallfiles=true  #使用较小的默认文件replSet =27017_0    #副本集名称, same replica set, name must be identical

4. Running Mongod Process

Cd/usr/local/bin/mongodb/bin/./mongod-f/mnt/mongodb/rs/config/mongod.conf

When the boot succeeds, the terminal will prompt

About-to-fork child process, waiting until server was ready for connections.forked Process:1286child process started Succe ssfully, parent exiting

Then in the same way at 119 that machine also let Mongod run up ...


5. Any machine enter the command line to manage MongoDB, start Rs:

./mongo 127.0.0.1:27017/admin

Enter the RS configuration command

config={_id: ' 27017_0 ', Members: [{_id:1, Host: ' 192.168.33.119:27017 '},{_id:2, Host: ' 192.168.33.112:27017 '}]}

The system will automatically format the display of:

{"_id": "27017_0", "members": [{"_id": 1, "host": "192.168.33.119:27017"},{"_id": 2, "host": "192.168.33.112:27017"}]}

Here we let 112 and 119来 do RS cluster, then initialize the cluster:

Rs.initiate (config);

The system will prompt: {"OK": 1} to prove the success of the launch!

Re-enter the command to check the RS status:

Rs.status ()

The system will display:

{"Set": "27017_0", "date": Isodate ("2015-08-24t19:41:37.778z"), "MyState": 2, "members": [{"_id": 1, "name": "192.168.33 .119:27017 "," Health ": 1," state ": 1," Statestr ":" PRIMARY "," uptime ": +," Optime ": Timestamp (1440445283, 1)," optimedate ": Isodate (" 2015-08-24t19:41:23z ")," Lastheartbeat ": Isodate (" 2015-08-24t19:41:37.226z ")," LASTHEARTBEATRECV ": Isodate ("2015-08-24t19:41:37.238z"), "Pingms": 1, "Electiontime": Timestamp (1440445283, 2), "Electiondate": Isodate (" 2015-08-24t19:41:23z ")," ConfigVersion ": 1},{" _id ": 2," name ":" 192.168.33.112:27017 "," Health ": 1," state ": 2," Statestr ":" Secondary "," uptime ": 129," Optime ": Timestamp (1440445283, 1)," Optimedate ": Isodate (" 2015-08-24t19:41:23z ")," ConfigVersion ": 1," Self ": true}]," OK ": 1}

Here we can see the RS cluster details, you can see 119 that machine was selected as primary node, 112 was selected as secondary node, and later if you want to add a machine to do scale-out, only need to add to the cluster of the machine add come in the line, Or if a machine fails to be eliminated, it can be easily removed.


6. Use PHP to connect:

For the application side (PHP), in the framework to connect to the MongoDB class inside, you can write:

$con = new Mongoclient ("mongodb://192.168.33.112:27017,192.168.33.119:27017", Array ("replicaset" = "27017_0"));

Note that the Replicaset (replica set name) is to be written so that the cluster can automatically discover the nodes ...


That ' s it~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MongoDB settings replica set cluster and use PHP to connect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.