• Mongodb 基础与安装


    官网链接:https://docs.mongodb.com/

    参考链接:https://www.runoob.com/mongodb/mongodb-linux-install.html

    1.什么是MongoDB 

    MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。
    
    在高负载的情况下,添加更多的节点,可以保证服务器性能。
    
    MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。
    
    MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

    2.主要特点有哪些

    主要特点
    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安装简单。

    3. Mongodb 安装部署

    #1.安装依赖包
    sudo yum install libcurl openssl     #centos redhat
    sudo apt-get install libcurl4 openssl   ubantu 18.04 LTS
    
    #2.下载源码包
     参考链接:
     MongoDB 源码下载地址:https://www.mongodb.com/download-center#community
     
    #3.下载二进制包
    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.17.tgz    # 下载
    
    #4.解压到具体目录
    mv mongodb-linux-x86_64-rhel70-4.2.17.tgz  /usr/local/mongodb4      # 将解压包拷贝到指定目录
    
    #5.配置环境变量
    echo "export PATH=$PATH:/usr/local/mongodb4/bin" >>/etc/profile
    source /etc/profile
    
    #6.创建相关目录
    数据存储目录:/var/lib/mongodb
    日志文件目录:/var/log/mongodb
    sudo mkdir -p /var/lib/mongo
    sudo mkdir -p /var/log/mongodb
    sudo chown `whoami` /var/lib/mongo     # 设置权限
    sudo chown `whoami` /var/log/mongodb   # 设置权限
    
    #7.启动mongodb服务   默认端口为:10503
     mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
    [root@shell ~23:18:56]# mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
    about to fork child process, waiting until server is ready for connections.
    forked process: 10503
    child process started successfully, parent exiting
    
    [root@shell ~23:19:10]# netstat -lntup |grep mongo
    tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      10503/mongod 
    
    #参数说明:
    -h:指明数据库宿主机的IP
    -u:指明数据库的用户名
    -p:指明数据库的密码
    -d:指明数据库的名字
    -c:指明collection的名字
    -o:指明到要导出的文件名
    -q:指明导出的过滤条件
    -j,--numParallclCollections= number of collectiions to dum in parallel
    
    #8.停止mongodb 
    方法一:
    mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --shutdown
    
    方法二:
    > use admin
    switched to db admin
    > db.shutdownServer()
    
    #9.查看日志文件是否启动成功
    [root@shell ~23:19:25]# tail -10f /var/log/mongodb/mongod.log
    2021-10-16T23:19:10.399+0800 I  STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with provided UUID: 520fda48-31c3-45f9-8394-048c2b664b49 and options: { uuid: UUID("520fda48-31c3-45f9-8394-048c2b664b49") }
    2021-10-16T23:19:10.400+0800 I  NETWORK  [listener] Listening on /tmp/mongodb-27017.sock
    2021-10-16T23:19:10.401+0800 I  NETWORK  [listener] Listening on 127.0.0.1
    2021-10-16T23:19:10.401+0800 I  NETWORK  [listener] waiting for connections on port 27017
    2021-10-16T23:19:10.414+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index _id_ on ns config.system.sessions
    2021-10-16T23:19:10.424+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: starting on config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 } using method: Hybrid
    2021-10-16T23:19:10.425+0800 I  INDEX    [LogicalSessionCacheRefresh] build may temporarily use up to 200 megabytes of RAM
    2021-10-16T23:19:10.425+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: collection scan done. scanned 0 total records in 0 seconds
    2021-10-16T23:19:10.426+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: inserted 0 keys from external sorter into index in 0 seconds
    2021-10-16T23:19:10.427+0800 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index lsidTTLIndex on ns config.system.sessions

    4.进入Mongodb 后台

    #1.进入安装路径的bin 目录下
     cd /usr/local/mongodb4/bin
     输入
     ./mongo +回车
     [root@shell /usr/local/mongodb4/bin23:22:58]# mongo
    MongoDB shell version v4.2.17
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("9370b83b-492c-4f62-b54d-e7cdadce6f06") }
    MongoDB server version: 4.2.17
    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
    Server has startup warnings: 
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] 
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] 
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] 
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] 
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] 
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2021-10-16T23:19:10.328+0800 I  CONTROL  [initandlisten] 
    ---
    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()
    
    #2.测试数据
    > 1 +1
    2
    > 1+4
    5
    > 100*100
    10000
     #插入数据
    > db.runoob.insert({x:10})
    WriteResult({ "nInserted" : 1 })
     #查看数据
    > db.runoob.find()
    { "_id" : ObjectId("616aee731b401b9fc72193da"), "x" : 10 }

    5.Mongodb 备份

    #1.全库备份
    mkdir /mongodb/backup
    mongodump -uroot -proot123 --port 27017 --authenticationDatabase admin -o /mongodb/backup
    #2.备份world库
    mongodum -uroot -proot123 --port 27017 --authenticationDatabase admin -d world -o /mongodb/backup
    #3.备份oldboy库下的Log集合
    mongodum -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldboy -c log -o /mongodb/backup
    #4.压缩备份
    mongodum -uroot -proot123 --port 27017 --authenticationDatabase admin -o /mongodb/backup/ --gzip
    #5.恢复test库
    mongorestore -uroot -proot123 --port 27017 --authenticationDatabase admin -d test1 /mongodb/backup/test --gzip
    Do everything well
  • 相关阅读:
    Sql server时间转时间long
    SQL Server死锁问题:事务(进程 ID x)与另一个进程被死锁在 锁 | 通信缓冲区资源上并且已被选作死锁牺牲品。请重新运行该事务。
    layui jquery ajax,url,type,async,dataType,data
    在 Postman 中报错:Self-signed SSL certificates are being blocked 的分析与解决
    SQL server CASE WHEN
    SQL server 统计分组经计
    Spring boot @Transactional
    基于mysql的sakila数据库脚本分析
    常用数据库JDBC
    在做银行支付接口案例的时候,遇到的编码问题!
  • 原文地址:https://www.cnblogs.com/linuxmysql/p/15440564.html
Copyright © 2020-2023  润新知