使用浏览器访问 https://www.mongodb.com/try/download/community 地址,选择 macOS x64 平台的安装包,如下图:
笔者这里选择了 5.0.20 版本,通过 Copy link 复制下载地址,地址为:https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-5.0.20.tgz
通过 curl -O 下载安装包,如下:
# 进入到 /usr/local 目录 hxstrive@hxstrivedeMac ~ % cd /usr/local # 下载安装包 hxstrive@hxstrivedeMac local % sudo curl -O https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-5.0.20.tgz Password: % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 70.5M 100 70.5M 0 0 4878k 0 0:00:14 0:00:14 --:--:-- 8782k
(1)使用 tar 命令解压下载的安装包,如下:
hxstrive@hxstrivedeMac local % sudo tar -xvzf mongodb-macos-x86_64-5.0.20.tgz x mongodb-macos-x86_64-5.0.20/LICENSE-Community.txt x mongodb-macos-x86_64-5.0.20/MPL-2 x mongodb-macos-x86_64-5.0.20/README x mongodb-macos-x86_64-5.0.20/THIRD-PARTY-NOTICES x mongodb-macos-x86_64-5.0.20/bin/install_compass x mongodb-macos-x86_64-5.0.20/bin/mongod x mongodb-macos-x86_64-5.0.20/bin/mongos x mongodb-macos-x86_64-5.0.20/macos_mongodb.plist
从上面的输出日志可以得知,bin 目录提供了 mongod 和 mongos 可执行文件,没有提供 mongodb 客户端工具。
(2)将解压后的安装包重命名为 mongodb,如下:
hxstrive@hxstrivedeMac local % sudo mv mongodb-macos-x86_64-5.0.20 mongodb
(1)创建数据和日志目录,用来存放 mongodb 的数据和日志,如下:
# 创建数据目录 hxstrive@hxstrivedeMac local % sudo mkdir -p /usr/local/var/mongodb # 创建日志目录 hxstrive@hxstrivedeMac local % sudo mkdir -p /usr/local/var/log/mongodb
(2)修改数据和日志目录的拥有者为 hxstrive(这里填写为笔者的登录名),如下:
hxstrive@hxstrivedeMac local % sudo chown hxstrive /usr/local/var/mongodb hxstrive@hxstrivedeMac local % sudo chown hxstrive /usr/local/var/log/mongodb
(3)直接进入 mongodb 的 bin 目录,如下:
hxstrive@hxstrivedeMac bin % pwd /usr/local/mongodb/bin # 查看 bin 目录内容 hxstrive@hxstrivedeMac bin % ls -l total 574152 -rwxr-xr-x@ 1 500 admin 15141 8 1 11:21 install_compass -rwxr-xr-x 1 500 admin 170867280 8 1 12:37 mongod -rwxr-xr-x 1 500 admin 120917664 8 1 12:37 mongos
执行 “./mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log” 命令启动 MongoDB 服务,如下:
hxstrive@hxstrivedeMac local % ./mongodb/bin/mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log {"t":{"$date":"2023-08-21T00:45:57.164Z"},"s":"F", "c":"CONTROL", "id":20574, "ctx":"thread1","msg":"Error during global initialization","attr":{"error":{"code":38,"codeName":"FileNotOpen","errmsg":"Can't initialize rotatable log file :: caused by :: Failed to open /usr/local/var/log/mongodb/mongo.log"}}}
注意:如果想让 mongodb 服务在后台运行,则在上述命令后面添加 --fork 参数,例如:
hxstrive@hxstrivedeMac bin % ./mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork about to fork child process, waiting until server is ready for connections. forked process: 842 child process started successfully, parent exiting
其中:
--dbpath 设置数据存放目录
--logpath 设置日志存放目录
--fork 在后台运行
(4)通过 ps aux 命令验证 mongodb 服务是否启动成功,如下:
hxstrive@hxstrivedeMac ~ % ps aux | grep -v grep | grep mongod hxstrive 670 0.4 1.6 6656352 67004 s000 S+ 5:47下午 0:06.31 ./mongodb/bin/mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log
注意:
(1)如果你想在任何目录都能正常执行 mongod 命令,则执行下面命令将 mongodb 的 bin 目录添加到 PATH 路径,例如:
export PATH=/usr/local/mongodb/bin:$PATH
(2)除了上面通过命令行设置 mongodb 运行参数的方式去运行 mongodb 服务外,我们也可以使用 mongodb 配置文件去运行 mongodb 服务,例如:
mongod --config /usr/local/etc/mongod.conf
注意,笔者下载的 5.0.20 版本的 MongoDB 安装包中没有提供 mongo 客户端命令,于是重新下载 4.4.23 的安装包,该安装包包含了 mongo 客户端工具。
(1)下载 4.4.23 安装包,如下:
hxstrive@hxstrivedeMac Desktop % sudo curl -O https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.4.23.tgz Password: % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 57.5M 100 57.5M 0 0 2851k 0 0:00:20 0:00:20 --:--:-- 5673k
(2)进入到 mongodb 4.4.23 的 bin 目录,然后执行 mongo 命令连接到 MongoDB 服务器,如下:
hxstrive@hxstrivedeMac bin % ./mongo MongoDB shell version v4.4.23 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("83c01525-526e-4d4e-b6c7-2df687bb8579") } MongoDB server version: 7.0.0 WARNING: shell and server versions do not match Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums https://community.mongodb.com --- The server generated these startup warnings when booting: 2023-08-20T21:34:30.506-07:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2023-08-20T21:34:30.507-07:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning 2023-08-20T21:34:30.507-07:00: Soft rlimits for open file descriptors too low 2023-08-20T21:34:30.507-07:00: currentValue: 256 2023-08-20T21:34:30.507-07:00: recommendedMinimum: 64000 ---
(3)使用 insert 和 find 命令向集合插入一个文档,和查询集合所有文档,如下:
> db.hxstrive.insert({name:"Hello MongoDB"}) WriteResult({ "nInserted" : 1 }) > db.hxstrive.find() { "_id" : ObjectId("64e2eb09bf04dfff61d79abf"), "name" : "Hello MongoDB" } >