mongodb3.0.1副本集安装部署(仲裁节点模式)
环境:
OS:Centos 7
db:3.0.1
两台物理机器,启用3个进程,各角色如下
192.168.1.118:28007 主
192.168.1.85:28008 从
192.168.1.85:28009 仲裁节点
1.下载安装介质,我这里下载的是mongodb-linux-x86_64-3.0.1.tgz
http://dl.mongodb.org/dl/linux/x86_64
-------------------在192.168.1.118上安装---------------------
1.安装数据库
每个节点都安装数据库,安装方法一样
[root@localhost soft]# tar -xvf mongodb-linux-x86_64-3.0.1.tgz
[root@localhost soft]# mv mongodb-linux-x86_64-3.0.1 /home/hxlmiao/mongodb
[root@localhost soft]# cd /home/hxlmiao/mongodb
[root@pxc01 soft]#mkdir data ##数据文件目录
[root@pxc01 soft]#mkdir log ##日志文件目录
[root@pxc01 soft]#mkdir conf ##配置文件目录
[root@pxc01 soft]#mkdir run ##配置pid文件路径
[root@pxc01 soft]#mkdir key ##认证文件目录,若采用密码认证的方式的话,需要创建该目录,我们这里不采用认证的方式
修改目录权限,将权限修改为普通hxlmiao用户
[root@localhost mongodb]# cd /home/hxlmiao
[root@localhost hxlmiao]# chown -R hxlmiao.hxlmiao ./mongodb/
2.创建配置文件mongo.cnf
su - hxlmiao
vi /home/hxlmiao/mongodb/conf/mongo.cnf
##主节点的配置参数
port =28007
fork =true
dbpath =/home/hxlmiao/mongodb/data
logpath =/home/hxlmiao/mongodb/log/logs
pidfilepath=/home/hxlmiao/mongodb/run/28007.pid
logappend =true
shardsvr =true
replSet =mymgd
bind_ip=192.168.1.118,127.0.0.1
oplogSize=10000
noprealloc=true
---------------192.168.1.85上操作-------------------
---------------从节点----
1.安装数据库
每个节点都安装数据库,安装方法一样
[root@localhost soft]# tar -xvf mongodb-linux-x86_64-3.0.1.tgz
[root@localhost soft]# mv mongodb-linux-x86_64-3.0.1 /home/hxlmiao/mongodb
[root@localhost soft]# cd /home/hxlmiao/mongodb
[root@pxc01 soft]#mkdir data ##数据文件目录
[root@pxc01 soft]#mkdir log ##日志文件目录
[root@pxc01 soft]#mkdir conf ##配置文件目录
[root@pxc01 soft]#mkdir run ##配置pid文件路径
[root@pxc01 soft]#mkdir key ##认证文件目录,若采用密码认证的方式的话,需要创建该目录,我们这里不采用认证的方式
修改目录权限,将权限修改为普通hxlmiao用户
[root@localhost mongodb]# cd /home/hxlmiao
[root@localhost hxlmiao]# chown -R hxlmiao.hxlmiao ./mongodb/
2.拷贝一份做为仲裁节点
[root@localhost hxlmiao]# cp -R ./mongodb ./mongodb_arbiter
修改权限
[root@localhost hxlmiao]#chown -R hxlmiao.hxlmiao ./mongodb_arbiter/
3.从库的配置文件
su - hxlmiao
vi /home/hxlmiao/mongodb/conf/mongo.cnf
内容如下:
port =28008
fork =true
dbpath =/home/hxlmiao/mongodb/data
logpath =/home/hxlmiao/mongodb/log/logs
pidfilepath=/home/hxlmiao/mongodb/run/28008.pid
logappend =true
shardsvr =true
replSet =mymgd
bind_ip=192.168.1.85,127.0.0.1
oplogSize=10000
noprealloc=true
4.仲裁节点的配置文件
su - hxlmiao
vi /home/hxlmiao/mongodb_arbiter/conf/mongo.cnf
port =28009
fork =true
dbpath =/home/hxlmiao/mongodb_arbiter/data
logpath =/home/hxlmiao/mongodb_arbiter/log/logs
pidfilepath=/home/hxlmiao/mongodb_arbiter/run/28009.pid
logappend =true
shardsvr =true
replSet =mymgd
bind_ip=192.168.1.85,127.0.0.1
oplogSize=10000
noprealloc=true
-----------------启动--------------------------
5.启动
主节点启动(192.168.1.118):
su - hxlmiao
-bash-4.2$ cd /home/hxlmiao/mongodb/bin
-bash-4.2$ ./mongod -f /home/hxlmiao/mongodb/conf/mongo.cnf
从节点启动(192.168.1.85):
su - hxlmiao
-bash-4.2$ cd /home/hxlmiao/mongodb/bin
-bash-4.2$ ./mongod -f /home/hxlmiao/mongodb/conf/mongo.cnf
仲裁节点启动:
-bash-4.2$ ./mongod -f /home/hxlmiao/mongodb_arbiter/conf/mongo.cnf
---------------创建副本集-----------------
这里我们在主节点上执行(192.168.1.118上面执行)
-bash-4.2$ cd /home/hxlmiao/mongodb/bin
-bash-4.2$ ./mongo 192.168.1.118:28007
use admin
config={_id:'mymgd',members:[{_id:0,host:'192.168.1.118:28007'},{_id:1,host:'192.168.1.85:28008'},{_id:2,host:'192.168.1.85:28009', arbiterOnly:true}]}
rs.initiate(config)
查看副本集情况
mymgd:OTHER> rs.status()
{
"set" : "mymgd",
"date" : ISODate("2019-08-29T01:01:56.237Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.1.118:28007",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1102,
"optime" : Timestamp(1567040486, 1),
"optimeDate" : ISODate("2019-08-29T01:01:26Z"),
"electionTime" : Timestamp(1567040490, 1),
"electionDate" : ISODate("2019-08-29T01:01:30Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.85:28008",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 29,
"optime" : Timestamp(1567040486, 1),
"optimeDate" : ISODate("2019-08-29T01:01:26Z"),
"lastHeartbeat" : ISODate("2019-08-29T01:01:54.847Z"),
"lastHeartbeatRecv" : ISODate("2019-08-29T01:01:54.895Z"),
"pingMs" : 1,
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.1.85:28009",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 29,
"lastHeartbeat" : ISODate("2019-08-29T01:01:54.847Z"),
"lastHeartbeatRecv" : ISODate("2019-08-29T01:01:54.885Z"),
"pingMs" : 1,
"configVersion" : 1
}
],
"ok" : 1
}
-----------验证-----------------------
测试写入数据:
use db_test
db.tb_test01.insert( {"name":"yiibai tutorials"})
db.tb_test01.find()
----------遇到的问题------------------
1.mongorc.js
2019-08-29T09:11:52.062+0800 I STORAGE In File::open(), ::open for '/home/hxlmiao/.mongorc.js' failed with errno:13 Permission denied
解决办法:
[root@localhost home]# chown -R hxlmiao.hxlmiao ./hxlmiao/
2.从库执行命令提示找不到master
Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
解决办法:
rs.slaveOk()
要想不用每次输入该命令,可以修改如下文件
-bash-4.2$ find / -name .mongorc.js
./.mongorc.js
在文件里面添加一行:rs.slaveOk();
一般这个文件都是空的,直接加上去。保存退出。之后退出mongo在进去就可以了.
如下:
-bash-4.2$ more .mongorc.js
rs.slaveOk();
----------------------数据迁移-------------------------------
1.导出数据库,指定导出某个数据库
./mongodump -h 192.168.1.118:28007 -d db_test --authenticationDatabase=admin -o /home/hxlmiao/mongobak
或是
./mongodump -h 192.168.1.118:28007 -d db_test -o /home/hxlmiao/mongobak
要是数据库很大的话,编写脚本后台导出
vi mongodump_db.sh
#!/bin/bash
/home/hxlmiao/mongodb/bin/mongodump -h 192.168.1.118:28007 -d db_test --authenticationDatabase=admin -o /home/hxlmiao/mongobak
后台执行:
nohup ./mongodump_db.sh > mongodump_db.out 2>&1 &
这里导出后会在/home/hxlmiao/mongobak目录下,每个数据库都有一个子目录,导入数据库的时候需要指定相对应的目录
2.在新的数据库进行导入
./mongorestore -h 192.168.1.118:28007 -d db_test --authenticationDatabase=admin /home/hxlmiao/mongobak/db_test
----------日常管理------------------------
[root@pxc01 bin]# ./mongo localhost:28007
MongoDB shell version: 2.6.5
connecting to: localhost:10000/test
> use admin
switched to db admin
> db.shutdownServer()
2018-03-14T21:31:43.194-0400 DBClientCursor::init call() failed
server should be down...
-- The End --