时间:2021-07-01 10:21:17 帮助过:10人阅读
生成鉴权需要的keyfile
openssl rand -base64 1024 > /usr/local/mongodb/keyfile chmod /usr/local/mongodb/keyfile
副本模式启动configsvr
mongod --dbpath /usr/local/mongodb/data/c0 --logpath /usr/local/mongodb/log/c0.log --fork --smallfiles --port 27020 --replSet cs --configsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/c1 --logpath /usr/local/mongodb/log/c1.log --fork --smallfiles --port 27021 --replSet cs --configsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/c2 --logpath /usr/local/mongodb/log/c2.log --fork --smallfiles --port 27022 --replSet cs --configsvr --bind_ip=192.168.1.9
集群配置,登陆任意一个configsvr
mongo 192.168.1.9:27020
var css={_id:"cs","configsvr":true,members:[{_id:0,host:"192.168.1.9:27020"},{_id:1,host:"192.168.1.9:27021"},{_id:2,host:"192.168.1.9:27022"}]} rs.initiate(css)
副本模式启动分片1
mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --fork --smallfiles --port 27010 --replSet shard1 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --fork --smallfiles --port 27011 --replSet shard1 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --fork --smallfiles --port 27012 --replSet shard1 --shardsvr --bind_ip=192.168.1.9
登陆任意一个分片1
副本模式启动分片2
mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --fork --smallfiles --port 27013 --replSet shard2 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --fork --smallfiles --port 27014 --replSet shard2 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --fork --smallfiles --port 27015 --replSet shard2 --shardsvr --bind_ip=192.168.1.9
登陆任意一个分片2,操作同分片1
启动路由
mongos --logpath /usr/local/mongodb/log/m23.log --port 27023 --fork --configdb cs/192.168.1.9:27020,192.168.1.9:27021,192.168.1.9:27022 --bind_ip=192.168.1.9
登陆路由
mongo 192.168.1.9:27023
添加分片
设置分片数据库,设置片键
mongos> sh.shardCollection("testdb.orderInfo",{"_id":"hashed"}) { "collectionsharded" : "testdb.orderInfo", "ok" : 1 }
趁还没有加上鉴权,赶紧添加用户
mongos> db.createUser( ... { ... user: "testuser", ... pwd: "testuser", ... roles: [ { role: "readWrite", db: "testdb" } ] ... } ... ) Successfully added user: { "user" : "testuser", "roles" : [ { "role" : "readWrite", "db" : "testdb" } ] } mongos> db.auth("testuser","testuser") 1 mongos> exit
然后依次关闭mongodb,等下添加鉴权再启动.
因为懒,我选择重启,自己的电脑,随便整,别太较真.......
依次启动mongod,这次加上鉴权参数 --keyFile /usr/local/mongodb/keyfile
mongod --dbpath /usr/local/mongodb/data/c0 --logpath /usr/local/mongodb/log/c0.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27020 --replSet cs --configsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/c1 --logpath /usr/local/mongodb/log/c1.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27021 --replSet cs --configsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/c2 --logpath /usr/local/mongodb/log/c2.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27022 --replSet cs --configsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27010 --replSet shard1 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27011 --replSet shard1 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27012 --replSet shard1 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27013 --replSet shard2 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27014 --replSet shard2 --shardsvr --bind_ip=192.168.1.9 mongod --dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port 27015 --replSet shard2 --shardsvr --bind_ip=192.168.1.9 mongos --logpath /usr/local/mongodb/log/m23.log --port 27023 --fork --keyFile /usr/local/mongodb/keyfile --configdb cs/192.168.1.9:27020,192.168.1.9:27021,192.168.1.9:27022 --bind_ip=192.168.1.9
测试:
[root@192 conf]# mongo 192.168.1.9:27023 MongoDB shell version v3.4.10 connecting to: 192.168.1.9:27023 MongoDB server version: 3.4.10 mongos> use testdb switched to db testdb mongos> db.auth("testuser","testuser") 1 mongos> db.auth("testuser","testuser") 1 mongos> exit bye
或者
mongo 192.168.1.9:27023/testdb -u testuser -p
测试不使用账号密码
mongodb 3.4 分片,副本,鉴权集群部署.
标签:test mon nec switch ons host bsp ges data