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

mongodb管理

#启动MongoDB
[root@linux bin]# ./mongod -f /nosql/mongodb/mongodb/bin/mongodb.conf
forked process: 3104
[root@linux bin]# all output going to: /nosql/mongodb/mongodb/log/mongodb.log
[root@linux bin]# pstree -p | grep mongo
        |-mongod(3104)-+-{mongod}(3105)
        | |-{mongod}(3106)
        | |-{mongod}(3107)
        | |-{mongod}(3108)
        | |-{mongod}(3109)
        | |-{mongod}(3110)
        | `-{mongod}(3111)

#文明的关闭mongodb
[root@linux bin]# ./mongo
MongoDB shell version: 2.0.9
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
Wed Dec 25 18:21:27 DBClientCursor::init call() failed
Wed Dec 25 18:21:27 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1
server should be down...
Wed Dec 25 18:21:27 trying reconnect to 127.0.0.1
Wed Dec 25 18:21:27 reconnect 127.0.0.1 failed couldn't connect to server 127.0.0.1
Wed Dec 25 18:21:27 Error: error doing query: unknown shell/collection.js:151
> exit
bye
[root@linux bin]# pstree -p | grep mongo

#增加用户
> use admin
switched 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 admin
switched to db admin
> show tables;
system.indexes
system.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 admin
switched to db admin                         #切换到admin库
> db.auth("root","root")
1                                            #登录成功(因为前面创建用户是在admin库建立的)
> use test
switched to db test                          #切换到test库
> db.test.insert({"name":"zhangsan"});
> db.test.find()
{ "_id" : ObjectId("52bab77b24204e3767449284"), "name" : "zhangsan" }

#删除用户
> use test
switched 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();

热备份

[root@linux bin]# ./mongodump -u root -p root -d test
connected to: 127.0.0.1
DATABASE: 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 objects
mongodump     #导入,用来备份
mongorestore  #导入,用来还原
具体的用户可以使用-help来查看帮组信息。

#强制一致,备份的时候保证数据一致性。
第9周 Mongodb数据库高可用,分布式集群部署_第1张图片

#修复数据库
修复所有数据库:用带—repair参数启动
db.repairDatabase()

主从复制

#可以配置为1-1,也可以配置1-n
#1-1复制配置
#配置Master
[root@linux bin]# ./mongod --dbpath /nosql/mongodb/dbs/master --port 10000 --master --rest
Wed 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 [initandlisten] MongoDB starting : pid=4289 port=10000 dbpath=/nosql/mongodb/dbs/master master=1 32-bit host=linux
Wed Dec 25 20:59:13 [initandlisten]
Wed Dec 25 20:59:13 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Wed Dec 25 20:59:13 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitations
Wed Dec 25 20:59:13 [initandlisten] ** with --journal, the limit is lower
Wed Dec 25 20:59:13 [initandlisten]
Wed Dec 25 20:59:13 [initandlisten] db version v2.0.9, pdfile version 4.5
Wed Dec 25 20:59:13 [initandlisten] git version: 7e34cb36a6ae64d527c0b0da81fa967606c55433
Wed Dec 25 20:59:13 [initandlisten] 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_41
Wed Dec 25 20:59:13 [initandlisten] options: { dbpath: "/nosql/mongodb/dbs/master", master: true, port: 10000, rest: true }
Wed Dec 25 20:59:13 [initandlisten] ******
Wed Dec 25 20:59:13 [initandlisten] creating replication oplog of size: 47MB...
Wed Dec 25 20:59:13 [FileAllocator] allocating new datafile /nosql/mongodb/dbs/master/local.ns, filling with zeroes...
Wed Dec 25 20:59:13 [FileAllocator] creating directory /nosql/mongodb/dbs/master/_tmp
Wed Dec 25 20:59:13 [FileAllocator] done allocating datafile /nosql/mongodb/dbs/master/local.ns, size: 16MB, took 0.309 secs
Wed Dec 25 20:59:13 [FileAllocator] allocating new datafile /nosql/mongodb/dbs/master/local.0, filling with zeroes...
Wed Dec 25 20:59:14 [FileAllocator] done allocating datafile /nosql/mongodb/dbs/master/local.0, size: 16MB, took 0.376 secs
Wed Dec 25 20:59:14 [FileAllocator] allocating new datafile /nosql/mongodb/dbs/master/local.1, filling with zeroes...
Wed Dec 25 20:59:15 [FileAllocator] done allocating datafile /nosql/mongodb/dbs/master/local.1, size: 32MB, took 0.654 secs
Wed Dec 25 20:59:15 [FileAllocator] allocating new datafile /nosql/mongodb/dbs/master/local.2, filling with zeroes...
Wed Dec 25 20:59:16 [FileAllocator] done allocating datafile /nosql/mongodb/dbs/master/local.2, size: 64MB, took 1.77 secs
Wed Dec 25 20:59:16 [initandlisten] ******
Wed Dec 25 20:59:16 [initandlisten] waiting for connections on port 10000
Wed Dec 25 20:59:16 [websvr] admin web console waiting for connections on port 11000

#master监控界面:http://10.10.10.8:11000/
#配置从节点
[root@linux bin]# ./mongod --dbpath /nosql/mongodb/dbs/slave --port 10001 --slave --rest --source 127.0.0.1:10000
Wed 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 [initandlisten] MongoDB starting : pid=4298 port=10001 dbpath=/nosql/mongodb/dbs/slave slave=1 32-bit host=linux
Wed Dec 25 20:59:29 [initandlisten]
Wed Dec 25 20:59:29 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Wed Dec 25 20:59:29 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitations
Wed Dec 25 20:59:29 [initandlisten] ** with --journal, the limit is lower
Wed Dec 25 20:59:29 [initandlisten]
Wed Dec 25 20:59:29 [initandlisten] db version v2.0.9, pdfile version 4.5
Wed Dec 25 20:59:29 [initandlisten] git version: 7e34cb36a6ae64d527c0b0da81fa967606c55433
Wed Dec 25 20:59:29 [initandlisten] 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_41
Wed Dec 25 20:59:29 [initandlisten] options: { dbpath: "/nosql/mongodb/dbs/slave", port: 10001, rest: true, slave: true, source: "127.0.0.1:10000" }
Wed Dec 25 20:59:29 [websvr] admin web console waiting for connections on port 11001
Wed Dec 25 20:59:29 [initandlisten] waiting for connections on port 10001
Wed Dec 25 20:59:30 [FileAllocator] allocating new datafile /nosql/mongodb/dbs/slave/local.ns, filling with zeroes...
Wed Dec 25 20:59:30 [FileAllocator] creating directory /nosql/mongodb/dbs/slave/_tmp
Wed Dec 25 20:59:30 [FileAllocator] done allocating datafile /nosql/mongodb/dbs/slave/local.ns, size: 16MB, took 0.45 secs
Wed Dec 25 20:59:30 [FileAllocator] allocating new datafile /nosql/mongodb/dbs/slave/local.0, filling with zeroes...
Wed Dec 25 20:59:31 [FileAllocator] done allocating datafile /nosql/mongodb/dbs/slave/local.0, size: 16MB, took 0.439 secs
Wed Dec 25 20:59:31 [FileAllocator] allocating new datafile /nosql/mongodb/dbs/slave/local.1, filling with zeroes...
Wed Dec 25 20:59:31 [replslave] build index local.sources { _id: 1 }
Wed Dec 25 20:59:31 [replslave] build index done 0 records 0.002 secs
Wed Dec 25 20:59:31 [replslave] repl: from host:127.0.0.1:10000
Wed Dec 25 20:59:31 [replslave] build index local.me { _id: 1 }
Wed Dec 25 20:59:31 [replslave] build index done 0 records 0 secs
Wed Dec 25 20:59:31 [FileAllocator] done allocating datafile /nosql/mongodb/dbs/slave/local.1, size: 32MB, took 0.909 secs
Wed Dec 25 20:59:38 [replslave] repl: applied 0 operations
Wed Dec 25 20:59:38 [replslave] repl: end sync_pullOpLog syncedTo: Dec 25 20:59:30 52bad6b2:1
Wed Dec 25 20:59:38 [replslave] repl: sleep 2 sec before next pass
Wed Dec 25 20:59:40 [replslave] repl: from host:127.0.0.1:10000
Wed Dec 25 20:59:47 [replslave] repl: applied 1 operations
Wed Dec 25 20:59:47 [replslave] repl: end sync_pullOpLog syncedTo: Dec 25 20:59:40 52bad6bc:1
Wed Dec 25 20:59:47 [replslave] repl: from host:127.0.0.1:10000

#slave监控界面:http://10.10.10.8:11001/
#验证复制
[root@linux bin]# ./mongo 127.0.0.1:10000
MongoDB shell version: 2.0.9
connecting to: 127.0.0.1:10000/test
> db.abcd.insert({"x":123})
> db.abcd.find()
{ "_id" : ObjectId("52bad6e38976ed3868507aec"), "x" : 123 }
> exit
bye
[root@linux bin]# ./mongo 127.0.0.1:10001
MongoDB shell version: 2.0.9
connecting to: 127.0.0.1:10001/test
> db.abcd.find()
{ "_id" : ObjectId("52bad6e38976ed3868507aec"), "x" : 123 }
> exit
bye

副本集

[root@linux dbs]# pwd
/nosql/mongodb/dbs
# mkdir -p node1 node2 node3
[root@linux dbs]# ls
node1 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
[root@linux bin]# ./mongo 127.0.0.1:10001
MongoDB shell version: 2.0.9
connecting 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 [rsMgr] replSet PRIMARY       #node1的日志
Wed Dec 25 21:19:53 [conn2] replSet RECOVERING    #node2的日志
Wed Dec 25 21:19:53 [conn2] replSet RECOVERING    #node3的日志

#观看状态
[root@linux bin]# ./mongo 127.0.0.1:10001
MongoDB shell version: 2.0.9
connecting to: 127.0.0.1:10001/test
PRIMARY> 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
}

#测试
#在主节点插入数据,在从节点观看(出错).
[root@linux bin]# ./mongo 127.0.0.1:10001
MongoDB shell version: 2.0.9
connecting to: 127.0.0.1:10001/test
PRIMARY> db.ccc.insert({"x":123000})
PRIMARY> db.ccc.find()
{ "_id" : ObjectId("52badedad8d47ba9499bc33a"), "x" : 123000 }
PRIMARY> exit
bye
[root@linux bin]# ./mongo 127.0.0.1:10003
MongoDB shell version: 2.0.9
connecting to: 127.0.0.1:10003/test
SECONDARY> db.ccc.find()
error: { "$err" : "not master and slaveok=false", "code" : 13435 }
SECONDARY> exit
bye

#退出主节点进程,模拟主节点失败,在原先的从节点观看状态.
[root@linux bin]# ./mongo 127.0.0.1:10003
MongoDB shell version: 2.0.9
connecting to: 127.0.0.1:10003/test
PRIMARY> 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> exit
bye

你可能感兴趣的:(第9周 Mongodb数据库高可用,分布式集群部署)