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

Deploy a MongoDB sharding cluster on CentOS7

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

Share

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

Overview

MongoDB sharding is a method of using multiple servers to store data to support huge data storage and data manipulation. Slicing technology can meet the needs of the massive growth of MongoDB data. When one MongoDB server is not enough to store massive data or provide acceptable read and write throughput, we can split the data on multiple servers, so that the database system can store and process more data.

The composition of MongoDB sharding cluster Shard: sharding server, used to store actual data blocks, in the actual production environment, a shard server role can be undertaken by several servers composed of a Replica Set to prevent a single point of failure of the host. Config Server: configuration server that stores configuration information for the entire sharded cluster, including chunk information. Routers: the router through which the client accesses and makes the entire cluster look like a single database, and the front-end applications can be used transparently. Deploy MongoDB sharding cluster

Experimental environment:

1 routing instance (port 27017) 1 configuration instance (port 37017) 2 shard instances (port 470171.47018)

1. Install MongoDB3.2

[root@localhost] # yum install openssl-devel-y [root@localhost tomcat] # tar zxvf mongodb-linux-x86_64-3.2.1.tgz-C / opt/

two。 Create 4 instances

[root@localhost opt] # mkdir-p / data/mongodb/mongodb {1 ulimit-p / data/mongodb/mongodb {1 ulimit-n 25000 / / maximum number of processes / / [root@localhost logs] # ulimit-u 25000 / / maximum number of files / /

3. Deployment configuration server

[root@localhost bin] # vim mongodb1.confport=37017dbpath=/data/mongodb/mongodb1logpath=/data/mongodb/logs/mongodb1.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1configsvr=true~

1) when a node runs out of memory, allocate memory from other nodes

[root@localhost bin] # sysctl-w vm.zone_reclaim_mode=0 / / if the kernel parameter is 0 Then the system will tend to allocate memory from other nodes / / [root@localhost bin] # echo never > / sys/kernel/mm/transparent_hugepage/ enabled [root @ localhost bin] # echo never > / sys/kernel/mm/transparent_hugepage/ defragroot @ localhost bin] # ln-s / usr/local/mongodb/bin/mongo / usr/bin/mongo [root@localhost bin] # ln-s / usr/local/mongodb/bin/mongod / usr/bin/mongod

2) start the configuration server

[root@localhost bin] # mongod-f mongodb1.conf

4. Configure sharding server

[root@localhost bin] # vim mongodb2.conf port=47017dbpath=/data/mongodb/mongodb2logpath=/data/mongodb/logs/mongodb2.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=true [root@localhost bin] # vim mongodb3.conf port=47018dbpath=/data/mongodb/mongodb3logpath=/data/mongodb/logs/mongodb3.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=true

1) start mongodb2 and mongodb3

[root@localhost bin] # mongod-f mongodb2.conf [root@localhost bin] # mongod-f mongodb3.conf

5. Start the routing server

[root@localhost bin] #. / mongos-- port 27017-- fork-- logpath=/usr/local/mongodb/bin/route.log-- configdb 192.168.126.141 port 37017-- chunkSize 1* shows * * 2018-07-18T09:21:53.507+0800 W SHARDING [ Main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.about to fork child process Waiting until server is ready for connections.forked process: 3580child process started successfully, parent exiting

6. Enable sharding server

Empty under [root@localhost bin] # mongomongos > show dbsconfig 0.031GBmongos > sh.status () / / # shards There is no sharding server / / mongos > sh.addShard ("192.168.126.204 sh.addShard 47017") mongos > sh.status () shards: {"_ id": "shard0000", "host": "192.168.126.141v 47017"} {"_ id": "shard0001", "host": "192.168.126.141purl 47018"}

1) create Kgc database, create users collection and insert 100000 pieces of data

Mongos > use kgcmongos > db.createCollection ('users') mongos > for (var iTunes 1 I db.users.find () {"_ id": ObjectId ("5b4e9b580f25d0730817aea1"), "id": 1, "name": "jack1"} {"_ id": ObjectId ("5b4e9b580f25d0730817aea2"), "id": 2, "name": "jack1"} {"_ id": ObjectId ("5b4e9b580f25d0730817aea3"), "id": 3, "name": "jack1"} {"_ id": ObjectId ("5b4e9b580f25d0730817aea4") "id": 4, "name": "jack1"} {"_ id": ObjectId ("5b4e9b590f25d0730817aea5"), "id": 5, "name": "jack1"} {"_ id": ObjectId ("5b4e9b590f25d0730817aea6"), "id": 6, "name": "jack1"} {"_ id": ObjectId ("5b4e9b590f25d0730817aea7"), "id": 7 "name": "jack1"} {"_ id": ObjectId ("5b4e9b590f25d0730817aea8"), "id": 8, "name": "jack1"} {"_ id": ObjectId ("5b4e9b590f25d0730817aea9"), "id": 9, "name": "jack1"} {"_ id": ObjectId ("5b4e9b590f25d0730817aeaa"), "id": 10 "name": "jack1"}. / / omitted / /

7. Realize the function of slicing

[root@localhost bin] #. / mongoimport-d kgc-c users-- file / opt/testdb.txtmongos > show dbsconfig 0.031GBkgc 0.078GBmongos > use kgcswitched to db kgcmongos > show tablessystem.indexesusersmongos > sh.status () / / View database sharding information / / databases: {"_ id": "kgc", "primary": "shard0000", "partitioned": false} / / Database has not been sliced / /

1) enable database fragmentation

Mongos > sh.enableSharding ("kgc") / / enable database sharding / / mongos > sh.status () databases: {"_ id": "kgc", "primary": "shard0000", "partitioned": true} mongos > db.users.createIndex ({"id": 1}) / / create index / mongos > sh.shardCollection ("kgc.users") for a pair of users tables {"id": 1}) / / Table fragmentation / / mongos > sh.status () shards: {"_ id": "shard0000", "host": "192.168.126.141sh.status 47017"} {"_ id": "shard0001", "host": "192.168.126.141sh.status 47018"}

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