• 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字段设置为主键
    你一定会喜欢那个因为喜欢她而发光的自己!
    个人博客:http://www.yanghelong.top
  • 相关阅读:
    超级好用的excel导出方法,比phpexcel快n倍,并且无乱码
    PHP生成随机数;订单号唯一
    php判断检测一个数组里有没有重复的值
    修改git 提交的用户名和用户Email命令
    利用 PHP CURL zip压缩文件上传
    Linux 重启 PHP-FPM 命令
    Postgresql 时间串转换格式
    rollup node.js 打包工具
    PHP正则表达式提取html超链接中的href地址
    解决Ubuntu系统下 mysql 远程连接失败的问题 ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xx.xx' (110)
  • 原文地址:https://www.cnblogs.com/zzu-general/p/13896092.html
Copyright © 2020-2023  润新知