Mac安装MongoDB教程

7,285 阅读6分钟

一、mac安装MongoDB

使用的是mac,所以直接使用homebrew安装了

1、打开控制台,输入下列代码安装

brew install mongodb

如果出现一下代码,即表示安装到本地完毕

Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/mongodb-4.0.3_1.mojave.bott
Already downloaded: /Users/gaoshang/Library/Caches/Homebrew/downloads/0496c82a6b2d3e5970ab2cbc97c3a19acba625a8ab1e008b9ad35b41e2a507a7--mongodb-4.0.3_1.mojave.bottle.tar.gz
==> Pouring mongodb-4.0.3_1.mojave.bottle.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
  brew services start mongodb
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf
==> Summary
🍺  /usr/local/Cellar/mongodb/4.0.3_1: 18 files, 258.1MB

mongo的配置文件位置:/usr/local/etc/mongod.conf

mongodb的安装位置默认在homebrew的应用安装位置:/usr/local/Cellar/mongodb/4.0.3_1

2、在环境变量中配置mongo的bin路径

输入以下代码,进入环境配置文件中

vim .bash_profile

输入i 然后拷贝下面的代码进去 然后输入:wq 退出

export PATH=/usr/local/Cellar/mongodb/4.0.3_1/bin:${PATH}

根据上面👆安装的版本在上述代码中改一下自己的版本号,以免后续版本号错误找不到

3、在本机home目录新建两个文件夹/data/db并设置权限

因为mongo启动需要找到这个文件夹存数据

安装位置自己知道就好不需要非在home目录下,只要后续记住自己创建的文件夹位置就好

输入以下代码 并输入本机密码

sudo mkdir -p /data/db

输入以下代码 并输入本机密码 给该文件夹分配权限(以下命令分配所有权限)

sudo chown /data/db

4、启动mongodb服务端

输入以下命令

mongod

如果出现以下代码 即表示成功 成功后不要关闭窗口

2019-08-10T22:46:23.715+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten] MongoDB starting : pid=85906 port=27017 dbpath=/data/db 64-bit host=JackGaoMacBook-Pro.local
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten] db version v4.0.3
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten] git version: 7ea530946fa7880364d88c8d8b6026bbc9ffa48c
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten] allocator: system
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten] modules: none
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten] build environment:
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten]     distarch: x86_64
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2019-08-10T22:46:23.726+0800 I CONTROL  [initandlisten] options: {}
2019-08-10T22:46:23.726+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2019-08-10T22:46:24.219+0800 I STORAGE  [initandlisten] WiredTiger message [1565448384:219837][85906:0x11740b5c0], txn-recover: Set global recovery timestamp: 0
2019-08-10T22:46:24.243+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-08-10T22:46:24.279+0800 I CONTROL  [initandlisten] 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2019-08-10T22:46:24.282+0800 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: 4a833e57-29c7-430a-9695-f00fb3c91350
2019-08-10T22:46:24.313+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2019-08-10T22:46:24.318+0800 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 149287e1-cad0-40c4-96ef-c48e3383e365
2019-08-10T22:46:24.352+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-08-10T22:46:24.356+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-08-10T22:46:24.356+0800 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: b6e9681f-2b97-48c9-84e4-8f73cf853ca3
2019-08-10T22:46:24.406+0800 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-08-10T22:46:24.406+0800 I INDEX    [LogicalSessionCacheRefresh] 	 building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-08-10T22:46:24.412+0800 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs
2019-08-10T22:46:33.296+0800 I NETWORK  [listener] connection accepted from 127.0.0.1:50930 #1 (1 connection now open)
2019-08-10T22:46:33.296+0800 I NETWORK  [conn1] received client metadata from 127.0.0.1:50930 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.3" }, os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "18.6.0" } }

开启后不要关闭该端口!!!

5、启动客户端

新建一个窗口 command+N 输入以下代码

mongo

出现下列代码即表示启动成功

MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("b634b600-04a3-4c86-af75-d3c659064a2f") }
MongoDB server version: 4.0.3
Server has startup warnings: 
2019-08-10T22:46:24.279+0800 I CONTROL  [initandlisten] 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] 
2019-08-10T22:46:24.280+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 

接下来就是底层操作mongodb了。