MongoDB 不在開源了,使用 Homebrew 安裝只能玩社區版了

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

brew install mongodbgit

升級 brew 也不行,這個 mac 機上從未安裝過 mongodb,但從錯誤信息中提示倒是要卸載它,真是醉了。github

從2019年9月2日開始 ,HomeBrew 也從核心倉庫 (#43770) 當中移除了mongodb 模塊web


 

不過,幸運的是 mongodb 團隊還在維護社區版的 Homebrew,最後仍是從Stack Overflow 上查找到答案:mongodb

 

爲了搞清楚這些是啥意思,查看了 homebrew-brew gitHub。shell

  1. 在安裝社區版前要先執行:brew tap mongodb/brew  這個過程有點久,我裝過可能須要30分鐘左右。
  2. 安裝最新社區版:brew reinstall mongodb-community  
  3. 啓動 mongodb-community 服務:brew services start mongodb-community ,提示 「Service `mongodb-community` already started」,說明服務啓動成功

mongodb-community 命令區別 mongodb數據庫

  1. 啓動服務:brew services start mongodb-community
  2. 重啓服務:  brew services restart mongodb-community
  3. 中止服務:brew services stop mongodb-community
  4. 安裝某個版本:brew install mongodb-community@x.x.x
  5. 手動啓動服務:mongod --config /usr/local/etc/mongod.conf

文件路徑:npm

  1.  配置文件:/usr/local/etc/mongod.conf
  2.  日誌目錄路徑:/usr/local/var/log/mongodb
  3.   數據目錄路徑:/usr/local/var/mongodb

這樣 MongoDB 服務就安裝和啓動好了。json

 


 

將 MongoDB 下載到本地,mongodb-osx-ssl-x86_64-4.0.13.tgz 我下載的是這個版本,解壓後,新建目錄,將解壓後的文件存放在裏面。promise

個人目錄爲:/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;

// db
const 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 dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> 

 

  • 查看 db 配置信息  npm run start ,成功的啓動 服務並鏈接 db
> 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:3000
MongoClient {
  _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]
    }
  }
}
相關文章
相關標籤/搜索