Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB master-slave + php implementation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Environment

172.16.115.202 primary

172.16.111.171 secondary

172.16.115.157 secondary

1. MongoDB installation

1. Yum download and install mongodb

Extended source http://repo.mongodb.org/yum/redhat/mongodb-org-3.0.repo

Mongodb-org-3.0.7-1.el6.x86_64.rpm # meta packet, which can automatically install the following four component packages

Mongodb-org-mongos-3.0.7-1.el6.x86_64.rpm # mongod daemon and related configuration and initialization scripts

Mongodb-org-server-3.0.7-1.el6.x86_64.rpm # mongos daemon

Mongodb-org-shell-3.0.7-1.el6.x86_64.rpm # ongo shell environment

Mongodb-org-tools-3.0.7-1.el6.x86_64.rpm # MongoDB tool

two。 Edit the configuration file vim / etc/mongod.conf

1) remove the comments that begin with # in the following two lines to prevent restart of the error report

Fork:true

PidFilePath:/var/mongodb/mongod.pid

2) bind_ip binds multiple IP, separated by commas. If you listen to all of them, leave them blank.

3. Modify configuration parameters to start mongd

Echo never > / sys/kernel/mm/transparent_hugepage/enabled

Echo never > / sys/kernel/mm/transparent_hugepage/defrag

Edit vim / etc/security/limits.conf to add:

Mongod soft nofile 64000

Mongod hard nofile 64000

Mongod soft nproc 32000

Mongod hard nproc 32000

Service mongd start

Note: the startup process is slow, this is it is writing data / var/lib/mongo, if it can not start, mongod-f / etc/mongod.conf mode, you can see the error report

4. Edit the MongoDB configuration file vim / etc/mongod.conf of the three machines, and restart

Content:

# # 1) remove the comments from replication

# # 2) add the following parameters (please leave 2 spaces in the first line, fixed format):

# # similar to binary logs

OplogSizeMB: 20

# # replica set name

ReplSetName: huangzhenping

5. View the log file tail / var/log/mongodb/mongod.log

6. Master configuration, build replica set

Use admin # enters the admin library on the host

Config= {_ id: "huangzhenping", members: [{_ id:0,host: "172.16.115.202 huangzhenping 27017"}, {_ id:1,host: "172.16.115.157purl 27017"}, {_ id:2,host: "172.16.111.171purposed 27017"}} # configure the machine

Rs.initiate (config) # Refresh

Rs.status () # View status

The result is as follows: the information of three machines appears, and the state of the master has changed to PRIMARY

> rs.initiate (config) {"ok": 1} huangzhenping:OTHER > rs.status () {"set": "huangzhenping", "date": ISODate ("2017-01-14T20:24:59.490Z"), "myState": 1, "members": [{"_ id": 0 "name": "172.16.115.202 state 27017", "health": 1, "state": 1, "stateStr": "PRIMARY", "uptime": 297, "optime": Timestamp (1484425453 1), "optimeDate": ISODate ("2017-01-14T20:24:13Z"), "electionTime": Timestamp (1484425455, 1), "electionDate": ISODate ("2017-01-14T20:24:15Z"), "configVersion": 1 "self": true}, {"_ id": 1, "name": "172.16.115.157 id 27017", "health": 1, "state": 2 StateStr: "SECONDARY", "uptime": 45, "optime": Timestamp (1484425453, 1), "optimeDate": ISODate ("2017-01-14T20:24:13Z"), "lastHeartbeat": ISODate ("2017-01-14T20:24:57.851Z") LastHeartbeatRecv: ISODate ("2017-01-14T20:24:58.343Z"), "pingMs": 0, "configVersion": 1}, {"_ id": 2 "name": "172.16.111.171 SECONDARY", "health": 1, "state": 2, "stateStr": "SECONDARY", "uptime": 45, "optime": Timestamp (1484425453) 1), "optimeDate": ISODate ("2017-01-14T20:24:13Z"), "lastHeartbeat": ISODate ("2017-01-14T20:24:57.863Z"), "lastHeartbeatRecv": ISODate ("2017-01-14T20:24:58.262Z"), "pingMs": 1 "configVersion": 1}], "ok": 1} huangzhenping:PRIMARY >

Note:

1) if you execute the rs.status () command and no three machines are found, you need to add them manually.

Rs.add ("172.16.115.157")

Rs.add ("172.16.111.171")

2) if the status of the slave is "stateStr": "STARTUP", you need to execute:

Var config= {_ id: "huangzhenping", members: [{_ id:0,host: "172.16.115.202 huangzhenping 27017"}, {_ id:1,host: "172.16.115.157purl 27017"}, {_ id:2,host: "172.16.111.171pur27017"}]}

