MongoDB最常見的10個錯誤說法

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我加入MongoDB還不到一年,但學到了很多東西。在面試這家公司前,我從未真正使用過MongoDB,儘管我聽過一些有關它的演講,並且它的簡單易用給我留下深刻印象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但和大多數人一樣,我也聽說過這些可怕的說法:“它不支持關係模型。如果你想存儲文檔,那沒問題,但如果以後想要進行聚合該怎麼辦?你將被困在這個錯誤的數據庫裏!而且不管怎樣,它不支持事務!”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我開始查找這些信息的來源,才意識到兩個問題:首先,這些帖子大多是十年前的,所以他們所說的是一個剛剛推出三年的產品,而不是今天這個成熟、經過實戰考驗的數據庫。其次,他們說的幾乎都不再是對的,而且在某些情況下從來都沒有對過。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以我決定做一個演講(並寫了這篇文章)來逐一澄清這些錯誤的說法。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思0:MongoDB是Web可伸縮的"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"YouTube上有個"},{"type":"link","attrs":{"href":"https:\/\/www.youtube.com\/watch?v=b2F-DItXtZs","title":"","type":null},"content":[{"type":"text","text":"視頻"}]},{"type":"text","text":",視頻裏有幾隻狗(看起來應該是狗吧?)。你可能已經看到了——其中一隻是盲目的新技術追隨者,它盲目相信MongoDB,沒有真正加以瞭解。另一隻狗比較理性,第一隻狗的盲從讓它感到沮喪。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/25\/254c0daa80054060a60457dbd067d7a6.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在加入MongoDB第一天,我的一個朋友給我發了這個視頻鏈接,但此前,我已經看過了。看看日期,這段視頻已經流傳十多年了。當時真的很有趣,但現在呢?裏面幾乎所有的東西都過時了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事實上,MongoDB公司很多人的T恤或筆記本電腦貼紙上都有這個角色,它是MongoDB的非官方吉祥物。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"MongoDB到底是什麼?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在開始講“MongoDB不是什麼”之前,我們來總結一下MongoDB到底是什麼。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB是一個分佈式文檔數據庫。集羣(我們稱之爲複本集)大多是自我管理的——當集羣中的每臺機器都知道了其他服務器的所在,如果其中一個節點宕機或網絡出現問題,它們會自動加以處理。如果其中一臺機器被關閉或發生崩潰,其他機器會接管。要實現仲裁,羣集中至少需要有3個節點。集羣中的每臺服務器都有數據庫所有數據的完整副本。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/af\/af46a04ae291dab34af1566663b52613.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"集羣是爲了冗餘,而不是爲了可伸縮性。所有的客戶端通常只連接到一臺服務器——被選舉出來的主服務器,它負責執行查詢和更新,並將數據變更傳輸到備用機器。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可以直接連接到備用服務器做一些有趣的事情,比如運行分析查詢。但一般來說,直接連接到備用節點意味着你可能會遇到一些舊數據,所以不應該連接到備用節點,除非你可以接受舊數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"講完“分佈式”,接下來講講“文檔數據庫”。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB與傳統關係型數據庫的不同之處在於,它不是將數據保存成扁平的行,然後存儲在數據庫的表中,而是在文檔中存儲分層結構的數據——基本上與JSON對象類似。文檔存儲在集合中,集合實際上就是一堆文檔。每個文檔可以具有與集合中其他文檔不同的結構或模式。你還可以(而且應該)根據將要運行的查詢類型和存儲數據對集合中的文檔進行索引。如果你想要確保集合中的所有文檔都遵循一種結構,可以將JSON模式應用於集合,作爲驗證器。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"{\n '_id': ObjectId('573a1390f29313caabcd4135'),\n 'title': 'Blacksmith Scene',\n 'fullplot': 'A stationary camera looks at a large anvil with a\n blacksmith behind it and one on either side.',\n 'cast': ['Charles Kayser', 'John Ott'],\n 'countries': ['USA'],\n 'directors': ['William K.L. Dickson'],\n 'genres': ['Short'],\n 'imdb': {'id': 5, 'rating': 6.2, 'votes': 1189},\n 'released': datetime.datetime(1893, 5, 9, 0, 0),\n 'runtime': 1,\n 'year': 1893\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面的文檔是一個例子,顯示了1893年的一部電影的信息!文檔是用"},{"type":"link","attrs":{"href":"https:\/\/docs.mongodb.com\/drivers\/python","title":"","type":null},"content":[{"type":"text","text":"PyMongo"}]},{"type":"text","text":"驅動程序獲取的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意,這裏有些值是數組,比如“countries”和“cast”,而有些值是對象(我們稱它們爲子文檔)。這就是MongoDB文檔的層次結構——它們不像關係數據庫中的錶行那樣扁平。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還需要注意的是,它還包含一個原生Python datetime類型的“released”字段,以及一個特殊的ObjectId類型的值。或許這個不是真正意義上的JSON文檔?我們待會再來說這個……"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面的文檔是一個例子,顯示了1893年的一部電影的信息!文檔是用"},{"type":"link","attrs":{"href":"https:\/\/docs.mongodb.com\/drivers\/python","title":"","type":null},"content":[{"type":"text","text":"PyMongo"}]},{"type":"text","text":"驅動程序獲取的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意,這裏有些值是數組,比如“countries”和“cast”,而有些值是對象(我們稱它們爲子文檔)。這就是MongoDB文檔的層次結構——它們不像關係數據庫中的錶行那樣扁平。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還需要注意的是,它還包含一個原生Python datetime類型的“released”字段,以及一個特殊的ObjectId類型的值。或許這個不是真正意義上的JSON文檔?我們待會再來說這個……"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思1:MongoDB的版本是3.2"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你在Debian Stretch上使用"},{"type":"codeinline","content":[{"type":"text","text":"apt get mongodb"}]},{"type":"text","text":"安裝MongoDB,它會安裝3.2版本。不幸的是,這個版本已經是5年前的了!從那時起,已經發布了5個主要版本,它們包含大量的新特性,以及安全性、性能和可伸縮性方面的改進。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB的當前版本是4.4(截至2020年末)。如果你要安裝,應該安裝"},{"type":"link","attrs":{"href":"https:\/\/www.mongodb.com\/try\/download\/community","title":"","type":null},"content":[{"type":"text","text":"MongoDB社區服務器版"}]},{"type":"text","text":",但最好請先了解一下"},{"type":"link","attrs":{"href":"https:\/\/www.mongodb.com\/cloud\/atlas","title":"","type":null},"content":[{"type":"text","text":"MongoDB Atlas"}]},{"type":"text","text":",也就是我們的託管數據庫即服務(DBaaS)產品!"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思2:MongoDB是一個JSON數據庫"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你肯定聽說過MongoDB是一個JSON數據庫的說法,特別是如果你最近訪問過MongoDB的主頁。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/f2\/f228c2b2283d3513aa6d5f60785bbc1e.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不過,正如前面提到的,MongoDB不是一個JSON數據庫。它支持額外的數據類型,比如對象、原生日期對象、更多的數字類型、地理原語和高效的二進制類型等等。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB實際上是一個BSON數據庫。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這似乎是一個微不足道的區別,但卻很重要。與基於文本的格式相比,它在存儲、傳輸和遍歷方面更高效,而且比JSON支持更多的數據類型。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB存儲的是BSON文檔"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用於查找的實際上也是BSON文檔"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結果以BSON文檔的形式輸出"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"BSON甚至是MongoDB的有線協議"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你習慣了使用JSON,可以將MongoDB看成是JSON數據庫,這就是爲什麼我們有時會這樣描述它!等你使用MongoDB一段時間,就會開始對BSON的優勢大加讚許。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思3:MongoDB不支持事務"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一些第三方文章將MongoDB描述成BASE數據庫。BASE是指“基本可用、軟狀態、最終一致”。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但這不是真的,從來都不是!MongoDB從來都不是“最終一致”的。對主文檔的讀寫是強一致性的,對單個文檔的更新始終是原子的。軟狀態是指需要持續不斷的更新數據,否則數據就會過期,但MongoDB並非如此。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後,如果太多的節點不可用,無法達成仲裁,MongoDB將進入只讀狀態(降低可用性)。這是有意這麼設計的,因爲這樣可以確保在出現問題時保持一致性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB是一個ACID數據庫。它支持原子性、一致性、隔離性和持久性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對單個文檔的更新始終是原子的,從4.0版本開始,MongoDB也支持跨多個文檔和集合的事務。從4.2開始,甚至支持分片集羣的跨分片事務。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"雖然MongoDB支持事務,但在使用它時仍然要謹慎。事務是以性能爲代價的,而且由於MongoDB支持豐富的分層文檔,如果你的模式設計正確,就沒有必要經常跨多個文檔更新數據。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思4:MongoDB不支持關係"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關於MongoDB另一個過時的說法是,你不能在集合或文檔之間建立關係。你可以使用"},{"type":"link","attrs":{"href":"https:\/\/docs.mongodb.com\/manual\/core\/aggregation-pipeline\/","title":"","type":null},"content":[{"type":"text","text":"聚合管道"}]},{"type":"text","text":"來連接文檔,它們非常強大,你可以使用直觀的查詢模型來查詢和轉換來自多個集合的數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB從2.2開始就支持查找(連接)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面的示例文檔演示了將訂單集合和庫存集合連接之後,在返回的訂單文檔中包含了嵌入到數組的庫存文檔。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/2d\/2df6e8dffee1e514467cb561ec030c61.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我認爲,能夠在返回的主文檔中嵌入相關文檔比在關係連接中爲每個關係複製行更直觀。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思5:MongoDB完全是關於分片"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可能聽人說過分片是MongoDB的一個很酷的特性。這絕對是MongoDB的一個很酷的核心特性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分片是指將數據分成片段,並將每個片段放到不同的複製集或集羣中。這是一種處理龐大數據集的技術。MongoDB可以自動將數據和請求發送到正確的副本集,並將來自多個分片的結果合併起來。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但分片存在一個很根本的問題。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我在前面提到過,副本集的最小節點數是3,這樣才能實現仲裁。在進行分片時,至少需要兩個副本集,因此至少需要6臺服務器。除此之外,你還需要運行多個mongos實例。mongos是分片集羣的代理,負責路由請求和響應。要實現高可用性,至少需要兩個mongos實例。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/7c\/7c8b3419746852cd721947c50c2758f5.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,這意味着一個分片集羣最少需要8臺服務器,每增加一個分片,就會增加至少3臺服務器。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分片集羣還會使數據變得難以管理,並且會限制可執行的查詢類型。如果你確實需要進行分片,那麼分片對你來說確實是有用的,但更簡單的辦法是直接升級硬件,通常這樣更便宜、更容易。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據的伸縮主要與RAM有關,所以如果可以的話,請購買更多的RAM。如果CPU是瓶頸,那就升級CPU。如果磁盤容量是個問題,那就買一個更大的磁盤。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB的分片功能一直都在,如果對內存的需求超過了一臺計算機的容量,可以使用分片。你還可以使用分片功能做一些有趣的事情,比如地理位置固定,可以將用戶數據存儲在離用戶位置更近的地方,從而減少延遲。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在決定使用分片進行數據伸縮時,應該先考慮一下硬件升級是否是一個更有效的選擇。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可以看看MongoDB Atlas,MongoDB的託管數據庫即服務(DBaaS)產品。MongoDB Atlas不僅可以在你選擇的雲平臺上託管數據庫,還可以根據需要伸縮數據庫,保持可用性,同時保持較低的成本。它將負責處理備份和冗餘,還提供了額外的功能,如圖表、文本搜索、無服務器功能等等。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思6:MongoDB不安全"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關於MongoDB的另一個誤解,是說它不安全。我個人認爲,這對MongoDB來說是一種不公平的評判,但不可否認的是,在互聯網上有很多不安全的MongoDB實例,而且發生了幾起涉及數據泄露的事故。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從歷史上看,這是由於MongoDB的發行方式導致的。一些Linux發行版曾經在發行MongoDB時啓用網絡並禁用身份驗證。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,如果你沒有防火牆,或者打開了防火牆上的MongoDB端口,那麼你的數據就有可能被竊取。現如今,機器人很可能會找到你的數據,把你的數據加密,然後再加上一個文檔向你勒索比特幣。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我想說的是,如果你將一個不受保護的數據庫服務器放到互聯網上,那就是你的錯——這種情況肯定已經發生過很多次了,而且肯定有辦法不讓事態變得如此糟糕。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們對MongoDB 3.6的一些默認設置進行了修復。MongoDB不會連接到網絡,除非啓用身份驗證,或者你給服務器設置了一個特定標誌來覆蓋這個行爲。所以,如果使用不當,仍然可能不安全。不管怎樣,你至少要先閱讀一下手冊。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除此之外,在安全方面,MongoDB還使用了行業標準,例如使用TLS對傳輸中的數據進行加密,使用SCRAM-SHA-256對用戶進行安全認證。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB還提供了客戶端字段級加密(FLE)功能,數據在客戶端和傳輸過程中都是經過加密的。也就是說,如果第三方拿到數據庫服務器的訪問權限,但沒有客戶端訪問權限,也無法讀取加密的數據。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思7:MongoDB會丟失數據"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這在Hacker News上是一個很經典的問題。有人發帖子分享他們如何成功地用MongoDB開發了一些東西,然後立刻有人評論說:“我知道這個人曾經把MongoDB裏所有的數據都弄丟了,所以請避免同樣的事情發生。”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你繼續聯繫這些用戶,並讓他們提交問題,他們就消失了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB被廣泛應用於那些非常關心數據保存問題的行業,從摩根士丹利、巴克萊和匯豐等大型銀行,到福布斯等大型出版品牌。我們從未收到過有關大規模數據丟失的報告。如果你確實有關於數據丟失的第一手故事要講,請向我們"},{"type":"link","attrs":{"href":"https:\/\/jira.mongodb.org\/","title":"","type":null},"content":[{"type":"text","text":"反饋"}]},{"type":"text","text":",無論你是付費企業用戶還是開源用戶,我們都會認真對待。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"迷思8:MongoDB只是一個玩具"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看到這裏,你就已經知道這是一個迷思了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MongoDB是一個用於存儲文檔的通用數據庫,它可以安全、原子地更新數據,可以與其他文檔連接,提供了豐富、強大和直觀的查詢語言。當你的數據規模大到一臺機器裝不下,可以進行分片,它還支持高級功能,例如用於保護敏感數據的客戶端字段級別加密、可以讓應用程序立即響應變更數據的"},{"type":"link","attrs":{"href":"https:\/\/docs.mongodb.com\/manual\/changeStreams\/","title":"","type":null},"content":[{"type":"text","text":"變更流"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文鏈接:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/developer.mongodb.com\/article\/everything-you-know-is-wrong","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/developer.mongodb.com\/article\/everything-you-know-is-wrong"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章