使用了大半年的 mongodb ,最近在公司的新專案中應用,在 mac 上安裝 mongodb 時發現始終安裝不了,一直在報下面這樣的錯誤:
brew install mongodb

升級 brew 也不行,這個 mac 機上從未安裝過 mongodb,但從錯誤資訊中提示卻是要卸載它,真是醉了,
從2019年9月2日開始 ,HomeBrew 也從核心倉庫 (#43770) 當中移除了mongodb 模塊
不過,幸運的是 mongodb 團隊還在維護社區版的 Homebrew,最后還是從Stack Overflow 上查找到答案:

為了搞清楚這些是啥意思,查看了 homebrew-brew gitHub,
- 在安裝社區版前要先執行:brew tap mongodb/brew 這個程序有點久,我裝過可能需要30分鐘左右,
- 安裝最新社區版:brew reinstall mongodb-community
- 啟動 mongodb-community 服務:
brew services start mongodb-community ,提示 “Service `mongodb-community` already started”,說明服務啟動成功
mongodb-community 命令區別 mongodb
- 啟動服務:brew services start mongodb-community
- 重啟服務: brew services restart mongodb-community
- 停止服務:brew services stop mongodb-community
- 安裝某個版本:brew install mongodb-community@x.x.x
- 手動啟動服務:mongod --config /usr/local/etc/mongod.conf
檔案路徑:
-
組態檔:/usr/local/etc/mongod.conf 日志目錄路徑:/usr/local/var/log/mongodb資料目錄路徑:/usr/local/var/mongodb
這樣 MongoDB 服務就安裝和啟動好了,
將 MongoDB 下載到本地,mongodb-osx-ssl-x86_64-4.0.13.tgz 我下載的是這個版本,解壓后,新建目錄,將解壓后的檔案存放在里面,
我的目錄為:/Volumes/code/localhost/node/mongodb/bin
- 創建存盤資料庫檔案
data
在任意盤符根目錄下創建一個 data 目錄,用來存放資料庫檔案, mongoDB 會自動把自己安裝位置的盤符根目錄下的 data 檔案夾作為自己的資料存盤目錄,這里也可以直接在安裝位置所在盤符創建,我是在 bin 目錄下創建的 data 目錄,
新開一個 shell,指定 db 路徑:
進入 /Volumes/code/localhost/node/mongodb/bin 目錄 ,輸入 mongod,會看到 :
MongoDB starting : pid=942 port=27017 dbpath=/data/db 64-bit
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
,,,
說明 db 啟動成功了!!
如果 data 目錄在其它位置,需要指定 data 目錄路徑,輸入 mongod --dbpath 路徑
- 連接資料庫 npm run start
在 server.js 中,進行配置:
const mongoose = require('mongoose');const mongoClient = require('mongodb').MongoClient;// dbconst dburl = "mongodb://127.0.0.1:27017/local";mongoClient.connect(dburl, (err, db) => { if (err) { console.log('資料庫連接失敗!'); return; }; console.log(db);});在 package.json 中,進行配置:
"scripts": { "server": "node server.js", "start": "nodemon server.js"},
const dburl = "mongodb://127.0.0.1:27017/local"; 默認情況下,db 啟動成功后,本地會有三個表 admin / config / local
> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB>
- 查看 db 配置資訊 npm run start ,成功的啟動 服務并連接 db
> [email protected] start /Volumes/code/localhost/node/web> nodemon server.js[nodemon] 1.19.4[nodemon] to restart at any time, enter `rs`[nodemon] watching dir(s): *.*[nodemon] watching extensions: js,mjs,json[nodemon] starting `node server.js`(node:1009) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.Successful! 訪問地址為 http://127.0.0.1:3000MongoClient { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, s: { url: 'mongodb://127.0.0.1:27017/local', options: { servers: [Array], caseTranslate: true, dbName: 'local', socketTimeoutMS: 360000, connectTimeoutMS: 30000, retryWrites: true, useRecoveryToken: true, readPreference: [ReadPreference], promiseLibrary: [Function: Promise] }, promiseLibrary: [Function: Promise], dbCache: Map {}, sessions: Set {}, writeConcern: undefined, namespace: MongoDBNamespace { db: 'admin', collection: undefined } }, topology: Server { _events: [Object: null prototype] { serverOpening: [Function], serverDescriptionChanged: [Function], serverHeartbeatStarted: [Function], serverHeartbeatSucceeded: [Function], serverHeartbeatFailed: [Function], serverClosed: [Function], topologyOpening: [Function], topologyClosed: [Function], topologyDescriptionChanged: [Function], commandStarted: [Function], commandSucceeded: [Function], commandFailed: [Function], joined: [Function], left: [Function], ping: [Function], ha: [Function], authenticated: [Function], error: [Function], timeout: [Function], close: [Function], parseError: [Function], open: [Function], fullsetup: [Function], all: [Function], reconnect: [Function] }, _eventsCount: 25, _maxListeners: Infinity, clientInfo: { driver: [Object], os: [Object], platform: 'Node.js v12.11.1, LE' }, s: { coreTopology: [Server], sCapabilities: null, clonedOptions: [Object], reconnect: true, emitError: true, poolSize: 5, storeOptions: [Object], store: [Store], host: '127.0.0.1', port: 27017, options: [Object], sessionPool: [ServerSessionPool], sessions: Set {}, promiseLibrary: [Function: Promise] } }}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/32643.html
標籤:NoSQL
