MongoDB5副本集高可用集群部署

1.MongoDB简介

MongoDB官方网站:https://www.mongodb.com

​ MongoDB最大的特点是表结构灵活可变,字段类型可以随时修改。MongoDB中的每一行数据只是简单的被转化成Json格式后存储,因此MongoDB中没有MySQL中表结构这样的概念,可以直接将任意结构的数据塞入同一个表中,不必考虑表结构,更不必像MySQL一样因为要修改数据表结构而大费周折。

​ MongoDB不需要定义表结构这个特点给表结构的修改带来了极大的方便,但是也给多表查询、复杂事务等高级操作带来了阻碍。因此,如果数据的逻辑结构非常复杂,经常需要进行复杂的多表查询或者事务操作,那显然还是MySQL这类关系型数据库更合适

面向集合存储,易存储对象类型的数据支持查询,以及动态查询支持RUBY,PYTHON,JAVA,C++,PHP,C#等多种语言文件存储格式为BSON(一种JSON的扩展)支持复制和故障恢复和分片支持事务支持索引 聚合 关联…

MongoDB结构对比

RDBMSMongoDB数据库数据库表集合行文档列字段

在MongoDB中,集合就是table表的概念

2.安装部署

2-0.机器规划与准备

主机名IP角色Rolehdt-dmcp-ops05172.20.12.179PRIMARYhdt-dmcp-ops04172.20.9.6SECONDARYhdt-dmcp-ops03172.20.14.243SECONDARY

本次部署环境为CentOS 7.4 ,搭建部署MongoDB-5.0.15

[wangting@hdt-dmcp-ops05 software]$ cat /etc/redhat-release

CentOS Linux release 7.4.1708 (Core)

2-1.下载安装包(hdt-dmcp-ops05操作)

[wangting@hdt-dmcp-ops05 software]$ cd /opt/software

[wangting@hdt-dmcp-ops05 software]$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.15-rc2.tgz

2-2.创建MongoDB相关目录(hdt-dmcp-ops05操作)

[wangting@hdt-dmcp-ops05 software]$ cd /opt/module/

[wangting@hdt-dmcp-ops05 module]$ mkdir MongoDB

[wangting@hdt-dmcp-ops05 module]$ cd MongoDB/

[wangting@hdt-dmcp-ops05 MongoDB]$ mkdir data log pid

路径均可自定义配置

/opt/module/MongoDB为mongo项目路径

data目录为数据保存路径

log为日志保存路径

pid为服务启动后的进程信息存储路径

2-3.解压安装包(hdt-dmcp-ops05操作)

[wangting@hdt-dmcp-ops05 software]$ cd /opt/software

[wangting@hdt-dmcp-ops05 software]$ tar -zxvf mongodb-linux-x86_64-rhel70-5.0.15-rc2.tgz -C /opt/module/MongoDB/

[wangting@hdt-dmcp-ops05 software]$ cd /opt/module/MongoDB

[wangting@hdt-dmcp-ops05 MongoDB]$ mv mongodb-linux-x86_64-rhel70-5.0.15-rc2 mongodbServer

2-4.定义配置文件(hdt-dmcp-ops05操作)

[wangting@hdt-dmcp-ops05 MongoDB]$ vim mongodbServer/bin/mongod.conf

storage:

journal:

enabled: true

dbPath: "/opt/module/MongoDB/data"

directoryPerDB: true

wiredTiger:

engineConfig:

cacheSizeGB: 1

directoryForIndexes: true

collectionConfig:

blockCompressor: zlib

indexConfig:

prefixCompression: true

systemLog:

destination: file

path: "/opt/module/MongoDB/log/mongod.log"

logAppend: true

net:

port: 27017

bindIpAll: true

maxIncomingConnections: 5000

processManagement:

fork: true

pidFilePath: /opt/module/MongoDB/pid/mongod.pid

#security:

# keyFile: /opt/module/MongoDB/mongodbServer/bin/mongo.keyfile

# authorization: enabled

replication:

oplogSizeMB: 4096

replSetName: rs1

【注意】:

