Mongodb副本集搭建

1,下载mongodb

社区版

https://www.mongodb.com/try/download

 MongoDB副本集搭建_bash

 

 

 

安装步骤

mkdir -p /opt/mongodb-replica

cd /opt/mongodb-replica

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.6.tgz

tar xf mongodb-linux-x86_64-rhel70-4.4.6.tgz

mv mongodb-linux-x86_64-rhel70-4.4.6 mongodb

vim /etc/profile

在下列文件里面加入环境变量

vim ~/.bash_profile或者在文件/etc/profile里面添加如下环境变量

export PATH="$PATH:/opt/mongodb-replica/mongodb/bin"

[root@master-cluster ~]# . ~/.bash_profile 或者[root@master-cluster ~]# sourch ~/.bash_profile

 

创三个mogodb实例配置

[root@master-cluster ~]# mkdir -pv /opt/mongodb-replica/mongodb1/{conf,log,pid,data}

[root@master-cluster ~]# mkdir -pv /opt/mongodb-replica/mongodb2/{conf,log,pid,data}

[root@master-cluster ~]# mkdir -pv /opt/mongodb-replica/mongodb3/{conf,log,pid,data}

 

在每个实例添加配置,修改对应的配置文件里面的路径跟端口

[root@master-cluster mongodb-replica]# vim mongodb1/conf/mongodb.conf

!注意4.4.6版本配置文件以yaml文件格式,格式很重要

storage:

 dbPath: /opt/mongodb-replica/mongodb1/data

 journal:

  enabled: true

systemLog:

 destination: file

 logAppend: true

 path: /opt/mongodb-replica/mongodb1/log/mongod.log

processManagement:

 fork: true

 pidFilePath: /opt/mongodb-replica/mongodb1/pid/mongod.pid

net:

 port: 27011

 bindIp: 0.0.0.0

maxIncomingConnections: 20000

replication:

 replSetName: "test"

编写启动脚本

[root@master-cluster mongodb-replica]# vim start.sh

. ~/.bash_profile

mongod -f /opt/mongodb-replica/mongodb1/conf/mongodb.conf

mongod -f /opt/mongodb-replica/mongodb2/conf/mongodb.conf

mongod -f /opt/mongodb-replica/mongodb3/conf/mongodb.conf

 

启动三个实例

[root@master-cluster mongodb-replica]# sh start.sh

 

配置副本集

[root@master-cluster mongodb-replica]# mongo --port 27011

> use admin

switched to db admin

test:SECONDARY> rs.initiate( { _id: "test", members: [ { _id: 0, host: "192.168.113.1:27011" }, { _id: 1, host: "192.168.113.2:27012" }, { _id: 2, host: "192.168.113.3:27013",arbiterOnly:true } ] })

rbiterOnly:true     仲裁节点

 MongoDB副本集搭建_linux_02

查看副本集状态

test:PRIMARY> rs.status()

 MongoDB副本集搭建_linux_03

 

 

 

登录两个从节点执行

test:ARBITER> db.getMongo().setSecondaryOk()

新版本db.getMongo().setSecondaryOk() #设置从节点为只读数据库

旧版本是db.getMongo().setSlaveOk() #设置从节点为只读数据库

 

创建超级用户

test:PRIMARY> db.createUser({user: "hcadmin", pwd: "Root#123", roles: [{role: "root", db: "admin"}]})

 MongoDB副本集搭建_bash_04

 

 

 

 

注意!

db: "admin" 是授权用户操作的库的权限

创建读写账号及授权多个库案例

test:PRIMARY> db.createUser({user: "hcwx", pwd: "hcwx", roles: [{role: "readWrite", db: "hcwx,hcwx_test"}]})

 

登录mongo并创建授权的读写账号的库

[root@master-cluster ~]# mongo --port 27012 -u hcwx -p 'hcwx' --authenticationDatabase admin

test:PRIMARY> use hcwx_test 

test:PRIMARY> use hcwx

db.route.insert({"name":"菜鸟教程"})   (创建集合并插入数据,routehcwx库的集合,创建的库不插入集合数据是不会显示数据库名的)

 

 

角色参数详解

 MongoDB副本集搭建_vim_05 

创建key文件

[root@ ~]# openssl rand -base64 90 > /opt/mongodb-replica/mongodb1/conf/keyfile

修改权限

chmod 600 /opt/mongodb-replica/mongodb1/conf/keyfile

拷贝到各个节点

cp /opt/mongodb-replica/mongodb1/conf/keyfile /opt/mongodb-replica/mongodb2/conf/keyfile

cp /opt/mongodb-replica/mongodb1/conf/keyfile /opt/mongodb-replica/mongodb3/conf/keyfile

 

修改每个实例的配置文件添加如下内容修改为各自实例的路径配置

security:

 authorization: enabled

 clusterAuthMode: keyFile

 keyFile: /opt/mongodb-replica/mongodb1/conf/keyfile

 

添加mongodb停止脚本

[root@master-cluster mongodb-replica]# vim stop.sh

. ~/.bash_profile

mongod -f /opt/mongodb-replica/mongodb1/conf/mongodb.conf -shutdown

mongod -f /opt/mongodb-replica/mongodb2/conf/mongodb.conf -shutdown

mongod -f /opt/mongodb-replica/mongodb3/conf/mongodb.conf -shutdown

 

添加配置后重启mongdb三个实例

[root@master-cluster mongodb-replica]# sh stop.sh  停止

[root@master-cluster mongodb-replica]# sh start.sh  启动

认证模式登录

 mongo --port 27011 -u hcadmin -p 'Root#123' --authenticationDatabase admin

 MongoDB副本集搭建_mongodb_06

Mongodb备份脚本案例

 MongoDB副本集搭建_vim_07

 

 

 

Java程序连接副本集的方式

mongodb://hcwx:hcwx@192.168.113.1:27011,192.168.113.2:27012,192.168.113.3:27013/hcwx?replicaSet=test&authSource=admin