• MongoDB学习一:相关概念和单机部署


    在这里插入图片描述

    一、MongoDB 应用场景:

    在这里插入图片描述

    二、什么时候使用MongoDB:

    在这里插入图片描述

    三、MongoDB简介:

    在这里插入图片描述

    四、体系结构:

    在这里插入图片描述

    在这里插入图片描述

    五、数据模型

    在这里插入图片描述

    在这里插入图片描述

    六、MongoDB的特点:

    在这里插入图片描述

    七、MongoDB单机部署

    1. windows部署:

    MongoDB下载地址:

    链接: https://www.mongodb.com/try/download/community

    MongoDB官网下载和安装(ZIP安装)
    在这里插入图片描述

    官网下载windows版本:

    第一步:下载安装包

    在这里插入图片描述

    根据上图所示下载 zip 包。
    提示:版本的选择:
    MongoDB的版本命名规范如:x.y.z;
    y为奇数时表示当前版本为开发版,如:1.5.2、4.1.13;
    y为偶数时表示当前版本为稳定版,如:1.6.3、4.0.10;
    z是修正版本号,数字越大越好。
    详情:http://docs.mongodb.org/manual/release-notes/#release-version-numbers

    在这里插入图片描述

    第二步:解压安装启动

    将压缩包解压到一个目录中。
    在解压目录中(和bin目录同级),手动建立一个目录用于存放数据文件,如 data/db

    windows的两种启动服务的方式:

    方式1:命令行参数方式启动服务

    在 bin 目录中打开命令行提示符,输入如下命令:

    mongod --dbpath=…\data\db

    我们在启动信息中可以看到,mongoDB的默认端口是27017,如果我们想改变默认的启动端口,可以通过–port来指定端口。
    为了方便我们每次启动,可以将安装目录的bin目录设置到环境变量的path中, bin 目录下是一些常用命令,比如 mongod 启动服务用的,
    mongo 客户端连接服务用的。

    在这里插入图片描述

    具体操作:

    新建存储数据的文件/data/db

    3.第三步:在“MongoDB”目录下新建“data\db”文件夹,它将会作为数据存放的根文件夹。
    4.第四步:在“MongoDB”目录下新建“data\log”文件夹,它将会作为日志文件。

    在这里插入图片描述

    在bin下输入cmd:

    在这里插入图片描述

    输入指令:

    mongod.exe --dbpath=…\data\db

    通过制定端口来启动:

    mongod --dbpath=…\data\db --port=27018

    在这里插入图片描述

    在这里插入图片描述

    测试是否启动成功:
    打开浏览器:
    http://localhost:27017

    在这里插入图片描述

    配置文件启动:

    方式2:配置文件方式启动服务
    在解压目录中新建 config 文件夹,该文件夹中新建配置文件 mongod.conf ,内如参考如下:

    storage:
    #The directory where the mongod instance stores its data.Default Value is "\data\db" on Windows.
    dbPath: D:\02_Server\DBServer\mongodb-win32-x86_64-2008plus-ssl-4.0.1\data
    
    • 1
    • 2
    • 3

    详细配置项内容可以参考官方文档:https://docs.mongodb.com/manual/reference/configuration-options/
    【注意】
    1)配置文件中如果使用双引号,比如路径地址,自动会将双引号的内容转义。如果不转义,则会报错:
    error-parsing-yaml-config-file-yaml-cpp-error-at-line-3-column-15-unknown-escape-character-d

    error-parsing-yaml-config-file-yaml-cpp-error-at-line-3-column-15-unknown-escape-character-d

    error-parsing-yaml-config-file-yaml-cpp-error-at-line-3-column-15-unknown-escape-character-d

    解决:
    a. 对 \ 换成 / 或 \
    b. 如果路径中没有空格,则无需加引号。

    2)配置文件中不能以Tab分割字段
    解决:
    将其转换成空格。

    在这里插入图片描述

    在这里插入图片描述

    启动方式:

    mongod -f …/config/mongod.conf

    mongod --config …/config/mongod.conf

    更多配置参数:

    systemLog:
    destination: file
    #The path of the log file to which mongod or mongos should send all diagnostic logging information
    path: "D:/02_Server/DBServer/mongodb-win32-x86_64-2008plus-ssl-4.0.1/log/mongod.log"
    logAppend: true
    storage:
    journal:
    enabled: true
    #The directory where the mongod instance stores its data.Default Value is "/data/db".
    dbPath: "D:/02_Server/DBServer/mongodb-win32-x86_64-2008plus-ssl-4.0.1/data"
    net:
    #bindIp: 127.0.0.1
    port: 27017
    setParameter:
    enableLocalhostAuthBypass: false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    使用shell命令连接mongodb:

    使用seell命令连接数据库:
    另外启动一个cmd:
    输入以下指令:

    mongo
    show dbs

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    图形化界面连接mongodb数据库

    Compass-图形化界面客户端

    到MongoDB官网下载MongoDB Compass,

    链接地址: https://www.mongodb.com/download-center/v2/compass?initial=true

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    如果是下载安装版,则按照步骤安装;
    如果是下载加压缩版zip,直接解压,执行里面的 MongoDBCompassCommunity.exe 文件即可。

    在打开的界面中,输入主机地址、端口等相关信息,点击连接:

    连接图示:
    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    2. linux部署:

    官网下载linux版本:tgz结尾的压缩包

    在这里插入图片描述

    在这里插入图片描述

    移动解压后的文件夹到 新建 文件夹 mongodb

    [root@localhost local]# mv mongodb-linux-x86_64-rhel70-4.0.28 /usr/local/mongodb

    在这里插入图片描述

    [root@localhost mongodb]# mkdir -p /mongodb/single/data/db
    [root@localhost mongodb]# mkdir -p /mongodb/single/log
    vi /mongodb/single/mongod.conf

    目标:在Linux中部署一个单机的MongoDB,作为生产环境下使用。
    提示:和Windows下操作差不多。
    步骤如下:
    (1)先到官网下载压缩包 mongod-linux-x86_64-4.0.10.tgz 。
    (2)上传压缩包到Linux中,解压到当前目录:
    (3)移动解压后的文件夹到指定的目录中:
    (4)新建几个目录,分别用来存储数据和日志:
    (5)新建并修改配置文件
    配置文件的内容如下:
    tar -xvf mongodb-linux-x86_64-4.0.10.tgz
    mv mongodb-linux-x86_64-4.0.10 /usr/local/mongodb
    #数据存储目录
    mkdir -p /mongodb/single/data/db
    #日志存储目录
    mkdir -p /mongodb/single/log
    vi /mongodb/single/mongod.conf

    在这里插入图片描述

    配置文件内容:

    systemLog:
    #MongoDB发送所有日志输出的目标指定为文件
    # #The path of the log file to which mongod or mongos should send all diagnostic logging information
    destination: file
    #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
    path: "/mongodb/single/log/mongod.log"
    #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
    logAppend: true
    storage:
    #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
    ##The directory where the mongod instance stores its data.Default Value is "/data/db".
    dbPath: "/mongodb/single/data/db"
    journal:
    #启用或禁用持久性日志以确保数据文件保持有效和可恢复。
    enabled: true
    processManagement:
    #启用在后台运行mongos或mongod进程的守护进程模式。
    fork: true
    net:
    #服务实例绑定的IP,默认是localhost
    bindIp: localhost,192.168.0.2
    
    #bindIp
    #绑定的端口,默认是27017
    port: 27017
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述

    linux下载:

    在这里插入图片描述

    在这里插入图片描述

    配置:

    在这里插入图片描述

    操作:

    在这里插入图片描述

    启动服务:到配置文件所在的目录下启动:

    [root@bobohost single]# /usr/local/mongodb/bin/mongod -f /mongodb/single/mongod.conf

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    sudo chmod -R 777 /usr/local/mongodb

    在这里插入图片描述

    mv mongodb-linux-x86_64-rhel70-4.4.18/* /usr/local/mongodb

    查看进程:

    ps -ef | grep mongod

  • 相关阅读:
    Linux·驱动
    如何检测出你们安装的依赖是否安全
    MySQL数据库的存储引擎
    寻找单身狗
    Intel Locked Atomic Operations
    【Linux网络编程】日志与守护进程
    keepalived实现nginx高可用
    秋招|阿里测试开发岗面经(一共七次面试)
    latex,没有边框的表格
    Tomcat
  • 原文地址:https://blog.csdn.net/weixin_38568503/article/details/127871814