security相关配置项为注释状态,第一次启动使用时,不加载security相关配置,需要机器配置完毕后再配置开启(因为具体的认证配置文件均未生成)PRIMARY和SECONDARY的配置文件没有特别配置不一样的参数,可以复用,角色的配置均在服务启动后访问server调整

2-5.分发安装目录(hdt-dmcp-ops05操作)

[wangting@hdt-dmcp-ops05 module]$ pwd

/opt/module

[wangting@hdt-dmcp-ops05 module]$ scp -r MongoDB hdt-dmcp-ops04:/opt/module/

[wangting@hdt-dmcp-ops05 module]$ scp -r MongoDB hdt-dmcp-ops03:/opt/module/

2-6.配置环境变量(所有节点机器操作)

# 增加mongo相关配置

[wangting@hdt-dmcp-ops05 MongoDB]$ sudo vim /etc/profile

# mongo

export MONGODB_HOME=/opt/module/MongoDB/mongodbServer

export PATH=$PATH:$MONGODB_HOME/bin

# 引用环境变量

[wangting@hdt-dmcp-ops05 MongoDB]$ source /etc/profile

【注意】:

所有3个节点机器都需要配置环境变量

( 暂不操作,这里了解 )-单个实例启动服务方式:

mongod命令 --config 配置文件路径

[wangting@hdt-dmcp-ops05 MongoDB]$ mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf

about to fork child process, waiting until server is ready for connections.

forked process: 12601

child process started successfully, parent exiting

( 暂不操作,这里了解 )-单个实例关闭服务方式:

[wangting@hdt-dmcp-ops05 MongoDB]$ mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown

2-7.编写集群启停脚本(hdt-dmcp-ops05操作)

[wangting@hdt-dmcp-ops05 bin]$ vim mymongo

#!/bin/bash

start() {

ssh hdt-dmcp-ops05 "/opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf >/dev/null 2>&1"

sleep 2

ssh hdt-dmcp-ops04 "/opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf >/dev/null 2>&1"

sleep 2

ssh hdt-dmcp-ops03 "/opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf >/dev/null 2>&1"

}

stop() {

ssh hdt-dmcp-ops05 "/opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown >/dev/null 2>&1"

sleep 2

ssh hdt-dmcp-ops04 "/opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown >/dev/null 2>&1"

sleep 2

ssh hdt-dmcp-ops03 "/opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown >/dev/null 2>&1"

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

*)

echo

$"Usage: $0 {start|stop|restart}"

exit 1

esac

[wangting@hdt-dmcp-ops05 bin]$ chmod +x mymongo

脚本可以使用for循环简化,这里为了操作方便,将操作集中到一个脚本中

2-8.启动停止服务(hdt-dmcp-ops05操作)

# 停止服务

[wangting@hdt-dmcp-ops05 bin]$ mymongo stop

killing process with pid: 12601

[wangting@hdt-dmcp-ops05 bin]$ netstat -tnlpu|grep 27017

[wangting@hdt-dmcp-ops05 bin]$

# 启动服务

[wangting@hdt-dmcp-ops05 bin]$ mymongo start

about to fork child process, waiting until server is ready for connections.

forked process: 13747

child process started successfully, parent exiting

[wangting@hdt-dmcp-ops05 bin]$ netstat -tnlpu|grep 27017

(Not all processes could be identified, non-owned process info

will not be shown, you would have to be root to see it all.)

tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 13747/mongod

2-9.初次登录初始化副本集(任意一节点连接mongo)

使用mongo shell连接到其中任意一个节点即可,来执行初次登录初始化副本集

[wangting@hdt-dmcp-ops05 module]$ mongo shell

> use admin

> config = {

_id : "rs1",

members : [

{_id:0, host:"172.20.12.179:27017"},

{_id:1, host:"172.20.9.6:27017"},

{_id:2, host:"172.20.14.243:27017"},

]

}

【注意】:

需要更改对应环境的IP地址

2-10.对副本集进行初始化

> rs.initiate(config)

{ "ok" : 1 }

rs1:SECONDARY>

如果返回 { “ok” : 0 }, 则说明初始化失败

