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

Mongodb3.2 replica sets

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

Share

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

I. configuration file / etc/mongod.conf

[root@mongo01 ~] # egrep-v "^ (# | $)" / etc/mongod.confsystemLog: destination: file logAppend: true path: / data/mongodb/log/rs1.logstorage: dbPath: / data/mongodb/data directoryPerDB: true journal: enabled: trueprocessManagement: fork: true # fork and run in background pidFilePath: / data/mongodb/conf/mongod.pid # location of pidfilenet: port: 27027replication: oplogSizeMB: 50000 replSetName: rs1 [root@mongo01 ~] # [root@ Mongo01 ~] # ls / data/mongodb/arbiter conf data log startaribiter.sh [root@mongo01 ~] # ls / data/mongodb/arbiter/arbiter.conf conf data log [root@mongo01 ~] # egrep-v "^ (# | $)" / data/mongodb/arbiter/arbiter.confsystemLog: destination: file logAppend: true path: / data/mongodb/arbiter/log/rs1.logstorage: dbPath: / data/mongodb/arbiter/data directoryPerDB: true journal: enabled: trueprocessManagement: fork: true # fork And run in background pidFilePath: / data/mongodb/arbiter/conf/mongod.pid # location of pidfilenet: port: 27029replication: oplogSizeMB: 50000 replSetName: rs1 [root@mongo01 ~] # cat / Datagram. / etc/rc.d/init.d/functionscd ${0% config arbiter.conf *} / arbiterdaemon-- user mongod "mongod-- config arbiter.conf" [root@mongo01 ~] #

Note: the owner and group of the arbiter.conf file is mongod

Start the service and configure the replica set

Service mongod start / data/mongodb/startaribiter.sh

Start the second node, and then do the following on primary:

Rs.initiate () rs.add ("mongodb2.example.net") rs.add ("mongodb3.example.net") rs.addArb ("mongodb1.example.net:27029")

If you want to lower the priority of primary:

Cfg = rs.conf () cfg.members [2] .priority = 0.5rs.reconfig (cfg) rs.status ()

After the replication is implemented, view the data on the slave node as follows:

SECONDARY > rs.slaveOk () SECONDARY > show collections

Third, use keyFile to realize replication verification.

Create the cluster user and password on the primary node as follows:

Admin = db.getSiblingDB ("admin") admin.createUser ({user: "clusteradmin", pwd: "clusterpwd", roles: [{role: "userAdminAnyDatabase", db: "admin"}, {role: "clusterAdmin", db: "admin"}, {role: "root", db: "admin"}]})

Test users:

Db.getSiblingDB ("admin") .auth ("clusteradmin", "clusterpwd")

Turn off the service on each slave node:

Use admin

Db.shutdownServer ()

Including the arbiter node, also need to shut down the service, and finally shut down the service of the primary node.

Add auth-related files to each profile mongod.conf file, as follows:

Security: keyFile: / data/mongodb/conf/keyfile.key [root@mongo01 ~] # cat / data/mongodb/conf/keyfile.keyabcdefgh the file must have permissions of 600,000 or 400and copy the file to each node (keeping the contents the same). The file can also be generated using the following command: openssl rand-base64 755 > path-to-keyfile

Finally, start the mongodb service of each node again, and re-login, test.

Reference:

Https://docs.mongodb.com/manual/tutorial/deploy-replica-set/

Https://docs.mongodb.com/manual/tutorial/enforce-keyfile-access-control-in-existing-replica-set/

IV. Supplement RS for deployment of mongodb 3.6on CentOS7

(1) configure mongod on the vm2 host as follows:

[root@vm2 ~] # mkdir-p / data/mongodb/ {conf,run,logs Data} [root@vm2 ~] # chown-R mongod.mongod / data/mongodb [root@vm2 ~] # vim / etc/mongod.conf [root@vm2 ~] # grep-Pv "^ (# | $)" / etc/mongod.conf systemLog: destination: file logAppend: true path: / data/mongodb/logs/mongod.logstorage: dbPath: / data/mongodb/data/ directoryPerDB: true journal: enabled: trueprocessManagement: fork: true # fork and run in background pidFilePath: / data/ Mongodb/run/mongod.pid # location of pidfile timeZoneInfo: / usr/share/zoneinfonet: port: 27027 bindIp: 0.0.0.0 # Listen to local interface only Comment to listen on all interfaces.security: authorization: enabled keyFile: / data/mongodb/conf/keyFilereplication: replSetName: rs0 [root@vm2 ~] # Note: you must ensure that the path settings in the mongod.conf file are consistent with those in the mongod.service file Otherwise, it will cause startup failure [root@vm2 ~] # cd / usr/lib/systemd/system/ [root@vm2 system] # head-15 mongod.service [Unit] Description=High-performance Schema-free document-oriented databaseAfter=network.targetDocumentation= https://docs.mongodb.org/manual[Service]User=mongodGroup=mongodEnvironment="OPTIONS=-f / etc/mongod.conf "ExecStart=/usr/bin/mongod $OPTIONSExecStartPre=/usr/bin/mkdir-p / data/mongodb/runExecStartPre=/usr/bin/chown mongod:mongod / data/mongodb/runExecStartPre=/usr/bin/chmod 0755 / data/mongodb/runPermissionsStartOnly=truePIDFile=/data/mongodb/run/mongod.pid [root@vm2 system] # [root@vm2 ~] # openssl rand-base64 755 > / data/mongodb/ Conf/keyFile [root@vm2 ~] # chown mongod.mongod / data/mongodb/conf/keyFile [root@vm2 ~] # chmod 400 / data/mongodb/conf/keyFile

