MongoDB初认识

MongoDB初认识



0. 写在前面

  • Linux版本:CentOS7.5

  • MongoDB版本:MongoDB-5.0.2(Linux环境下安装)

1. MongoDB是什么

  • MongoDB 是由 C++ 语言编写的,是一个基于分布式文件存储的开源数据库系统。

  • MongoDB 旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

  • MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。

  • MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

如下图所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I45MwF65-1675912006899)(./1.jpg)]

2. MongoDB的优缺点

优点:

  • MongoDB 是一个面向文档存储的数据库,操作起来比较简单和容易;

  • 内置GridFS,支持大容量的存储;

  • 可以在MongoDB记录中设置任何属性的索引;

  • MongoDB支持各种编程语言:RUBY,PYTHON,JAVA,C++,PHP,C#等多种语言;

  • 安装简单;

  • 复制(复制集)和支持自动故障恢复;

  • MapReduce 支持复杂聚合。

缺点:

  • 不支持事务;

  • 占用空间过大;

  • 不能进行表关联;

  • 复杂聚合操作通过MapReduce创建,速度慢;

  • MongoDB 在你删除记录后不会在文件系统回收空间。除非你删掉数据库。

3. 基础概念解析

SQL 术语/概念 MongoDB术语/概念 解释/说明
database database 数据库
table collection 数据库表/集合
row document 数据记录行/文档
column field 数据字段/域
index index 索引
table joins 不支持 表连接,MongoDB 不支持
primary key primary key 主键,MongoDB 自动将_id 字段设置为主键

通过下图实例,我们也可以更直观的了解 Mongo 中的一些概念:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rdsewoM6-1675912006900)(2.jpg)]

4. 安装

4.1 下载地址

https://www.mongodb.com/download-center#community

4.2 安装MongoDB

本人安装在node02机器

  • 上传下载好的压缩包到虚拟机中,然后解压安装
[whybigdata@node02 software]$ tar -zxvf mongodb-linux-x86_64-rhel70-5.0.2.tgz -C /opt/module/        
  • 重命名
[whybigdata@node02 module]$ mv mongodb-linux-x86_64- rhel70-5.0.2/ mongodb 
  • 创建数据库目录

MongoDB 的数据存储在 data 目录的 db 目录下,但是这个目录在安装过程不会自动创建,所以需要手动创建data 目录,并在 data 目录中创建 db 目录。

[whybigdata@node02 module]$ sudo mkdir -p /data/db 
[whybigdata@node02 mongodb]$ sudo chmod 777 -R /data/db/   
  • 启动 MongoDB 服务
[whybigdata@node02 mongodb]$ bin/mongod
  • 查看mongod进程
ps -ef | grep mongod | grep -v grep | awk '{print$2}'

上述进程查询命令等价于以下命令

pgrep –f mongo

4.3 pgrep使用

  • 使用方式
[whybigdata@node02 mongodb-5.0.2]$ pgrep --help

Usage:
 pgrep [options] <pattern>

Options:
 -d, --delimiter <string>  specify output delimiter
 -l, --list-name           list PID and process name
 -a, --list-full           list PID and full command line
 -v, --inverse             negates the matching
 -w, --lightweight         list all TID
 -c, --count               count of matching processes
 -f, --full                use full process name to match
 -g, --pgroup <PGID,...>   match listed process group IDs
 -G, --group <GID,...>     match real group IDs
 -n, --newest              select most recently started
 -o, --oldest              select least recently started
 -P, --parent <PPID,...>   match only child processes of the given parent
 -s, --session <SID,...>   match session IDs
 -t, --terminal <tty,...>  match by controlling terminal
 -u, --euid <ID,...>       match by effective IDs
 -U, --uid <ID,...>        match by real IDs
 -x, --exact               match exactly with the command name
 -F, --pidfile <file>      read PIDs from file
 -L, --logpidfile          fail if PID file is not locked
 --ns <PID>                match the processes that belong to the same
                           namespace as <pid>
 --nslist <ns,...>         list which namespaces will be considered for
                           the --ns option.
                           Available namespaces: ipc, mnt, net, pid, user, uts

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see pgrep(1).

4.4 进入 shell 交互页面

[whybigdata@node02 mongodb]$ bin/mongo

Linux安装MongoDB可以参考这篇文章:

https://juejin.cn/post/7147226612978122760

结束!

猜你喜欢

转载自blog.csdn.net/m0_52735414/article/details/128949474
今日推荐