MongoDB是一个基于分布式文件存储的数据库。MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。
MongoDB 源码下载地址:https://www.mongodb.com/try/download/community
选择 tgz 下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.1.tgz
mv mongodb-linux-x86_64-rhel70-4.4.1/ /usr/local/mongodb
vi /etc/profile
PATH=/usr/local/mongodb/bin:$PATH
source /etc/profile
创建数据库目录
默认情况下 MongoDB 启动后会初始化以下两个目录:
我们在启动前可以先创建这两个目录并设置当前用户有读写权限:
sudo mkdir -p /var/lib/mongodb
sudo mkdir -p /var/log/mongodb
sudo chown ‘current_user‘ /var/lib/mongo # 设置权限
sudo chown ‘current_user‘ /var/log/mongodb # 设置权限
创建配置文件
vi /usr/local/mongodb/mongodb.conf
# mongodb 配置文件
port=27017 #端口
bind_ip=0.0.0.0 #默认是127.0.0.1
dbpath=/var/lib/mongodb/ #数据库存放
logpath=/var/log/mongodb/mongodb.log #日志文件
fork=true #设置后台运行
#auth=true #开启认证
启动 Mongodb 服务:
mongod --config /usr/local/mongodb/mongodb.conf
浏览器访问 IP:27017(这里是虚拟机),出现以下界面表示启动成功。

mongodb 后台管理
你需要先打开 mongodb 装目录的下的 bin 目录,然后执行 mongo 命令文件。
MongoDB Shell 是 MongoDB 自带的交互式 Javascript shell,用来对 MongoDB 进行操作和管理的交互式环境。
当你进入 mongoDB 后台后,它默认会链接到 test 文档(数据库):
$ cd /usr/local/mongodb/bin
$ ./mongo
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0795c040-abef-4fec-90c9-6b9a7a78e3c2") }
MongoDB server version: 4.4.1
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2020-10-27T03:29:57.206-04:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2020-10-27T03:29:57.207-04:00: You are running this process as the root user, which is not recommended
2020-10-27T03:29:57.207-04:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
2020-10-27T03:29:57.207-04:00: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘. We suggest setting it to ‘never‘
2020-10-27T03:29:57.207-04:00: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘. We suggest setting it to ‘never‘
2020-10-27T03:29:57.207-04:00: Soft rlimits too low
2020-10-27T03:29:57.207-04:00: currentValue: 1024
2020-10-27T03:29:57.207-04:00: recommendedMinimum: 64000
---
---
Enable MongoDB‘s free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
……
关闭 MongoDB
#先通过shell连上服务器:
mongo
use admin
db.shutdownServer()
MongoDB 工具
mongoimport mongoexport mongodump mongorestore 等工具,作为 mongodb database tools 提供了单独的下载入口
地址 : https://www.mongodb.com/try/download/database-tools
下载后解压到安装路径下的bin目录中,这里是 /usr/local/mongodb/bin/
SQL术语/概念 | MongoDB术语/概念 | 解释/说明 |
---|---|---|
database | database | 数据库 |
table | collection | 数据库表/集合 |
row | document | 数据记录行/文档 |
column | field | 数据字段/域 |
index | index | 索引 |
table joins | 表连接,MongoDB不支持 | |
primary key | primary key | 主键,MongoDB自动将_id字段设置为主键 |
原文:https://www.cnblogs.com/zzu-general/p/13896092.html