(2) configure arbiter on the vm2 host as follows:

[root@vm2 data] # mkdir arbiter/ {conf,data,logs Run}-p [root@vm2 data] # cp mongodb/conf/keyFile arbiter/conf/ [root@vm2 data] # chmod 400 / data/arbiter/conf/ [root@vm2 data] # cp / etc/mongod.conf arbiter/conf/ [root@vm2 data] # mv conf/mongod.conf conf/arbiter.conf [root@vm2 data] # vim arbiter/conf/arbiter.conf [root@vm2 data] # chown-R mongod.mongod arbiter [root@vm2 data] # grep-Pv "^ (# | $)" Arbiter/conf/arbiter.conf systemLog: destination: file logAppend: true path: / data/arbiter/logs/arbiter.logstorage: dbPath: / data/arbiter/data/ directoryPerDB: true journal: enabled: trueprocessManagement: fork: true # fork and run in background pidFilePath: / data/arbiter/run/arbiter.pid # location of pidfile timeZoneInfo: / usr/share/zoneinfonet: port: 37027 bindIp: 0.0.0.0 # Listen to local interface only Comment to listen on all interfaces.security: authorization: enabled keyFile: / data/arbiter/conf/keyFilereplication: replSetName: rs0 [root@vm2 data] # [root@vm2 data] # cd / usr/lib/systemd/system [root@vm2 system] # cp mongod.service arbiter.service [root@vm2 system] # vim arbiter.service [root@vm2 system] # head-15 arbiter.service [Unit] Description=High-performance Schema-free document-oriented databaseAfter=network.targetDocumentation= https://docs.mongodb.org/manual[Service]User=mongodGroup=mongodEnvironment="OPTIONS=-f / data/arbiter/conf/arbiter.conf "ExecStart=/usr/bin/mongod $OPTIONSExecStartPre=/usr/bin/mkdir-p / data/arbiter/runExecStartPre=/usr/bin/chown mongod:mongod / data/arbiter/runExecStartPre=/usr/bin/chmod 0755 / data/arbiter/runPermissionsStartOnly=truePIDFile=/data/arbiter/run/arbiter.pid [root@vm2 system] # [root@vm2 system] # systemctl daemon-reload [root@ Vm2 arbiter] # tree. ├── conf │ ├── arbiter.conf │ └── keyFile ├── data ├── logs └── run4 directories 2 files [root@vm2 arbiter] # [root@vm2 data] # echo never > / sys/kernel/mm/transparent_hugepage/ enabled [root @ vm2 data] # systemctl restart mongod [root@vm2 data] # systemctl restart arbiter [root@vm2 data] # ps-ef | grep mongodmongod 130256 1 8 12:46? 00:00:01 / usr/bin/mongod-f / etc/mongod.confmongod 130354 1 14 12:46? 00:00:01 / usr/bin / mongod-f / data/arbiter/conf/arbiter.confroot 130387 125564 0 12:46 pts/0 00:00:00 grep-- color=auto mongod [root@vm2 data] #

(3) install and configure mongodb on another mongodb server, and the configuration content is exactly the same as on vm2. Then add rs as follows:

