1.下载安装
在网上搜了一下,安装MongoDB有三种方式:
(1)手动命令安装
(2)采用Homebrew
(3)下载安装包。
由于使用前两种方式安装尝试都失败了,所以被迫尝试第三种方式进行安装。
1.访问MongoDB官方下载地址
官网地址是:MongoDB Download Center | MongoDB社区版
2.点击“DOWNLOAD(tgz)”按钮,下载文件.
我下载的版本是4.4.6.
文件: mongodb-macos-x86_64-4.4.6.tgz
在下载文件夹解压, 并重命名解压后的目录为MongoDB
-
将解压后的文件放入 /usr/local
[Tips]打开Finder后按 shift + command +G 输入 /usr/local 后回车便能看到这个隐藏的目录了。
-
.配置环境变量,打开终端,输入“open -e ~/.bash_profile”,在打开的文件中加入
export PATH=${PATH}:/usr/local/MongoDB/bin
用Command+S保存配置,关闭上面的~/.bash_profile编辑窗口.
然后在终端中输入source ~/.bash_profile
使配置生效.
输入"mongod -version",回车后如果看到下面的版本号则说明MongoDB已经成功安装到了Mac上。
$ mongod --version
db version v4.4.6
Build Info: {
"version": "4.4.6",
"gitVersion": "72e66213c2c3eab37d9358d5e78ad7f5c1d0d0d7",
"modules": [],
"allocator": "system",
"environment": {
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
- 在终端输入命令
sudo mkdir -p /data/db
, 建立数据文件夹
修改数据文件夹属性
sudo chown -R `id -un` /data/db
2. 运行
在终端输入命令mongod
, 如果看到下面信息就表示服务端被启动了
$ mongod
{"t":{"$date":"2021-07-05T00:26:48.858+08:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-07-05T00:26:48.861+08:00"},"s":"W", "c":"ASIO", "id":22601, "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-07-05T00:26:48.861+08:00"},"s":"I", "c":"NETWORK", "id":4648602, "ctx":"main","msg":"Implicit TCP FastOpen in use."}
{"t":{"$date":"2021-07-05T00:26:48.861+08:00"},"s":"I", "c":"STORAGE", "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":2961,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"XXX"}}
{"t":{"$date":"2021-07-05T00:26:48.861+08:00"},"s":"I", "c":"CONTROL", "id":23403, "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.6","gitVersion":"72e66213c2c3eab37d9358d5e78ad7f5c1d0d0d7","modules":[],"allocator":"system","environment":{"distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2021-07-05T00:26:48.862+08:00"},"s":"I", "c":"CONTROL", "id":51765, "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Mac OS X","version":"18.7.0"}}}
{"t":{"$date":"2021-07-05T00:26:48.862+08:00"},"s":"I", "c":"CONTROL", "id":21951, "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{}}}
{"t":{"$date":"2021-07-05T00:26:48.864+08:00"},"s":"E", "c":"STORAGE", "id":20568, "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"REPL", "id":4784900, "ctx":"initandlisten","msg":"Stepping down the ReplicationCoordinator for shutdown","attr":{"waitTimeMillis":10000}}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"COMMAND", "id":4784901, "ctx":"initandlisten","msg":"Shutting down the MirrorMaestro"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"SHARDING", "id":4784902, "ctx":"initandlisten","msg":"Shutting down the WaitForMajorityService"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"NETWORK", "id":4784905, "ctx":"initandlisten","msg":"Shutting down the global connection pool"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"NETWORK", "id":4784918, "ctx":"initandlisten","msg":"Shutting down the ReplicaSetMonitor"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"SHARDING", "id":4784921, "ctx":"initandlisten","msg":"Shutting down the MigrationUtilExecutor"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"CONTROL", "id":4784925, "ctx":"initandlisten","msg":"Shutting down free monitoring"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"STORAGE", "id":4784927, "ctx":"initandlisten","msg":"Shutting down the HealthLog"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"STORAGE", "id":4784929, "ctx":"initandlisten","msg":"Acquiring the global lock for shutdown"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"-", "id":4784931, "ctx":"initandlisten","msg":"Dropping the scope cache for shutdown"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"FTDC", "id":4784926, "ctx":"initandlisten","msg":"Shutting down full-time data capture"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"CONTROL", "id":20565, "ctx":"initandlisten","msg":"Now exiting"}
{"t":{"$date":"2021-07-05T00:26:48.865+08:00"},"s":"I", "c":"CONTROL", "id":23138, "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":48}}
$
重点是这一行
{"pid":2961,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"XXX"}}
验证:
打开浏览器,输入localhost:27017 ,会出现这样一行文字。
It looks like you are trying to access MongoDB over HTTP on the native driver port.
表示服务启动成功
3. 可视化工具
MongoDB Compass
使用:
按照下图输入链接, 之后正常连接数据库
完成!ENJOY!