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 replication set

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

MongoDB replication sets Overview replication sets are additional data replicas, the process of synchronizing data across multiple servers, replication sets provide redundancy and add data availability, and the advantages of restoring replication sets to services interrupted by hardware failures through a replication set are as follows: make the data more secure. High data availability (2407) disaster recovery no-downtime maintenance (such as backup, index reconstruction, failover) read scaling (additional replica reads) replica sets are transparent to applications. How the replication set works MongoDB requires at least two nodes, one of which is the master node (primary), which is responsible for handling the client's requests, and the rest is the slave node (secondary), which is responsible for replicating the data on the master node. The common collocation of each node in MongoDB is: one master, one slave or one master and multiple slaves. The master node records all its operations to the oplog, the slave node polls the master node regularly to obtain these operations, and then performs these operations on its own data copy, so as to ensure that the data of the slave node and the master node translator. The characteristics of the replication set are as follows: n node cluster level any node guest as the primary node all write operations in the primary node automatic failover automatic recovery start deployment create multi-instance / / create multi-instance shutdown firewall setenforce / some blocking projects mkdir-p / data/mongodb/mongodb {2jue 3pm 4} / create data directory mkdir logs/ / to / data/mongodb/ create log file directory touch logs/mongodb {2jue 3 4} .log / / create log file cd logs/ to directory chmod 777 * .log / / grant permissions / / create a multi-instance configuration file And open the service mongod-f / etc/mongod.conf-- shutdown / / modify the master configuration file and close it. Enable mongod-f / etc/mongod.conf / / enable all services one by one and enter the default port to enable replication set cp-p / etc/mongod.conf / etc/mongod2.conf / / two instance configuration files vim / etc/mongod2.conf path: / data/mongodb/logs/mongod2.log / / Syslog storage path dbPath: / data/mongodb/mongo / / data storage log port: 27018 / / end port mkdir-p / data/mongodb/ create system log and data storage log without directory cd / data/mongodb/ mkdir mongo touch mongod2.log / / create a system log file chmod 777 mongod2.log / / and give permission to mongod-f / etc/mongod2.conf / / start the multi-instance service mongo-- port 27018 / / enter mongodb 27018netstat-ntap / / View the process cfg= {"_ id": "abc" "members": [{"_ id": 0, "host": "192.168.10.5 id 27017", "priority": 100}, {"_ id": 1, "host": "192.168.10.5 id 27018", "priority": 100}, {"_ id": 2, "host": "192.168.10.5 id 27019", "priority": 0}, {"_ id": 3, "host": "192.168.10.5 host 27020" "arbiterOnly": true}]} / / pay attention to IP priority to judge priority ": 100 / / High as table quasi node priority": 0 / / low as passive node arbiterOnly ": true / / arbitration node rs.initiate (cfg) / / you can see your identity after initialization refresh. Rs.isMaster () / / View the basic additions, deletions, changes and queries of node status db.t1.insert ({" id ": 1) "name": "tom"}) / / create a collection db.t1.find () / / View the current data db.t1.update ({"id": 2}, {$set: {"name": "jack"}}) / / modify the specified content db.t1.remove ({"id": 1}) / / delete the specified data

Whether the simulated failure node experiment can automatically preempt show dbs / / display all databases use local / / enter the database show collections / / display data where log files are stored all collections db.oplog.rs.find () # View log records all operations simulation table quasi-node 1 failure mongod-f / etc/mongod.conf-- shutdown # turn off a master node service to see if there are any other mongoDB There is no preemption mongo-port 27018 # the second standard node will be selected to simulate the label Node 2 failure mongod-f / etc/mongod2.conf-- shutdown # turn off the second master node mongo-- port 27019 # at this time the passive node cannot become the master node to allow the slave node to read data rs.slaveOk () # allow default read data from the node to view replication status information the arbitration node will not replicate Information rs.help () # help information rs.printReplicationInfo () # View log file size information configured oplog size: 990MB # this log file size can be modified rs.printSlaveReplicationInfo () # it seems to be synchronized with that node # you will find that the arbitration node does not have the data replication change oplog size modified at 00:00 1. To adjust the log file size, the slave node will find the master node to synchronize each other's files so as to expand its capacity. For the master node, its own log file size is the best. If it is not enough, it can be adjusted at 00:00 by default, 990m by default, adjust the slave node 1) turn off the node service, because the slave node is a member of the replication set of the master node 2) can only be modified separately First close the service and withdraw it from the replication set, and then push it out to become a single instance. 3) use admin # first shut down the service 4) db.shutdownServer () # exit the replication set offline upgrade 5) modify the slave node configuration file 6) mongod-f / etc/mongod2.conf / / start the service 7) mongo-- port 27028 / / login 8) mongodump-- port 27028-- db local-- collection 'oplog.rs' / / oplog for integrity 9) mongo-- port 27028 / / entry port 10) use local > db.oplog .rs.drop () / delete the original file 11) db.runCommand ({create: "oplog.rs" Capped: true Size: (2 * 1024 * 1024 * 1024)}) / change the prototype to 204812) use admin > db.shutdownServer () / close the service again 13) modify the original data again at replication:replSetName: abcoplogSizeMB: 2048 / / add 14) mongod-f / etc/mongod2.conf / / restart service mongo-- port 27018 enter port 15) rs.printReplicationInfo () / / at this point it becomes 2048 pairs But valid for modification 16) rs.stepDown () # effectively generates elections in the master to allow the identity of the owner to deploy authentication replication 1. Long create a user on the primary server as rootuse admin db.createUser ({"user": "root", "pwd": "123") "roles": ["root"]}) # create user password vim / etc/mongod.conf / / modify main configuration file synchronization and have an authentication modification in the middle all configuration files security: keyFile: / usr/bin/kgcrskey1 / / verify file path clusterAuthMode: keyFile / / verify key restart service mongod-f / etc/mongod.conf-- shutdownmongod-f / etc/mongod.conf / the other servers all restart in the same way. Repeat operations to authenticate the login (first verify the master, then verify the slave) when you directly use the login command to log in to the system, you cannot use show dbs to view the data, so you need to use authentication mongo-port 27018use admindb.auth ("root", "123123")

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