win7 安装mongodb
cmd
进入mongodb的文件目录
执行:
E:\mongodb\bin>mongod --dbpath=E:\mongodb\db
> var single={"name":"jack","password":"12345","age":20,
"address":{"province":"anhui","city":"hefei"},
"favourite":["apple","banana"]}
> db.runCommand({"replSetInitiate":{}
... "_id":"shopex",
... "members":[
... {
... "_id":1,
... "host":"127.0.0.1:2222"
... },
... {
... "_id":2,
... "host":"127.0.0.1:3333"
... }
... ]}}}
centos 6.6 安装mongodb 副本集
IP:192.168.5.100 192.168.5.102 192.168.5.103
有必要关掉防火强 或放开27017端口
service iptables stop
DEVICE="eth0"
BOOTPROTO="static"
HWADDR="00:0C:29:ED:98:47"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="ba7c7b18-8a97-464f-a113-984e7761497d"
IPADDR=192.168.5.101 /192.168.0.102/192.168.5.103
NETMASK=255.255.255.0
GATEWAY=192.168.5.1
然后最好改一下主机名
如node1 192.168.5.100
这样方便管理
然后HOST文件里面加入
[root@node1 ~]# vim /etc/hosts
192.168.5.100 node1
192.168.5.102 node2
192.168.5.103 node3
现在我就不做这些了。
然后我从网上下载mongodb 解压缩放到 /usr/local/mongodb-2.6.5/ 文件夹里面
注意版本一定要选对。 32/64 要不然服务启动有问题的
然后在/usr/local/mongodb-2.6.5/ 创建两个文件夹 一个data 一个conf conf文件夹里面创建名叫mongodb.conf的文件
如图:
vi /usr/local/mongodb-2.6.5/conf/mongodb.conf
写入以下内容
dbpath = /usr/local/mongodb-2.6.5/data --数据文件存放
logpath = /var/log/mongodb.log --日志文件存放
fork = true --后台启动
nojournal = true
port = 27017 --端口
replSet = blort --副本集选项
下面启动
[root@localhost ~]# cd /usr/local/mongodb-2.6.5/bin/
[root@localhost bin]# ./mongod -f ../conf/mongodb.conf
-bash: ./mongod: Permission denied //提示这个是没有权限 那么下面看怎么加权限
[root@localhost ~]# sudo chmod 777 /usr/local/mongodb-2.6.5/
[root@localhost ~]# cd /usr/local/mongodb-2.6.5/
[root@localhost ~]# chmod -R 700 /usr/local/mongodb-2.6.5/bin/
[root@localhost bin]# ./mongod -f ../conf/mongodb.conf
出现以下内容证明启动成功了
about to fork child process, waiting until server is ready for connections.
forked process: 1758
child process started successfully, parent exiting
下面检查一下子
[root@localhost bin]# netstat -antp | grep 27017 //这里的27017 是mongodb的启动默认端口 当然你也可以改端口的
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1758/./mongod
做到这里 第一台就基本成功了
然后下面把 其它两台做同样的操作 就可以初始化副本集了
下面开始初始化副本集了
[root@localhost bin]# ./mongo 192.168.5.100:27017
> rs.initiate({"_id":"blort",members:[
... {"_id":1,"host":"192.168.5.100:27017"},
... {"_id":2,"host":"192.168.5.102:27017"},
... {"_id":3,"host":"192.168.5.103:27017"},
... ]})
执行这个 返回
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
blort:PRIMARY> rs.status()
{
"set" : "blort",
"date" : ISODate("2015-01-08T22:11:57Z"),
"myState" : 1,
"members" : [
{
"_id" : 1,
"name" : "192.168.5.100:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", //主
"uptime" : 498,
"optime" : Timestamp(1420755058, 1),
"optimeDate" : ISODate("2015-01-08T22:10:58Z"),
"electionTime" : Timestamp(1420755066, 1),
"electionDate" : ISODate("2015-01-08T22:11:06Z"),
"self" : true
},
{
"_id" : 2,
"name" : "192.168.5.102:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 59,
"optime" : Timestamp(1420755058, 1),
"optimeDate" : ISODate("2015-01-08T22:10:58Z"),
"lastHeartbeat" : ISODate("2015-01-08T22:11:56Z"),
"lastHeartbeatRecv" : ISODate("2015-01-08T22:11:57Z"),
"pingMs" : 2,
"syncingTo" : "192.168.5.100:27017"
},
{
"_id" : 3,
"name" : "192.168.5.103:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 59,
"optime" : Timestamp(1420755058, 1),
"optimeDate" : ISODate("2015-01-08T22:10:58Z"),
"lastHeartbeat" : ISODate("2015-01-08T22:11:56Z"),
"lastHeartbeatRecv" : ISODate("2015-01-08T22:11:57Z"),
"pingMs" : 1,
"syncingTo" : "192.168.5.100:27017"
}
],
"ok" : 1
}
然后测试 主的创建数据 是否同步到另外两台
blort:SECONDARY> db.user.find();
error: { "$err" : "not master and slaveOk=false", "code" : 13435 } --从服务器不能查数据
blort:SECONDARY>
5.测试 主从切换
停止主节点的Mongodb服务
[root@node2 bin]# netstat -antp | grep mon
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 22501/./mongod
tcp 0 0 192.168.1.251:27017 192.168.1.252:49729 ESTABLISHED 22501/./mongod
tcp 0 0 192.168.1.251:27017 192.168.1.252:50514 ESTABLISHED 22501/./mongod
tcp 0 0 192.168.1.251:27017 192.168.1.250:37970 ESTABLISHED 22501/./mongod
tcp 0 0 192.168.1.251:27017 192.168.1.250:37844 ESTABLISHED 22501/./mongod
tcp 0 0 192.168.1.251:27017 192.168.1.252:49728 ESTABLISHED 22501/./mongod
tcp 0 0 192.168.1.251:33255 192.168.1.250:27017 ESTABLISHED 22501/./mongod
tcp 0 0 192.168.1.251:27017 192.168.1.250:37845 ESTABLISHED 22501/./mongod
tcp 0 0 192.168.1.251:46086 192.168.1.252:27017 ESTABLISHED 22501/./mongod
[root@node2 bin]# pkill mongod
[root@node2 bin]# netstat -antp | grep mon
[root@node2 bin]#
切换后 在查询数据库 数据还在。 正明数据已经同步
在主节点上删除node3服务器
blort:PRIMARY>rs.remove("node3:27017");
在主节点上添加其它节点到副本集中
blort:PRIMARY>rs.add("node3:27017");
在从节点读取数据
blort:SECONDARY> db.user.find();
error: { "$err" : "not master and slaveOk=false", "code" : 13435 } --查找数据提示出错
blort:SECONDARY> db.getMongo().setSlaveOk(); --执行可以从主节点读取
blort:SECONDARY> db.user.find(); --在从节点可以读取数据了