IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> Mongodb使用学习笔记(一) -> 正文阅读

[大数据]Mongodb使用学习笔记(一)

MongoDB使用学习笔记

1. MongoDB安装

2. MongoDB使用

(1)MongoDB的启停

  • 参考博客:

  • 启动:

    • 一些参数说明:详细说明可以看本节第一篇参考博客。./bin/mongod是安装mongoDB的bin目录下的文件。./data/db是数据目录。./logs/mongodb.log是日志文件。前面的参数就是指定相应配置的参数。
      ./bin/mongod --dbpath ./data/db --fork --logpath=./logs/mongodb.log
      
      在这里插入图片描述
  • 停止:

    ./bin/mongod --dbpath ./data/db --shutdown
    

    在这里插入图片描述

  • 顺带提几句,参考博客里面说到停止mongoDB, 如果使用了杀死进程的方法,不能使用pkill,kill -9这些命令。

    • 先上一篇参考博客:Mongodb入门系列(3)————kill -9 mongod之后mongodb服务无法开启问题

    • 大概意思就是用了这两个命令后,就不能正常启动mongoDB服务了。而我上面讲到的那种停止的命令是可以正常再次启动mongoDB的。如果真的用了kill -9杀进程了,那可以通过“删除/data下的mongod.lock”解决。

    • 实际上,我刚刚试着用kill -9杀进程,还是可以正常启动的……并且也能使用。待我多用一段时间看看有没有长进之后再看看这个情况吧。后续有什么新认识再补充回来。

      [root@VM-20-8-centos mongdb]# ./bin/mongod --dbpath ./data/db --shutdown
      {"t":{"$date":"2021-11-21T12:11:13.071+08:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}}
      {"t":{"$date":"2021-11-21T12:11:13.071+08:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"-","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
      {"t":{"$date":"2021-11-21T12:11:13.071+08:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
      {"t":{"$date":"2021-11-21T12:11:13.071+08:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
      {"t":{"$date":"2021-11-21T12:11:13.073+08:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
      {"t":{"$date":"2021-11-21T12:11:13.073+08:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","ns":"config.tenantMigrationDonors"}}
      {"t":{"$date":"2021-11-21T12:11:13.073+08:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","ns":"config.tenantMigrationRecipients"}}
      There doesn't seem to be a server running with dbpath: ./data/db
      [root@VM-20-8-centos mongdb]# ./bin/mongod --dbpath ./data/db --fork --logpath=./logs/mongodb.log
      about to fork child process, waiting until server is ready for connections.
      forked process: 7367
      child process started successfully, parent exiting
      [root@VM-20-8-centos mongdb]# kill -9 7367
      [root@VM-20-8-centos mongdb]# ./bin/mongod --dbpath ./data/db --fork --logpath=./logs/mongodb.log
      about to fork child process, waiting until server is ready for connections.
      forked process: 7545
      child process started successfully, parent exiting
      [root@VM-20-8-centos mongdb]# ./bin/mongo
      MongoDB shell version v5.0.4
      connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
      Implicit session: session { "id" : UUID("d9afca99-c310-4e8f-8aa7-af9ef8748fcb") }
      MongoDB server version: 5.0.4
      ================
      Warning: the "mongo" shell has been superseded by "mongosh",
      which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
      an upcoming release.
      For installation instructions, see
      https://docs.mongodb.com/mongodb-shell/install/
      ================
      ---
      The server generated these startup warnings when booting:
              2021-11-21T12:11:35.302+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
              2021-11-21T12:11:35.747+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
              2021-11-21T12:11:35.747+08:00: You are running this process as the root user, which is not recommended
              2021-11-21T12:11:35.747+08: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
              2021-11-21T12:11:35.747+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
              2021-11-21T12:11:35.747+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
      ---
      ---
              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()
      ---
      > exit
      bye
      [root@VM-20-8-centos mongdb]#
      
      

(2)进入MongoDB命令行

  • 安装MongoDB的bin目录下包含两个文件:mongo和mongod
  • 进入MongoDB命令行
    ./mongo
    
    在这里插入图片描述

3. MongoDB命令学习

(1)创建数据库

  • use DATABASE_NAME,如果数据库不存在,则创建数据库,否则切换到指定数据库。
    use DATABASE_NAME
    

(2)查看所有数据库

  • 只能显示有数据的数据库,所以想要看到想看的数据库,需要往里面插入几条数据。
  • 集合只有在内容插入后才会创建! 就是说,创建集合(数据表)后要再插入一个文档(记录),集合才会真正创建。
  • MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中。
    show dbs
    

(3)MongoDB 创建集合

  • 参数说明:
    • name: 要创建的集合名称
    • options: 可选参数, 指定有关内存大小及索引的选项
    db.createCollection(name, options)
    

(4)MongoDB 插入文档

  • 参数说明:
    • name: 要创建的集合名称
    • options: 可选参数, 指定有关内存大小及索引的选项
    db.createCollection(name, options)
    
  • 上述操作记录:
    > use cmdb;
    switched to db cmdb
    > show dbs;
    admin   0.000GB
    config  0.000GB
    local   0.000GB
    > db.createCollection(hardware);
    uncaught exception: ReferenceError: hardware is not defined :
    @(shell):1:1
    > db.createCollection("hardware");
    { "ok" : 1 }
    > db.hardware.in
    db.hardware.initializeOrderedBulkOp(    db.hardware.insert(                     db.hardware.insertOne(
    db.hardware.initializeUnorderedBulkOp(  db.hardware.insertMany(
    > db.hardware.insert({)
    ... ^C
    
    > db.hardware.insert({"name":"我是程咬金"});
    uncaught exception: SyntaxError: illegal character :
    @(shell):1:36
    > show dbs;
    admin   0.000GB
    cmdb    0.000GB
    config  0.000GB
    local   0.000GB
    > show collections;
    hardware
    >
    
    

(5)MongoDB 删除数据库

  • db.dropDatabase()
    > use testdel;
    switched to db testdel
    > show dbs;
    admin   0.000GB
    cmdb    0.000GB
    config  0.000GB
    local   0.000GB
    > db.createCollection("testdelCollection")
    { "ok" : 1 }
    > db.testdelCollection.insert({"word":"台独必将灭亡"});
    WriteResult({ "nInserted" : 1 })
    > show dbs;
    admin    0.000GB
    cmdb     0.000GB
    config   0.000GB
    local    0.000GB
    testdel  0.000GB
    > use testdel;
    switched to db testdel
    > db.dropDatabase();
    { "ok" : 1 }
    > show dbsl
    uncaught exception: Error: don't know how to show [dbsl] :
    shellHelper.show@src/mongo/shell/utils.js:1211:11
    shellHelper@src/mongo/shell/utils.js:838:15
    @(shellhelp2):1:1
    > show dbs;
    admin   0.000GB
    cmdb    0.000GB
    config  0.000GB
    local   0.000GB
    >```
    

(6)MongoDB 删除Collection

  • db.collectionName.drop()
    > use cmdb;
    switched to db cmdb
    > show collections;
    hardware
    > db.createCollection("server");
    { "ok" : 1 }
    > db.server.insert({"word":"猥琐发育,别浪"})
    WriteResult({ "nInserted" : 1 })
    > show collections;
    hardware
    server
    > db.server.drop();
    true
    > show collections;
    hardware
    >
    
  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-11-23 12:26:02  更:2021-11-23 12:28:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 8:55:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码