MongoDB入门实战教程(1)
<p>对于后端开发工程师,NoSQL是一个需要掌握的技术点,而NoSQL中比较火热的技术当属MongoDB。欢迎入门MongoDB,进入无模式的文档数据库世界。</p><h1>1 关于MongoDB</h1>
<p>通过下面几个问题,我们来快速地认识一下MongoDB吧<strong>。</strong></p>
<p style="text-align: center"><strong><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605150519815-1346467924.jpg"></strong></p>
<h3><strong>什么是MongoDB?</strong></h3>
<p>一个以JSON为数据模型的文档数据库。</p>
<h3><strong>为什么叫文档数据库?</strong></h3>
<p>这里的文档来自于“<strong>JSON Document</strong>”,而不是我们一般理解的PDF、WORD等文档。</p>
<h3><strong>是谁开发的MongoDB?</strong></h3>
<p>一个名叫 MongoDB Inc 的科技公司,总部在美国纽约。</p>
<h3><strong>MongoDB的主要用途是什么?</strong></h3>
<p>应用数据库,类似于MySQL、Oracle、MSSQL等。</p>
<p>海量数据处理,数据平台等。</p>
<h3><strong>MongoDB的主要特点是什么?</strong></h3>
<p>建模不再是必选,而是可选;</p>
<p>JSON数据模型比较适合开发者快速迭代;</p>
<p>横向扩展可以支撑很大的数据量和并发量;</p>
<h3><strong>MongoDB是免费的么?</strong></h3>
<p>MongoDB有两个发布版本:社区版 和 企业版;</p>
<p>社区版是基于SSPL协议,这是一种和AGPL协议类似的开源协议,对于云厂商封装云产品有一定限制,其他场景均无限制,免费使用;</p>
<p>企业版则是基于商业协议,需要付费使用;</p>
<h3><strong>MongoDB各版本有什么变迁?</strong></h3>
<p>一图胜前言:</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605150600000-750214292.png"></p>
<p>值得一提的是,4.x 版本开始支持事务了。</p>
<h3><strong>MongoDB和关系型DB有什么异同点?</strong></h3>
<p>一表胜前言:</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605150608312-906582095.png"></p>
<p>对于基本概念术语,MongoDB与关系型数据库的区别如下表:</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605150616951-92528799.png"></p>
<p>可以看到,在MongoDB中每一行被称做一个文档,这也是MongoDB被称为文档型数据库的最大特点。</p>
<h1>2 MongoDB的特色和优势</h1>
<h3><strong>特色:灵活的文档模型</strong></h3>
<p>在传统的关系型数据库中,我们往往需要建立错综复杂的关系模型。</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605150925895-2108985526.png"></p>
<p>而在MongoDB中,我们只需要简单快速的创建一个对象模型即可。</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605151101840-705904347.png"></p>
<p>这个对象模型就是我们常说的JSON文档文档模型,它具有以下几个特性:</p>
<p>(1)数据库引擎只需要在一个存储区读写;</p>
<p>(2)反范式、无关联的组织极大优化查询速度;</p>
<p>(3)动态数据模式,支持应用开发快速迭代;</p>
<h3><strong>优势:原生的高可用和横向扩展能力</strong></h3>
<p>在传统的关系型数据库中,我们往往需要借助一些组件花费很多功夫才能做到高可用和横向扩展,而这些在MongoDB中就是与生俱来的,你不需要花费很多功夫就可以实现。</p>
<p>例如,可以直接借助MongoDB提供的复制集的能力实现高可用,最大可支持50个复制集,完全可以实现多中心的容灾能力。</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605151110514-1735080738.png"></p>
<p>此外,还可以直接借助MongoDB提供的分片集的能力实现横向扩展,我们要做的只是在需要的时候无缝扩展,它支持多种数据分布策略(Hash、范围等),可以较为轻松地支持TB到PB级的数据量。</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605151117803-1564606554.png"></p>
<h1>3 快速安装MongoDB</h1>
<p>MongoDB支持多种安装方式和多平台(Windows/Linux),还支持Docker部署。这里为了快速演示,我们来在Linux下安装一个适用于开发测试环境的MongoDB社区版实例。</p>
<blockquote data-type="2" data-url="" data-author-name="" data-content-utf8-length="175" data-source-title="">此外,你也可以通过官方提供的云托管服务来创建一个免费的MongoDB集群用于学习,这也是一个快速学习MongoDB的方式,限制是免费集群的存储大小只有512MB。当然,你还可以通过Docker来部署一个MongoDB社区版实例,不过我的习惯一般是数据库类有状态服务的运行环境都不用Docker来部署,开发环境和测试环境可以考虑采用Docker来部署。</blockquote>
<h3><strong>前置条件</strong></h3>
<p>一台Linux主机 或 虚拟机 或 云主机,建议 CentOS 7.x 版本。</p>
<p>配置好静态IP、关闭防火墙、主机名等基本操作,不再赘述。</p>
<h3><strong>下载安装包</strong></h3>
<p>从官网(https://www.mongodb.com/try/download/community)下载MongoDB 4.4.5的tgz包:</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605152947605-1887752901.jpg"></p>
<p>下载完成后将其拷贝到Linux中,这里我们暂且将其拷贝到 /usr/local/mongodb/source 目录下。</p>
<p>当然,你要先创建这个目录:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">mkdir</span> /usr/local/<span style="color: rgba(0, 0, 0, 1)">mongodb
</span><span style="color: rgba(0, 0, 255, 1)">mkdir</span> /usr/local/mongodb/source</pre>
</div>
<p>然后,进入 source<span class="Apple-converted-space"> 目录下解压,并将压缩后的所有文件移动到 /usr/local/mongodb 目录下<br></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">mv</span> mongodb-linux-x86_64-rhel70-<span style="color: rgba(128, 0, 128, 1)">4.4</span>.<span style="color: rgba(128, 0, 128, 1)">5</span> /usr/local/mongodb</pre>
</div>
<h3><strong>准备Mongo目录与配置文件</strong></h3>
<p>首先,在 /usr/local/mongodb 目录下分别创建db目录 和 log目录:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">mkdir</span> /usr/local/mongodb/data/<span style="color: rgba(0, 0, 0, 1)">db
</span><span style="color: rgba(0, 0, 255, 1)">mkdir</span> /usr/local/mongodb/logs/
<span style="color: rgba(0, 0, 255, 1)">mkdir</span> /usr/local/mongodb/logs/mongodb.log</pre>
</div>
<p>然后,创建最核心的mongo配置文件:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">vi</span> /usr/local/mongodb/mongodb.conf</pre>
</div>
<p>配置文件内容如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">systemLog:
destination: </span><span style="color: rgba(0, 0, 255, 1)">file</span><span style="color: rgba(0, 0, 0, 1)">
path: </span>/usr/local/mongodb/logs/<span style="color: rgba(0, 0, 0, 1)">mongodb.log # log path
logAppend: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
storage:
dbPath: </span>/usr/local/mongodb/data/<span style="color: rgba(0, 0, 0, 1)">db # data directory
net:
bindIp: </span><span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span><span style="color: rgba(0, 0, 0, 1)">
port: </span><span style="color: rgba(128, 0, 128, 1)">27017</span><span style="color: rgba(0, 0, 0, 1)"> # port
processManagement:
fork: </span><span style="color: rgba(0, 0, 255, 1)">true</span></pre>
</div>
<h3><strong>修改环境变量</strong></h3>
<p>执行以下命令修改环境变量:</p>
<div class="cnblogs_code">
<pre>export PATH=$PATH:/usr/local/mongodb/<span style="color: rgba(0, 0, 0, 1)">bin
source </span>/etc/profile</pre>
</div>
<p>验证一下:</p>
<div class="cnblogs_code">
<pre>mongo -version</pre>
</div>
<h3 style="text-align: center"><strong><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605153224813-1352114823.jpg"></strong></h3>
<h3><strong>设置开机启动项</strong></h3>
<p>首先,进入 /lib/systemd/system 目录下,执行以下命令:</p>
<div class="cnblogs_code">
<pre>cd /lib/systemd/<span style="color: rgba(0, 0, 0, 1)">system
</span><span style="color: rgba(0, 0, 255, 1)">cat</span> >>mongodb.service<<<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">EOF</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
在</span>><span style="color: rgba(0, 0, 0, 1)">提示下拷贝以下内容:
Description</span>=<span style="color: rgba(0, 0, 0, 1)">mongodb
After</span>=network.target remote-fs.target nss-<span style="color: rgba(0, 0, 0, 1)">lookup.target
Type</span>=<span style="color: rgba(0, 0, 0, 1)">forking
ExecStart</span>=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/<span style="color: rgba(0, 0, 0, 1)">mongodb.conf
ExecReload</span>=/bin/<span style="color: rgba(0, 0, 255, 1)">kill</span> -<span style="color: rgba(0, 0, 0, 1)">s HUP $MAINPID
ExecStop</span>=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/<span style="color: rgba(0, 0, 0, 1)">mongodb.conf
PrivateTmp</span>=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
WantedBy</span>=multi-<span style="color: rgba(0, 0, 0, 1)">user.target
EOF</span></pre>
</div>
<p>设置mongodb.service执行权限</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">chmod</span> +x mongodb.service</pre>
</div>
<p>设置mongodb.service开机自启动</p>
<div class="cnblogs_code">
<pre>systemctl enable mongodb.service</pre>
</div>
<p>这时开机自启动配置完成,reboot一下验证看看。</p>
<h3><strong>试玩MongoDB</strong></h3>
<p>首先,执行以下命令进入Mongo Shell:</p>
<div class="cnblogs_code">
<pre>mongo</pre>
</div>
<p>然后,执行以下命令可以看到目前已有的数据库:</p>
<div class="cnblogs_code">
<pre>show dbs</pre>
</div>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605153444177-147941834.png"></p>
<p>接下来,我们来创建一个 students 数据库并新增一个文档 用于把玩:</p>
<div class="cnblogs_code">
<pre>><span style="color: rgba(0, 0, 0, 1)"> use students
switched to db students
</span>> db.records.insertOne({name:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Edison</span><span style="color: rgba(128, 0, 0, 1)">"</span>, gender:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Male</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">})
{
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">acknowledged</span><span style="color: rgba(128, 0, 0, 1)">"</span> : <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">insertedId</span><span style="color: rgba(128, 0, 0, 1)">"</span> : ObjectId(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6092aa664e88a1d766523bc4</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
}
</span>> db.records.<span style="color: rgba(0, 0, 255, 1)">find</span><span style="color: rgba(0, 0, 0, 1)">().pretty()
{
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">_id</span><span style="color: rgba(128, 0, 0, 1)">"</span> : ObjectId(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6092aa664e88a1d766523bc4</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">),
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">"</span> : <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Edison</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gender</span><span style="color: rgba(128, 0, 0, 1)">"</span> : <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Male</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>暂且先不用管这个语法,你只需要知道它向test数据库的students集合中新增了一行记录 并 通过find查询到了这一行记录 即可。</p>
<h3><strong>使用Compass客户端</strong></h3>
<p>MongoDB除了提供了shell命令供我们使用,还提供了一个免费的图形化客户端工具Compass。</p>
<p>下载地址:https://www.mongodb.com/products/compass</p>
<p>安装完成后,配置一下连接参数:</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605153530642-1282854702.jpg"></p>
<p>即可看到所有的数据库和集合了:</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605153548022-1790898933.jpg"></p>
<p>通过Compass查看刚刚把玩的students数据库:</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605153612351-2078915623.jpg"></p>
<p>当然,除了Compass之外呢,可以选择的可视化工具还有Robo 3T 以及 Navicat,如果你已经安装了Navicat,那就直接使用Navicat连接也是一个不错的选择。</p>
<p>OK,到此试玩结束。</p>
<h1>4 总结</h1>
<p>本文总结了MongoDB的基本概念、文档模型 及 技术优势,并介绍了如何在Linux下快速部署安装一个MongoDB实例 以及 使用Compass客户端工具连接MongoDB。</p>
<p>下一篇,我们会学习如何在Linux下安装部署一个三节点MongoDB的高可用复制集集群,有兴趣的童鞋可以继续关注。</p>
<p> </p>
<h1><strong>参考资料</strong></h1>
<p>唐建法,《MongoDB高手课》(极客时间)</p>
<p>郭远威,《MongoDB实战指南》(图书)</p>
<p style="text-align: center"><img src="https://img2020.cnblogs.com/blog/381412/202106/381412-20210605150036439-1705761076.jpg"></p>
<p style="text-align: center">△推荐订阅学习</p>
<p> </p>
<p style="text-align: center"><img src="https://images.cnblogs.com/cnblogs_com/edisonchou/1647700/o_200902144330EdisonTalk-Footer.jpg"></p>
<div id="Copyright">
<p>作者:周旭龙</p>
<p>出处:https://edisonchou.cnblogs.com</p>
<p>本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。</p>
</div>
</div>
<div id="MySignature" role="contentinfo">
<div align="center"><img border="0" src="http://service.t.sina.com.cn/widget/qmd/2068032061/d643d182/10.png"></div><br><br>
来源:https://www.cnblogs.com/edisontalk/p/mongodb_learning_summary_part1.html
頁:
[1]