时间:2021-07-01 10:21:17 帮助过:10人阅读
生成鉴权需要的keyfile
- openssl rand -base64 <span style="color: #800080">1024</span> > /usr/local/mongodb/<span style="color: #000000">keyfile
- </span><span style="color: #0000ff">chmod</span> /usr/local/mongodb/keyfile
副本模式启动configsvr
- 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">
- 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">
- 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
- mongo <span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27020</span>
- 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">}]}
- rs.initiate(css)</span>
副本模式启动分片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">
- 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">
- 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
- 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">
- 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">
- 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
启动路由
- 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>
登陆路由
- mongo <span style="color: #800080">192.168</span>.<span style="color: #800080">1.9</span>:<span style="color: #800080">27023</span>
添加分片
设置分片数据库,设置片键
- 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">})
- { </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> }
趁还没有加上鉴权,赶紧添加用户
- mongos><span style="color: #000000"> db.createUser(
- ... {
- ... user: </span><span style="color: #800000">"</span><span style="color: #800000">testuser</span><span style="color: #800000">"</span><span style="color: #000000">,
- ... </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">,
- ... 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"> } ]
- ... }
- ... )
- Successfully added user: {
- </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">,
- </span><span style="color: #800000">"</span><span style="color: #800000">roles</span><span style="color: #800000">"</span><span style="color: #000000"> : [
- {
- </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">,
- </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">
- }
- ]
- }
- 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">)
- </span><span style="color: #800080">1</span><span style="color: #000000">
- mongos</span>> 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 <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">
- 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">
- 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">
- 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">
- 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">
- 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">
- 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">
- 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">
- 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">
- 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>
测试:
- [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">
- MongoDB shell version v3.</span><span style="color: #800080">4.10</span><span style="color: #000000">
- 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">
- MongoDB server version: </span><span style="color: #800080">3.4</span>.<span style="color: #800080">10</span><span style="color: #000000">
- mongos</span>><span style="color: #000000"> use testdb
- switched to db testdb
- 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">)
- </span><span style="color: #800080">1</span><span style="color: #000000">
- 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">)
- </span><span style="color: #800080">1</span><span style="color: #000000">
- mongos</span>><span style="color: #000000"> exit
- bye</span>
或者
- 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