Mongodb 3.4.1 sharding replica yaml配置

mongodb的版本升级太快了,而且版本之间的变化很多。

在此吐槽下国人写文档的陋习:
1 .网上人写的很多都不全,有些配置都是错的。可能大多数人的习惯就是这样吧。
2 . 写的技术文档遮遮掩掩,不全。好像怕一些关键参数被别人知道了。
其实过了很久,即使自己也不一定能想起那些没有记录下来的关键配置。
损人不利己。

下面开始配置:

1 .环境介绍:
mongodb版本 3.4.1
系统版本 : debian 7
网络规划:
config 端口 47017
mongos 端口 37017
sharding 端口 27017

3台机器 :
192.168.1.9
192.168.1.10
192.168.1.3

各个层做什么作用的 ,这里不再多说,可以参考我以前的记录
http://blog.csdn.net/hkyw000/article/details/52026242

2 . 创建mongodb用户

groupadd -g 20001 mongodb
useradd -u 20001 -g mongodb -d /data/mongodb/install -s /bin/bash mongodb 

3 . 创建目录:

cd /data
mkdir /data/mongodb
cd mongodb
mkdir  mongos
mkdir mongos/log
mkdir config
mkdir config/{data,log}
mkdir shard1
mkdir shard2
mkdir shard3
mkdir shard1/{data,log}
mkdir shard2/{data,log}
mkdir shard3/{data,log}
chown mongodb:mongodb /data/mongodb -R

4.解压 并配置mongodb 家目录pfile

tar vzxf mongodb-linux-x86_64-debian71-3.4.1.tgz -C /data/mongodb/
mv mongodb-linux-x86_64-debian71-3.4.1/ install/

su -  mongodb
vi ~/.bash_profile

export MONGODB_HOME=/data/mongodb/install
export PATH=/usr/sbin:$MONGODB_HOME/bin:/usr/local/bin:$PATH
export PATH

5 .参数更改:

3.4版本或者3以上版本都建议 以numactl启动,为了是提升CPU的性能。具体可以去研究下什么是numa。debian自己去下载numa,现在稳定的是numactl-2.0.10。
redhat 可以直接用 yum 安装。

因为用到numactl 所以一些配置要改:

echo 0 > /proc/sys/vm/zone_reclaim_mode
vi /proc/sys/vm/zone_reclaim_mode
sysctl -w vm.zone_reclaim_mode=0

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

3.4版本建议XFS 文件系统,我这里不方便更改依然用的是ext4.
后面启动numactl的时候还报了个错,这里提前写出来,方便大家更改

numactl: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

这个是因为找不到库文件libnuma.so.1。 不是文件不存在,lib的目录没有加载到配置文件中。

# cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

# echo "/usr/local/lib" >>/etc/ld.so.conf
# ldconfig

6 . 配置 3个节点的 mongodb用户能够无密码互访。
这点可以参考我的博客文章:
http://blog.csdn.net/hkyw000/article/details/53318894
注意是 mongodb 到mongodb用户能够互访,不是root 到mongodb。
家目录注意好。

7.配置config节点

3.0以上配置文件支持yaml文件类型。至于yaml文件比conf文件的优势,现在不清楚。可能有优势吧。知道的可以留言交流下:

这里注意下: 这种文件类型很注重格式,即空格。 及每个属性下面的配置参数,必须比属性头的位置 有缩进。 中间多一个空格或者少一个空个都会出现报错。

vi /data/mongodb/config/mongod.yaml---congfig层的配置

systemLog:
   destination: file
   path: "/data/mongodb/config/log/mongod.log"
   logAppend: true
storage:
   dbPath: "/data/mongodb/config/data"
   journal:
      enabled: true
#   directoryPerDB: true
   syncPeriodSecs: 60
   engine: wiredTiger # MongoDB 3.0版本开始务必使用WT引擎
   wiredTiger:
      engineConfig:
         cacheSizeGB: 16
         statisticsLogDelaySecs: 0
         journalCompressor: snappy
#         directoryForIndexes: true
      collectionConfig:
         blockCompressor: snappy
      indexConfig:
         prefixCompression: true
operationProfiling:
   slowOpThresholdMs: 200
   mode: slowOp
#security:
   ##keyFile: /data/mongodb/config/data/keyfile
   ##authorization: enabled
processManagement:
   fork: true
   pidFilePath: /data/mongodb/config/data/mongodb.pid
net:
   bindIp: 192.168.1.9,127.0.0.1
   port: 47017
replication:
  replSetName: configRS
sharding:
##分片角色
 clusterRole: configsvr

然后启动
numactl --interleave=all /data/mongodb/install/bin/mongod -f /data/mongodb/config/mongod.conf

这里注意要写 clusterRole: configsvr ,3.4版本建议的模式。
每个节点的相应IP自己改下,写3个不同的yaml文件,3台机器分别启动。

8.初始化config

只用登录一台即可

mongo --port 47017

rs.initiate({
_id: "configRS",
configsvr: true,
members: [
{ _id: 0, host: "192.168.1.9:47017" },
{ _id: 1, host: "192.168.1.10:47017" },
{ _id: 2, host: "192.168.1.3:47017" }
]
} );


{ "ok" : 1 }

9 . 配置mongos 的yaml文件

vi /data/mongodb/mongos/mongos.yaml

  ##日志配置
systemLog:
 destination: file
