使用了大半年的 mongodb ,最近在公司的新項(xiàng)目中應(yīng)用,在 mac 上安裝 mongodb 時(shí)發(fā)現(xiàn)始終安裝不了,一直在報(bào)下面這樣的錯(cuò)誤:
brew install mongodb
升級(jí) brew 也不行,這個(gè) mac 機(jī)上從未安裝過 mongodb,但從錯(cuò)誤信息中提示卻是要卸載它,真是醉了。
從2019年9月2日開始 ,HomeBrew 也從核心倉庫 (#43770) 當(dāng)中移除了mongodb 模塊
不過,幸運(yùn)的是 mongodb 團(tuán)隊(duì)還在維護(hù)社區(qū)版的 Homebrew,最后還是從Stack Overflow 上查找到答案:
為了搞清楚這些是啥意思,查看了 homebrew-brew gitHub。
brew services start mongodb-community ,提示 “Service `mongodb-community` already started”,說明服務(wù)啟動(dòng)成功
mongodb-community 命令區(qū)別 mongodb
文件路徑:
配置文件:/usr/local/etc/mongod.conf
日志目錄路徑:/usr/local/var/log/mongodb
數(shù)據(jù)目錄路徑:/usr/local/var/mongodb
這樣 MongoDB 服務(wù)就安裝和啟動(dòng)好了。
將 MongoDB 下載到本地,mongodb-osx-ssl-x86_64-4.0.13.tgz 我下載的是這個(gè)版本,解壓后,新建目錄,將解壓后的文件存放在里面。
我的目錄為:/Volumes/code/localhost/node/mongodb/bin
data
在任意盤符根目錄下創(chuàng)建一個(gè) data
目錄,用來存放數(shù)據(jù)庫文件。 mongoDB
會(huì)自動(dòng)把自己安裝位置的盤符根目錄下的 data
文件夾作為自己的數(shù)據(jù)存儲(chǔ)目錄,這里也可以直接在安裝位置所在盤符創(chuàng)建,我是在 bin 目錄下創(chuàng)建的 data 目錄。
新開一個(gè) shell,指定 db 路徑:
進(jìn)入 /Volumes/code/localhost/node/mongodb/bin 目錄 ,輸入 mongod,會(huì)看到 :
MongoDB starting : pid=942 port=27017 dbpath=/data/db 64-bit
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
。。。
說明 db 啟動(dòng)成功了!!
如果 data 目錄在其它位置,需要指定 data 目錄路徑,輸入 mongod --dbpath 路徑
在 server.js 中,進(jìn)行配置:
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('數(shù)據(jù)庫連接失??!'); return; }; console.log(db);});
在 package.json 中,進(jìn)行配置:
"scripts": { "server": "node server.js", "start": "nodemon server.js"},
const dburl = "mongodb://127.0.0.1:27017/local";
默認(rèn)情況下,db 啟動(dòng)成功后,本地會(huì)有三個(gè)表 admin / config / local
> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB>
> web@1.0.0 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] } }}
聯(lián)系客服