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


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

brew install mongodb

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

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


 

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

 

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

  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

文件路徑:

  1.  配置文件:/usr/local/etc/mongod.conf
  2.  日志目錄路徑:/usr/local/var/log/mongodb
  3.   數據目錄路徑:/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;

// 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]
    }
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM