1.1 Mongodb介绍
中文官网:https://www.mongodb.com/zh-cn
中文文档:https://www.mongodb.com/zh-cn/docs/
MongoDB 是一个开源的 NoSQL 数据库,采用文档导向(Document-Oriented)的存储方式,基于分布式架构,适合存储大量结构化或半结构化数据。通过灵活的 schema 设计、横向扩展能力和高性能的读写操作,广泛应用于需要高可扩展性和快速开发的场景。
MongoDB 将数据存储为 BSON(Binary JSON)格式的文档,每个文档是一个键值对的集合,类似于 JSON,但支持更多的数据类型。文档可以包含嵌套的子文档,支持灵活的嵌套结构
MongoDB 支持多种查询方式,包括简单查询、范围查询、正则表达式查询、聚合操作等。此外,还支持全文索引、地理空间查询等高级功能
MySQL | MongoDB |
---|---|
Database | Database |
Table、View | Collection(集合) |
Row | Document |
Index | Index |
Join | Embedded Document |
Foreign Key | Reference |
Partition | Shard |
1.2 MongoDB的安装
1.2.1 安装前系统优化
安装前需确保系统如下的配置正确:
01 在redhat或centos 6.2以上系统进行部署
02 系统开发工具包安装完整
03 系统IP地址和hosts文件解析正常
04 系统防火墙功能与SElinux安全功能均要关闭
05 关闭系统大页内存机制
1.禁用大页内存功能,增加一个脚本
bash
[root@xiaoQ ~]# vi /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
2.执行脚本使配置生效:
bash
bash /etc/rc.local
关闭系统大页内存功能原因:
Transparent Huge Pages (THP) is a Linux memory management system that reduces the overhead of
Translation Lookaside Buffer (TLB)
大页内存是一种Linux系统中的内存管理系统,可以减少编译后备缓冲区(TLB)的开销;
lookups on machines with large amounts of memory by using larger memory pages.
通过使用较大的内存页在具有大量内存的机器上查找
However, database workloads often perform poorly with THP.
通常使用THP时,数据库工作负载表示经常是不佳的
because they tend to have sparse rathar than contiguous memory access patterns.
因为相较于相邻的访问模式,更倾向于系统的访问模式
You should disable THP on Linux machines to ensure best performance with MongoDB
1.2.2 执行安装
以 v4.2.24 版本为例
1.解压软件包,执行安装
bash
cd /usr/local
tar xvf mongodb-linux-x86_64-rhel70-4.2.24.tgz
ln -svf mongodb-linux-x86_64-rhel70-4.2.24 mongodb
2.配置服务环境变量:
bash
[root@master local]# vim /etc/profile
export PATH=/usr/local/mongodb/bin:$PATH
3.创建用户进行授权,设置一个简单的密码即可
bash
[root@master local]# useradd mongod
[root@master local]# passwd mongod
[root@master local]# mkdir -pv /mongodb/{data,conf,log}
[root@master local]# chown -R mongod:mongod /mongodb/
[root@master local]# su - mongod
[mongod@master ~]$ vim ~/.bash_profile
export PATH=/usr/local/mongodb/bin:$PATH
[mongod@master ~]$ source ~/.bash_profile
4.使用mongod虚拟用户启动数据库:
yaml
[mongod@db01~]$ cat > /mongodb/conf/mongo.conf <<'EOF'
systemLog:
destination: file
path: "/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data"
processManagement:
# 守护进程方式运行
fork: true
net:
port: 27017
bindIp: 10.0.0.51, 127.0.0.1
EOF
# 仅做测试,测试后会立即退出
[root@db01~]# mongod -f /mongodb/conf/mongo.conf -shutdown
killing process with pid: 2154
5.加载启动命令参数,实现程序启动
bash
mongod \
--dbpath=/mongodb/data \
--logpath=/mongodb/log/mongodb.log \
--port=27017 \
--logappend \
--fork
[mongod@master ~]$ netstat -antlp | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 32706/mongod
yaml
cat > /mongodb/conf/mongo.conf <<'EOF'
systemLog::
destination: file
path: "/mongodb/log/mongodb.log"
# 以追加的方式记录
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data"
# 后台运行
processManagement:
fork: true
# 一般不用配置,PID 会自动写入到 data 目录下
pidFilePath: <string>
net:
port: 27017
bindIp: 10.0.0.51,127.0.0.1
# 暂不启用安全认证
#security:
# authorization: enabled
EOF
1.2.3 systemd管理Mongodb
1.确保虚拟用户和对应的目录权限
bash
useradd -s /sbin/nologin mongodb
mkdir -pv /mongodb/{conf,data,log}
chown mongodb.mongodb -R /mongodb
2.书写的MongoDB配置为:
yaml
cat > /mongodb/conf/mongodb.yaml <<'EOF'
systemLog:
destination: file
path: "/mongodb/log/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data"
processManagement:
# 守护进程方式运行
fork: true
net:
port: 27017
bindIp: "0.0.0.0"
EOF
3.书写systemd配置:
bash
[root@db51~]# systemctl cat mongod.service
# /etc/systemd/system/mongod.service
[Unit]
Description=MongoDB Database Server
After=network.target
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/local/mongodb/bin/mongod -f /mongodb/conf/mongodb.yaml
Type=forking
Restart=on-failure
LimitNOFILE=65535
TimeoutStopSec=5
PermissionsStartOnly=true
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
启动之后会产生一个socket
bash
ll /tmp/mongodb-27017.sock
srwx------ 1 mongodb mongodb 0 Dec 21 11:43 /tmp/mongodb-27017.sock
1.3 登录mongodb
方式1:使用本地工具登录
shell
mongo --host 127.0.0.1 --port 27018
如果使用了账号密码:
shell
# 未完...待续