q29191 发表于 2015-11-11 10:34:46

第9周 Mongodb数据库高可用,分布式集群部署

mongodb管理
#启动MongoDB# ./mongod -f /nosql/mongodb/mongodb/bin/mongodb.confforked process: 3104# all output going to: /nosql/mongodb/mongodb/log/mongodb.log# pstree -p | grep mongo      |-mongod(3104)-+-{mongod}(3105)      | |-{mongod}(3106)      | |-{mongod}(3107)      | |-{mongod}(3108)      | |-{mongod}(3109)      | |-{mongod}(3110)      | `-{mongod}(3111)
#文明的关闭mongodb# ./mongoMongoDB shell version: 2.0.9connecting to: test> use adminswitched to db admin> db.shutdownServer()Wed Dec 25 18:21:27 DBClientCursor::init call() failedWed Dec 25 18:21:27 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1server should be down...Wed Dec 25 18:21:27 trying reconnect to 127.0.0.1Wed Dec 25 18:21:27 reconnect 127.0.0.1 failed couldn't connect to server 127.0.0.1Wed Dec 25 18:21:27 Error: error doing query: unknown shell/collection.js:151> exitbye# pstree -p | grep mongo
#增加用户> use adminswitched to db admin> db.addUser("root","root");                  #用户root 密码root{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }{      "user" : "root",      "readOnly" : false,                   #非只读      "pwd" : "2a8025f0885adad5a8ce0044070032b3",      "_id" : ObjectId("52bab2821fad02ed3e00ae5c")}> db.addUser("ing","ing",true);                #用户ing 密码ing{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }{      "user" : "ing",      "readOnly" : true,                  #只读      "pwd" : "d57523920faca67a5fa36692e6aed5e0",      "_id" : ObjectId("52bab2e91fad02ed3e00ae5d")}
#查询用户> use adminswitched to db admin> show tables;system.indexessystem.users> db.system.indexes.find();{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "admin.system.users", "name" : "_id_" }> db.system.users.find(){ "_id" : ObjectId("52bab2821fad02ed3e00ae5c"), "user" : "root", "readOnly" : false, "pwd" : "2a8025f0885adad5a8ce0044070032b3" }{ "_id" : ObjectId("52bab2e91fad02ed3e00ae5d"), "user" : "ing", "readOnly" : true, "pwd" : "d57523920faca67a5fa36692e6aed5e0" }
#权限控制:配置auth项#在配置文件:mongodb.conf中加入一行:auth=true#启动权限控制,然后重启数据库。connecting to: test                        #当前在test库> db.test.insert({"name":"zhangsan"});unauthorized                                 #没权限insert> db.test.insert({"name":"zhangsan"});unauthorized> db.auth("root","root")0                                          #登录失败> use adminswitched to db admin                         #切换到admin库> db.auth("root","root")1                                          #登录成功(因为前面创建用户是在admin库建立的)> use testswitched to db test                        #切换到test库> db.test.insert({"name":"zhangsan"});> db.test.find(){ "_id" : ObjectId("52bab77b24204e3767449284"), "name" : "zhangsan" }
#删除用户> use testswitched to db test> db.auth("test_user","aaa")1> db.system.users.find();{ "_id" : ObjectId("52bab92224204e3767449285"), "user" : "test_user", "readOnly" : false, "pwd" : "49002e50ff5749b8f7ef46f4260087e6" }> db.system.users.remove({"user":"test_user"})      #删除> db.auth("test_user","aaa")0> db.system.users.find();
热备份
# ./mongodump -u root -p root -d testconnected to: 127.0.0.1DATABASE: test to dump/test      test.foo to dump/test/foo.bson               1 objects      test.system.indexes to dump/test/system.indexes.bson               7 objects      test.c to dump/test/c.bson               100 objects      test.result to dump/test/result.bson               6 objects      test.books to dump/test/books.bson               1 objects      test.people to dump/test/people.bson               1 objects      test.test to dump/test/test.bson               1 objects      test.system.users to dump/test/system.users.bson               0 objectsmongodump   #导入,用来备份mongorestore#导入,用来还原具体的用户可以使用-help来查看帮组信息。
#强制一致,备份的时候保证数据一致性。