[root@meteor ~] # cat / sys/kernel/mm/transparent_hugepage/ enabled [always] madvise never [root@meteor ~] # echo never > / sys/kernel/mm/transparent_hugepage/ enabled [root @ meteor ~] # systemctl restart mongod [root@meteor ~] # mongo localhost:27027/admin-uroot-pMongoDB shell version v3.6.5Enter password: connecting to: mongodb://localhost:27027/adminMongoDB server version: 3.6.5 > show dbs2018-05-26T20:41:42.392+0800 E QUERY [ Thread1] Error: listDatabases failed: {"ok": 0 "errmsg": "not master and slaveOk=false", "code": 13435 "codeName": "NotMasterNoSlaveOk"}: _ getErrorWithCode@src/mongo/shell/utils.js:25:13Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1shellHelper.show@src/mongo/shell/utils.js:849:19shellHelper@src/mongo/shell/utils.js:739:15@ (shellhelp2): 1:1 > rs.slaveOk () > rs.status () {"info": "run rs.initiate (...) If not yet done for the set "," ok ": 0," errmsg ":" no replset config has been received "," code ": 94," codeName ":" NotYetInitialized "} > config= {" _ id ":" rs0 "," members ": [. {" _ id ": 0," host ":" 192.168.20.221 errmsg 27027 "," priority ": 100},. {" _ id ": 1 "host": "192.168.40.206host 27027", "priority": 1},... {"_ id": 2, "host": "192.168.40.206VR 37027", "priority": 0, "arbiterOnly": true}...]} {"_ id": "rs0", "members": [{"_ id": 0 "host": "192.168.20.221 id 27027", "priority": 100}, {"_ id": 1, "host": "192.168.40.206 id 27027", "priority": 1}, {"_ id": 2 "host": "192.168.40.206 true 37027", "priority": 0, "arbiterOnly": true}]} > rs.initiate (config) {"ok": 1} rs0:SECONDARY > rs0:SECONDARY > rs0:PRIMARY > rs0:PRIMARY > rs.status () {"set": "rs0", "date": ISODate ("2018-05-26T12:49:29.987Z") MyState: 1, term: NumberLong (1), heartbeatIntervalMillis: NumberLong (2000), optimes: {"lastCommittedOpTime": {"ts": Timestamp (1527338963, 1), "t": NumberLong (1)}, "readConcernMajorityOpTime": {"ts": Timestamp (1527338963, 1) "t": NumberLong (1)}, "appliedOpTime": {"ts": Timestamp (1527338963, 1), "t": NumberLong (1)}, "durableOpTime": {"ts": Timestamp (1527338963, 1), "t": NumberLong (1)} "members": [{"_ id": 0, "name": "192.168.20.221 name", "health": 1, "state": 1, "stateStr": "PRIMARY", "uptime": 503 Optime: {"ts": Timestamp (1527338963, 1), "t": NumberLong (1)}, "optimeDate": ISODate ("2018-05-26T12:49:23Z"), "infoMessage": "could not find member to sync from", "electionTime": Timestamp (1527338942, 1) "electionDate": ISODate ("2018-05-26T12:49:02Z"), "configVersion": 1, "self": true}, {"_ id": 1, "name": "192.168.40.206 Vera 27027", "health": 1, "state": 2 "stateStr": "SECONDARY", "uptime": 38, "optime": {"ts": Timestamp (1527338963, 1), "t": NumberLong (1)}, "optimeDurable": {"ts": Timestamp (1527338963) "t": NumberLong (1)}, "optimeDate": ISODate ("2018-05-26T12:49:23Z"), "optimeDurableDate": ISODate ("2018-05-26T12:49:23Z"), "lastHeartbeat": ISODate ("2018-05-26T12:49:28.268Z") "lastHeartbeatRecv": ISODate ("2018-05-26T12:49:28.274Z"), "pingMs": NumberLong (1), "syncingTo": "192.168.20.221 26T12:49:28.274Z 27027", "configVersion": 1}, {"_ id": 2, "name": "192.168.40.206 26T12:49:28.274Z 37027" "health": 1, "state": 7, "stateStr": "ARBITER", "uptime": 38, "lastHeartbeat": ISODate ("2018-05-26T12:49:28.266Z"), "lastHeartbeatRecv": ISODate ("2018-05-26T12:49:29.107Z"), "pingMs": NumberLong "configVersion": 1}], "ok": 1, "operationTime": Timestamp (1527338963, 1), "$clusterTime": {"clusterTime": Timestamp (1527338963, 1), "signature": {"hash": BinData (0, "xxW1WBNE99FOs8BNdAanWKOhFUk="), "keyId": NumberLong ("6559870810092208129")} rs0:PRIMARY >

(4) Log in on the secondary node and check the status to verify data consistency.

Description:

Priority: indicates that a member is elected as the priority of a Primary node. The default value is 1. Values range from 0 to 100. setting priority to 0 has a special meaning: a member with a Priority of 0 can never become a Primary node. In Replica Set, the highest member of Priority will be first elected as the Primary node, as long as it meets the conditions.

Hidden: configure a member as a hidden member, requiring a Priority of 0. Client does not send requests to hidden members, so hidden members do not receive Request from Client.

SlaveDelay: in seconds, the Secondary member is configured as a delayed backup node. A Priority of 0 is required, which means that the member lags behind the Primary member for a specified time before synchronizing writes on the Primary to the local. For consistency in data reading, the hidden of the delayed backup node should be set to true to prevent users from reading data that is obviously lagging behind. Delayed members maintain a copy of the data that reflects the state of the data at some time in the past.

Votes: the valid value is 0 or 1, and the default value is 1. If votes is 1, the member (voting member) has permission to elect Primary members. In a Replica Set, there are up to seven members whose votes attribute has a value of 1.

ArbiterOnly: indicates that the member is the arbiter, and the only function of arbiter is to participate in the election. Its votes attribute is 1 votes, which does not save data and does not provide services for client.

Summary: the first half of this content uses rs.add ("....") The second half is to add a replica set using the method of defining config first and then rs.initiate (config). Friends can choose different methods according to their needs.

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