【mongodb 基础2】Install MongoDB Community Edition on macOS 文章目录一. 安装准备Install Xcode Command-Line ToolsInstall Homebrew二. Installing MongoDB 6.0 Community Edition1. 下载MongoDB Homebrew 组件包2. 更新组件包3. 安装MongoDBTo install MongoDB三. 安装后包含的组件四. Runstop MongoDB1. 作为macOS服务的方式运行mongodb2. stop mongod3. verify that MongoDB is running4. the log file五. Connect and Use MongoDB1. Connect MongoDB2. other tools3. default setting本文参考官网通过Homebrew安装MongoDBUse this tutorial to install MongoDB 6.0 Community Edition on macOS using the third-party Homebrew package manager.一. 安装准备Install Xcode Command-Line ToolsInstall the Xcode command-line tools by running the following command in your macOS Terminal:xcode-select --installInstall HomebrewmacOS does not include the Homebrew brew package by default./bin/bash -c$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)brew官网https://brew.sh/#install二. Installing MongoDB 6.0 Community Edition1. 下载MongoDB Homebrew 组件包Tap the MongoDB Homebrew Tap to download the official Homebrew formula for MongoDB and the Database Tools,brew tap mongodb/brew2. 更新组件包To update Homebrew and all existing formulae:brew update3. 安装MongoDBTo install MongoDBbrew install mongodb-community6.0可能出现的问题Error:Command failed with exit 128:git执行brew -vlianggaoLiangdeMacBook-Pro ~ % brew -v Homebrew 4.0.6fatal:detected dubious ownership in repository at/usr/local/Homebrew/Library/Taps/homebrew/homebrew-coreTo add an exception for this directory,call:git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core Homebrew/homebrew-core(no Git repository)fatal:detected dubious ownership in repository at/usr/local/Homebrew/Library/Taps/homebrew/homebrew-caskTo add an exception for this directory,call:git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask Homebrew/homebrew-cask(no Git repository)根据提示添加lianggaoLiangdeMacBook-Pro ~ % git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core lianggaoLiangdeMacBook-Pro ~ % git config --global --add safe.directory /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask三. 安装后包含的组件The installation includes the following binaries:The mongod serverThe mongos sharded cluster query routerThe MongoDB Shell, mongosh创建的目录和文件安装位置check where brew has installed these files and directories:% brew --prefix /usr/localIn addition, the installation creates the following files and directories at the location specified below, depending on your Apple hardware:四. Runstop MongoDB两种方式运行mongodbYou can run MongoDB as a macOS service using brew, or you can run MongoDB manually as a background process.1. 作为macOS服务的方式运行mongodbIt is recommended to run MongoDB as a macOS service, as doing so sets the correct system ulimit values automatically (see ulimit settings for more information).To run MongoDB (i.e. the mongod process) as a macOS service, run:brew services start mongodb-community6.0 。。。 Successfully started mongodb-community(label:homebrew.mxcl.mongodb-commu2. stop mongodTo stop a mongod running as a macOS service, use the following command as needed:brew services stop mongodb-community6.03. verify that MongoDB is runningTo verify that MongoDB is running, perform one of the following: Ifyou started MongoDB as a macOS service:brew services list4. the log fileYou can also view the log file to see the current status of your mongod process:/usr/local/var/log/mongodb/mongo.log.五. Connect and Use MongoDB1. Connect MongoDBTo begin using MongoDB, connect mongosh to the running instance.lianggaoLiangdeMacBook-Pro ~ % mongosh lianggaoLiangdeMacBook-Pro ~ % mongosh Current Mongosh LogID:641168ac9149fea4d2578ae1 Connectingto:mongodb://127.0.0.1:27017/?directConnectiontrueserverSelectionTimeoutMS2000appNamemongosh1.8.0 UsingMongoDB:6.0.5 UsingMongosh:1.8.0 。。。 test2. other toolsincluding data backup and import/export tools likemongoimportandmongodumpas well as monitoring tools likemongotop.% mongotopIt should start up, connect to your running mongod, and start reporting usage statistics.3. default settingmongoDB默认绑定的ip是127.0.0.1也就是说所有的请求只能来自本地一个节点。By default, MongoDB launches with bindIp set to 127.0.0.1, which binds to the localhost network interface. This means that the mongod can only accept connections from clients that are running on the same machine. Remote clients will not be able to connect to the mongod, and the mongod will not be able to initialize a replica set unless this value is set to a valid network interface.通过设置ip来允许与外界的通讯This value can be configured either:in the MongoDB configuration file with bindIp,via the command-line argument --bind_ipFor more information on configuring bindIp, see IP Binding.