中国建设银行蚌埠官方网站,网络营销的方式有几种,wordpress主题信息,超市微信小程序怎么做MongoDB5副本集高可用集群部署 1.MongoDB简介
MongoDB官方网站#xff1a;https://www.mongodb.com
MongoDB最大的特点是表结构灵活可变#xff0c;字段类型可以随时修改。MongoDB中的每一行数据只是简单的被转化成Json格式后存储#xff0c;因此MongoDB中没有MySQL中表… MongoDB5副本集高可用集群部署 1.MongoDB简介
MongoDB官方网站https://www.mongodb.com
MongoDB最大的特点是表结构灵活可变字段类型可以随时修改。MongoDB中的每一行数据只是简单的被转化成Json格式后存储因此MongoDB中没有MySQL中表结构这样的概念可以直接将任意结构的数据塞入同一个表中不必考虑表结构更不必像MySQL一样因为要修改数据表结构而大费周折。
MongoDB不需要定义表结构这个特点给表结构的修改带来了极大的方便但是也给多表查询、复杂事务等高级操作带来了阻碍。因此如果数据的逻辑结构非常复杂经常需要进行复杂的多表查询或者事务操作那显然还是MySQL这类关系型数据库更合适
面向集合存储易存储对象类型的数据支持查询,以及动态查询支持RUBYPYTHONJAVACPHPC#等多种语言文件存储格式为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 [wangtinghdt-dmcp-ops05 software]$ cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)2-1.下载安装包(hdt-dmcp-ops05操作)
[wangtinghdt-dmcp-ops05 software]$ cd /opt/software
[wangtinghdt-dmcp-ops05 software]$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.15-rc2.tgz2-2.创建MongoDB相关目录(hdt-dmcp-ops05操作)
[wangtinghdt-dmcp-ops05 software]$ cd /opt/module/
[wangtinghdt-dmcp-ops05 module]$ mkdir MongoDB
[wangtinghdt-dmcp-ops05 module]$ cd MongoDB/
[wangtinghdt-dmcp-ops05 MongoDB]$ mkdir data log pid路径均可自定义配置 /opt/module/MongoDB为mongo项目路径 data目录为数据保存路径 log为日志保存路径 pid为服务启动后的进程信息存储路径 2-3.解压安装包(hdt-dmcp-ops05操作)
[wangtinghdt-dmcp-ops05 software]$ cd /opt/software
[wangtinghdt-dmcp-ops05 software]$ tar -zxvf mongodb-linux-x86_64-rhel70-5.0.15-rc2.tgz -C /opt/module/MongoDB/
[wangtinghdt-dmcp-ops05 software]$ cd /opt/module/MongoDB
[wangtinghdt-dmcp-ops05 MongoDB]$ mv mongodb-linux-x86_64-rhel70-5.0.15-rc2 mongodbServer2-4.定义配置文件(hdt-dmcp-ops05操作)
[wangtinghdt-dmcp-ops05 MongoDB]$ vim mongodbServer/bin/mongod.conf
storage:journal:enabled: truedbPath: /opt/module/MongoDB/datadirectoryPerDB: truewiredTiger:engineConfig:cacheSizeGB: 1directoryForIndexes: truecollectionConfig:blockCompressor: zlibindexConfig:prefixCompression: truesystemLog:destination: filepath: /opt/module/MongoDB/log/mongod.loglogAppend: truenet:port: 27017bindIpAll: truemaxIncomingConnections: 5000processManagement:fork: truepidFilePath: /opt/module/MongoDB/pid/mongod.pid#security:
# keyFile: /opt/module/MongoDB/mongodbServer/bin/mongo.keyfile
# authorization: enabledreplication:oplogSizeMB: 4096replSetName: rs1【注意】 security相关配置项为注释状态第一次启动使用时不加载security相关配置需要机器配置完毕后再配置开启因为具体的认证配置文件均未生成PRIMARY和SECONDARY的配置文件没有特别配置不一样的参数可以复用角色的配置均在服务启动后访问server调整 2-5.分发安装目录(hdt-dmcp-ops05操作)
[wangtinghdt-dmcp-ops05 module]$ pwd
/opt/module
[wangtinghdt-dmcp-ops05 module]$ scp -r MongoDB hdt-dmcp-ops04:/opt/module/
[wangtinghdt-dmcp-ops05 module]$ scp -r MongoDB hdt-dmcp-ops03:/opt/module/2-6.配置环境变量(所有节点机器操作)
# 增加mongo相关配置
[wangtinghdt-dmcp-ops05 MongoDB]$ sudo vim /etc/profile
# mongo
export MONGODB_HOME/opt/module/MongoDB/mongodbServer
export PATH$PATH:$MONGODB_HOME/bin# 引用环境变量
[wangtinghdt-dmcp-ops05 MongoDB]$ source /etc/profile【注意】: 所有3个节点机器都需要配置环境变量 ( 暂不操作这里了解 )-单个实例启动服务方式
mongod命令 --config 配置文件路径
[wangtinghdt-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( 暂不操作这里了解 )-单个实例关闭服务方式
[wangtinghdt-dmcp-ops05 MongoDB]$ mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown2-7.编写集群启停脚本(hdt-dmcp-ops05操作)
[wangtinghdt-dmcp-ops05 bin]$ vim mymongo
#!/bin/bashstart() {
ssh hdt-dmcp-ops05 /opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf /dev/null 21
sleep 2
ssh hdt-dmcp-ops04 /opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf /dev/null 21
sleep 2
ssh hdt-dmcp-ops03 /opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf /dev/null 21
}stop() {
ssh hdt-dmcp-ops05 /opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown /dev/null 21
sleep 2
ssh hdt-dmcp-ops04 /opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown /dev/null 21
sleep 2
ssh hdt-dmcp-ops03 /opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown /dev/null 21
}
case $1 instart)start;;stop)stop;;restart)stopstart;;*)echo
$Usage: $0 {start|stop|restart}exit 1
esac[wangtinghdt-dmcp-ops05 bin]$ chmod x mymongo脚本可以使用for循环简化这里为了操作方便将操作集中到一个脚本中 2-8.启动停止服务(hdt-dmcp-ops05操作)
# 停止服务
[wangtinghdt-dmcp-ops05 bin]$ mymongo stop
killing process with pid: 12601
[wangtinghdt-dmcp-ops05 bin]$ netstat -tnlpu|grep 27017
[wangtinghdt-dmcp-ops05 bin]$# 启动服务
[wangtinghdt-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
[wangtinghdt-dmcp-ops05 bin]$ netstat -tnlpu|grep 27017
(Not all processes could be identified, non-owned process infowill 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/mongod2-9.初次登录初始化副本集(任意一节点连接mongo)
使用mongo shell连接到其中任意一个节点即可来执行初次登录初始化副本集
[wangtinghdt-dmcp-ops05 module]$ mongo shelluse adminconfig {_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:27017syncedTo: Wed Mar 08 2023 16:27:45 GMT0800 (CST)0 secs (0 hrs) behind the primary
source: 172.20.14.243:27017syncedTo: Wed Mar 08 2023 16:27:45 GMT0800 (CST)0 secs (0 hrs) behind the primary2-11.设置超级管理员账号和密码 use admin
switched to db admindb.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.设置数据库读写用户方法
[wangtinghdt-dmcp-ops05 bin]$ mongouse bigdatadb
switched to db bigdatadbdb.createUser({user: bigdata,pwd: bigdata123,roles:[{role: readWrite,db: bigdatadb}]})Successfully added user: {user : bigdata,roles : [{role : readWrite,db : bigdatadb}]
} exit
byeMongoDB 数据库角色信息
角色描述角色标识数据库用户角色read、readWrite数据库管理角色dbAdmin、dbOwner、userAdmin集群管理角色clusterAdmin、clusterManager、clusterMonitor、hostManager备份恢复角色backup、restore所有数据库角色readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、 dbAdminAnyDatabase超级用户角色root
2-13.开启副本集认证
# 创建副本集认证的key文件
[wangtinghdt-dmcp-ops05 bin]$ pwd
/opt/module/MongoDB/mongodbServer/bin
[wangtinghdt-dmcp-ops05 bin]$ openssl rand -base64 90 -out /opt/module/MongoDB/mongodbServer/bin/mongo.keyfile
[wangtinghdt-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
[wangtinghdt-dmcp-ops05 bin]$ scp mongo.keyfile hdt-dmcp-ops04:$PWD
[wangtinghdt-dmcp-ops05 bin]$ scp mongo.keyfile hdt-dmcp-ops03:$PWD# 将配置文件mongod.conf的配置注释打开(去掉注释)
# 【注意】3个节点配置文件的security都要打开
security:keyFile: /opt/module/MongoDB/mongodbServer/bin/mongo.keyfileauthorization: enabledmongo.keyfile认证文件mongo集群必须要用同一份keyfile一般都在一台机器上生成然后分发到其他节点且必须有读的权限否则将来会报错 路径保持一致否则mongod.conf中的keyFile路径就要根据不同节点配置不同路径相对不好维护 重启服务生效
# 关闭服务
[wangtinghdt-dmcp-ops05 bin]$ mymongo stop
# 启动服务
[wangtinghdt-dmcp-ops05 bin]$ mymongo start再次连接mongo
# 原方式mongo admin --host 172.20.12.179 --port 27017 -u mongouser -p 123456
# 这里采用auther认证方式验证访问
[wangtinghdt-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/?authSourceadmincompressorsdisabledgssapiServiceNamemongodb
Implicit session: session { id : UUID(66c31018-e1a1-45e9-8742-4496e0266ba0) }
MongoDB server version: 5.0.15-rc2
rs1:PRIMARY3.验证调试及使用介绍
3-1.访问MongoDB
方式1常用
# 原始方式
[wangtinghdt-dmcp-ops05 ~]$ mongo admin --host 172.20.12.179 --port 27017 -u mongouser -p 123456方式2常用
# 使用用户名、免密、认证库登录MongoDB副本集主节点
[wangtinghdt-dmcp-ops05 module]$ mongo -u mongouser -p 123456 --host hdt-dmcp-ops05 --port 27017 -authenticationDatabase admin方式3
# 机器上直接使用mongo访问
[wangtinghdt-dmcp-ops05 bin]$ mongouse admin
switched to db admindb.auth(admin, admin123)
1db
admin普通用户访问方式相同
[wangtinghdt-dmcp-ops05 ~]$ mongo bigdatadb --host 172.20.12.179 --port 27017 -u bigdata -p bigdata123db
bigdatadbexit
bye3-2.MongoDB远程连接控制
[wangtinghdt-dmcp-ops05 ~]$ cd /opt/module/MongoDB/mongodbServer/bin/
[wangtinghdt-dmcp-ops05 bin]$ vim mongod.conf
#####
net:port: 27017bindIpAll: true
####bindIp: 0.0.0.0 或者 bindIpAll: true 即允许所有的IPv4和IPv6地址访问 net.bindIp和net.bindIpAll是互斥的。可以指定其中一个但不能同时指定两个配置 如需要指定个别IP或者某个网段访问可以配置bindIp 例如 bindIp:192.168.3.11192.168.3.12192.168.3.13 bindIp: localhost 3-3.MongoDB使用介绍
登录
[wangtinghdt-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 test111db.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;
table1db.table1.drop()
trueshow 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 i0;i10;i){db.table1.insert({_id:i,name:wangi,age : 18i,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 }