MongoDB简介---MongoDB基础用法(一)

Mongo

MongoDB是一个基于分布式文件存储的数据库。MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

crudannotateddocument.png?imageView2/2/w/1280/format/jpg/interlace/1/q/100

主要特点

  • MongoDB 是一个面向文档存储的数据库,操作起来比较简单和容易。
  • 你可以在MongoDB记录中设置任何属性的索引 (如:FirstName="Sameer",Address="8 Gandhi Road")来实现更快的排序。
  • 你可以通过本地或者网络创建数据镜像,这使得MongoDB有更强的扩展性。
  • 如果负载的增加(需要更多的存储空间和更强的处理能力) ,它可以分布在计算机网络中的其他节点上这就是所谓的分片。
  • Mongo支持丰富的查询表达式。查询指令使用JSON形式的标记,可轻易查询文档中内嵌的对象及数组。
  • MongoDb 使用update()命令可以实现替换完成的文档(数据)或者一些指定的数据字段 。
  • Mongodb中的Map/reduce主要是用来对数据进行批量处理和聚合操作。
  • Map和Reduce。Map函数调用emit(key,value)遍历集合中所有的记录,将key与value传给Reduce函数进行处理。
  • Map函数和Reduce函数是使用Javascript编写的,并可以通过db.runCommand或mapreduce命令来执行MapReduce操作。
  • GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件。
  • MongoDB允许在服务端执行脚本,可以用Javascript编写某个函数,直接在服务端执行,也可以把函数的定义存储在服务端,下次直接调用即可。
  • MongoDB支持各种编程语言:RUBY,PYTHON,JAVA,C++,PHP,C#等多种语言。
  • MongoDB安装简单。

CentOS 安装配置

MongoDB 源码下载地址:https://www.mongodb.com/try/download/community

image20201027151430023.png?imageView2/2/w/1280/format/jpg/interlace/1/q/100

选择 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 启动后会初始化以下两个目录:

  • 数据存储目录:/var/lib/mongodb
  • 日志文件目录:/var/log/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(这里是虚拟机),出现以下界面表示启动成功。

![image-20201027160147079](file://C:/Users/yanghelong/Desktop/img/img/NoSQL/image-20201027160147079.png?imageView2/2/w/1280/format/jpg/interlace/1/q/100?lastModify=1603852614)

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/

MongoDB 概念解析

SQL术语/概念 MongoDB术语/概念 解释/说明
database database 数据库
table collection 数据库表/集合
row document 数据记录行/文档
column field 数据字段/域
index index 索引
table joins 表连接,MongoDB不支持
primary key primary key 主键,MongoDB自动将_id字段设置为主键
posted @ 2020-10-29 12:33  风雨长安  阅读(158)  评论(0编辑  收藏  举报
博客