查看集群状态

rs1:PRIMARY> rs.status()

...

...

"members" : [

{

"_id" : 0,

"name" : "172.20.12.179:27017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

{

"_id" : 1,

"name" : "172.20.9.6:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 5382,

{

"_id" : 2,

"name" : "172.20.14.243:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 5382,

查看延时从库信息

rs1:PRIMARY> rs.printSlaveReplicationInfo()

WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.

source: 172.20.9.6:27017

syncedTo: Wed Mar 08 2023 16:27:45 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

source: 172.20.14.243:27017

syncedTo: Wed Mar 08 2023 16:27:45 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

2-11.设置超级管理员账号和密码

> use admin

switched to db admin

> db.createUser({

user: 'mongouser',

pwd: '123456',

roles:[{

role: 'root',

db: 'admin'

}]

})

Successfully added user: {

"user" : "admin",

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}

其它用户相关常用命令:

show users //查看当前库下的用户

db.dropUser('testadmin') // 删除用户

db.updateUser('admin', {pwd: 'newpasswd'}) // 修改用户密码

db.auth('admin', 'password') // 密码认证

2-12.设置数据库读写用户方法

[wangting@hdt-dmcp-ops05 bin]$ mongo

> use bigdatadb

switched to db bigdatadb

> db.createUser({

user: 'bigdata',

pwd: 'bigdata123',

roles:[{

role: 'readWrite',

db: 'bigdatadb'

}]

})

Successfully added user: {

"user" : "bigdata",

"roles" : [

{

"role" : "readWrite",

"db" : "bigdatadb"

}

]

}

> exit

bye

MongoDB 数据库角色信息:

角色描述角色标识数据库用户角色read、readWrite数据库管理角色dbAdmin、dbOwner、userAdmin集群管理角色clusterAdmin、clusterManager、clusterMonitor、hostManager备份恢复角色backup、restore所有数据库角色readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、 dbAdminAnyDatabase超级用户角色root

2-13.开启副本集认证

# 创建副本集认证的key文件

[wangting@hdt-dmcp-ops05 bin]$ pwd

/opt/module/MongoDB/mongodbServer/bin

[wangting@hdt-dmcp-ops05 bin]$ openssl rand -base64 90 -out /opt/module/MongoDB/mongodbServer/bin/mongo.keyfile

[wangting@hdt-dmcp-ops05 bin]$ ll

total 242228

-rwxr-xr-x 1 wangting wangting 15205 Feb 17 03:54 install_compass

-rwxr-xr-x 1 wangting wangting 60321720 Feb 17 04:43 mongo

-rwxr-xr-x 1 wangting wangting 110313632 Feb 17 04:46 mongod

-rw-rw-r-- 1 wangting wangting 629 Mar 8 14:19 mongod.conf

-rw-rw-r-- 1 wangting wangting 122 Mar 8 14:43 mongo.keyfile

-rwxr-xr-x 1 wangting wangting 77374072 Feb 17 04:43 mongos

# 分发mongo.keyfile

[wangting@hdt-dmcp-ops05 bin]$ scp mongo.keyfile hdt-dmcp-ops04:$PWD

[wangting@hdt-dmcp-ops05 bin]$ scp mongo.keyfile hdt-dmcp-ops03:$PWD

# 将配置文件mongod.conf的配置注释打开(去掉注释)

# 【注意】:3个节点配置文件的security都要打开!

security:

keyFile: /opt/module/MongoDB/mongodbServer/bin/mongo.keyfile

authorization: enabled

mongo.keyfile认证文件mongo集群必须要用同一份keyfile,一般都在一台机器上生成,然后分发到其他节点,且必须有读的权限,否则将来会报错

路径保持一致,否则mongod.conf中的keyFile路径就要根据不同节点配置不同路径,相对不好维护

重启服务生效

# 关闭服务

[wangting@hdt-dmcp-ops05 bin]$ mymongo stop

# 启动服务

[wangting@hdt-dmcp-ops05 bin]$ mymongo start

再次连接mongo

# 原方式:mongo admin --host 172.20.12.179 --port 27017 -u mongouser -p 123456

# 这里采用auther认证方式验证访问

[wangting@hdt-dmcp-ops05 module]$ mongo -u mongouser -p 123456 --host hdt-dmcp-ops05 --port 27017 -authenticationDatabase admin

MongoDB shell version v5.0.15-rc2

connecting to: mongodb://hdt-dmcp-ops05:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("66c31018-e1a1-45e9-8742-4496e0266ba0") }

MongoDB server version: 5.0.15-rc2

rs1:PRIMARY>

3.验证调试及使用介绍

3-1.访问MongoDB

方式1(常用):

# 原始方式

[wangting@hdt-dmcp-ops05 ~]$ mongo admin --host 172.20.12.179 --port 27017 -u mongouser -p 123456

方式2(常用)

# 使用用户名、免密、认证库登录MongoDB副本集主节点

[wangting@hdt-dmcp-ops05 module]$ mongo -u mongouser -p 123456 --host hdt-dmcp-ops05 --port 27017 -authenticationDatabase admin

方式3:

# 机器上直接使用mongo访问

[wangting@hdt-dmcp-ops05 bin]$ mongo

> use admin

switched to db admin

> db.auth('admin', 'admin123')

1

> db

admin

普通用户访问方式相同:

[wangting@hdt-dmcp-ops05 ~]$ mongo bigdatadb --host 172.20.12.179 --port 27017 -u bigdata -p bigdata123

> db

bigdatadb

> exit

bye

3-2.MongoDB远程连接控制

[wangting@hdt-dmcp-ops05 ~]$ cd /opt/module/MongoDB/mongodbServer/bin/

[wangting@hdt-dmcp-ops05 bin]$ vim mongod.conf

#####

net:

port: 27017

bindIpAll: true

####

bindIp: 0.0.0.0 或者 bindIpAll: true 即允许所有的IPv4和IPv6地址访问

net.bindIp和net.bindIpAll是互斥的。可以指定其中一个,但不能同时指定两个配置

如需要指定个别IP或者某个网段访问可以配置bindIp

例如:

bindIp:192.168.3.11,192.168.3.12,192.168.3.13

bindIp: localhost

3-3.MongoDB使用介绍

登录

[wangting@hdt-dmcp-ops05 ~]$ mongo admin --host 172.20.12.179 --port 27017 -u mongouser -p 123456

rs1:PRIMARY> show dbs

admin 0.000GB

config 0.000GB

local 0.000GB

切换数据库

rs1:PRIMARY> use bigdata

switched to db bigdata

注意: use 代表创建并使用,当库中没有数据时默认不显示这个库

删除数据库

db.dropDatabase()

> use test111

switched to db test111

> db.dropDatabase()

{ "ok" : 1 }

> show dbs

admin 0.000GB

bigdata 0.000GB

config 0.000GB

local 0.000GB

>

查看表清单

> show tables

# 或者

> show collections

表创建

db.createCollection('集合名称', [options])

> db.createCollection("table1")

{ "ok" : 1 }

> show tables

table1

[options]可选参数:

字段类型描述capped布尔(可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。 当该值为 true 时,必须指定 size 参数size数值(可选)为固定集合指定一个最大值,即字节数。 如果 capped 为 true,也需要指定该字段max数值(可选)指定固定集合中包含文档的最大数量

注意:当集合(表)不存在时,向集合中插入文档也会自动创建该集合

表中插入数据

db.【表名】.insert("【JSON格式的数据】")

> db.table1.insert({"id":"1","name":"wangting111"})

WriteResult({ "nInserted" : 1 })

> db.table1.insert({"id":"2","name":"wangting222"})

WriteResult({ "nInserted" : 1 })

> db.table1.insert({"id":"3","name":"wangting333"})

WriteResult({ "nInserted" : 1 })

>

查看表中数据

> db.table1.find()

{ "_id" : ObjectId("63f5a21faa98d270a51a7968"), "id" : "1", "name" : "wangting111" }

{ "_id" : ObjectId("63f5a228aa98d270a51a7969"), "id" : "2", "name" : "wangting222" }

{ "_id" : ObjectId("63f5a22faa98d270a51a796a"), "id" : "3", "name" : "wangting333" }

>

删除集合(表)

> show tables;

table1

> db.table1.drop()

true

> show tables;

>

文档操作

# 单条文档

> db.table1.insert({"name":"wang111","age":18,"bir":"1989-09-07"});

WriteResult({ "nInserted" : 1 })

> db.table1.find()

{ "_id" : ObjectId("63f5a96c86e1dd7e4acc842f"), "name" : "wang111", "age" : 18, "bir" : "1989-09-07" }

# 多条文档

db.table1.insert([

{"name":"wang222","age":18,"bir":"1989-09-08"},

{"name":"wang333","age":18,"bir":"1989-09-09"},

{"name":"wang444","age":20,"bir":"1989-09-10"},

{"name":"wang555","age":19,"bir":"1989-09-11"}

]);

BulkWriteResult({

"writeErrors" : [ ],

"writeConcernErrors" : [ ],

"nInserted" : 4,

"nUpserted" : 0,

"nMatched" : 0,

"nModified" : 0,

"nRemoved" : 0,

"upserted" : [ ]

})

> db.table1.find()

{ "_id" : ObjectId("63f5a96c86e1dd7e4acc842f"), "name" : "wang111", "age" : 18, "bir" : "1989-09-07" }

{ "_id" : ObjectId("63f5aac286e1dd7e4acc8430"), "name" : "wang222", "age" : 18, "bir" : "1989-09-08" }

{ "_id" : ObjectId("63f5aac286e1dd7e4acc8431"), "name" : "wang333", "age" : 18, "bir" : "1989-09-09" }

{ "_id" : ObjectId("63f5aac286e1dd7e4acc8432"), "name" : "wang444", "age" : 20, "bir" : "1989-09-10" }

{ "_id" : ObjectId("63f5aac286e1dd7e4acc8433"), "name" : "wang555", "age" : 19, "bir" : "1989-09-11" }

>

# 支持利用代码循环批量插入

> for(let i=0;i<10;i++){

db.table1.insert({"_id":i,"name":"wang"+i,"age" : 18+i,"bir" : "1989-09-07"})

}

WriteResult({ "nInserted" : 1 })

> db.table1.find()

{ "_id" : ObjectId("63f5a96c86e1dd7e4acc842f"), "name" : "wang111", "age" : 18, "bir" : "1989-09-07" }

{ "_id" : ObjectId("63f5aac286e1dd7e4acc8430"), "name" : "wang222", "age" : 18, "bir" : "1989-09-08" }

{ "_id" : ObjectId("63f5aac286e1dd7e4acc8431"), "name" : "wang333", "age" : 18, "bir" : "1989-09-09" }

{ "_id" : ObjectId("63f5aac286e1dd7e4acc8432"), "name" : "wang444", "age" : 20, "bir" : "1989-09-10" }

{ "_id" : ObjectId("63f5aac286e1dd7e4acc8433"), "name" : "wang555", "age" : 19, "bir" : "1989-09-11" }

{ "_id" : 0, "name" : "wang0", "age" : 18, "bir" : "1989-09-07" }

{ "_id" : 1, "name" : "wang1", "age" : 19, "bir" : "1989-09-07" }

{ "_id" : 2, "name" : "wang2", "age" : 20, "bir" : "1989-09-07" }

{ "_id" : 3, "name" : "wang3", "age" : 21, "bir" : "1989-09-07" }

{ "_id" : 4, "name" : "wang4", "age" : 22, "bir" : "1989-09-07" }

{ "_id" : 5, "name" : "wang5", "age" : 23, "bir" : "1989-09-07" }

{ "_id" : 6, "name" : "wang6", "age" : 24, "bir" : "1989-09-07" }

{ "_id" : 7, "name" : "wang7", "age" : 25, "bir" : "1989-09-07" }

{ "_id" : 8, "name" : "wang8", "age" : 26, "bir" : "1989-09-07" }

{ "_id" : 9, "name" : "wang9", "age" : 27, "bir" : "1989-09-07" }

>

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。