#创建mongo相应的目录
mkdir -pv /data/app/mongodb/conf mkdir -pv /data/app/mongodb/{configset,shared1,shared2,shared3}/log mkdir -pv /data/mongodb/{configset,shared1,shared2,shared3}/data
#创建于用户,给权限
useradd -s /sbin/nologin -M mongod chown -R mongod.mongod /data/mongodb/ chown -R mongod.mongod /data/app/mongodb/
#创建证书,将这个证书copy到另外2个节点
openssl rand -base64 753 > /data/app/mongodb/conf/keyFile.key chmod 0600 /data/app/mongodb/conf/keyFile.key chown mongod.mongod /data/app/mongodb/conf/keyFile.key
#设置mongo环境变量
echo "export PATH=$PATH:/data/app/mongodb/bin" > /etc/profile.d/mongodb.sh source /etc/profile.d/mongodb.sh
#秘钥分发
scp /data/app/mongodb/conf/keyFile.key [email protected]:/data/app/mongodb/conf/keyFile.key scp /data/app/mongodb/conf/keyFile.key [email protected]:/data/app/mongodb/conf/keyFile.key scp /data/app/mongodb/conf/keyFile.key [email protected]:/data/app/mongodb/conf/keyFile.key
mv mongodb /data/app/mongodb/
yum install -y numactl
#创建config节点的服务
cat /etc/systemd/system/mongo-configset.service [Unit] Description=High-performance, schema-free document-oriented database After=network.target [Service] User=mongod Type=forking ExecStart=/usr/bin/numactl --interleave=all /data/app/mongodb/bin/mongod --config /data/app/mongodb/conf/configset.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/data/app/mongodb/bin/mongod --shutdown --config /data/app/mongodb/conf/configset.conf LimitNOFILE=65535 [Install] WantedBy=multi-user.target systemctl daemon-reload systemctl start mongo-configset chown -R mongod.mongod /data/
#配置config节点配置文件
cat /etc/systemd/system/mongo-configset.service [Unit] Description=High-performance, schema-free document-oriented database After=network.target [Service] User=mongod Type=forking ExecStart=/usr/bin/numactl --interleave=all /data/app/mongodb/bin/mongod --config /data/app/mongodb/conf/configset.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/data/app/mongodb/bin/mongod --shutdown --config /data/app/mongodb/conf/configset.conf LimitNOFILE=65535 [Install] WantedBy=multi-user.target systemctl daemon-reload systemctl start mongo-configset chown -R mongod.mongod /data/
#检查端口是否启动
ss -tnl | grep 2100
#添加config节点
mongo 192.168.1.101:21000 rs.add("192.168.1.102:21000") rs.add("192.168.1.103:21000") rs.status()
#添加用户
mongo 192.168.1.101:21000 use admin db.createUser({user:"root",pwd:'密码',roles:["root","clusterAdmin","userAdminAnyDatabase","readWriteAnyDatabase"]}) db.auth("root","密码")
#用户创建完毕后修改config节点配置文件,放开认证部分
systemctl restart mongo-configset
#下次登录使用户用户名密码登录
mongo -u root -p root 192.168.1.101:21000/admin
配置shard配置文件,mongodb1,mongodb2,mongodb3都要配置shard1.conf,shard2.conf,shard3.conf,注意修改名称和端口号
[root@mongodb1 ~]# cat /data/app/mongodb/conf/shared1.conf storage: dbPath: /data/mongodb/shared1/data journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 4 systemLog: destination: file logAppend: true path: /data/app/mongodb/shared1/log/shared1.log replication: replSetName: shared1 processManagement: fork: true pidFilePath: /data/app/mongodb/shared1/log/shared1.pid net: port: 27001 bindIp: 0.0.0.0 security: keyFile: /data/app/mongodb/conf/keyFile.key authorization: enabled sharding: # 分片集群中当前实例的角色(configsvr:配置中心实例,shardsvr:分片实例) clusterRole: shardsvr
#配置shard服务 mongo-27001,mongo-27002,mongo-27003
[root@mongodb1 ~]# cat /etc/systemd/system/mongo-27001.service [Unit] Description=High-performance,schema-free document-oriented database After=network.target [Service] User=mongod Type=forking ExecStart=/usr/bin/numactl --interleave=all /data/app/mongodb/bin/mongod --config /data/app/mongodb/conf/shared1.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/data/app/mongodb/bin/mongod --shutdown --config /data/app/mongodb/conf/shared1.conf LimitNOFILE=65535 [Install] WantedBy=multi-user.target
#shard1初始化,并添加其他两个节点
mongo 192.168.1.101:27001 rs.initiate() rs.add("192.168.1.102:27001") rs.add("192.168.1.103:27001") rs.status()
#shard2初始化,并添加其他两个节点
mongo 192.168.1.102:27002 rs.initiate() rs.add("192.168.1.101:27002") rs.add("192.168.1.103:27002") rs.status()
#shard3初始化,并添加其他两个节点
mongo 192.168.1.103:27003 rs.initiate() rs.add("192.168.1.101:27003") rs.add("192.168.1.102:27003") rs.status()
use admin db.createUser({user:"root",pwd:'密码',roles:["root","clusterAdmin","userAdminAnyDatabase","readWriteAnyDatabase"]}) db.auth("root","密码")
#用户创建完毕后所有节点配置文件开启认证
systemctl restart mongo-27001 mongo-27002 mongo-27003 mongo -u root -p "密码" 192.168.1.101:27001/admin
#配置路由服务
mkdir -pv /data/app/mongodb/mongos/log chown -R mongod.mongod /data/app/mongodb
#配置mongos路由配置文件
[root@mongodb1 ~]# cat /data/app/mongodb/conf/mongos.conf systemLog: destination: file logAppend: true path: /data/app/mongodb/mongos/log/mongos.log processManagement: fork: true pidFilePath: /data/app/mongodb/mongos/log/mongos.pid net: port: 20000 bindIp: 0.0.0.0 sharding: configDB: configset/192.168.1.101:21000,192.168.1.102:21000,192.168.1.103:21000 security: keyFile: /data/app/mongodb/conf/keyFile.key
#配置mongos服务
[root@mongodb1 ~]# cat /etc/systemd/system/mongos.service [Unit] Description=High-performance, shcema-free document-oriented database After=network.target [Service] User=mongod Type=forking ExecStart=/usr/bin/numactl --interleave=all /data/app/mongodb/bin/mongos --config /data/app/mongodb/conf/mongos.conf ExecReload=/bin/kill -s HUP $MAINPID LimitNOFILE=65535 [Install] WantedBy=multi-user.target
#mongos添加shard成员
sh.addShard("shard1/192.168.1.101:27001") sh.addShard("shard1/192.168.1.102:27001") sh.addShard("shard1/192.168.1.103:27001") sh.addShard("shard1/192.168.1.101:27002") sh.addShard("shard1/192.168.1.102:27002") sh.addShard("shard1/192.168.1.103:27002") sh.addShard("shard1/192.168.1.101:27003") sh.addShard("shard1/192.168.1.102:27003") sh.addShard("shard1/192.168.1.103:27003") sh.status()
#测试分片
mongo -u root -p "密码" 192.168.1.101:20000/admin
#查看分片节点
db.getSiblingDB("config").shards.find()
#查看那些数据库开启了分片
db.getSiblingDB("config").databases.find()
#创建并进入testdb库
use testdb
#开启test01库的分片
sh.enableSharding("testkdb")
#根据id建进行hash分片
sh.shardCollection("testdb.test01",{"id":"hashed"})
#模拟插入9999条数据
use testdb for(i=1;i<10000;i++){db.test01.insert({"id":i,"name":"aaa"})}
#查看分片的片建
use config db.collections.find().pretty()
#hash分片数据验证,hashed分片会尽可能均匀分布在每个primary节点上
shard1: mongo -u root -p "密码" 192.168.1.101:27001/admin use testdb db.test01.count() shard2: mongo -u root -p "密码" 192.168.1.102:27002/admin use testdb db.test01.count() shard3: mongo -u root -p "密码" 192.168.1.103:27003/admin use testdb db.test01.count()
标签:mongodb4.4,mongo,mongodb,app,192.168,centos7,conf,分片,data From: https://www.cnblogs.com/fengzi7314/p/17296981.html