mongos> sh.enableSharding("admin") { "ok" : 0, "errmsg" : "can't shard admin database" }
server1.cluster.com:~# apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 server1.cluster.com:~# echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list server1.cluster.com:~# apt-get update server1.cluster.com:~# apt-get install -y mongodb-org
server1.cluster.com:~# service mongod stop
. ├── cfg ├── data │ ├── config │ ├── rs0 │ ├── rs1 │ └── rs2 ├── keys └── logs
mongod --port 27000 --dbpath /root/mongodb/data/rs0
mongo --port 27000
> db.getName() test
> db.dropDatabase() { "ok" : 1 }
> use analytics switched to db analytics
> db.sensors.insert({'s':1001, 'n': 'Sensor1001', 'o': true, 'ip': '192.168.88.20', 'a': ISODate('2016-07-20T20:34:16.001Z'), 'e': 0}) WriteResult({ "nInserted" : 1 }) > db.sensors.insert({'s':1002, 'n': 'Sensor1002', 'o': false, 'ip': '192.168.88.30', 'a': ISODate('2016-07-19T13:40:22.483Z'), 'e': 0}) WriteResult({ "nInserted" : 1 })
> db.statistics.insert({'s':1001, 'ts': ISODate('2016-08-04T20:34:16.001Z'), 'param1': 123, 'param2': 23.45, 'param3': “OK”, 'param4': True, 'param5': '-1000', 'param6': [1,2,3,4,5]) WriteResult({ "nInserted" : 1 })
mongos> db.statistics.ensureIndex({"s":1})
server1.cluster.com:~/mongodb/keys# openssl genrsa -out mongodb-private.key -aes256 Generating RSA private key, 2048 bit long modulus .....................+++ ........................................................+++ e is 65537 (0x10001) Enter pass phrase for mongodb-private.key: Verifying - Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# openssl req -x509 -new -extensions v3_ca -key mongodb-private.key -days 36500 -out mongodb-CA-cert.crt
Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: CaServer Email Address []: info@SomeSystems.com
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout server1.key -out server1.csr Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: server1.cluster.com Email Address []: info@SomeSystems.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in server1.csr -out server1.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=Statistics/CN=server1.cluster.com/emailAddress=info@SomeSystems.com Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat server1.key server1.crt > server1.pem
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout server2.key -out server2.csr Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: server2.cluster.com Email Address []: info@SomeSystems.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in server2.csr -out server2.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=Statistics/CN=server2.cluster.com/emailAddress=info@SomeSystems.com Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat server2.key server2.crt > server2.pem
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout server3.key -out server3.csr Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: server3.cluster.com Email Address []: info@SomeSystems.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in server3.csr -out server3.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=Statistics/CN=server3.cluster.com/emailAddress=info@SomeSystems.com Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat server3.key server3.crt > server3.pem
mongod --config <path-to-config-file>
# # /root/mongodb/cfg/mongod-rs0.conf # replication: replSetName: "rs0" # net: port: 27000 ssl: mode: requireSSL # PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false # allowInvalidCertificates: false # security: authorization: enabled # clusterAuthMode: x509 # - MONGODB-X509 storage: dbPath : /root/mongodb/data/rs0 # systemLog: destination: file # path: /root/mongodb/logs/mongod-rs0.log # - logAppend: true # -
# # /root/mongodb/cfg/mongod-rs1.conf # replication: replSetName: "rs1" net: port: 27001 ssl: mode: requireSSL PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false allowInvalidCertificates: false security: authorization: enabled clusterAuthMode: x509 storage: dbPath : /root/mongodb/data/rs1 systemLog: destination: file path: /root/mongodb/logs/mongod-rs1.log logAppend: true
# # /root/mongodb/cfg/mongod-rs2.conf # replication: replSetName: "rs2" net: port: 27002 ssl: mode: requireSSL PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false allowInvalidCertificates: false security: authorization: enabled clusterAuthMode: x509 storage: dbPath : /root/mongodb/data/rs2 systemLog: destination: file path: /root/mongodb/logs/mongod-rs2.log logAppend: true
# # /root/mongodb/cfg/mongod-rscfg.conf # sharding: clusterRole: configsvr # - replication: replSetName: "rscfg" # net: port: 27888 ssl: mode: requireSSL PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false allowInvalidCertificates: false security: authorization: enabled clusterAuthMode: x509 storage: dbPath : /root/mongodb/data/config systemLog: destination: file path: /root/mongodb/logs/mongod-rscfg.log logAppend: true
mongod --port 27000 --dbpath /root/mongodb/data/rs0
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout rsroot.key -out rsroot.csr Generating a 2048 bit RSA private key ........................................................................+++ .........................+++ writing new private key to 'rsroot.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: StatisticsClient Common Name (eg server FQDN or YOUR name) []: rsroot Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in rsroot.csr -out rsroot.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=StatisticsClient/CN=rsroot Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat rsroot.key rsroot.crt > rsroot.pem
{ "ok" : 0, "errmsg" : "Cannot create an x.509 user with a subjectname that would be recognized as an internal cluster member.", "code" : 2 }
server1.cluster.com:~/mongodb/keys# openssl x509 -in rsroot.pem -inform PEM -subject -nameopt RFC2253 subject= CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU -----BEGIN CERTIFICATE-----
mongo --port 27000
> db.getSiblingDB("$external").runCommand({createUser: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "root", db: "admin"}] })
server1.cluster.com:~/mongodb/keys# mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rsroot.pem --host server1.cluster.com --port 27000
> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" })
rs.initiate( { _id: "rs0", members: [ { _id: 0, host : "server1.cluster.com:27000" }, { _id: 1, host : "server2.cluster.com:27000" }, { _id: 2, host : "server3.cluster.com:27000", arbiterOnly: true }, ] } )
mongod --port 27001 --dbpath /root/mongodb/data/rs1
mongo --port 27001
> db.getSiblingDB("$external").runCommand({createUser: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "root", db: "admin"}] })
root@server1.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs1.conf root@server2.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs1.conf root@server3.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs1.conf
root@server1.cluster.com# mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rsroot.pem --host server1.cluster.com --port 27001
> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" }) > rs.initiate( { _id: "rs1", members: [ { _id: 0, host : "server1.cluster.com:27001" }, { _id: 1, host : "server2.cluster.com:27001" }, { _id: 2, host : "server3.cluster.com:27001", arbiterOnly: true }, ] } )
mongod --port 27002 --dbpath /root/mongodb/data/rs2
mongo --port 27002
> db.getSiblingDB("$external").runCommand({createUser: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "root", db: "admin"}] })
root@server1.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs2.conf root@server2.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs2.conf root@server3.cluster.com# mongod --config /root/mongodb/cfg/mongod-rs2.conf
root@server1.cluster.com# mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rsroot.pem --host server1.cluster.com --port 27002
> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=rsroot,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" }) > rs.initiate( { _id: "rs2", members: [ { _id: 0, host : "server1.cluster.com:27002" }, { _id: 1, host : "server2.cluster.com:27002" }, { _id: 2, host : "server3.cluster.com:27002", arbiterOnly: true }, ] } )
{ "ok" : 0, "errmsg" : "Arbiters are not allowed in replica set configurations being used for config servers", "code" : 93 }
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout rootuser.key -out rootuser.csr Generating a 2048 bit RSA private key ......................+++ .........................................+++ writing new private key to 'rootuser.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: StatisticsClient Common Name (eg server FQDN or YOUR name) []: root Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in rootuser.csr -out rootuser.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=StatisticsClient/CN=root Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat rootuser.key rootuser.crt > rootuser.pem
server1.cluster.com:~/mongodb/keys# openssl x509 -in rootuser.pem -inform PEM -subject -nameopt RFC2253 subject= CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU -----BEGIN CERTIFICATE-----
server1.cluster.com:~/mongodb/keys# mongod --port 27888 --dbpath /root/mongodb/data/config
server1.cluster.com:~/mongodb/keys# mongo --port 27888
> db.getSiblingDB("$external").runCommand({createUser: "CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "root", db: "admin"}] })
root@server1.cluster.com# mongod --config /root/mongodb/cfg/mongod-rscfg.conf root@server2.cluster.com# mongod --config /root/mongodb/cfg/mongod-rscfg.conf root@server3.cluster.com# mongod --config /root/mongodb/cfg/mongod-rscfg.conf
root@server1.cluster.com# mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rootuser.pem --host server1.cluster.com --port 27888
> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" }) > rs.initiate( { _id: "rscfg", members: [ { _id: 0, host : "server1.cluster.com:27888" }, { _id: 1, host : "server2.cluster.com:27888" }, { _id: 2, host : "server3.cluster.com:27888" } ] } )
# # /root/mongodb/cfg/mongos.conf # sharding: configDB: "rscfg/server1.cluster.com:27888,server2.cluster.com:27888,server3.cluster.com:27888" net: port: 27017 ssl: mode: requireSSL PEMKeyFile: /root/mongodb/keys/server1.pem clusterFile: /root/mongodb/keys/server1.pem CAFile: /root/mongodb/keys/mongodb-CA-cert.crt weakCertificateValidation: false allowInvalidCertificates: false security: clusterAuthMode: x509 systemLog: destination: file path: /root/mongodb/logs/mongos.log logAppend: true
mongos --config /root/mongodb/cfg/mongos.conf
mongo admin --ssl --sslCAFile /root/mongodb/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb/keys/rootuser.pem --host server1.cluster.com --port 27017
mongos> db.getSiblingDB("$external").auth({ mechanism:"MONGODB-X509", user: "CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" })
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout analyticsuser.key -out analyticsuser.csr Generating a 2048 bit RSA private key ......................+++ .........................................+++ writing new private key to 'analyticsuser.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: StatisticsClient Common Name (eg server FQDN or YOUR name) []: analyticsuser Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in analyticsuser.csr -out analyticsuser.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=StatisticsClient/CN=analyticsuser Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat analyticsuser.key analyticsuser.crt > analyticsuser.pem
server1.cluster.com:~/mongodb/keys# openssl x509 -in rootuser.pem -inform PEM -subject -nameopt RFC2253 subject= CN=analyticsuser,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU -----BEGIN CERTIFICATE-----
mongos> db.getSiblingDB("$external").runCommand({createUser: "CN=analyticsuser,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "readWrite", db: "analytics"}] })
mongo --ssl --sslCAFile /root/mongodb1/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb1/keys/rootuser.pem --host server1.cluster.com --port 27017 --authenticationMechanism "MONGODB-X509" --authenticationDatabase "$external" -u “CN=root,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU”
mongos> use config mongos> db.settings.save({_id: "chunksize", value: NumberLong(32)}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
mongos> db.settings.find({'_id':"chunksize" }) { "_id" : "chunksize", "value" : NumberLong(32) }
server1.cluster.com:~/mongodb/keys# openssl req -new -nodes -newkey rsa:2048 -keyout clusterAdmin.key -out aclusterAdmin.csr Generating a 2048 bit RSA private key ................+++ .......................................+++ writing new private key to 'clusterAdmin.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: RU State or Province Name (full name) [Some-State]: MoscowRegion Locality Name (eg, city) []: Moscow Organization Name (eg, company) [Internet Widgits Pty Ltd]: SomeSystems Organizational Unit Name (eg, section) []: Statistics Common Name (eg server FQDN or YOUR name) []: clusteradmin Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
server1.cluster.com:~/mongodb/keys# openssl x509 -CA mongodb-CA-cert.crt -CAkey mongodb-private.key -CAcreateserial -req -days 36500 -in clusterAdmin.csr -out clusterAdmin.crt Signature ok subject=/C=RU/ST=MoscowRegion/L=Moscow/O=SomeSystems/OU=Statistics/CN=clusteradmin Getting CA Private Key Enter pass phrase for mongodb-private.key:
server1.cluster.com:~/mongodb/keys# cat clusterAdmin.key clusterAdmin.crt > clusterAdmin.pem
server1.cluster.com:~/mongodb/keys# openssl x509 -in clusterAdmin.pem -inform PEM -subject -nameopt RFC2253 subject= CN=clusteradmin,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU -----BEGIN CERTIFICATE-----
mongos> db.getSiblingDB("$external").runCommand({ createUser: "CN=clusteradmin,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "clusterAdmin", db: "admin"}] })
mongo --ssl --sslCAFile /root/mongodb1/keys/mongodb-CA-cert.crt --sslPEMKeyFile /root/mongodb1/keys/clusterAdmin.pem --host server1.cluster.com --port 27017 --authenticationMechanism "MONGODB-X509" --authenticationDatabase "$external" -u “CN=clusteradmin,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU”
mongos> sh.addShard("rs0/server1.cluster.com:27000,server2.cluster.com:27000") mongos> sh.addShard("rs1/server1.cluster.com:27001,server2.cluster.com:27001") mongos> sh.addShard("rs2/server1.cluster.com:27002,server2.cluster.com:27002")
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5795284cd589624d4e36b7d4") } shards: { "_id" : "rs0", "host" : "rs0/server1.cluster.com:27100,server2.cluster.com:27200" } { "_id" : "rs1", "host" : "rs1/server1.cluster.com:27101,server2.cluster.com:27201" } { "_id" : "rs2", "host" : "rs2/server1.cluster.com:27102,server2.cluster.com:27202" } active mongoses: "3.2.8" : 1 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases:
mongos> sh.enableSharding("statistics")
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5795284cd589624d4e36b7d4") } shards: { "_id" : "rs0", "host" : "rs0/server1.cluster.com:27000,server2.cluster.com:27000" } { "_id" : "rs1", "host" : "rs1/server1.cluster.com:27001,server2.cluster.com:27001" } { "_id" : "rs2", "host" : "rs2/server1.cluster.com:27002,server2.cluster.com:27002" } active mongoses: "3.2.8" : 1 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "analytics", "primary" : "rs2", "partitioned" : true }
mongos> use analytics mongos> db.statistics.ensureIndex({"s":1})
mongos> sh.shardCollection("analytics.statistics", {"s":1})
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5773899ee3456024f8ef4895") } shards: { "_id" : "rs0", "host" : "rs0/server1.cluster.com:27000,server2.cluster.com:27000" } { "_id" : "rs1", "host" : "rs1/server1.cluster.com:27001,server2.cluster.com:27001" } { "_id" : "rs2", "host" : "rs2/server1.cluster.com:27002,server2.cluster.com:27002" } active mongoses: "3.2.8" : 1 balancer: Currently enabled: yes Currently running: yes Balancer lock taken at Sun Jul 29 2016 10:18:32 GMT+0000 (UTC) by MongoDB:27017:1468508127:-1574651753:Balancer Collections with active migrations: statistic.statistic started at Sun Jul 29 2016 10:18:32 GMT+0000 (UTC) Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 3 : Success 2 : Failed with error 'aborted', from rs2 to rs0 databases: { "_id" : "analytics", "primary" : "rs2", "partitioned" : true } analytics.statistics shard key: { "s" : 1 } unique: false balancing: true chunks: rs0 1 rs1 2 rs2 21 too many chunks to print, use verbose if you want to force print
supervisorctl start all supervisorctl stop all
# apt-get install supervisor
# # /etc/supervisor/conf.d/mongod-rs0.conf # [program:mongod-rs0] command=mongod --config /root/mongodb/cfg/rs0.conf user=root stdout_logfile=/root/mongodb/logs/supervisor/mongod-rs0-stdout.log redirect_stderr=true autostart=true autorestart=true stopwaitsecs=60
# supervisorctl reload
# supervisorctl stop mongod-rs0 # supervisorctl start mongod-rs0 # supervisorctl status mongod-rs0
mongoexport --db analytics --collection statistics --out statistics.json
> use analytics > db.statistics.drop()
> db.analytics.ensureIndex({"s":1})
> sh.shardCollection("analytics.statistics", {"s":1})
mongoimport --db analytics --collection statistics --file statistics.json
CN=backuper,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU
mongos> db.getSiblingDB("$external").runCommand({ createUser: "CN=backuper,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", roles: [{role: "backup", db: "admin"}] })
mongodump --ssl --sslCAFile “/root/mongodb/keys/mongodb-CA-cert.crt” --sslPEMKeyFile “/root/mongodb/keys/backuper.pem” -u "CN=backuper,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU" --host server1.cluster.com --port 27017 --authenticationMechanism "MONGODB-X509" --authenticationDatabase "$external" --db analytics --gzip -o "/path/to/backup/"
#include <mongo/client/dbclient.h> #include <mongo/client/options.h> ... mongo::DBClientConnection client(true); // try { // SSL mongo::client::Options options; options.setSSLMode(mongo::client::Options::SSLModes::kSSLRequired); options.setSSLCAFile("/path_to_certs/mongodb-CA-cert.crt"); options.setSSLPEMKeyFile("/path_to_certs/analyticsuser.PEM"); mongo::Status status = mongo::client::initialize(options); mongo::massertStatusOK(status); // , client.connect("www.server1.cluster.com:27017"); // mongos // : , , mongo::BSONObjBuilder auth_params; auth_params.append("db", "$external"); auth_params.append("user", "CN=username,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU"); auth_params.append("mechanism", "MONGODB-X509"); client.auth(auth_params.obj()); // } catch (const mongo::DBException &e) { std::cout << "DBException : " << e.toString() << std::endl; } ...
import ssl db_hosts="server1.cluster.com:27017,server2.cluster.com:27017" db_port=None client = MongoClient(db_hosts, db_port, read_preference=ReadPreference.NEAREST, ssl=True, ssl_certfile="/path_to_certs/analyticsuser.PEM", ssl_cert_reqs=ssl.CERT_REQUIRED, ssl_ca_certs="/path_to_certs/mongodb-CA-cert.crt") db = client[db_name] db.authenticate(name=db_user, source="$external", mechanism="MONGODB-X509")
def pymongo_reconnect(attempts=5): def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): tries_reconnect = attempts if tries_reconnect <= 0: tries_reconnect = 1 while tries_reconnect: try: return f(*args, **kwargs) except AutoReconnect as ar: tries_reconnect -= 1 print("Caught AutoReconnect exception.") if tries_reconnect <= 0: raise ar time.sleep(0.1) print("Attempt to reconnect (%d more)...\n" % tries_reconnect) continue return decorated_function return decorator
connect('default', host, port)
import ssl from mongoengine import DEFAULT_CONNECTION_NAME, register_connection db_hosts="server1.cluster.com:27017,server2.cluster.com:27017" db_port=None ssl_config = { 'ssl': True, 'ssl_certfile': "/path_to_certs/analyticsuser.PEM", 'ssl_cert_reqs': ssl.CERT_REQUIRED, 'ssl_ca_certs': "/path_to_certs/mongodb-CA-cert.crt", } register_connection(alias=DEFAULT_CONNECTION_NAME, name="statistic", host=db_hosts, port=db_port, username="CN=username,OU=StatisticsClient,O=SomeSystems,L=Moscow,ST=MoscowRegion,C=RU", password=None, read_preference=ReadPreference.NEAREST, authentication_source="$external", authentication_mechanism="MONGODB-X509", **ssl_config)
Source: https://habr.com/ru/post/308740/
All Articles