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结构对比
RDBMS | MongoDB |
---|---|
数据库 | 数据库 |
表 | 集合 |
行 | 文档 |
列 | 字段 |
在MongoDB中,集合就是table表的概念
2.安装部署
本次部署环境为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.下载安装包
[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相关目录
[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
路径均可自定义配置
/opt/module/MongoDB为mongo项目路径
data目录为数据保存路径
log为日志保存路径
2-2.解压安装包
[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-3.定义相关配置
[wangting@hdt-dmcp-ops05 MongoDB]$ vim mongodbServer/bin/mongod.conf
storage:
dbPath: "/opt/module/MongoDB/data"
systemLog:
destination: file
path: "/opt/module/MongoDB/log/mongod.log"
logAppend: true
net:
port: 27017
bindIpAll: true
processManagement:
fork: true
2-4.配置环境变量
# 增加mongo相关配置
[wangting@hdt-dmcp-ops05 MongoDB]$ sudo vim /etc/profile
# mongo
export MONGODB_HOME=/usr/local/MongoDB/mongodbServer
export PATH=$PATH:$MONGODB_HOME/bin
# 引用环境变量
[wangting@hdt-dmcp-ops05 MongoDB]$ source /etc/profile
启动服务方式:
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
2-5.编写启停脚本
[wangting@hdt-dmcp-ops05 bin]$ vim mymongo
#!/bin/bash
start() {
/opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf
}
stop() {
/opt/module/MongoDB/mongodbServer/bin/mongod --config /opt/module/MongoDB/mongodbServer/bin/mongod.conf --shutdown
}
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
2-6.启动停止服务
# 停止服务
[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-7.初次登录验证
[wangting@hdt-dmcp-ops05 bin]$ mongo
MongoDB shell version v5.0.15-rc2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("62eb0433-074a-4417-b869-6f2132a955f0") }
MongoDB server version: 5.0.15-rc2
================
Warning: the "mongo" shell has been superseded by "mongosh",which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions,see
https://docs.mongodb.com/mongodb-shell/install/
================
> use admin
switched to db admin
> exit
bye
2-8.设置超级管理员账号和密码
[wangting@hdt-dmcp-ops05 bin]$ mongo
> use admin
switched to db admin
> db.createUser({
user: 'admin',pwd: 'admin123',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-9.设置数据库读写用户
[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-10.开启权限验证
# 在配置文件最后增加新配置项security,enabled为开启
[wangting@hdt-dmcp-ops05 MongoDB]$ vim mongodbServer/bin/mongod.conf
storage:
dbPath: "/opt/module/MongoDB/data"
systemLog:
destination: file
path: "/opt/module/MongoDB/log/mongod.log"
logAppend: true
net:
port: 27017
bindIpAll: true
processManagement:
fork: true
security:
authorization: enabled
需要重启服务生效
[wangting@hdt-dmcp-ops05 bin]$ mymongo stop
[wangting@hdt-dmcp-ops05 bin]$ mymongo start
2-11.查看用户列表
show users
> show users
{
"_id" : "admin.admin","userId" : UUID("df225aee-a01b-45f4-9716-389b9fe20465"),"user" : "admin","db" : "admin","db" : "admin"
}
],"mechanisms" : [
"SCRAM-SHA-1","SCRAM-SHA-256"
]
}
{
"_id" : "admin.bigdata","userId" : UUID("fbf096c7-3a82-49de-a1ea-fed235269cd1"),"user" : "bigdata","roles" : [
{
"role" : "dbOwner","db" : "bigdatadb"
}
],"SCRAM-SHA-256"
]
}
3.验证调试及使用介绍
3-1.访问MongoDB
方式1(常用):
[wangting@hdt-dmcp-ops05 ~]$ mongo admin --host 172.20.12.179 --port 27017 -u admin -p admin123
方式2:
[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
storage:
dbPath: "/opt/module/MongoDB/data"
systemLog:
destination: file
path: "/opt/module/MongoDB/log/mongod.log"
logAppend: true
net:
port: 27017
bindIpAll: true
processManagement:
fork: true
security:
authorization: enabled
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 admin -p admin123
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
- 切换数据库
> 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","bir":"1989-09-08"},{"name":"wang333","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"),"bir" : "1989-09-07" }
{ "_id" : ObjectId("63f5aac286e1dd7e4acc8430"),"name" : "wang222","bir" : "1989-09-08" }
{ "_id" : ObjectId("63f5aac286e1dd7e4acc8431"),"name" : "wang333","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"),"bir" : "1989-09-11" }
{ "_id" : 0,"name" : "wang0","bir" : "1989-09-07" }
{ "_id" : 1,"name" : "wang1","bir" : "1989-09-07" }
{ "_id" : 2,"name" : "wang2","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" }
>