Last active
May 10, 2018 09:23
-
-
Save allenyang79/62fc09d0a6bab37331a5ea5d793534e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # vi: set ts=2 sw=2: | |
| # a structure support mongo replica + sharding | |
| version: '3.4' | |
| services: | |
| rs_1: | |
| image: mongo:3.2 | |
| ports: | |
| - 27011:27017 | |
| volumes: | |
| - ./rs_1.conf:/etc/mongo/mongod.conf:ro | |
| command: mongod --config /etc/mongo/mongod.conf | |
| rs_2: | |
| image: mongo:3.2 | |
| ports: | |
| - 27021:27017 | |
| volumes: | |
| - ./rs_2.conf:/etc/mongo/mongod.conf:ro | |
| command: mongod --config /etc/mongo/mongod.conf | |
| #rs_1_2: | |
| # image: mongo:3.2 | |
| # ports: | |
| # - 27002:27017 | |
| # volumes: | |
| # - ./rs_1.conf:/etc/mongo/mongod.conf:ro | |
| # command: mongod --config /etc/mongo/mongod.conf | |
| #rs_1_3: | |
| # image: mongo:3.2 | |
| # ports: | |
| # - 27003:27017 | |
| # volumes: | |
| # - ./rs_1.conf:/etc/mongo/mongod.conf:ro | |
| # command: mongod --config /etc/mongo/mongod.conf | |
| # --shardsvr | |
| mongo-config: | |
| image: mongo:3.2 | |
| ports: | |
| - 27027:27017 | |
| #command: mongod --configsvr --port 27017 --storageEngine wiredTiger --directoryperdb --logpath /dev/stdout | |
| volumes: | |
| - ./mongo-config.conf:/etc/mongo/mongod.conf:ro | |
| command: mongod --config /etc/mongo/mongod.conf | |
| mongos: | |
| image: mongo:3.2 | |
| ports: | |
| - 27017:27017 | |
| depends_on: | |
| - mongo-config | |
| volumes: | |
| - ./mongos.conf:/etc/mongo/mongod.conf:ro | |
| command: mongos --config /etc/mongo/mongod.conf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for(var i=0; i<100000; i++){ | |
| db.orders.insertOne({ | |
| _id: "oid_" + i, | |
| oid: "oid_" + i, | |
| created_at: i | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # vi: set ts=2 sw=2: | |
| net: | |
| port: 27017 | |
| bindIp: 0.0.0.0 | |
| systemLog: | |
| destination: file | |
| logAppend: true | |
| path: /dev/stdout | |
| sharding: | |
| clusterRole: "configsvr" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # vi: set ts=2 sw=2: | |
| net: | |
| port: 27017 | |
| bindIp: 0.0.0.0 | |
| systemLog: | |
| destination: file | |
| logAppend: true | |
| path: /dev/stdout | |
| sharding: | |
| configDB: "mongo-config:27017" | |
| autoSplit: true | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // use admin db | |
| admin_db = db.getSiblingDB('admin'); | |
| admin_db.runCommand({addshard:'rs_1/rs_1:27017'}); | |
| admin_db.runCommand({addshard:'rs_2/rs_2:27017'}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # vi: set ts=2 sw=2: | |
| net: | |
| port: 27017 | |
| bindIp: 0.0.0.0 | |
| systemLog: | |
| destination: file | |
| logAppend: true | |
| path: /dev/stdout | |
| storage: | |
| engine: "wiredTiger" | |
| directoryPerDB: true | |
| replication: | |
| replSetName: rs_1 | |
| sharding: | |
| clusterRole: "shardsvr" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var config = { | |
| _id: 'rs_1', | |
| members: [ | |
| {_id: 0, host: 'rs_1:27017'}, | |
| //{_id: 1, host: 'rs_1_2:27017'}, | |
| //{_id: 2, host: 'rs_1_3:27017'} | |
| ] | |
| }; | |
| rs.initiate(config); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # vi: set ts=2 sw=2: | |
| net: | |
| port: 27017 | |
| bindIp: 0.0.0.0 | |
| systemLog: | |
| destination: file | |
| logAppend: true | |
| path: /dev/stdout | |
| storage: | |
| engine: "wiredTiger" | |
| directoryPerDB: true | |
| replication: | |
| replSetName: rs_2 | |
| sharding: | |
| clusterRole: "shardsvr" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var config = { | |
| _id: 'rs_2', | |
| members: [ | |
| {_id: 0, host: 'rs_2:27017'}, | |
| //{_id: 1, host: 'rs_2_2:27017'}, | |
| //{_id: 2, host: 'rs_2_3:27017'} | |
| ] | |
| }; | |
| rs.initiate(config); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| db = db.getSiblingDB('test_db'); | |
| sh.enableSharding('test_db'); | |
| db.orders.createIndex({'oid': 1}); | |
| sh.shardCollection( "test_db.orders", {"oid": 1}) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use admin | |
| db.createUser({ | |
| user: "admin", | |
| pwd: "admin", | |
| roles: [ | |
| { role: "userAdminAnyDatabase", db: "admin" } | |
| ] | |
| }) | |
| use test_db | |
| db.createUser({ | |
| user: "user", | |
| pwd: "user", | |
| roles: [ | |
| { role: "dbOwner", db: "test_db" } | |
| ] | |
| }) | |
| # https://docs.mongodb.com/manual/reference/built-in-roles/#dbOwner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment