MongoDB集群配置(一)
本实验环境是:mongodb v3.4.2, 三台 CentOS v7.2,mongodb不同版本可能配置略有差异。
mongodb原理
mongodb可以通过命令行配置参数,也可以通过配置文件进行参数配置。
包括:mongos、config server、shard server几个部分。
Config Server
Shard Server
mongos
mongodb环境搭建
Create user and directories
配置服务
**1. shard1 .conf**
[mongo@localhost config]$ pwd
/data/mongo/config
[mongo@localhost config]$ cat shard1.conf
systemLog:
destination: file
path: /data/mongo/logs/shard1.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongo/shard1/shard1.pid"
net:
port: 10001
storage:
dbPath: "/data/mongo/shard1"
engine: wiredTiger
journal:
enabled: true
directoryPerDB: true
operationProfiling:
slowOpThresholdMs: 10
mode: "slowOp"
replication:
oplogSizeMB: 50
replSetName: "shard1"
secondaryIndexPrefetch: "all"
sharding:
clusterRole: shardsvr
**2. shard2 .conf**
[mongo@localhost config]$ cat shard2.conf
systemLog:
destination: file
path: /data/mongo/logs/shard2.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongo/shard1/shard2.pid"
net:
port: 10002
storage:
dbPath: "/data/mongo/shard2"
engine: wiredTiger
journal:
enabled: true
directoryPerDB: true
operationProfiling:
slowOpThresholdMs: 10
mode: "slowOp"
replication:
oplogSizeMB: 50
replSetName: "shard2"
secondaryIndexPrefetch: "all"
sharding:
clusterRole: shardsvr
**3. shard3 .conf**
[mongo@localhost config]$ cat shard3.conf
systemLog:
destination: file
path: /data/mongo/logs/shard3.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongo/shard1/shard3.pid"
net:
port: 10003
storage:
dbPath: "/data/mongo/shard3"
engine: wiredTiger
journal:
enabled: true
directoryPerDB: true
operationProfiling:
slowOpThresholdMs: 10
mode: "slowOp"
replication:
oplogSizeMB: 50
replSetName: "shard3"
secondaryIndexPrefetch: "all"
sharding:
clusterRole: shardsvr
**4. configsvr.conf**
[mongo@localhost config]$ cat configsvr.conf
systemLog:
destination: file
path: /data/mongo/logs/configsvr.log
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongo/configsvr/configsvr.pid"
net:
port: 10004
storage:
dbPath: "/data/mongo/configsvr"
engine: wiredTiger
journal:
enabled: true
replication:
replSetName: cfgRepSet
sharding:
clusterRole: configsvr
**5. mongos.conf**
[mongo@localhost config]$ cat mongos.conf
systemLog:
destination: file
path: /data/mongo/logs/mongos.log
logAppend: true
processManagement:
fork: true
pidFilePath: /data/mongo/mongos/mongos.pid
net:
port: 10005
sharding:
configDB: cfgRepSet/192.168.198.20:10004,192.168.198.21:10004,192.168.198.25:10004
在一台服务器上配置完后,直接把整个目录复制到另外两台服务器上。
关闭防火墙和selinux
启动sharded server
[mongo@localhost bin]$ ./mongod -f /data/mongo/config/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 20817
child process started successfully, parent exiting
[mongo@localhost bin]$ ./mongod -f /data/mongo/config/shard2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 20912
child process started successfully, parent exiting
[mongo@localhost bin]$ ./mongod -f /data/mongo/config/shard3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 20938
child process started successfully, parent exiting
[mongo@localhost bin]$ ./mongo --port 10001
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:10001/
MongoDB server version: 3.4.2
Server has startup warnings:
2017-03-07T18:47:57.764+0800 I CONTROL [initandlisten]
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten]
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten]
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten]
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-03-07T18:47:57.765+0800 I CONTROL [initandlisten]
shard1:SECONDARY>
根据提示需要在所有服务器上都关闭‘transparent_hugepage’服务。