MongoDB的安装,mongod和mongo的区别
<p> </p><h3>一. mongoDB安装路径</h3>
<p>安装路径(最新4.0.11):https://www.mongodb.com/download-center/community?jmp=nav</p>
<p>建议另外找路径下载,外网太慢,等不住。。</p>
<p>这是一位博主提供的下载路径(4.0.10):</p>
<p>百度链接:https://pan.baidu.com/s/1xhFsENTVvU-tnjK9ODJ7Ag 密码:ctyy</p>
<p> </p>
<p> </p>
<h3>二. 安装步骤</h3>
<p>正常的安装步骤</p>
<p>1. 勾选协议,然后Next</p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731214403752-513507439.png"></p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731214655995-1768994147.png"></p>
<p> </p>
<p>2. 选择"Custom"自定义安装,不要选择“Complete”</p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731214806119-215366088.png"></p>
<p> </p>
<p>3. 安装在自己选择的(Browse...)文件夹下</p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731215056587-90185758.png"></p>
<p> </p>
<p>4. 下一步安装 <strong>"install mongoDB compass"</strong> 不勾选,否则可能要很长时间都一直在执行安装,MongoDB Compass 是一个图形界面管理工具,我们可以在后面自己到官网下载安装</p>
<p> <img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731215504997-39678549.png"></p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731215518910-846273751.png"></p>
<p> </p>
<p> 5. 点击 “Install” 安装即可</p>
<p> </p>
<h3>三 安装完成后</h3>
<p><strong>1. 安装完成后的目录结构</strong></p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731220044073-741981816.png"></p>
<p><strong>2. 安装完成的验证</strong></p>
<div class="cnblogs_code">
<pre>>mongo -version</pre>
</div>
<p>查看当前mongo的版本,</p>
<p>可以在mongoDB/bin目录下打开执行命令</p>
<p>也可配置环境变量:即配置bin文件夹的路径</p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731223605051-1561670177.png"></p>
<p> </p>
<p><strong>3. 启动mongodb服务</strong></p>
<div class="cnblogs_code">
<pre>>mongod -dbpath "D:\mongodatabase\data"</pre>
</div>
<p><span style="color: rgba(255, 0, 0, 1)">mongod 命令 是启用数据库服务,即搭建并开启服务器,可以通过端口被访问(27017)</span></p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731223853051-1800013703.png"></p>
<p>表示在27017端口启动成功,浏览器打开 http://localhost:27017/ 显示如下</p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731224710079-323319885.png"></p>
<p>注意:</p>
<p>mongod --dbpath 命令是创建数据库文件的存放位置,启动mongodb服务时需要先确定数据库文件存放的位置,否则系统不会自动创建,启动会不成功。</p>
<p>mongod --logpath 表示日志文件存放的路径 --logappend 表示以追加的方式写日志文件</p>
<p> </p>
<p>在 " bin/mongod.cfg " 文件中也会有 dbPath 和 logPath的配置</p>
<p>另外:</p>
<p>在 bin目录下的 mongod.exe文件,双击会闪出,就是因为没有执行上面的 mongod --dbpath 命令</p>
<p>在 bin目录下的 mongo.exe文件,双击来打开 MongoDB 客户端,进行数据库操作</p>
<p> </p>
<p><strong>4. mongo的操作</strong></p>
<p><span style="color: rgba(255, 0, 0, 1)">mongo 命令 是连接数据库服务,即连接服务器,可以通过端口进行访问(27017)</span></p>
<p><img src="https://img2018.cnblogs.com/blog/1383488/201907/1383488-20190731233855223-1676755080.png"></p>
<p>我们的连接:</p>
<p><em id="__mceDel">connecting to: mongodb://127.0.0.1:27017</em></p>
<p> </p>
<p><strong>5. 操作数据库</strong></p>
<p>到这一步数据库已经成功跑起来了,接下来就是操作一些命令向数据库里面插入数据等并且可以看到自己对数据库的一系列操作的结果了</p>
<p>db.stats() 命令将显示数据库名称,数据库中的集合和文档数量</p>
<div class="cnblogs_code">
<pre>><span style="color: rgba(0, 0, 0, 1)"> db.stats()
{
</span>"db" : "test"<span style="color: rgba(0, 0, 0, 1)">,
</span>"collections" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"views" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"objects" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"avgObjSize" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"dataSize" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"storageSize" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"numExtents" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"indexes" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"indexSize" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"fileSize" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"fsUsedSize" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"fsTotalSize" : 0<span style="color: rgba(0, 0, 0, 1)">,
</span>"ok" : 1<span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p> </p>
<p>db.help()将列出一个命令列表</p>
<div class="cnblogs_code">
<pre>><span style="color: rgba(0, 0, 0, 1)"> db.help()
DB methods:
db.adminCommand(nameOrDocument) </span>- switches to 'admin'<span style="color: rgba(0, 0, 0, 1)"> db, and runs command
db.aggregate(, {options}) </span>- performs a collectionless aggregation on <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)"> database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost) </span>-<span style="color: rgba(0, 0, 0, 1)"> deprecated
db.commandHelp(name) returns the help </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> the command
db.copyDatabase(fromdb, todb, fromhost) </span>-<span style="color: rgba(0, 0, 0, 1)"> deprecated
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations </span><span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> the db
db.dropDatabase()
db.eval() </span>-<span style="color: rgba(0, 0, 0, 1)"> deprecated
db.fsyncLock() flush data to disk and lock server </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db[</span>'cname'<span style="color: rgba(0, 0, 0, 1)">] or db.cname
db.getCollectionInfos() </span>- returns a list that contains the names and options of the db'<span style="color: rgba(0, 0, 0, 1)">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</span>'<span style="color: rgba(0, 0, 0, 1)">s host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation </span><span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts </span><span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> 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.</span><span style="color: rgba(0, 0, 255, 1)">if</span> cmdObj is a string, turns it into {cmdObj: 1<span style="color: rgba(0, 0, 0, 1)">}
db.serverStatus()
db.setLogLevel(level,</span><component><span style="color: rgba(0, 0, 0, 1)">)
db.setProfilingLevel(level,slowms) </span>0=off 1=slow 2=<span style="color: rgba(0, 0, 0, 1)">all
db.setWriteConcern(</span><write concern doc>) - sets the write concern <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> writes to the db
db.unsetWriteConcern(</span><write concern doc>) - unsets the write concern <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> writes to the db
db.setVerboseShell(flag) display extra information </span><span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
</span>></pre>
</div>
<p> </p>
<p> </p>
<h3>四 最后</h3>
<p> 1. 注意 mongod 和 mongo 的区别</p>
<p> 前者是启用MongoDB进程,后者是对MongoDB进行连接操作</p>
<p> 2. 执行 mongod 需要 配置路径 进行启用,单纯的双击 mongod.exe文件会闪出,正确的是执行mongod --dbpath 命令</p>
<p> 3. 在执行mongod命令启用MongoDB进程(服务器)的基础上,再使用mongo 对其进行连接操作,</p>
<p> </p>
<h3>五,几个概念:</h3>
<p>来源 MongoDB面试题问题以及参考答案</p>
<p>8.什么是”mongod“<br> mongod是处理MongoDB系统的主要进程。它处理数据请求,管理数据存储,和执行后台管理操作。当我们运行mongod命令意味着正在启动MongoDB进程,并且在后台运行。</p>
<p>9."mongod"参数有什么<br> 传递数据库存储路径,默认是"/data/db"<br> 端口号 默认是 "27017"<br>10.什么是"mongo"<br> 它是一个命令行工具用于连接一个特定的mongod实例。当我们没有带参数运行mongo命令它将使用默认的端口号和localhost连接</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/nangezi/p/11279401.html
頁:
[1]