mongodb3.4 sharding安装文档
下载
wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.4.tgz
下载相对应的测试机为7.0系统因此使用7的
7|6 安装以及配置没有区别
安装
二进制的软件包,解压即用
安装前配置
hugepage_transport禁用该项。 echo ''' if test -f /sys/kernel/mm/transparent_hugepage/enabled; then echo never > /sys/kernel/mm/transparent_hugepage/enabled fi if test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defrag fi ''' >>/etc/rc.local echo 0 > /proc/sys/vm/zone_reclaim_mode
配置
mongo-config
systemLog: destination: file path: "/data/logs/mongo-config.log" logAppend: true storage: dbPath: "/data/mongodb/data/mongo-config" journal: enabled: true syncPeriodSecs: 60 engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 1 statisticsLogDelaySecs: 0 journalCompressor: snappy collectionConfig: blockCompressor: snappy indexConfig: prefixCompression: true operationProfiling: slowOpThresholdMs: 200 mode: slowOp #security: ##keyFile: /data/mongodb/config/data/keyfile #authorization: enabled processManagement: fork: true pidFilePath: "/home/service/mongodb-3.4.4/mongo-config.pid" net: bindIp: 192.168.233.138,127.0.0.1 port: 30000 replication: replSetName: configRS sharding: clusterRole: configsvr #启动 #!/bin/bash MONGODB_HOME=/home/service/mongodb-3.4.4 numactl --interleave=all ${MONGODB_HOME}/bin/mongod -f /home/service/mongodb-3.4.4/conf/mongo-config.conf
mongos
systemLog: destination: file path: /data/logs/mongos.log logAppend: true processManagement: fork: true pidFilePath: /home/service/mongodb-3.4.4/mongos.pid net: port: 30001 bindIp: 192.168.233.139,127.0.0.1 sharding: configDB: configRS/192.168.233.139:30000,192.168.233.138:30000,192.168.233.135:30000
shard0 (shard1|2同0 仅需更改目录端口即可,其他两台机器配置同这个 改ip即可)
systemLog: destination: file path: /data/logs/mongo_logs/shard0.log logAppend: true storage: journal: enabled: true dbPath: /data/mongo_data/shard0 directoryPerDB: true engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 10 statisticsLogDelaySecs: 0 journalCompressor: snappy directoryForIndexes: true collectionConfig: blockCompressor: snappy indexConfig: prefixCompression: true net: port: 27017 bindIp: 192.168.233.135,127.0.0.1 replication: oplogSizeMB: 1024 replSetName: "shard0" secondaryIndexPrefetch: "all" #security: #keyFile: /data/mongo_data/keyfile #clusterAuthMode: "keyFile" #authorization: enabled sharding: clusterRole: shardsvr processManagement: fork: true pidFilePath: "/home/service/mongodb-3.2.8/shard0.pid" operationProfiling: slowOpThresholdMs: 10 mode: "slowOp"
启动config(全部启动)
#!/bin/bash MONGODB_HOME=/home/service/mongodb-3.4.4 numactl --interleave=all ${MONGODB_HOME}/bin/mongod -f /home/service/mongodb-3.4.4/conf/mongo-config.conf
启动shard012
#!/bin/bash MONGODB_HOME=/home/service/mongodb-3.4.4 numactl --interleave=all ${MONGODB_HOME}/bin/mongod -f /home/service/mongodb-3.4.4/conf/shard0.conf
初始化configsrv
任何一台即可
mongo --port 30000 # /home/service/mongodb-3.4.4/bin/mongo --port 30000 MongoDB shell version v3.4.4 connecting to: mongodb://127.0.0.1:30000/ MongoDB server version: 3.4.4 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2017-05-22T16:55:21.147+0800 I CONTROL [initandlisten] 2017-05-22T16:55:21.147+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-05-22T16:55:21.147+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-05-22T16:55:21.147+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2017-05-22T16:55:21.147+0800 I CONTROL [initandlisten] 2017-05-22T16:55:21.148+0800 I CONTROL [initandlisten] 2017-05-22T16:55:21.148+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2017-05-22T16:55:21.148+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-05-22T16:55:21.148+0800 I CONTROL [initandlisten] 2017-05-22T16:55:21.148+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2017-05-22T16:55:21.148+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-05-22T16:55:21.148+0800 I CONTROL [initandlisten] > rs.initiate({_id:"configRS0", configsvr: true, members:[ { _id:0,host:"192.168.233.135:30000"}, { _id:1,host:"192.168.233.138:30000"}, { _id:2,host:"192.168.233.139:30000"} ] }); { "ok" : 1 }
配置副本集
# /home/service/mongodb-3.4.4/bin/mongo --port 27017 MongoDB shell version v3.4.4 connecting to: mongodb://127.0.0.1:27017/ MongoDB server version: 3.4.4 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2017-05-22T18:22:56.293+0800 I CONTROL [initandlisten] 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-05-22T18:22:56.294+0800 I CONTROL [initandlisten] > use admin; switched to db admin >config = {_id:"shard0",members:[{_id:0,host:"192.168.233.135:27017",priority:1},{_id:1,host:"192.168.233.139:27017",priority:3},{_id:2,host:"192.168.233.138:27017",arbiterOnly:true}]} { "_id" : "shard0", "members" : [ { "_id" : 0, "host" : "192.168.233.135:27017", "priority" : 1 }, { "_id" : 1, "host" : "192.168.233.139:27017", "priority" : 3 }, { "_id" : 2, "host" : "192.168.233.138:27017", "arbiterOnly" : true } ] } > rs.initiate(config); { "ok" : 1 } 修改priority: shard0:PRIMARY> cfg = rs.config(); { "_id" : "shard0", "version" : 1, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "192.168.233.135:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "192.168.233.139:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 3, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "192.168.233.138:27017", "arbiterOnly" : true, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : 2000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5922bdcd0ef1a7889bb9056f") } } shard0:PRIMARY> cfg.members[0].priority = 99; 99 shard0:PRIMARY> rs.reconfig(cfg) { "ok" : 1 } 修改完成 135:27017 成为新的主节点,
副本集shard1,2同0初始化方法一样,这里不在演示。(注意priority值越大就越优先成为主节点)
添加sharding
任意一台登陆mongos 30001端口
# /home/service/mongodb-3.4.4/bin/mongo --port 30001 MongoDB shell version v3.4.4 connecting to: mongodb://127.0.0.1:30001/ MongoDB server version: 3.4.4 Server has startup warnings: 2017-05-22T19:16:52.825+0800 I CONTROL [main] 2017-05-22T19:16:52.825+0800 I CONTROL [main] ** WARNING: Access control is not enabled for the database. 2017-05-22T19:16:52.825+0800 I CONTROL [main] ** Read and write access to data and configuration is unrestricted. 2017-05-22T19:16:52.825+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended. 2017-05-22T19:16:52.825+0800 I CONTROL [main] mongos> db.runCommand({"addShard":"shard2/192.168.233.135:29017,192.168.233.138:29017,192.168.233.139:29017"}); { "shardAdded" : "shard2", "ok" : 1 } mongos> db.runCommand({"addShard":"shard1/192.168.233.135:28017,192.168.233.138:28017,192.168.233.138:29017"}); { "shardAdded" : "shard1", "ok" : 1 } mongos> db.runCommand({"addShard":"shard0/192.168.233.135:27017,192.168.233.138:27017,192.168.233.139:27017"}); { "shardAdded" : "shard0", "ok" : 1 } #分片添加完成
开启库分片&&开启表分片
mongos> use admin switched to db admin mongos> db.runCommand({enablesharding :"test"}); { "ok" : 1 } mongos> sh.shardCollection("test.Log", { id: 1}); { "collectionsharded" : "test.Log", "ok" : 1 }
验证
mongo用–auth选项开启认证,但是在开启认证之前必须有一个有userAdminAnyDatabase角色的管理员账户。
因此首先我们要创建一个账户,然后修改配置文件加上auth选项,重启mongo。
mongo内置用户角色说明:
- 数据库用户角色:read、readWrite;
- 数据库管理角色:dbAdmin、dbOwner、userAdmin;
- 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
- 备份恢复角色:backup、restore;
- 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
- 超级用户角色:root (如果用户同时有dbOwner 、userAdmin、userAdminAnyDatabase三个角色,就间接或直接提供了系统超级用户的访问)
创建用户
mongos> use admin; switched to db admin mongos> db.createUser({ ... user: "admin", ... pwd: "admin", ... roles: [{role:"root",db:"admin"}] ... }); Successfully added user: { "user" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] }
关闭三台机器上的mongod和configsvr以及mongos服务
将配置文件注释的key选项打开
security: keyFile: /data/mongodb/config/data/keyfile authorization: enabled
生成keyfile
openssl rand -base64 753 >keyfile
chmod 600 keyfile
注意权限600
将keyfile 复制到各自的目录 (所有机器用一个key)
configsrv mongos shard0 shard1 shard2
启动脚本
#!/bin/sh # #chkconfig: 2345 80 90 #description: mongodb MONGODB_HOME=/home/service/mongodb-3.4.4 start() { numactl --interleave=all ${MONGODB_HOME}/bin/mongod -f ${MONGODB_HOME}/conf/configsvr.conf } stop() { numactl --interleave=all ${MONGODB_HOME}/bin/mongod -f ${MONGODB_HOME}/conf/configsvr.conf --shutdown } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac