环境说明
建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。创新互联专业提供网站制作、成都网站建设,网页设计,网站制作(企业站、响应式网站、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!
IP | Role |
10.240.216.151 | master |
10.240.216.152 | slave(standby master) |
10.240.216.153 | slave |
MongoDB下载与安装
在10.240.216.151(主库)上
# wgethttp://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz
(如果不能正常下载,可以直接从浏览器下载下来,然后传到服务器上)
# tar -zxf mongodb-linux-x86_64-2.0.4.tgz-C /usr/local/
# mkdir -p /data/{db/geomaster/,log}
# touch /data/log/geomaster.log
# cd /usr/local/
# mv mongodb-linux-x86_64-2.0.4 mongodb
# /usr/local/mongodb/bin/mongod --fork--port 4000 --master --dbpath /data/db/geomaster/ --logpath /data/log/geomaster.log--logappend 启动数据库
# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'` 关闭数据库
在10.240.216.152 和 10.240.216.153(备库)上
# wgethttp://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz
(如果不能正常下载,可以直接从浏览器下载下来,然后传到服务器上)
# tar -zxf mongodb-linux-x86_64-2.0.4.tgz-C /usr/local/
# mkdir -p /data/{db/geoslave/,log}
# touch /data/log/geosalve.log
# cd /usr/local/
# mv mongodb-linux-x86_64-2.0.4 mongodb
# /usr/local/mongodb/bin/mongod --fork--port 4000 --slave --source 10.240.216.151:4000 --dbpath /data/db/geoslave/--logpath /data/log/geosalve.log --logappend --nojournal 启动数据库
# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'` 关闭数据库
验证
在主库上
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> db.runCommand ( {"isMaster": 1 } )
{ "ismaster" : true,"maxBsonObjectSize" : 16777216, "ok" : 1 }
在备库上
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> db.runCommand ( {"isMaster": 1 } )
{
"ismaster" : 0,
"info" : "dead: data toostale halted replication",
"maxBsonObjectSize" :16777216,
"ok" : 1
}
> use local
switched to db local
> db.sources.find()
{ "_id" :ObjectId("55ebfa53ecf735439d6bafeb"), "host" :"10.240.216.151:4000", "source" : "main","syncedTo" : { "t" : 1441528400000, "i" : 1 } }
在主库上创建库和表以及添加一条数据
> use mydb
switched to db mydb
> db
mydb
>db.mydb.user.insert({"username":"zdh"});
> db.mydb.user.find();
{ "_id" :ObjectId("55ebfc2d92e237c3cb30ef9a"), "username" :"zdh" }
> show dbs;
local 6.2001953125GB
mydb 0.203125GB
在从库上检查数据同步情况
> show dbs;
local 0.203125GB
mydb 0.203125GB
> use mydb
switched to db mydb
> db.mydb.user.find()
{"_id" : ObjectId("55ebfc2d92e237c3cb30ef9a"),"username" : "zdh" }
以上输出结果表示OK
对于上面这种master-slave结构有一个很重要的缺陷就是当主库挂掉后,从库要晋升为主库时,需要手动做大量的工作才行,具体步骤有:
1.停掉其中一台slave,然后重新以master的身份启动(晋升10.240.216.152为主库)
# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'`
# /usr/local/mongodb/bin/mongod --fork--port 4000 --master --dbpath /data/db/geoslave/ --logpath/data/log/geosalve.log –logappend
2.如果不能找到研发修改代码指向新的主库IP,那么可以将原主库关机并将其的IP切到新主库服务器上(切IP场景,这种情况下slave端不需要重启mongodb进程,此时原主库将不能启动,这是一个问题)
# ifconfig bond0:1 10.240.216.151 netmask255.255.255.0 up
#arping -I bond0 -c 3 -s 10.240.216.15110.240.216.254
3.将从库10.240.216.153指向新的主库,需要先停掉mongodb,然后source指向新主库启动(在不切IP的情况下操作)
# kill -2 `ps -ef |grep mongod |grep -vgrep |awk '{print $2}'`
# rm -f /data/db/geoslave/mongod.lock /data/db/geoslave/local.*
# /usr/local/mongodb/bin/mongod --fork--port 4000 --slave --source 10.240.216.152:4000 --dbpath /data/db/geoslave/--logpath /data/log/geosalve.log --logappend –nojournal
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> use local
switched to db local
> db.sources.find()
{ "_id" :ObjectId("55ec04dadfd5d65f2f7fe404"), "host" :"10.240.216.152:4000", "source" : "main","syncedTo" : { "t" : 1441531234000, "i" : 1 } }
针对以上操作复杂的过程,我们作如下优化
在主库10.240.216.151上执行
# /usr/local/mongodb/bin/mongod--fork --port 4000 --dbpath /data/db/geomaster/ --logpath/data/log/geomaster.log --logappend --master
在从库(备主)10.240.216.152上执行
# /usr/local/mongodb/bin/mongod--fork --port 4000 --master --slave --dbpath /data/db/geoslave/ --logpath/data/log/geosalve.log –logappend
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> use local
switched to db local
> db.sources.find()
>db.sources.insert({"host":"10.240.216.151:4000"});
> db.sources.find()
{ "_id" :ObjectId("55ec120751fd14c6b2f23a18"), "host" :"10.240.216.151:4000" }
> db.sources.find() 隔30秒左右
{ "_id" :ObjectId("55ec120751fd14c6b2f23a18"), "host" :"10.240.216.151:4000", "source" : "main", "syncedTo": { "t" : 1441534596000, "i" : 1 } }
在从库10.240.216.153上执行
#/usr/local/mongodb/bin/mongod --fork --port 4000 --slave --dbpath /data/db/geomaster/ --logpath/data/log/geosalve.log --logappend –nojournal
# /usr/local/mongodb/bin/mongo127.0.0.1:4000
> use local
switched to db local
> db.sources.find()
>db.sources.insert({"host":"10.240.216.151:4000"})
>db.sources.insert({"host":"10.240.216.152:4000"})
> db.sources.find()
{ "_id" :ObjectId("55ec127a1924006f4e8b2e97"), "host" : "10.240.216.151:4000","source" : "main", "syncedTo" : { "t" :1441534586000, "i" : 1 } }
{ "_id" :ObjectId("55ec127e1924006f4e8b2e98"), "host" :"10.240.216.152:4000", "source" : "main","syncedTo" : { "t" : 1441534593000, "i" : 1 } }
以上配置完成。就实现了当主库10.240.216.151挂掉后,备用主库10.240.216.152可以直接提供对外的服务,无需重启mongodb进程,并且从库无需作任何更改。
验证
在主库10.240.216.151上
> show dbs
local 6.2001953125GB
mydb 0.203125GB
> use mydb
switched to db mydb
>db.mydb.user.insert({"username":"zhongguo","age":"26"})
> db.mydb.user.find()
{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }
>db.mydb.user.insert({"username":"zhongguo2","age":"30"})
在从库(备主)10.240.216.152上
> use mydb
switched to db mydb
> db.mydb.user.find()
{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }
> db.mydb.user.insert({"username":"finished","time":"2015-09-06"})
> db.mydb.user.find()
{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }
{ "_id" : ObjectId("55ec133e51fd14c6b2f23a19"),"username" : "finished", "time" :"2015-09-06" }
> db.mydb.user.find()
{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }
{ "_id" :ObjectId("55ec133e51fd14c6b2f23a19"), "username" :"finished", "time" : "2015-09-06" }
{ "_id" :ObjectId("55ec136abab480b5485dd72f"), "username" :"zhongguo2", "age" : "30" }
在从库10.240.216.153上
> use mydb
switched to db mydb
> db.mydb.user.find()
{ "_id" :ObjectId("55ec12e4bab480b5485dd72e"), "username" :"zhongguo", "age" : "26" }
> db.mydb.user.find()
{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }
{ "_id" : ObjectId("55ec133e51fd14c6b2f23a19"),"username" : "finished", "time" :"2015-09-06" }
> db.mydb.user.find()
{ "_id" : ObjectId("55ec12e4bab480b5485dd72e"),"username" : "zhongguo", "age" : "26" }
{ "_id" :ObjectId("55ec133e51fd14c6b2f23a19"), "username" :"finished", "time" : "2015-09-06" }
{ "_id" :ObjectId("55ec136abab480b5485dd72f"), "username" :"zhongguo2", "age" : "30" }
上面的结果说明,备用从库是即可生效的,需要保证的是我们的写是确保从主库上写入的。