#修复数据库修复所有数据库:用带—repair参数启动db.repairDatabase()
主从复制
#可以配置为1-1,也可以配置1-n#1-1复制配置#配置Master# ./mongod --dbpath /nosql/mongodb/dbs/master --port 10000 --master --restWed Dec 25 20:59:13 Wed Dec 25 20:59:13 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.Wed Dec 25 20:59:13 Wed Dec 25 20:59:13 MongoDB starting : pid=4289 port=10000 dbpath=/nosql/mongodb/dbs/master master=1 32-bit host=linuxWed Dec 25 20:59:13 Wed Dec 25 20:59:13 ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of dataWed Dec 25 20:59:13 ** see http://blog.mongodb.org/post/137788967/32-bit-limitationsWed Dec 25 20:59:13 ** with --journal, the limit is lowerWed Dec 25 20:59:13 Wed Dec 25 20:59:13 db version v2.0.9, pdfile version 4.5Wed Dec 25 20:59:13 git version: 7e34cb36a6ae64d527c0b0da81fa967606c55433Wed Dec 25 20:59:13 build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_41Wed Dec 25 20:59:13 options: { dbpath: "/nosql/mongodb/dbs/master", master: true, port: 10000, rest: true }Wed Dec 25 20:59:13 ******Wed Dec 25 20:59:13 creating replication oplog of size: 47MB...Wed Dec 25 20:59:13 allocating new datafile /nosql/mongodb/dbs/master/local.ns, filling with zeroes...Wed Dec 25 20:59:13 creating directory /nosql/mongodb/dbs/master/_tmpWed Dec 25 20:59:13 done allocating datafile /nosql/mongodb/dbs/master/local.ns, size: 16MB, took 0.309 secsWed Dec 25 20:59:13 allocating new datafile /nosql/mongodb/dbs/master/local.0, filling with zeroes...Wed Dec 25 20:59:14 done allocating datafile /nosql/mongodb/dbs/master/local.0, size: 16MB, took 0.376 secsWed Dec 25 20:59:14 allocating new datafile /nosql/mongodb/dbs/master/local.1, filling with zeroes...Wed Dec 25 20:59:15 done allocating datafile /nosql/mongodb/dbs/master/local.1, size: 32MB, took 0.654 secsWed Dec 25 20:59:15 allocating new datafile /nosql/mongodb/dbs/master/local.2, filling with zeroes...Wed Dec 25 20:59:16 done allocating datafile /nosql/mongodb/dbs/master/local.2, size: 64MB, took 1.77 secsWed Dec 25 20:59:16 ******Wed Dec 25 20:59:16 waiting for connections on port 10000Wed Dec 25 20:59:16 admin web console waiting for connections on port 11000
#master监控界面:http://10.10.10.8:11000/#配置从节点# ./mongod --dbpath /nosql/mongodb/dbs/slave --port 10001 --slave --rest --source 127.0.0.1:10000Wed Dec 25 20:59:29 Wed Dec 25 20:59:29 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.Wed Dec 25 20:59:29 Wed Dec 25 20:59:29 MongoDB starting : pid=4298 port=10001 dbpath=/nosql/mongodb/dbs/slave slave=1 32-bit host=linuxWed Dec 25 20:59:29 Wed Dec 25 20:59:29 ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of dataWed Dec 25 20:59:29 ** see http://blog.mongodb.org/post/137788967/32-bit-limitationsWed Dec 25 20:59:29 ** with --journal, the limit is lowerWed Dec 25 20:59:29 Wed Dec 25 20:59:29 db version v2.0.9, pdfile version 4.5Wed Dec 25 20:59:29 git version: 7e34cb36a6ae64d527c0b0da81fa967606c55433Wed Dec 25 20:59:29 build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_41Wed Dec 25 20:59:29 options: { dbpath: "/nosql/mongodb/dbs/slave", port: 10001, rest: true, slave: true, source: "127.0.0.1:10000" }Wed Dec 25 20:59:29 admin web console waiting for connections on port 11001Wed Dec 25 20:59:29 waiting for connections on port 10001Wed Dec 25 20:59:30 allocating new datafile /nosql/mongodb/dbs/slave/local.ns, filling with zeroes...Wed Dec 25 20:59:30 creating directory /nosql/mongodb/dbs/slave/_tmpWed Dec 25 20:59:30 done allocating datafile /nosql/mongodb/dbs/slave/local.ns, size: 16MB, took 0.45 secsWed Dec 25 20:59:30 allocating new datafile /nosql/mongodb/dbs/slave/local.0, filling with zeroes...Wed Dec 25 20:59:31 done allocating datafile /nosql/mongodb/dbs/slave/local.0, size: 16MB, took 0.439 secsWed Dec 25 20:59:31 allocating new datafile /nosql/mongodb/dbs/slave/local.1, filling with zeroes...Wed Dec 25 20:59:31 build index local.sources { _id: 1 }Wed Dec 25 20:59:31 build index done 0 records 0.002 secsWed Dec 25 20:59:31 repl: from host:127.0.0.1:10000Wed Dec 25 20:59:31 build index local.me { _id: 1 }Wed Dec 25 20:59:31 build index done 0 records 0 secsWed Dec 25 20:59:31 done allocating datafile /nosql/mongodb/dbs/slave/local.1, size: 32MB, took 0.909 secsWed Dec 25 20:59:38 repl: applied 0 operationsWed Dec 25 20:59:38 repl: end sync_pullOpLog syncedTo: Dec 25 20:59:30 52bad6b2:1Wed Dec 25 20:59:38 repl: sleep 2 sec before next passWed Dec 25 20:59:40 repl: from host:127.0.0.1:10000Wed Dec 25 20:59:47 repl: applied 1 operationsWed Dec 25 20:59:47 repl: end sync_pullOpLog syncedTo: Dec 25 20:59:40 52bad6bc:1Wed Dec 25 20:59:47 repl: from host:127.0.0.1:10000
#slave监控界面:http://10.10.10.8:11001/#验证复制# ./mongo 127.0.0.1:10000MongoDB shell version: 2.0.9connecting to: 127.0.0.1:10000/test> db.abcd.insert({"x":123})> db.abcd.find(){ "_id" : ObjectId("52bad6e38976ed3868507aec"), "x" : 123 }> exitbye# ./mongo 127.0.0.1:10001MongoDB shell version: 2.0.9connecting to: 127.0.0.1:10001/test> db.abcd.find(){ "_id" : ObjectId("52bad6e38976ed3868507aec"), "x" : 123 }> exitbye
副本集
# pwd/nosql/mongodb/dbs# mkdir -p node1 node2 node3# lsnode1 node2 node3#启动节点1、节点2、节点3./mongod --dbpath /nosql/mongodb/dbs/node1 --port 10001 --replSet blort./mongod --dbpath /nosql/mongodb/dbs/node2 --port 10002 --replSet blort./mongod --dbpath /nosql/mongodb/dbs/node3 --port 10003 --replSet blort#初始化副本集rs.initiate({"_id" : "blort","members" : [{"_id" : 1,"host" : "linux:10001"},{"_id" : 2,"host" : "linux:10002"},{"_id" : 3,"host" : "linux:10003"},]})#blort是副本集的名字,linux是服务器的hostname# ./mongo 127.0.0.1:10001MongoDB shell version: 2.0.9connecting to: 127.0.0.1:10001/test> rs.initiate({"_id" : "blort","members" : [... {"_id" : 1,"host" : "linux:10001"},... {"_id" : 2,"host" : "linux:10002"},... {"_id" : 3,"host" : "linux:10003"},... ]}){      "info" : "Config now saved locally. Should come online in about a minute.",      "ok" : 1}#随便连接到哪个mongodb都可以执行上面的脚本。执行之后可以看见各个节点日志:Wed Dec 25 21:19:53 replSet PRIMARY       #node1的日志Wed Dec 25 21:19:53 replSet RECOVERING    #node2的日志Wed Dec 25 21:19:53 replSet RECOVERING    #node3的日志
#观看状态# ./mongo 127.0.0.1:10001MongoDB shell version: 2.0.9connecting to: 127.0.0.1:10001/testPRIMARY> rs.status(){      "set" : "blort",      "date" : ISODate("2013-12-25T13:26:09Z"),      "myState" : 1,      "members" : [                {                        "_id" : 1,                        "name" : "linux:10001",                        "health" : 1,                        "state" : 1,                        "stateStr" : "PRIMARY",                        "optime" : {                              "t" : 1387977577000,                              "i" : 1                        },                        "optimeDate" : ISODate("2013-12-25T13:19:37Z"),                        "self" : true                },                {                        "_id" : 2,                        "name" : "linux:10002",                        "health" : 1,                        "state" : 2,                        "stateStr" : "SECONDARY",                        "uptime" : 392,                        "optime" : {                              "t" : 1387977577000,                              "i" : 1                        },                        "optimeDate" : ISODate("2013-12-25T13:19:37Z"),                        "lastHeartbeat" : ISODate("2013-12-25T13:26:08Z"),                        "pingMs" : 0                },                {                        "_id" : 3,                        "name" : "linux:10003",                        "health" : 1,                        "state" : 2,                        "stateStr" : "SECONDARY",                        "uptime" : 392,                        "optime" : {                              "t" : 1387977577000,                              "i" : 1                        },                        "optimeDate" : ISODate("2013-12-25T13:19:37Z"),                        "lastHeartbeat" : ISODate("2013-12-25T13:26:08Z"),                        "pingMs" : 0                }      ],      "ok" : 1}
#测试#在主节点插入数据,在从节点观看(出错).# ./mongo 127.0.0.1:10001MongoDB shell version: 2.0.9connecting to: 127.0.0.1:10001/testPRIMARY> db.ccc.insert({"x":123000})PRIMARY> db.ccc.find(){ "_id" : ObjectId("52badedad8d47ba9499bc33a"), "x" : 123000 }PRIMARY> exitbye# ./mongo 127.0.0.1:10003MongoDB shell version: 2.0.9connecting to: 127.0.0.1:10003/testSECONDARY> db.ccc.find()error: { "$err" : "not master and slaveok=false", "code" : 13435 }SECONDARY> exitbye
#退出主节点进程,模拟主节点失败,在原先的从节点观看状态.# ./mongo 127.0.0.1:10003MongoDB shell version: 2.0.9connecting to: 127.0.0.1:10003/testPRIMARY> rs.status(){      "set" : "blort",      "date" : ISODate("2013-12-25T13:35:49Z"),      "myState" : 1,      "syncingTo" : "linux:10001",      "members" : [                {                        "_id" : 1,                        "name" : "linux:10001",                        "health" : 0,                        "state" : 8,                        "stateStr" : "(not reachable/healthy)",                        "uptime" : 0,                        "optime" : {                              "t" : 1387978459000,                              "i" : 1                        },                        "optimeDate" : ISODate("2013-12-25T13:34:19Z"),                        "lastHeartbeat" : ISODate("2013-12-25T13:35:21Z"),                        "pingMs" : 0,                        "errmsg" : "socket exception"                },                {                        "_id" : 2,                        "name" : "linux:10002",                        "health" : 1,                        "state" : 2,                        "stateStr" : "SECONDARY",                        "uptime" : 967,                        "optime" : {                              "t" : 1387978459000,                              "i" : 1                        },                        "optimeDate" : ISODate("2013-12-25T13:34:19Z"),                        "lastHeartbeat" : ISODate("2013-12-25T13:35:49Z"),                        "pingMs" : 0                },                {                        "_id" : 3,                        "name" : "linux:10003",                        "health" : 1,                        "state" : 1,                        "stateStr" : "PRIMARY",                        "optime" : {                              "t" : 1387978459000,                              "i" : 1                        },                        "optimeDate" : ISODate("2013-12-25T13:34:19Z"),                        "self" : true                }      ],      "ok" : 1}PRIMARY> db.ccc.find(){ "_id" : ObjectId("52badedad8d47ba9499bc33a"), "x" : 123000 }PRIMARY> exitbye
         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: 第9周 Mongodb数据库高可用,分布式集群部署