您的位置:首页 > 博客中心 > 数据库 >

MongoDB的安装,mongod和mongo的区别

时间:2022-03-16 11:55

一. mongoDB安装路径

安装路径(最新4.0.11):

建议另外找路径下载,外网太慢,等不住。。

这是一位博主提供的下载路径(4.0.10):

百度链接: 密码:ctyy

 

 

二. 安装步骤

正常的安装步骤

1. 勾选协议,然后Next

技术图片

技术图片

 

2. 选择"Custom"自定义安装,不要选择“Complete”

技术图片

 

3. 安装在自己选择的(Browse...)文件夹下

技术图片

 

4. 下一步安装 "install mongoDB compass" 不勾选,否则可能要很长时间都一直在执行安装,MongoDB Compass 是一个图形界面管理工具,我们可以在后面自己到官网下载安装

 技术图片

技术图片

 

 5. 点击 “Install” 安装即可

 

三 安装完成后

1. 安装完成后的目录结构

技术图片

2. 安装完成的验证

>mongo -version

查看当前mongo的版本,

可以在mongoDB/bin目录下打开执行命令

也可配置环境变量:即配置bin文件夹的路径

技术图片

 

3. 启动mongodb服务

>mongod -dbpath "D:\mongodatabase\data"

mongod 命令 是启用数据库服务,即搭建并开启服务器,可以通过端口被访问(27017)

技术图片

表示在27017端口启动成功,浏览器打开  显示如下

技术图片

注意:

mongod --dbpath 命令是创建数据库文件的存放位置,启动mongodb服务时需要先确定数据库文件存放的位置,否则系统不会自动创建,启动会不成功。

mongod --logpath 表示日志文件存放的路径     --logappend  表示以追加的方式写日志文件

 

在 " bin/mongod.cfg " 文件中也会有 dbPath 和 logPath的配置

另外:

在 bin目录下的 mongod.exe文件,双击会闪出,就是因为没有执行上面的 mongod --dbpath 命令

在 bin目录下的 mongo.exe文件,双击来打开 MongoDB 客户端,进行数据库操作

 

4. mongo的操作

mongo 命令 是连接数据库服务,即连接服务器,可以通过端口进行访问(27017)

技术图片

我们的连接:

connecting to: mongodb://127.0.0.1:27017

 

5. 操作数据库

到这一步数据库已经成功跑起来了,接下来就是操作一些命令向数据库里面插入数据等并且可以看到自己对数据库的一系列操作的结果了

db.stats() 命令将显示数据库名称,数据库中的集合和文档数量

> db.stats()
{
        "db" : "test",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "fsUsedSize" : 0,
        "fsTotalSize" : 0,
        "ok" : 1
}

 

db.help()将列出一个命令列表

> db.help()
DB methods:
        db.adminCommand(nameOrDocument) - switches to ‘admin‘ db, and runs command [just calls db.runCommand(...)]
        db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
        db.auth(username, password)
        db.cloneDatabase(fromhost) - deprecated
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost) - deprecated
        db.createCollection(name, {size: ..., capped: ..., max: ...})
        db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
        db.createUser(userDocument)
        db.currentOp() displays currently executing operations in the db
        db.dropDatabase()
        db.eval() - deprecated
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnlock() unlocks server following a db.fsyncLock()
        db.getCollection(cname) same as db[‘cname‘] or db.cname
        db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db‘s collections
        db.getCollectionNames()
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getLogComponents()
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.getName()
        db.getPrevError()
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the same server as this one
        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
        db.hostInfo() get details about the server‘s host
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        db.loadServerScripts() loads all the scripts in db.system.js
        db.logout()
        db.printCollectionStats()
        db.printReplicationInfo()
        db.printShardingStatus()
        db.printSlaveReplicationInfo()
        db.dropUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into {cmdObj: 1}
        db.serverStatus()
        db.setLogLevel(level,<component>)
        db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
        db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
        db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
        db.setVerboseShell(flag) display extra information in shell output
        db.shutdownServer()
        db.stats()
        db.version() current version of the server
>

 

 

四 最后

  1. 注意 mongod 和 mongo 的区别

  前者是启用MongoDB进程,后者是对MongoDB进行连接操作

  2. 执行 mongod 需要 配置路径 进行启用,单纯的双击 mongod.exe文件会闪出,正确的是执行mongod --dbpath 命令

  3. 在执行mongod命令启用MongoDB进程(服务器)的基础上,再使用mongo 对其进行连接操作,

  

五,几个概念:

来源 

8.什么是”mongod“
  mongod是处理MongoDB系统的主要进程。它处理数据请求,管理数据存储,和执行后台管理操作。当我们运行mongod命令意味着正在启动MongoDB进程,并且在后台运行。

9."mongod"参数有什么
  传递数据库存储路径,默认是"/data/db"
  端口号 默认是 "27017"
10.什么是"mongo"
  它是一个命令行工具用于连接一个特定的mongod实例。当我们没有带参数运行mongo命令它将使用默认的端口号和localhost连接

 

 

 

 

 

.

本类排行

今日推荐

热门手游