mirror of https://github.com/dunwu/db-tutorial.git
135 lines
56 KiB
HTML
135 lines
56 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en-US">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
<title>MongoDB 运维 | DB-TUTORIAL</title>
|
||
<meta name="generator" content="VuePress 1.9.9">
|
||
<link rel="icon" href="/db-tutorial/img/favicon.ico">
|
||
<script src="https://cdn.wwads.cn/js/makemoney.js" type="text/javascript"></script>
|
||
<meta name="description" content="☕ db-tutorial 是一个数据库教程。">
|
||
<meta name="keywords" content="vuepress,theme,blog,vdoing">
|
||
<meta name="theme-color" content="#11a8cd">
|
||
<meta name="wwads-cn-verify" content="mxqWx62nfQQ9ocT4e5DzISHzOWyF4s">
|
||
|
||
<link rel="preload" href="/db-tutorial/assets/css/0.styles.51390d19.css" as="style"><link rel="preload" href="/db-tutorial/assets/js/app.be3f2e92.js" as="script"><link rel="preload" href="/db-tutorial/assets/js/2.aded268b.js" as="script"><link rel="preload" href="/db-tutorial/assets/js/42.14a20b0f.js" as="script"><link rel="prefetch" href="/db-tutorial/assets/js/10.551ab278.js"><link rel="prefetch" href="/db-tutorial/assets/js/11.c049b6a2.js"><link rel="prefetch" href="/db-tutorial/assets/js/12.92d587d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/13.a7b35fa5.js"><link rel="prefetch" href="/db-tutorial/assets/js/14.9ec959b5.js"><link rel="prefetch" href="/db-tutorial/assets/js/15.e7943372.js"><link rel="prefetch" href="/db-tutorial/assets/js/16.c6ad7b26.js"><link rel="prefetch" href="/db-tutorial/assets/js/17.39500ebd.js"><link rel="prefetch" href="/db-tutorial/assets/js/18.29949979.js"><link rel="prefetch" href="/db-tutorial/assets/js/19.78f879b8.js"><link rel="prefetch" href="/db-tutorial/assets/js/20.3ce14df7.js"><link rel="prefetch" href="/db-tutorial/assets/js/21.9d311c7c.js"><link rel="prefetch" href="/db-tutorial/assets/js/22.90234550.js"><link rel="prefetch" href="/db-tutorial/assets/js/23.12cf9e2d.js"><link rel="prefetch" href="/db-tutorial/assets/js/24.5b436e46.js"><link rel="prefetch" href="/db-tutorial/assets/js/25.8f2dc7d9.js"><link rel="prefetch" href="/db-tutorial/assets/js/26.61665ff4.js"><link rel="prefetch" href="/db-tutorial/assets/js/27.dfc7cc88.js"><link rel="prefetch" href="/db-tutorial/assets/js/28.a6286a66.js"><link rel="prefetch" href="/db-tutorial/assets/js/29.bfa8c106.js"><link rel="prefetch" href="/db-tutorial/assets/js/3.82108019.js"><link rel="prefetch" href="/db-tutorial/assets/js/30.c93b7a2e.js"><link rel="prefetch" href="/db-tutorial/assets/js/31.d48aab81.js"><link rel="prefetch" href="/db-tutorial/assets/js/32.33f8bb7c.js"><link rel="prefetch" href="/db-tutorial/assets/js/33.eb3b622d.js"><link rel="prefetch" href="/db-tutorial/assets/js/34.1ba1a06c.js"><link rel="prefetch" href="/db-tutorial/assets/js/35.24e4fee8.js"><link rel="prefetch" href="/db-tutorial/assets/js/36.dc61504e.js"><link rel="prefetch" href="/db-tutorial/assets/js/37.f1c9c729.js"><link rel="prefetch" href="/db-tutorial/assets/js/38.5024023f.js"><link rel="prefetch" href="/db-tutorial/assets/js/39.30ed016e.js"><link rel="prefetch" href="/db-tutorial/assets/js/4.884deeca.js"><link rel="prefetch" href="/db-tutorial/assets/js/40.6a6327f0.js"><link rel="prefetch" href="/db-tutorial/assets/js/41.3767f4d8.js"><link rel="prefetch" href="/db-tutorial/assets/js/43.b059081c.js"><link rel="prefetch" href="/db-tutorial/assets/js/44.73f724d9.js"><link rel="prefetch" href="/db-tutorial/assets/js/45.68f2ab27.js"><link rel="prefetch" href="/db-tutorial/assets/js/46.aed8a61f.js"><link rel="prefetch" href="/db-tutorial/assets/js/47.3bbf2366.js"><link rel="prefetch" href="/db-tutorial/assets/js/48.81688356.js"><link rel="prefetch" href="/db-tutorial/assets/js/49.1d366c93.js"><link rel="prefetch" href="/db-tutorial/assets/js/5.42f2cdb7.js"><link rel="prefetch" href="/db-tutorial/assets/js/50.ee6f08b7.js"><link rel="prefetch" href="/db-tutorial/assets/js/51.47835d9d.js"><link rel="prefetch" href="/db-tutorial/assets/js/52.b0cab79c.js"><link rel="prefetch" href="/db-tutorial/assets/js/53.106440a2.js"><link rel="prefetch" href="/db-tutorial/assets/js/54.340ab690.js"><link rel="prefetch" href="/db-tutorial/assets/js/55.00623de9.js"><link rel="prefetch" href="/db-tutorial/assets/js/56.9f086601.js"><link rel="prefetch" href="/db-tutorial/assets/js/57.f5ad5f3f.js"><link rel="prefetch" href="/db-tutorial/assets/js/58.14827368.js"><link rel="prefetch" href="/db-tutorial/assets/js/59.c41f57d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/6.5a5b4e54.js"><link rel="prefetch" href="/db-tutorial/assets/js/60.314534c0.js"><link rel="prefetch" href="/db-tutorial/assets/js/61.1ea5e4b7.js"><link rel="prefetch" href="/db-tutorial/assets/js/62.8b52e8fc.js"><link rel="prefetch" href="/db-tutorial/assets/js/63.d2fa8325.js"><link rel="prefetch" href="/db-tutorial/assets/js/64.ea2577e7.js"><link rel="prefetch" href="/db-tutorial/assets/js/65.563da2bb.js"><link rel="prefetch" href="/db-tutorial/assets/js/66.34eb51bd.js"><link rel="prefetch" href="/db-tutorial/assets/js/67.ab57f04f.js"><link rel="prefetch" href="/db-tutorial/assets/js/68.15b6f540.js"><link rel="prefetch" href="/db-tutorial/assets/js/69.54590de4.js"><link rel="prefetch" href="/db-tutorial/assets/js/7.913bec54.js"><link rel="prefetch" href="/db-tutorial/assets/js/70.40a2cea2.js"><link rel="prefetch" href="/db-tutorial/assets/js/71.3ce50922.js"><link rel="prefetch" href="/db-tutorial/assets/js/72.b9c022e9.js"><link rel="prefetch" href="/db-tutorial/assets/js/73.fba94661.js"><link rel="prefetch" href="/db-tutorial/assets/js/74.998d6c2f.js"><link rel="prefetch" href="/db-tutorial/assets/js/75.6efb68b0.js"><link rel="prefetch" href="/db-tutorial/assets/js/76.57273256.js"><link rel="prefetch" href="/db-tutorial/assets/js/77.3ddffb5b.js"><link rel="prefetch" href="/db-tutorial/assets/js/78.8fde3d74.js"><link rel="prefetch" href="/db-tutorial/assets/js/79.7a472c31.js"><link rel="prefetch" href="/db-tutorial/assets/js/8.103b4774.js"><link rel="prefetch" href="/db-tutorial/assets/js/80.4c55c65f.js"><link rel="prefetch" href="/db-tutorial/assets/js/81.cbdb67b8.js"><link rel="prefetch" href="/db-tutorial/assets/js/82.9c14d852.js"><link rel="prefetch" href="/db-tutorial/assets/js/83.a3d7d272.js"><link rel="prefetch" href="/db-tutorial/assets/js/84.6994dacc.js"><link rel="prefetch" href="/db-tutorial/assets/js/85.aca8d788.js"><link rel="prefetch" href="/db-tutorial/assets/js/86.00671865.js"><link rel="prefetch" href="/db-tutorial/assets/js/87.9076c4e8.js"><link rel="prefetch" href="/db-tutorial/assets/js/88.bbe68dab.js"><link rel="prefetch" href="/db-tutorial/assets/js/89.e2173071.js"><link rel="prefetch" href="/db-tutorial/assets/js/9.386bfe3a.js"><link rel="prefetch" href="/db-tutorial/assets/js/90.429162f2.js"><link rel="prefetch" href="/db-tutorial/assets/js/91.6ecc7c85.js"><link rel="prefetch" href="/db-tutorial/assets/js/92.efe42934.js"><link rel="prefetch" href="/db-tutorial/assets/js/93.69562766.js"><link rel="prefetch" href="/db-tutorial/assets/js/94.04aebfbc.js"><link rel="prefetch" href="/db-tutorial/assets/js/95.fe7cfce4.js"><link rel="prefetch" href="/db-tutorial/assets/js/96.674475d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/97.8a7b73f2.js"><link rel="prefetch" href="/db-tutorial/assets/js/98.bf85add1.js">
|
||
<link rel="stylesheet" href="/db-tutorial/assets/css/0.styles.51390d19.css">
|
||
</head>
|
||
<body class="theme-mode-light">
|
||
<div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/db-tutorial/" class="home-link router-link-active"><img src="https://raw.githubusercontent.com/dunwu/images/master/common/dunwu-logo.png" alt="DB-TUTORIAL" class="logo"> <span class="site-name can-hide">DB-TUTORIAL</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/db-tutorial/12.数据库/01.数据库综合/" class="nav-link">数据库综合</a></div><div class="nav-item"><a href="/db-tutorial/12.数据库/02.数据库中间件/" class="nav-link">数据库中间件</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关系型数据库" class="dropdown-title"><a href="/db-tutorial/12.数据库/03.关系型数据库/" class="link-title">关系型数据库</a> <span class="title" style="display:none;">关系型数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/01.综合/" class="nav-link">综合</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/02.Mysql/" class="nav-link">Mysql</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/99.其他/" class="nav-link">其他</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档数据库" class="dropdown-title"><!----> <span class="title" style="display:;">文档数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/04.文档数据库/01.MongoDB/" class="nav-link">MongoDB</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="KV数据库" class="dropdown-title"><!----> <span class="title" style="display:;">KV数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="搜索引擎数据库" class="dropdown-title"><!----> <span class="title" style="display:;">搜索引擎数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/01.Elasticsearch/" class="nav-link">Elasticsearch</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/02.Elastic/" class="nav-link">Elastic技术栈</a></li></ul></div></div> <a href="https://github.com/dunwu/db-tutorial" target="_blank" rel="noopener noreferrer" class="repo-link">
|
||
GitHub
|
||
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/db-tutorial/12.数据库/01.数据库综合/" class="nav-link">数据库综合</a></div><div class="nav-item"><a href="/db-tutorial/12.数据库/02.数据库中间件/" class="nav-link">数据库中间件</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关系型数据库" class="dropdown-title"><a href="/db-tutorial/12.数据库/03.关系型数据库/" class="link-title">关系型数据库</a> <span class="title" style="display:none;">关系型数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/01.综合/" class="nav-link">综合</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/02.Mysql/" class="nav-link">Mysql</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/99.其他/" class="nav-link">其他</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档数据库" class="dropdown-title"><!----> <span class="title" style="display:;">文档数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/04.文档数据库/01.MongoDB/" class="nav-link">MongoDB</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="KV数据库" class="dropdown-title"><!----> <span class="title" style="display:;">KV数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="搜索引擎数据库" class="dropdown-title"><!----> <span class="title" style="display:;">搜索引擎数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/01.Elasticsearch/" class="nav-link">Elasticsearch</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/02.Elastic/" class="nav-link">Elastic技术栈</a></li></ul></div></div> <a href="https://github.com/dunwu/db-tutorial" target="_blank" rel="noopener noreferrer" class="repo-link">
|
||
GitHub
|
||
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库综合</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>关系型数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>文档数据库</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>MongoDB</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/db-tutorial/pages/3288f3/" class="sidebar-link">MongoDB 应用指南</a></li><li><a href="/db-tutorial/pages/7efbac/" class="sidebar-link">MongoDB 的 CRUD 操作</a></li><li><a href="/db-tutorial/pages/75daa5/" class="sidebar-link">MongoDB 的聚合操作</a></li><li><a href="/db-tutorial/pages/4574fe/" class="sidebar-link">MongoDB 事务</a></li><li><a href="/db-tutorial/pages/562f99/" class="sidebar-link">MongoDB 建模</a></li><li><a href="/db-tutorial/pages/88c7d3/" class="sidebar-link">MongoDB 建模示例</a></li><li><a href="/db-tutorial/pages/10c674/" class="sidebar-link">MongoDB 索引</a></li><li><a href="/db-tutorial/pages/505407/" class="sidebar-link">MongoDB 复制</a></li><li><a href="/db-tutorial/pages/ad08f5/" class="sidebar-link">MongoDB 分片</a></li><li><a href="/db-tutorial/pages/5e3c30/" aria-current="page" class="active sidebar-link">MongoDB 运维</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/5e3c30/#mongodb-安装" class="sidebar-link">MongoDB 安装</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/5e3c30/#windows" class="sidebar-link">Windows</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/5e3c30/#linux" class="sidebar-link">Linux</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/5e3c30/#设置用户名、密码" class="sidebar-link">设置用户名、密码</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/5e3c30/#备份和恢复" class="sidebar-link">备份和恢复</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/5e3c30/#数据备份" class="sidebar-link">数据备份</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/5e3c30/#数据恢复" class="sidebar-link">数据恢复</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/5e3c30/#导入导出" class="sidebar-link">导入导出</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/5e3c30/#导入操作" class="sidebar-link">导入操作</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/5e3c30/#导出操作" class="sidebar-link">导出操作</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/5e3c30/#参考资料" class="sidebar-link">参考资料</a></li></ul></li></ul></section></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>KV数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>列式数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>搜索引擎数据库</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/db-tutorial/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/db-tutorial/categories/?category=%E6%95%B0%E6%8D%AE%E5%BA%93" title="分类" data-v-06225672>数据库</a></li><li data-v-06225672><a href="/db-tutorial/categories/?category=%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93" title="分类" data-v-06225672>文档数据库</a></li><li data-v-06225672><a href="/db-tutorial/categories/?category=MongoDB" title="分类" data-v-06225672>MongoDB</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/dunwu" target="_blank" title="作者" class="beLink" data-v-06225672>dunwu</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2020-09-09</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABH1JREFUSA3tVl1oHFUUPmdmd2ltklqbpJDiNnXFmgbFktho7YMPNiJSSZM0+CAYSkUELVhM6YuwIPpgoOKDqOBDC0XE2CQoNtQXBUFTTcCi+Wlh1V2TQExsUzcltd3M9Tt3ZjZzZ2fT+OJTL8yeM+eee757fmeJbq//KQL8X3DUSFOcfr7cRsRtxNQMWueeVzOkaITIGqQHNg5y8+jNW9ldM7A6nTpAjuolUikAwq7CE3WcM2RRDz+XGVgN3FptU/aUSlvq9Pa3iZ1+sgAqJyyAFqkipd9dqiwHF3P65YycLWc/6sqGrvoEoIp6DOFaX5h6+dnfjkWprwqsPk0dUGq5vySwDImC10KxFHgGL1SWoc92O3eVht09qdXNH11I2SsTsJYqMWzihqGMi+A+Garf3BAuuLI5oGlULyNfyB/HYNujwktOfRrMr5t77NmevqaUopx0grnKAyvVpmwUDB4x6FPXuGvYLTDwWsejwgtgkYKPqRJg8SV6xaiZ3ZTppGneS4yfH5/66fZSDHv+QZci/+h5c5UHtpy67JUqGppM0sh0Nc1dW6/N1W5Yoqat8/TU/VnadmdeW2PLLSyh0cvxBs3KbqTmwYPpxN4do/mzE8nEpvX/UMu2Wbp74zUAK5q6WkHns7V0eWkdPbPzd3rxkTGybadYySumVzhcaJFbs5UrEkQ/+CK8gF5dnh/6ciIZ73gwQ927L1IitoxKLXYP3SjYdOrHHfTZhRRlFyrorafPk20B3HPD1y2G3qKZME5Jcf3t/HUC13/8tSd++vqFveMUTwAUxSUFI1QekR1+bIze3D9MF2aq6cPvG72CgnldWCFqyRw3lwH8ZMerjTD9ElRO7Gv44wNpC90aASqGfVlz/Rx17srQ57/UU26hkhQqUB7dBR71WmzQhHUnblGmVOEw0jhbV1n9OlXUDCIRGaNV5Jp43N516fN7JmnTHdfp7Hgy0luO4aMhtkLL8Bi3bUWYvzh5Mn1dTxrL6QmGuRhGL/TiTTxRoEdTszSaq9GR0NGA3KdkOz3hqSV3MIDhQ5IVX/Ivx3umBti2es2h4eZby7x8br1rkf7Mo90AqC8aQ3sJeNzqFRu+vSANAQe3PL7l0HGOAdwDCeZYvNKeoZp1Qfs6Aipndh86HmFRi0LAnEO47wsqM6cdfjh3jBPUzhZy7nvlUfFsamED1VQt6aISHVymXZ/B2aCtIG8AI8xfobj2d3en1wWVhOeHELKmLQ1s211s88comkv4UCwWyF787mJdYXtNfhKAXVqnKTq8QZvGAGGOfaTo5pGZ/PwbUCr5+DPr/1J92JNHr9aOl/F3iI5+O1nfybsGxoimvZ3ViWSluDITw3P37mypheDIPY0tw7+O/5ApbkYw+zpfaUVu32Pi98+defdUhEpZkRFq0aqyNh9FuL9hpYbEm6iwi0z2REd09ZmyENEbuhjDWzKvZXTqKYaBIr3tt5kuPtQBZFvEUwHt60vfCNu41XsksH9Ij1BMMz1Y0OOunHNShFIP5868g5zeXmuLwL9T4b6Q2+KejgAAAABJRU5ErkJggg==">MongoDB 运维<!----></h1> <div class="theme-vdoing-content content__default"><h1 id="mongodb-运维"><a href="#mongodb-运维" class="header-anchor">#</a> MongoDB 运维</h1> <h2 id="mongodb-安装"><a href="#mongodb-安装" class="header-anchor">#</a> MongoDB 安装</h2> <h3 id="windows"><a href="#windows" class="header-anchor">#</a> Windows</h3> <p>(1)下载并解压到本地</p> <p>进入官网下载地址:<a href="https://www.mongodb.com/try/download/community" target="_blank" rel="noopener noreferrer"><strong>官方下载地址</strong><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> ,选择合适的版本下载。</p> <p>(2)创建数据目录</p> <p>MongoDB 将数据目录存储在 db 目录下。但是这个数据目录不会主动创建,我们在安装完成后需要创建它。</p> <p>例如:<code>D:\Tools\Server\mongodb\mongodb-4.4.0\data\db</code></p> <p>(3)运行 MongoDB 服务</p> <div class="language-shell extra-class"><pre class="language-shell"><code>mongod <span class="token parameter variable">--dbpath</span> D:<span class="token punctuation">\</span>Tools<span class="token punctuation">\</span>Server<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>mongodb-4.4.0<span class="token punctuation">\</span>data<span class="token punctuation">\</span>db
|
||
</code></pre></div><p>(4)客户端连接 MongoDB</p> <p>可以在命令窗口中运行 mongo.exe 命令即可连接上 MongoDB</p> <p>(5)配置 MongoDB 服务</p> <h3 id="linux"><a href="#linux" class="header-anchor">#</a> Linux</h3> <p>(1)使用安装包安装</p> <p>安装前我们需要安装各个 Linux 平台依赖包。</p> <p><strong>Red Hat/CentOS:</strong></p> <div class="language- extra-class"><pre class="language-text"><code>sudo yum install libcurl openssl
|
||
</code></pre></div><p><strong>Ubuntu 18.04 LTS ("Bionic")/Debian 10 "Buster":</strong></p> <div class="language- extra-class"><pre class="language-text"><code>sudo apt-get install libcurl4 openssl
|
||
</code></pre></div><p><strong>Ubuntu 16.04 LTS ("Xenial")/Debian 9 "Stretch":</strong></p> <div class="language- extra-class"><pre class="language-text"><code>sudo apt-get install libcurl3 openssl
|
||
</code></pre></div><p>(2)创建数据目录</p> <p>默认情况下 MongoDB 启动后会初始化以下两个目录:</p> <ul><li>数据存储目录:/var/lib/mongodb</li> <li>日志文件目录:/var/log/mongodb</li></ul> <p>我们在启动前可以先创建这两个目录并设置当前用户有读写权限:</p> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token function">sudo</span> <span class="token function">mkdir</span> <span class="token parameter variable">-p</span> /var/lib/mongo
|
||
<span class="token function">sudo</span> <span class="token function">mkdir</span> <span class="token parameter variable">-p</span> /var/log/mongodb
|
||
<span class="token function">sudo</span> <span class="token function">chown</span> <span class="token variable"><span class="token variable">`</span><span class="token function">whoami</span><span class="token variable">`</span></span> /var/lib/mongo <span class="token comment"># 设置权限</span>
|
||
<span class="token function">sudo</span> <span class="token function">chown</span> <span class="token variable"><span class="token variable">`</span><span class="token function">whoami</span><span class="token variable">`</span></span> /var/log/mongodb <span class="token comment"># 设置权限</span>
|
||
</code></pre></div><p>(3)运行 MongoDB 服务</p> <div class="language-shell extra-class"><pre class="language-shell"><code>mongod <span class="token parameter variable">--dbpath</span> /var/lib/mongo <span class="token parameter variable">--logpath</span> /var/log/mongodb/mongod.log <span class="token parameter variable">--fork</span>
|
||
</code></pre></div><p>打开 /var/log/mongodb/mongod.log 文件看到以下信息,说明启动成功。</p> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token comment"># tail -10f /var/log/mongodb/mongod.log</span>
|
||
<span class="token number">2020</span>-07-09T12:20:17.391+0800 I NETWORK <span class="token punctuation">[</span>listener<span class="token punctuation">]</span> Listening on /tmp/mongodb-27017.sock
|
||
<span class="token number">2020</span>-07-09T12:20:17.392+0800 I NETWORK <span class="token punctuation">[</span>listener<span class="token punctuation">]</span> Listening on <span class="token number">127.0</span>.0.1
|
||
<span class="token number">2020</span>-07-09T12:20:17.392+0800 I NETWORK <span class="token punctuation">[</span>listener<span class="token punctuation">]</span> waiting <span class="token keyword">for</span> connections on port <span class="token number">27017</span>
|
||
</code></pre></div><p>(4)客户端连接 MongoDB</p> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token builtin class-name">cd</span> /usr/local/mongodb4/bin
|
||
./mongo
|
||
</code></pre></div><blockquote><p><a href="https://github.com/dunwu/linux-tutorial/tree/master/codes/linux/soft" target="_blank" rel="noopener noreferrer">Linux 安装脚本<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <h3 id="设置用户名、密码"><a href="#设置用户名、密码" class="header-anchor">#</a> 设置用户名、密码</h3> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token operator">></span> use admin
|
||
switched to db admin
|
||
<span class="token operator">></span> db.createUser<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"user"</span><span class="token builtin class-name">:</span><span class="token string">"root"</span>,<span class="token string">"pwd"</span><span class="token builtin class-name">:</span><span class="token string">"root"</span>,<span class="token string">"roles"</span>:<span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string">"role"</span><span class="token builtin class-name">:</span><span class="token string">"userAdminAnyDatabase"</span>,<span class="token string">"db"</span><span class="token builtin class-name">:</span><span class="token string">"admin"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
|
||
Successfully added user: <span class="token punctuation">{</span>
|
||
<span class="token string">"user"</span> <span class="token builtin class-name">:</span> <span class="token string">"root"</span>,
|
||
<span class="token string">"roles"</span> <span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
|
||
<span class="token punctuation">{</span>
|
||
<span class="token string">"role"</span> <span class="token builtin class-name">:</span> <span class="token string">"userAdminAnyDatabase"</span>,
|
||
<span class="token string">"db"</span> <span class="token builtin class-name">:</span> <span class="token string">"admin"</span>
|
||
<span class="token punctuation">}</span>
|
||
<span class="token punctuation">]</span>
|
||
<span class="token punctuation">}</span>
|
||
<span class="token operator">></span>
|
||
</code></pre></div><h2 id="备份和恢复"><a href="#备份和恢复" class="header-anchor">#</a> 备份和恢复</h2> <h3 id="数据备份"><a href="#数据备份" class="header-anchor">#</a> 数据备份</h3> <p>在 Mongodb 中,使用 <code>mongodump</code> 命令来备份 MongoDB 数据。该命令可以导出所有数据到指定目录中。</p> <p><code>mongodump</code> 命令可以通过参数指定导出的数据量级转存的服务器。</p> <p>mongodump 命令语法如下:</p> <div class="language- extra-class"><pre class="language-text"><code>mongodump -h dbhost -d dbname -o dbdirectory
|
||
</code></pre></div><ul><li><p>-h:MongDB 所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017</p></li> <li><p>-d:需要备份的数据库实例,例如:test</p></li> <li><p>-o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在 dump 目录下建立一个 test 目录,这个目录里面存放该数据库实例的备份数据。</p></li></ul> <p><code>mongodump</code> 命令可选参数列表如下所示:</p> <table><thead><tr><th style="text-align:left;">语法</th> <th style="text-align:left;">描述</th> <th style="text-align:left;">实例</th></tr></thead> <tbody><tr><td style="text-align:left;">mongodump --host HOST_NAME --port PORT_NUMBER</td> <td style="text-align:left;">该命令将备份所有 MongoDB 数据</td> <td style="text-align:left;">mongodump --host runoob.com --port 27017</td></tr> <tr><td style="text-align:left;">mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY</td> <td style="text-align:left;"></td> <td style="text-align:left;">mongodump --dbpath /data/db/ --out /data/backup/</td></tr> <tr><td style="text-align:left;">mongodump --collection COLLECTION --db DB_NAME</td> <td style="text-align:left;">该命令将备份指定数据库的集合。</td> <td style="text-align:left;">mongodump --collection mycol --db test</td></tr></tbody></table> <p>【示例】备份全量数据</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongodump <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-o</span> test2
|
||
<span class="token punctuation">..</span>.
|
||
<span class="token number">2020</span>-09-11T11:55:58.086+0800 <span class="token keyword">done</span> dumping test.company <span class="token punctuation">(</span><span class="token number">18801</span> documents<span class="token punctuation">)</span>
|
||
<span class="token number">2020</span>-09-11T11:56:00.725+0800 <span class="token punctuation">[</span><span class="token comment">#############...........] test.people 559101/1000000 (55.9%)</span>
|
||
<span class="token number">2020</span>-09-11T11:56:03.725+0800 <span class="token punctuation">[</span><span class="token comment">###################.....] test.people 829496/1000000 (82.9%)</span>
|
||
<span class="token number">2020</span>-09-11T11:56:06.725+0800 <span class="token punctuation">[</span><span class="token comment">#####################...] test.people 884614/1000000 (88.5%)</span>
|
||
<span class="token number">2020</span>-09-11T11:56:08.088+0800 <span class="token punctuation">[</span><span class="token comment">########################] test.people 1000000/1000000 (100.0%)</span>
|
||
<span class="token number">2020</span>-09-11T11:56:08.350+0800 <span class="token keyword">done</span> dumping test.people <span class="token punctuation">(</span><span class="token number">1000000</span> documents<span class="token punctuation">)</span>
|
||
</code></pre></div><p>【示例】备份指定数据库</p> <div class="language-shell extra-class"><pre class="language-shell"><code>mongodump <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> admin <span class="token parameter variable">-o</span> test3
|
||
</code></pre></div><h3 id="数据恢复"><a href="#数据恢复" class="header-anchor">#</a> 数据恢复</h3> <p>mongodb 使用 <code>mongorestore</code> 命令来恢复备份的数据。</p> <p><code>mongorestore</code> 命令语法如下:</p> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token operator">></span> mongorestore <span class="token parameter variable">-h</span> <span class="token operator"><</span>hostname<span class="token operator">></span><span class="token operator"><</span>:port<span class="token operator">></span> <span class="token parameter variable">-d</span> dbname <span class="token operator"><</span>path<span class="token operator">></span>
|
||
</code></pre></div><ul><li><p><code>--host <:port></code>, <code>-h <:port></code>:MongoDB 所在服务器地址,默认为: localhost:27017</p></li> <li><p><code>--db</code> , <code>-d</code> :需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如 test2</p></li> <li><p><code>--drop</code>:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!</p></li> <li><p><code><path></code>:mongorestore 最后的一个参数,设置备份数据所在位置,例如:c:\data\dump\test。你不能同时指定 <code><path></code> 和 <code>--dir</code> 选项,<code>--dir</code> 也可以设置备份目录。</p></li> <li><p><code>--dir</code>:指定备份的目录。你不能同时指定 <code><path></code> 和 <code>--dir</code> 选项。</p></li></ul> <p>【示例】</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongorestore <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">--dir</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">--drop</span>
|
||
<span class="token punctuation">..</span>.
|
||
<span class="token number">2020</span>-09-11T11:46:16.053+0800 finished restoring test.tweets <span class="token punctuation">(</span><span class="token number">966</span> documents, <span class="token number">0</span> failures<span class="token punctuation">)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:18.256+0800 <span class="token punctuation">[</span><span class="token comment">###.....................] test.people 164MB/1.03GB (15.6%)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:21.255+0800 <span class="token punctuation">[</span><span class="token comment">########................] test.people 364MB/1.03GB (34.6%)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:24.256+0800 <span class="token punctuation">[</span><span class="token comment">############............] test.people 558MB/1.03GB (53.0%)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:27.255+0800 <span class="token punctuation">[</span><span class="token comment">###############.........] test.people 700MB/1.03GB (66.5%)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:30.257+0800 <span class="token punctuation">[</span><span class="token comment">###################.....] test.people 846MB/1.03GB (80.3%)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:33.255+0800 <span class="token punctuation">[</span><span class="token comment">######################..] test.people 990MB/1.03GB (94.0%)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:34.542+0800 <span class="token punctuation">[</span><span class="token comment">########################] test.people 1.03GB/1.03GB (100.0%)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:34.543+0800 no indexes to restore
|
||
<span class="token number">2020</span>-09-11T11:46:34.543+0800 finished restoring test.people <span class="token punctuation">(</span><span class="token number">1000000</span> documents, <span class="token number">0</span> failures<span class="token punctuation">)</span>
|
||
<span class="token number">2020</span>-09-11T11:46:34.544+0800 <span class="token number">1000966</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> restored successfully. <span class="token number">0</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> failed to restore.
|
||
</code></pre></div><h2 id="导入导出"><a href="#导入导出" class="header-anchor">#</a> 导入导出</h2> <p><code>mongoimport</code> 和 <code>mongoexport</code> 并不能可靠地保存所有的富文本 BSON 数据类型,因为 JSON 仅能代表一种 BSON 支持的子集类型。因此,数据用这些工具导出导入或许会丢失一些精确程度。</p> <h3 id="导入操作"><a href="#导入操作" class="header-anchor">#</a> 导入操作</h3> <p>在 MongoDB 中,使用 <code>mongoimport</code> 来导入数据。 默认情况下,<code>mongoimport</code> 会将数据导入到本地主机端口 27017 上的 MongoDB 实例中。要将数据导入在其他主机或端口上运行的 MongoDB 实例中,请通过包含 <code>--host</code> 和 <code>--port</code> 选项来指定主机名或端口。 使用 <code>--drop</code> 选项删除集合(如果已经存在)。 这样可以确保该集合仅包含您要导入的数据。</p> <p>语法格式:</p> <div class="language-bash extra-class"><pre class="language-bash"><code>mongoimport <span class="token parameter variable">-h</span> IP <span class="token parameter variable">--port</span> 端口 <span class="token parameter variable">-u</span> 用户名 <span class="token parameter variable">-p</span> 密码 <span class="token parameter variable">-d</span> 数据库 <span class="token parameter variable">-c</span> 表名 <span class="token parameter variable">--type</span> 类型 <span class="token parameter variable">--headerline</span> <span class="token parameter variable">--upsert</span> <span class="token parameter variable">--drop</span> 文件名
|
||
</code></pre></div><p>【示例】导入表数据</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongoimport <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">-c</span> book <span class="token parameter variable">--drop</span> test/book.dat
|
||
<span class="token number">2020</span>-09-11T10:53:56.359+0800 connected to: mongodb://127.0.0.1:27017/
|
||
<span class="token number">2020</span>-09-11T10:53:56.372+0800 dropping: test.book
|
||
<span class="token number">2020</span>-09-11T10:53:56.628+0800 <span class="token number">431</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> imported successfully. <span class="token number">0</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> failed to import.
|
||
</code></pre></div><p>【示例】从 json 文件中导入表数据</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongoimport <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">-c</span> student <span class="token parameter variable">--upsert</span> test/student.json
|
||
<span class="token number">2020</span>-09-11T11:02:55.907+0800 connected to: mongodb://127.0.0.1:27017/
|
||
<span class="token number">2020</span>-09-11T11:02:56.068+0800 <span class="token number">200</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> imported successfully. <span class="token number">0</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> failed to import.
|
||
</code></pre></div><p>【示例】从 csv 文件中导入表数据</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongoimport <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">-c</span> product <span class="token parameter variable">--type</span> csv <span class="token parameter variable">--headerline</span> test/product.csv
|
||
<span class="token number">2020</span>-09-11T11:07:49.788+0800 connected to: mongodb://127.0.0.1:27017/
|
||
<span class="token number">2020</span>-09-11T11:07:51.051+0800 <span class="token number">11</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> imported successfully. <span class="token number">0</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> failed to import.
|
||
</code></pre></div><p>【示例】导入部分表字段数据</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongoimport <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">-c</span> product <span class="token parameter variable">--type</span> json <span class="token parameter variable">--upsertFields</span> name,price test/product.json
|
||
<span class="token number">2020</span>-09-11T11:14:05.410+0800 connected to: mongodb://127.0.0.1:27017/
|
||
<span class="token number">2020</span>-09-11T11:14:05.612+0800 <span class="token number">11</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> imported successfully. <span class="token number">0</span> document<span class="token punctuation">(</span>s<span class="token punctuation">)</span> failed to import.
|
||
</code></pre></div><h3 id="导出操作"><a href="#导出操作" class="header-anchor">#</a> 导出操作</h3> <p>语法格式:</p> <div class="language-shell extra-class"><pre class="language-shell"><code>mongoexport <span class="token parameter variable">-h</span> <span class="token operator"><</span>IP<span class="token operator">></span> <span class="token parameter variable">--port</span> <span class="token operator"><</span>端口<span class="token operator">></span> <span class="token parameter variable">-u</span> <span class="token operator"><</span>用户名<span class="token operator">></span> <span class="token parameter variable">-p</span> <span class="token operator"><</span>密码<span class="token operator">></span> <span class="token parameter variable">-d</span> <span class="token operator"><</span>数据库<span class="token operator">></span> <span class="token parameter variable">-c</span> <span class="token operator"><</span>表名<span class="token operator">></span> <span class="token parameter variable">-f</span> <span class="token operator"><</span>字段<span class="token operator">></span> <span class="token parameter variable">-q</span> <span class="token operator"><</span>条件导出<span class="token operator">></span> <span class="token parameter variable">--csv</span> <span class="token parameter variable">-o</span> <span class="token operator"><</span>文件名<span class="token operator">></span>
|
||
</code></pre></div><ul><li><code>-f</code>:导出指字段,以逗号分割,<code>-f name,email,age</code> 导出 name,email,age 这三个字段</li> <li><code>-q</code>:可以根查询条件导出,<code>-q '{ "uid" : "100" }'</code> 导出 uid 为 100 的数据</li> <li><code>--csv</code>:表示导出的文件格式为 csv 的,这个比较有用,因为大部分的关系型数据库都是支持 csv,在这里有共同点</li></ul> <p>【示例】导出整张表</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongoexport <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">-c</span> product <span class="token parameter variable">-o</span> test/product.dat
|
||
<span class="token number">2020</span>-09-11T10:44:23.161+0800 connected to: mongodb://127.0.0.1:27017/
|
||
<span class="token number">2020</span>-09-11T10:44:23.177+0800 exported <span class="token number">11</span> records
|
||
</code></pre></div><p>【示例】导出表到 json 文件</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongoexport <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">-c</span> product <span class="token parameter variable">--type</span> json <span class="token parameter variable">-o</span> test/product.json
|
||
<span class="token number">2020</span>-09-11T10:49:52.735+0800 connected to: mongodb://127.0.0.1:27017/
|
||
<span class="token number">2020</span>-09-11T10:49:52.750+0800 exported <span class="token number">11</span> records
|
||
</code></pre></div><p>【示例】导出表中部分字段到 csv 文件</p> <div class="language-shell extra-class"><pre class="language-shell"><code>$ mongoexport <span class="token parameter variable">-h</span> <span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span> <span class="token number">27017</span> <span class="token parameter variable">-d</span> <span class="token builtin class-name">test</span> <span class="token parameter variable">-c</span> product <span class="token parameter variable">--type</span> csv <span class="token parameter variable">-f</span> name,price <span class="token parameter variable">-o</span> test/product.csv
|
||
<span class="token number">2020</span>-09-11T10:47:33.160+0800 connected to: mongodb://127.0.0.1:27017/
|
||
<span class="token number">2020</span>-09-11T10:47:33.176+0800 exported <span class="token number">11</span> records
|
||
</code></pre></div><h2 id="参考资料"><a href="#参考资料" class="header-anchor">#</a> 参考资料</h2> <ul><li><a href="https://www.mongodb.com/" target="_blank" rel="noopener noreferrer">MongoDB 官网<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/mongodb/mongo" target="_blank" rel="noopener noreferrer">MongoDB Github<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://university.mongodb.com/" target="_blank" rel="noopener noreferrer">MongoDB 官方免费教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.runoob.com/mongodb/mongodb-tutorial.html" target="_blank" rel="noopener noreferrer">MongoDB 教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/dunwu/db-tutorial/edit/master/docs/12.数据库/04.文档数据库/01.MongoDB/20.MongoDB运维.md" target="_blank" rel="noopener noreferrer">📝 帮助改善此页面!</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/db-tutorial/tags/?tag=%E6%95%B0%E6%8D%AE%E5%BA%93" title="标签">#数据库</a><a href="/db-tutorial/tags/?tag=%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93" title="标签">#文档数据库</a><a href="/db-tutorial/tags/?tag=MongoDB" title="标签">#MongoDB</a><a href="/db-tutorial/tags/?tag=%E8%BF%90%E7%BB%B4" title="标签">#运维</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2024/10/09, 07:16:02</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/db-tutorial/pages/ad08f5/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">MongoDB 分片</div></a> <a href="/db-tutorial/pages/451b73/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">Redis 面试总结</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
|
||
←
|
||
<a href="/db-tutorial/pages/ad08f5/" class="prev">MongoDB 分片</a></span> <span class="next"><a href="/db-tutorial/pages/451b73/">Redis 面试总结</a>→
|
||
</span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/db-tutorial/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/db-tutorial/pages/b59ba2/"><div>
|
||
HBase Java API 管理功能
|
||
<!----></div></a> <span class="date">04-13</span></dt></dl><dl><dd>02</dd> <dt><a href="/db-tutorial/pages/ce5ca0/"><div>
|
||
HBase Java API 其他高级特性
|
||
<!----></div></a> <span class="date">03-31</span></dt></dl><dl><dd>03</dd> <dt><a href="/db-tutorial/pages/c8cfeb/"><div>
|
||
HBase 数据模型
|
||
<!----></div></a> <span class="date">03-16</span></dt></dl> <dl><dd></dd> <dt><a href="/db-tutorial/archives/" class="more">更多文章></a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:forbreak@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/dunwu" title="GitHub" target="_blank" class="iconfont icon-github"></a></div>
|
||
Theme by
|
||
<a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a>
|
||
| Copyright © 2019-2024
|
||
<span>钝悟(dunwu) | CC-BY-SA-4.0</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
|
||
跟随系统
|
||
</li><li class="iconfont icon-rijianmoshi">
|
||
浅色模式
|
||
</li><li class="iconfont icon-yejianmoshi">
|
||
深色模式
|
||
</li><li class="iconfont icon-yuedu">
|
||
阅读模式
|
||
</li></ul></div></div> <!----> <!----> <div class="custom-html-window custom-html-window-rb" style="display:;"><div class="custom-wrapper"><span class="close-but">×</span> <div>
|
||
<div class="wwads-cn wwads-vertical windowRB" data-id="261" style="max-width:160px;
|
||
min-width: auto;min-height:auto;"></div>
|
||
<style>
|
||
.windowRB{ padding: 0;}
|
||
.windowRB .wwads-img{margin-top: 10px;}
|
||
.windowRB .wwads-content{margin: 0 10px 40px 10px;}
|
||
.custom-html-window-rb .close-but{
|
||
display: none;
|
||
}
|
||
</style>
|
||
</div></div></div></div><div class="global-ui"><div></div></div></div>
|
||
<script src="/db-tutorial/assets/js/app.be3f2e92.js" defer></script><script src="/db-tutorial/assets/js/2.aded268b.js" defer></script><script src="/db-tutorial/assets/js/42.14a20b0f.js" defer></script>
|
||
</body>
|
||
</html>
|