使用mac 包管理工具homebrew是我最喜欢的方式!
➜ ~ brew install mongodb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
clang-format pulumi
git-archive-all selenium-server-standalone
git-subrepo weaver
grafana yamllint
phpunit
==> Downloading https://homebrew.bintray.com/bottles/mongodb-4.0.4_1.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-4.0.4_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.4_1: 18 files, 259.8MB
➜ ~ sudo mkdir -p /data/db
➜ ~ whoami
alicelmx
➜ ~ sudo chown alicelmx /data/db
brew install
安装的所有包都是在/usr/local/Cellar/
这个路径下,显得非常整齐,我就很喜欢。这个路径是不可见的,你可以在finder中按住shift+command+G
输入文件路径来进入。➜ ~ vim ~/.zshrc
写入:export PATH=/usr/local/Cellar/mongodb/4.0.4_1/bin:${PATH}
运行下面这条命令使环境变量生效
➜ ~ source ~/.zshrc
sudo vim /usr/local/etc/mongod.confsudo vim /usr/local/etc/mongod.conf
➜ ~ mongod
2018-11-15T09:42:23.905+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] MongoDB starting : pid=74127 port=27017 dbpath=/data/db 64-bit host=xiaomingjianglmxdeMacBook-Pro.local
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] db version v4.0.4
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] allocator: system
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] modules: none
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] build environment:
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] distarch: x86_64
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] target_arch: x86_64
2018-11-15T09:42:23.958+0800 I CONTROL [initandlisten] options: {}
2018-11-15T09:42:23.960+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),
2018-11-15T09:42:24.458+0800 I STORAGE [initandlisten] WiredTiger message [1542246144:458527][74127:0x117b9f5c0], txn-recover: Set global recovery timestamp: 0
2018-11-15T09:42:24.487+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten]
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten]
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip to specify which IP
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten]
2018-11-15T09:42:24.521+0800 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: a0d768fd-89dc-4d92-94b7-95778d976446
2018-11-15T09:42:24.552+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.0
2018-11-15T09:42:24.557+0800 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: 2d8d4acf-5734-4296-ae71-b203250053df
2018-11-15T09:42:24.590+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-11-15T09:42:24.594+0800 I NETWORK [initandlisten] waiting for connections on port 27017
2018-11-15T09:42:24.594+0800 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: 88e84afc-c52c-402a-bf51-b5f80d9689e3
2018-11-15T09:42:24.635+0800 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2018-11-15T09:42:24.635+0800 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-11-15T09:42:24.643+0800 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records. 0 secs
注意观察:waiting for connections on port 27017 字样时,我们的服务器已经启动成功,它正在运行和侦听端口27017。
这样就可以开始与服务器进行交互了,例如只需打开一个新的终端选项卡并运行 mongo ,这将打开 mongo 的交互式控制台并连接到默认服务器(localhost:27017):
➜ ~ mongod -version
db version v4.0.4
git version: f288a3bdf201007f3693c58e140056adf8b04839
allocator: system
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
➜ ~ mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("9b0c3a4f-c86e-4511-aa8a-0d05cb899601") }
MongoDB server version: 4.0.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten]
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten]
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip to specify which IP
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-11-15T09:42:24.516+0800 I CONTROL [initandlisten]
---
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的时候一定要正确的退出,不然下次再次连接数据库会出现问题,使用下面的两行代码可以完成这一操作。
> use admin;
switched to db admin
> db.shutdownServer();
server should be down...
2018-11-15T09:49:11.058+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2018-11-15T09:49:11.059+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
安装就写到这里啦,已经力求写的比较详细了,如果有mac新手对那块不是很清楚,欢迎评论区找我,我能很快回复的!