当前位置:Gxlcms > 数据库问题 > mongodb 3.4 分片,副本,鉴权集群部署.

mongodb 3.4 分片,副本,鉴权集群部署.

时间:2021-07-01 10:21:17 帮助过:10人阅读

/usr/local/mongodb/data mkdir /usr/local/mongodb/log cd /usr/local/mongodb/data mkdir c0 && mkdir c1 && mkdir c2 && mkdir s100 && mkdir s101 && mkdir s102 && mkdir s200 && mkdir s201 && mkdir s202

生成鉴权需要的keyfile

  1. openssl rand -base64 <span style="color: #800080">1024</span> > /usr/local/mongodb/<span style="color: #000000">keyfile
  2. </span><span style="color: #0000ff">chmod</span> /usr/local/mongodb/keyfile

副本模式启动configsvr

  1. mongod --dbpath /usr/local/mongodb/data/c0 --logpath /usr/local/mongodb/log/c0.log --fork --smallfiles --port <span style="color: #800080">27020</span> --replSet cs --configsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  2. mongod </span>--dbpath /usr/local/mongodb/data/c1 --logpath /usr/local/mongodb/log/c1.log --fork --smallfiles --port <span style="color: #800080">27021</span> --replSet cs --configsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  3. mongod </span>--dbpath /usr/local/mongodb/data/c2 --logpath /usr/local/mongodb/log/c2.log --fork --smallfiles --port <span style="color: #800080">27022</span> --replSet cs --configsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>

