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

Build a highly available mongo cluster

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

Share

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

Brief introduction:

MongoDB is a scalable, high-performance distributed document storage database, written by C language, which aims to provide scalable high-performance data storage solutions for web applications. It is characterized by high performance, easy to deploy, easy to use, and it is very convenient to store data. MongoDB is a product between relational database and non-relational database, which is the most functional and most like relational database in non-relational database. The data structure he supports is very loose, which is similar to json's bjson format, so it can store more complex data types. The most important feature of Mongo is that the query language it supports is very powerful, and its syntax is somewhat similar to that of object-oriented query language. It can almost achieve most of the functions of single table query in relational database, and it also supports the establishment of data indexing. It is characterized by high performance, easy to deploy, easy to use, and it is very convenient to store data.

Functional features:

For collection storage, it is easy to store data of object type.

Mode freedom.

Dynamic query is supported.

Full indexing is supported, including internal objects.

Query is supported.

Replication and failure recovery are supported.

Use efficient binary data storage, including large objects such as video, etc.

Automatically handle fragments to support scalability at the cloud computing level

Support for RUBY,PYTHON,JAVA,C++,PHP and other languages.

The file is stored in BSON (an extension of JSON)

Accessible through the network

Construction method:

The cluster is built in the way of replica set + shard, which is used in the production environment.

Network Topology:

Build process:

① enables the configuration server (config):

172.16.2.230:mongod-port 20000-dbpath data/-logpath log/config.log-logappend &

172.16.2.231:mongod-port 20000-dbpath data/-logpath log/config.log-logappend &

172.16.2.232:mongod-port 20000-dbpath data/-logpath log/config.log-logappend &

② enable routing (mongos):

172.16.2.230:mongos-- port 27017-configdb 172.16.2.230 configdb 20000172.16.2.231 configdb 20000172.16.2.232 logpath data/mongos.log-logappend &

172.16.2.231:mongos-- port 27017-configdb 172.16.2.230 configdb 20000172.16.2.231 configdb 20000172.16.2.232 logpath data/mongos.log-logappend &

172.16.2.232:mongos-- port 27017-configdb 172.16.2.230 configdb 20000172.16.2.231 configdb 20000172.16.2.232 logpath data/mongos.log-logappend &

Startup and configuration of the ③ replica set:

Part one:

172.16.2.233:mongod-- port 27017-- replSet shard1-- dbpath data/-- logpath log/shard1.log-logappend &

172.16.2.234:mongod-- port 27017-- replSet shard1-- dbpath data/-- logpath log/shard1.log-logappend &

172.16.2.235:mongod-- port 27017-- replSet shard1-- dbpath data/-- logpath log/shard1.log-logappend &

Initialize the replica set:

172.16.2.233:mongo localhost:27017/admin

> rsconfig= {"_ id": "shard1", "members": [{"_ id": 0, "host": "172.16.2.233vam27017"}]}

> rs.initiate (rs.config) # initialization

> rs.add ("172.16.2.234 purl 27017")

> rs.add ("172.16.2.235 purl 27017")

> rs.conf () # check the configuration status (check whether the shards are dead)

Slice 2:

172.16.2.236:mongod-- port 27017-- replSet shard2-- dbpath data/-- logpath log/shard1.log-logappend &

172.16.2.237:mongod-- port 27017-- replSet shard2-- dbpath data/-- logpath log/shard1.log-logappend &

172.16.2.238:mongod-- port 27017-- replSet shard2-- dbpath data/-- logpath log/shard1.log-logappend &

Initialize the replica set:

172.16.2.236:mongo localhost:27017/admin

> rsconfig= {"_ id": "shard2", "members": [{"_ id": 0, "host": "172.16.2.236pur27017"}]}

> rs.initiate (rs.config) # initialization

> rs.add ("172.16.2.237purl 27017")

> rs.add ("172.16.2.238 purl 27017")

> rs.conf () # check the configuration status (check whether the shards are dead)

Part 3:

172.16.2.239:mongod-- port 27017-- replSet shard3-- dbpath data/-- logpath log/shard1.log-logappend &

172.16.2.240:mongod-- port 27017-- replSet shard3-- dbpath data/-- logpath log/shard1.log-logappend &

172.16.2.241:mongod-- port 27017-- replSet shard3-- dbpath data/-- logpath log/shard1.log-logappend &

Initialize the replica set:

172.16.2.239:mongo localhost:27017/admin

> rsconfig= {"_ id": "shard3", "members": [{"_ id": 0, "host": "172.16.2.239mot27017"}]}

> rs.initiate (rs.config) # initialization

> rs.add ("172.16.2.240virtual 27017")

> rs.add ("172.16.2.241purl 27017")

> rs.conf () # check the configuration status (check whether the shards are dead)

④ adds shards (replica sets) to the cluster:

172.16.2.230:mongo 172.16.2.230:27017/admin

> db.runCommand ({"addshard": "shard1/172.16.2.233:27017172.16.2.234:27017172.16.2.235:27017"})

> db.runCommand ({"addshard": "shard2/172.16.2.236:27017172.16.2.237:27017172.16.2.238:27017"})

> db.runCommand ({"addshard": "shard3/172.16.2.239:27017172.16.2.240:27017172.16.2.241:27017"})

> db.runCommand ({"listshard": 1}) # View the added results (check health status)

⑤ activates db and colletion sharding features:

172.16.2.230:mongo 172.16.2.230:27017/admin

> db.runCommand ({"enablesharding": "database_1"})

Note: activate the databas_1 sharding function, which allows the database to praise shard. If you do not execute this command, the data can only be stored on one shard. After that, different collections will be placed on different shard, but the same collection will not be split.

> db.runCommand ({"shardcollection": "database_1.collection_1", "key": {"_ id": 1}}) # the chip key value "_ id" splits the collection

⑥ to view the sharding:

> use database_1

> db.collection_1.stats () # View the collection sharding status

> db.printShardingStatus () # View database sharding

> printShardingStatus (db.getsisterDB ("config"), 1) # you can use this command to view when there are too many chunk to display.

At this point, the mongo cluster has been built and is suitable for the production environment. However, because this framework has three interfaces (routing), many times only one interface is used, resulting in the other two being wasted. To avoid wasting resources, you can do a load balance in front of three APIs, such as haproxy.

Pay:

Add shards: db.runCommand ({addshard: "ip:port", allowLocal:true})

Delete shard: db.runCommand ({removeshard: "ip:port"})

Data backup:. / bin/mongodump-h remote database to be backed up ip-port 27017-d database to be backed up-c collection to be backed up (optional)-o backup somewhere

Data recovery:. / bin/mongorestore-h remote database to restore ip-p 27017-d database to be restored-location of previous backup by drop / database name

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

Wechat

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

12
Report