mongo高可用架构
v 0.1
配置规划
操作系统CentOS 6.7MongoDB数据库版本Mongo-2.2.6mongo主节点192.168.1.143:10111mongo从节点192.168.1.145:10111mongo从节点192.168.1.146:10111mongo仲裁节点192.168.1.145:20111
(可根据情况增加,删除从节点)
db.auth(‘admin’,‘111000’)
部署方案如下:
1
#下载wget工具
ansible mongos -m yum -a “name=wget state=installed”
2
#官网下载二进制文件包
ansible mongos -m shell -a “wget -P /home/ http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.2.6.tgz?_ga=1.193687085.1603445767.1489042040”
3
#解压mongo程序,安装至 /usr/local/mongodb/目录下
ansible mongos -m shell -a “mv /home/mongodb-linux-x86_64-2.2.6.tgz* /home/mongodb-linux-x86_64-2.2.6.tgz”
ansible mongos -m shell -a “tar -zxvf /home/mongodb-linux-x86_64-2.2.6.tgz -C /usr/local/”
ansible mongos -m shell -a “mv /usr/local/mongodb-linux-x86_64-2.2.6 /usr/local/mongodb”
4
#建立数据存储目录,配置目录,日志目录
ansible mongos -m shell -a “mkdir /usr/local/mongodb/{data,conf,logs}”
5
#创建节点数据目录
cd /usr/local/mongodb
主节点:192.168.1.143
mkdir -p ./data/master
备节点:192.168.1.145
mkdir -p ./data/slaver
备节点:192.168.1.146
mkdir -p ./data/slaver
仲裁节点:192.168.1.145
mkdir -p ./data/arbiter
#三个目录分别对应主,备,仲裁节点
6
#mongo配置文件编写
1.主节点192.168.1.143 配置文件
conf/mongodb-master.conf
- dbpath=/usr/local/mongodb-2.2.6/data/master
- logpath=/usr/local/mongodb-2.2.6/logs/master.log
- pidfilepath=/usr/local/mongodb-2.2.6/data/master.pid
- directoryperdb=true
- logappend=true
- replSet=hx_set
- bind_ip=192.168.1.143
- port=10111
- oplogSize=1000
- fork=true
- noprealloc=true
2.备节点192.168.1.145配置文件
conf/mongodb-slaver.conf
- dbpath=/usr/local/mongodb-2.2.6/data/slaver
- logpath=/usr/local/mongodb-2.2.6/logs/slaver.log
- pidfilepath=/usr/local/mongodb-2.2.6/data/slaver.pid
- directoryperdb=true
- logappend=true
- replSet=hx_set
- bind_ip=192.168.1.145
- port=10111
- oplogSize=1000
- fork=true
- noprealloc=true
- smallfiles=true
3.备节点192.168.1.146配置文件
- dbpath=/usr/local/mongodb-2.2.6/data/slaver
- logpath=/usr/local/mongodb-2.2.6/logs/slaver.log
- pidfilepath=/usr/local/mongodb-2.2.6/data/slaver.pid
- directoryperdb=true
- logappend=true
- replSet=hx_set
- bind_ip=192.168.1.146
- port=10111
- oplogSize=1000
- fork=true
- noprealloc=true
- smallfiles=true
4.仲裁节点192.168.1.145 配置文件conf/mongodb-arbiter.conf
- dbpath=/usr/local/mongodb-2.2.6/data/arbiter
- logpath=/usr/local/mongodb-2.2.6/logs/arbiter.log
- pidfilepath=/usr/local/mongodb-2.2.6/data/arbiter.pid
- directoryperdb=true
- logappend=true
- replSet=hx_set
- bind_ip=192.168.1.145
- port=20111
- oplogSize=1000
- fork=true
- noprealloc=true
7
#进入每个mongodb节点根据配置,启动服务
./bin/monood -f conf/mongodb-master.conf
./bin/mongod -f conf/mongodb-slaver.conf
./bin/mongod -f conf/mongodb-arbiter.conf
8
#另外,可以通过如下步骤动态添加删除,修改主、备、仲裁节点
可以通过客户端连接mongodb,也可以直接在三个节点中选择一个连接mongodb。
###注意标点符号
cfg = { _id:"hx_set1,
members:[
{_id:0, host:‘192.168.2.21:27017’, priority:3},
{_id:1, host:‘192.168.2.20:27017’, priority:2} ,
{_id:2, host:‘192.168.2.20:27108’, arbiterOnly:true},
]
};
rs.initiate(cfg);
9
#查看Mongo副本集配置结果
rs.status();
#从节点的查询需要运行此命令
db.setSlaveOk() 或者rs.slaveOk();
10 初始Mongo DB
#添加Mongo 数据库
use hx;
#添加用户 hxuser,并设置密码与读写权限
db.addUser(‘hxuser’,‘123456’);
db.auth(‘admin’,‘111000’)
#验证客户端访问,及读写数据库权限正确
./bin/mongo -u hxuser -p123456 hx
11
#uc模块下配置连接Mongo DB服:q!
务参数
配置/usr/local/java/uc_tomcat/webapps/uc/WEB-INF/classes/properties/mongodb.properties
mongo.replicaSet=192.168.1.143:10111
mongo.host=192.168.1.143
mongo.port=10111
mongo.database=hx
mongo.username=hxuser
mongo.password=123456
后记:
#默认只在主节点进行数据读写,主节点数据会同步到从节点,当主节点出现故障,或者需要维护时,从节点切换为主节点。以此消除单点故障,达到服务高可用的目的。
管理员账户 admin库,admin/111000
hx库账户 root/123
db.auth(‘root’,‘123456’)
警告 [ReplicaSetStatus:Updater] com.mongodb.DBTCPConnector.setMasterAddress Primary switching from /192.168.2.21:27017 to /192.168.2.20:27017
#不识别这个参数
连接参数:
https://mongodb-documentation.readthedocs.io/en/latest/reference/connection-string.html
Read Preference Options¶
Read Preference Options¶
● Connect to a replica set with two members, one on db1.example.net and the other on db2.example.net:
mongodb://db1.example.net,db2.example.com
● Connect to a replica set with three members running on localhost, on ports 27017, 27018, and 27019:
mongodb://localhost,localhost:27018,localhost:27019
● Connect to a replica set with three members. Send all writes to the primary and distribute reads to the secondaries:
mongodb://example1.com,example2.com,example3.com/?readPreference=secondary
● Connect to a replica set with write concern configured to wait for replication to succeed on at least two members, with a two-second timeout.
mongodb://example1.com,example2.com,example3.com/?w=2&wtimeoutMS=2000
https://blog.51cto.com/lizhuquan0769/1763287
进一步的分片方式
https://blog.csdn.net/hjsw1/article/details/78402930
#mongoClient