集群配置,登陆任意一个configsvr

  1. mongo <span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27020</span>
  1. var css={_id:<span style="color: #800000">"</span><span style="color: #800000">cs</span><span style="color: #800000">"</span>,<span style="color: #800000">"</span><span style="color: #800000">configsvr</span><span style="color: #800000">"</span>:<span style="color: #0000ff">true</span>,members:[{_id:<span style="color: #800080">0</span>,host:<span style="color: #800000">"</span><span style="color: #800000">192.168.1.9:27020</span><span style="color: #800000">"</span>},{_id:<span style="color: #800080">1</span>,host:<span style="color: #800000">"</span><span style="color: #800000">192.168.1.9:27021</span><span style="color: #800000">"</span>},{_id:<span style="color: #800080">2</span>,host:<span style="color: #800000">"</span><span style="color: #800000">192.168.1.9:27022</span><span style="color: #800000">"</span><span style="color: #000000">}]}
  2. rs.initiate(css)</span>

副本模式启动分片1

  1. mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --fork --smallfiles --port <span style="color: #800080">27010</span> --replSet shard1 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  2. mongod </span>--dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --fork --smallfiles --port <span style="color: #800080">27011</span> --replSet shard1 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  3. mongod </span>--dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --fork --smallfiles --port <span style="color: #800080">27012</span> --replSet shard1 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>

登陆任意一个分片1

技术分享

 

 

副本模式启动分片2

  1. mongod --dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --fork --smallfiles --port <span style="color: #800080">27013</span> --replSet shard2 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  2. mongod </span>--dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --fork --smallfiles --port <span style="color: #800080">27014</span> --replSet shard2 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  3. mongod </span>--dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --fork --smallfiles --port <span style="color: #800080">27015</span> --replSet shard2 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>

 登陆任意一个分片2,操作同分片1

启动路由

  1. mongos --logpath /usr/local/mongodb/log/m23.log --port <span style="color: #800080">27023</span> --fork --configdb cs/<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27020</span>,<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27021</span>,<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27022</span> --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>

登陆路由

  1. mongo <span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27023</span>

添加分片

技术分享

设置分片数据库,设置片键

  1. mongos> <span style="color: #0000ff">sh</span>.shardCollection(<span style="color: #800000">"</span><span style="color: #800000">testdb.orderInfo</span><span style="color: #800000">"</span>,{<span style="color: #800000">"</span><span style="color: #800000">_id</span><span style="color: #800000">"</span>:"hashed"<span style="color: #000000">})
  2. { </span><span style="color: #800000">"</span><span style="color: #800000">collectionsharded</span><span style="color: #800000">"</span> : <span style="color: #800000">"</span><span style="color: #800000">testdb.orderInfo</span><span style="color: #800000">"</span>, <span style="color: #800000">"</span><span style="color: #800000">ok</span><span style="color: #800000">"</span> : <span style="color: #800080">1</span> }

趁还没有加上鉴权,赶紧添加用户

  1. mongos><span style="color: #000000"> db.createUser(
  2. ...   {
  3. ...     user: </span><span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span><span style="color: #000000">,
  4. ...     </span><span style="color: #0000ff">pwd</span>: <span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span><span style="color: #000000">,
  5. ...     roles: [ { role: </span><span style="color: #800000">"</span><span style="color: #800000">readWrite</span><span style="color: #800000">"</span>, db: <span style="color: #800000">"</span><span style="color: #800000">testdb</span><span style="color: #800000">"</span><span style="color: #000000"> } ]
  6. ...   }
  7. ... )
  8. Successfully added user: {
  9. </span><span style="color: #800000">"</span><span style="color: #800000">user</span><span style="color: #800000">"</span> : <span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span><span style="color: #000000">,
  10. </span><span style="color: #800000">"</span><span style="color: #800000">roles</span><span style="color: #800000">"</span><span style="color: #000000"> : [
  11. {
  12. </span><span style="color: #800000">"</span><span style="color: #800000">role</span><span style="color: #800000">"</span> : <span style="color: #800000">"</span><span style="color: #800000">readWrite</span><span style="color: #800000">"</span><span style="color: #000000">,
  13. </span><span style="color: #800000">"</span><span style="color: #800000">db</span><span style="color: #800000">"</span> : <span style="color: #800000">"</span><span style="color: #800000">testdb</span><span style="color: #800000">"</span><span style="color: #000000">
  14. }
  15. ]
  16. }
  17. mongos</span>> db.auth(<span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span>,<span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span><span style="color: #000000">)
  18. </span><span style="color: #800080">1</span><span style="color: #000000">
  19. mongos</span>> exit

然后依次关闭mongodb,等下添加鉴权再启动.

因为懒,我选择重启,自己的电脑,随便整,别太较真.......

技术分享

依次启动mongod,这次加上鉴权参数   --keyFile /usr/local/mongodb/keyfile

  1. mongod --dbpath /usr/local/mongodb/data/c0 --logpath /usr/local/mongodb/log/c0.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27020</span> --replSet cs --configsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  2. mongod </span>--dbpath /usr/local/mongodb/data/c1 --logpath /usr/local/mongodb/log/c1.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27021</span> --replSet cs --configsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  3. mongod </span>--dbpath /usr/local/mongodb/data/c2 --logpath /usr/local/mongodb/log/c2.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27022</span> --replSet cs --configsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  4. mongod </span>--dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27010</span> --replSet shard1 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  5. mongod </span>--dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27011</span> --replSet shard1 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  6. mongod </span>--dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27012</span> --replSet shard1 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  7. mongod </span>--dbpath /usr/local/mongodb/data/s100 --logpath /usr/local/mongodb/log/s100.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27013</span> --replSet shard2 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  8. mongod </span>--dbpath /usr/local/mongodb/data/s101 --logpath /usr/local/mongodb/log/s101.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27014</span> --replSet shard2 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  9. mongod </span>--dbpath /usr/local/mongodb/data/s102 --logpath /usr/local/mongodb/log/s102.log --keyFile /usr/local/mongodb/keyfile --fork --smallfiles --port <span style="color: #800080">27015</span> --replSet shard2 --shardsvr --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span><span style="color: #000000">
  10. mongos </span>--logpath /usr/local/mongodb/log/m23.log --port <span style="color: #800080">27023</span> --fork --keyFile /usr/local/mongodb/keyfile --configdb cs/<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27020</span>,<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27021</span>,<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27022</span> --bind_ip=<span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>

测试:

  1. [root@<span style="color: #800080">192</span> conf]# mongo <span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27023</span><span style="color: #000000">
  2. MongoDB shell version v3.</span><span style="color: #800080">4.10</span><span style="color: #000000">
  3. connecting to: </span><span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27023</span><span style="color: #000000">
  4. MongoDB server version: </span><span style="color: #800080">3.4</span>.<span style="color: #800080">10</span><span style="color: #000000">
  5. mongos</span>><span style="color: #000000"> use testdb
  6. switched to db testdb
  7. mongos</span>> db.auth(<span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span>,<span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span><span style="color: #000000">)
  8. </span><span style="color: #800080">1</span><span style="color: #000000">
  9. mongos</span>> db.auth(<span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span>,<span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span><span style="color: #000000">)
  10. </span><span style="color: #800080">1</span><span style="color: #000000">
  11. mongos</span>><span style="color: #000000"> exit
  12. bye</span>

或者

  1. mongo <span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27023</span>/testdb -u testuser -p

测试不使用账号密码

技术分享

 

mongodb 3.4 分片,副本,鉴权集群部署.

标签:test   mon   nec   switch   ons   host   bsp   ges   data   

人气教程排行