Rs.initiate (config)

7. Log in from above and the status has been changed to: SECONDARY

Second, MongoDB master-slave test

1. Mongodb replica set test

1) build libraries and collections on the master

Use mydb

Db.createCollection ('testA')

Show dbs

Show tables

2) viewed from above, libraries and collections have been synchronized successfully

Rs.slaveOk ()

Show dbs

two。 Change the weight of the replica set to simulate the main downtime

1) set different weights on the master and view

Cfg = rs.conf ()

Cfg.members [0] .priority = 3

Cfg.members [1] .priority = 2

Cfg.members [2] .priority = 1

Rs.reconfig (cfg)

Rs.config ()

View the effective weights:

Huangzhenping:PRIMARY > rs.config () {"_ id": "huangzhenping", "version": 2, "members": [{"_ id": 0, "host": "172.16.115.202 id 27017", "arbiterOnly": false "buildIndexes": true, "hidden": false, "priority": 3, "tags": {}, "slaveDelay": 0 "votes": 1}, {"_ id": 1, "host": "172.16.115.157 host", "arbiterOnly": false, "buildIndexes": true "hidden": false, "priority": 2, "tags": {}, "slaveDelay": 0, "votes": 1} {"_ id": 2, "host": "172.16.111.171 arbiterOnly", "arbiterOnly": false, "buildIndexes": true, "hidden": false "priority": 1, "tags": {}, "slaveDelay": 0, "votes": 1}], "settings": {"chainingAllowed": true "heartbeatTimeoutSecs": 10, "getLastErrorModes": {}, "getLastErrorDefaults": {"w": 1, "wtimeout": 0}

2) Port 27017 is disabled on the host, and the main downtime is simulated

Iptables-I INPUT-p tcp-- dport 27017-j DROP

Original owner: failed to connect

Original slave: status has changed to PRIMARY

Rs.status () View status:

Huangzhenping:PRIMARY > rs.status () {"set": "huangzhenping", "date": ISODate ("2017-01-22T15:48:15.453Z"), "myState": 1, "members": [{"_ id": 0, "name": "172.16.115.202 huangzhenping 27017" "health": 0, "state": 8, "stateStr": "(not reachable/healthy)", "uptime": 0, "optime": Timestamp (0,0) OptimeDate: ISODate ("1970-01-01T00:00:00Z"), "lastHeartbeat": ISODate ("2017-01-22T15:48:14.551Z"), "lastHeartbeatRecv": ISODate ("2017-01-22T15:48:14.632Z"), "pingMs": 0 "lastHeartbeatMessage": "Failed attempt to connect to 172.16.115.202 purl 27017" Couldn't connect to server 172.16.115.202 couldn't connect to server 27017 (172.16.115.202), connection attempt failed "," configVersion ":-1}, {" _ id ": 1," name ":" 172.16.115.157displacement 27017 " "health": 1, "state": 1, "stateStr": "PRIMARY", "uptime": 16333, "optime": Timestamp (1484429320, 1) OptimeDate: ISODate ("2017-01-14T21:28:40Z"), "electionTime": Timestamp (1485100022, 1), "electionDate": ISODate ("2017-01-22T15:47:02Z"), "configVersion": 2 "self": true}, {"_ id": 2, "name": "172.16.111.171id 27017", "health": 1, "state": 2 StateStr: "SECONDARY", "uptime": 5989, "optime": Timestamp (1484429320, 1), "optimeDate": ISODate ("2017-01-14T21:28:40Z"), "lastHeartbeat": ISODate ("2017-01-22T15:48:14.751Z") "lastHeartbeatRecv": ISODate ("2017-01-22T15:48:14.749Z"), "pingMs": 0, "lastHeartbeatMessage": "could not find member to sync from", "configVersion": 2}], "ok": 1}

3) on the new master, create a library and collection

Use mydb2

Db.createCollection ('testB')

4) if you restore the old master, you will automatically switch to MASTER and synchronize the libraries created on the new master when you are offline.

Iptables-D INPUT-p tcp-- dport 27017-j DROP

III. MongoDB combined with php

1. Download and install the mongo extension module

Wget https://github.com/mongodb/mongo-php-driver-legacy/archive/master.zip

Unzip mongo-php-driver-legacy-master.zip

Cd mongo-php-driver-legacy-master

/ usr/bin/phpize

. / configure-- with-php-config=/usr/bin/php-config

Make;make install

two。 Edit the php configuration file vim / etc/php.ini to load the extension module

3. Edit the test file vim / usr/local/apache2/htdocs/1.php

Content:

4. Test curl localhost/1.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report