##日志位置
 path: /data/mongodb/mongos/log/mongos.log
 logAppend: true
processManagement:
  fork: true
  pidFilePath: /data/mongodb/mongos/mongos.pid
##网路配置
net:
##端口配置
 port: 37017
 bindIp: 192.168.1.9,127.0.0.1
##分片配置
sharding:
##指定config server
 configDB:   configRS/192.168.1.9:47017,192.168.1.10:47017,192.168.1.3:47017
##security:
  ##keyFile: "/data/mongodb/mongos/keyfile"
  ##clusterAuthMode: "keyFile"

然后启动mongos
numactl --interleave=all /data/mongodb/install/bin/mongos -f /data/mongodb/mongos/mongos.yaml

每个节点都配置一下,对应IP 改掉,然后分别启动mongos

10 . 启动sharding

vi /data/mongodb/shard1/shard1.yaml


systemLog:
 destination: file
###日志存储位置
 path: /data/mongodb/shard1/log/shard1.log
 logAppend: true
storage:
##journal配置
 journal:
  enabled: true
##数据文件存储位置
 dbPath: /data/mongodb/shard1/data
##是否一个库一个文件夹
 directoryPerDB: true
##数据引擎
 engine: wiredTiger
##WT引擎配置
 wiredTiger:
  engineConfig:
##WT最大使用cache(根据服务器实际情况调节)
   cacheSizeGB: 10
   statisticsLogDelaySecs: 0
   journalCompressor: snappy
##是否将索引也按数据库名单独存储
   directoryForIndexes: true
##表压缩配置
  collectionConfig:
   blockCompressor: snappy
##索引配置
  indexConfig:
   prefixCompression: true
##端口配置
net:
 port: 27017
 bindIp: 192.168.1.10,127.0.0.1

replication:
  oplogSizeMB: 1024
  replSetName: "shard1"
  secondaryIndexPrefetch: "all"

#security:
#  keyFile: "/data/mongodb/shard1/data/keyfile"
#  clusterAuthMode: "keyFile"
#  authorization: enabled

sharding:
  clusterRole: shardsvr


processManagement:
  fork: true
  pidFilePath: "/data/mongodb/shard1/data/shard1.pid"

operationProfiling:
  slowOpThresholdMs: 10
  mode: "slowOp"

shard2 ,shard3 也同样配置。然后启动3个shard

numactl --interleave=all /data/mongodb/install/bin/mongod -f /data/mongodb/shard1/shard1.yaml
numactl --interleave=all /data/mongodb/install/bin/mongod -f /data/mongodb/shard2/shard2.yaml
numactl --interleave=all /data/mongodb/install/bin/mongod -f /data/mongodb/shard3/shard3.yaml

11 . 配置sharding的冗余

mongo 192.168.1.9:27017/admin

config = {_id:"shard1",members:[
{_id:0,host:"192.168.1.9:27017",priority:1},
{_id:1,host:"192.168.1.10:27017",priority:2},
{_id:2,host:"192.168.1.3:27017",arbiterOnly:true}
]}

rs.initiate(config);


mongo 192.168.1.10:27018/admin

config = {_id:"shard2",members:[
{_id:0,host:"192.168.1.9:27018",arbiterOnly:true},
{_id:1,host:"192.168.1.10:27018",priority:1},
{_id:2,host:"192.168.1.3:27018",priority:2}
]}

rs.initiate(config);



mongo 192.168.1.3:27019/admin
config = {_id:"shard3",members:[
{_id:0,host:"192.168.1.9:27019",priority:2},
{_id:1,host:"192.168.1.10:27019",arbiterOnly:true},
{_id:2,host:"192.168.1.3:27019",priority:1}
]}
rs.initiate(config);

12 . 进入mongos把sharding串联起来:(注意好端口)

mongo 192.168.1.9:37017/admin

mongos> db.runCommand({"addShard":"shard1/192.168.1.9:27017,192.168.1.10:27017,192.168.1.3:27017"});



db.runCommand({"addshard":"shard2/192.168.1.9:27018,192.168.1.10:27018,192.168.1.3:27018"});

db.runCommand({"addshard":"shard3/192.168.1.9:27019,192.168.1.10:27019,192.168.1.3:27019"})

13 . 开启库分片:

db.runCommand({enablesharding :"test"});

开启表分片:

sh.shardCollection("test.Log", { id: 1})

14 .开启认证。

这里开启认证,要注意用户密码的加密方式,

credentials” : { “MONGODB-CR”—-mongVUE 可以连接。
“credentials” : { “SCRAM-SHA-1” —-mongoVUE等软件不能连接。

这里可以参考我以前的文章把 version改成3 即可。

http://blog.csdn.net/hkyw000/article/details/51671173

关闭所有mongod 和mongos进程

[root@test~]# openssl rand -base64 753 >keyfile
[root@test~]# chmod 600 keyfile ---这点很重要,注意是600
[root@test~]# cat keyfile 

rsync -av keyfile  /data/mongodb/config/data/
rsync  -av keyfile  /data/mongodb/mongos/
rsync -av keyfile /data/mongodb/shard1/data/
rsync -av keyfile /data/mongodb/shard2/data/
rsync -av keyfile /data/mongodb/shard3/data/

chown mongodb:mongodb /data/mongodb/ -R

重启所有进程,认证完成。
发布了75 篇原创文章 · 获赞 15 · 访问量 14万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章