“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Introduction to the
Since I recently switched to a new MAC, I needed to set up a base environment. But in the process of building and encountered some big and small problems, so I write this article to summarize, I hope to help you avoid the pit.
Last time we talked about installing Redis for MAC, today we will talk about installing and configuring mongodb.
MAC environment installation mongodb online articles vary, but no one can put the installation and configuration clearly, more or less there will be some problems, so the author today to explain in detail under the MAC environment mongodb should be how to install and configuration.
download
First, let’s go to mongodb official website to download the corresponding version of mongodb. If you don’t want to download or don’t know what version to download, you can leave a message asking me to install the package.
The installation
Unzip the package, rename it mongodb, and cut it to /usr/local.
The new directory
The directory where data is stored
We first need to create a directory to store our data.
The default configuration of mongodb is to store data files in /data/db, so we need to create /data/db folder in the root directory of the computer. However, there is a MAC security problem. If you create a new folder in the root directory, you will be told that the read-only file system failed to be created.
You are advised to create a data directory in /usr/local/mongodb to store data. You can then use the configuration file to tell mongodb to store data in this directory.
Directory for storing logs
Second, we need to create a directory to store the logs.
You are advised to create a logs directory in /usr/local/mongodb and a mongodb. You can then have mongodb write logs to the configuration file.
The directory where configuration files are stored
A configuration file is not required, but I recommend creating one because it makes our startup easier.
You are advised to create an etc directory in /usr/local/mongodb and create a mongod. This configuration file can then be used to start mongodb.
run
If we don’t use configuration files, we need to pass parameters when we start mongodb to tell mongodb where the data is stored and where the log files are stored.
Mongod --dbpath /usr/local/mongodb/data --logpath /usr/local/mongodb/logs/mongodb.logCopy the code
If there are other parameters we need to spell behind, this is too complicated for boot! Therefore, I suggest using configuration files to start mongodb.
We can configure the configuration we need in the configuration file. Here I will briefly introduce a few common configurations.
# database file storage directory dbpath = / usr/local/mongo/data # deposit database log directory logpath = / usr/local/mongo/logs/mongo. # log records in the form of additional log Logappend =true # Fork =trueCopy the code
Start the mongodb service again using the configuration file
/usr/local/mongodb/bin /mongod --config.. /etc/mongod.confCopy the code
Isn’t that much easier? And the configuration doesn’t need to be passed at startup.
To improve the
What? /usr/local/mongodb/bin is a better way to run the mongodb service in any directory that I need to go to every startup. The answer is yes.
-
All we need to do is use CD ~ to go into our user directory and find the.bash_profile file, which is hidden. In access we need to use command + Shift +. To enable the display of hidden files. If we don’t have one we need to create one ourselves.
-
Export PATH=${PATH}:/usr/local/mongodb/bin
-
Then use source.bash_profile in the user directory for the configuration to take effect immediately.
This allows you to use mongodb commands in any directory, such as mongod or mongo.
Run again
We started the mongodb service again.
mongod --config /usr/local/mongodb/etc/mongod.conf
Copy the code
Connect to mongodb for database operations.
mongo
Copy the code
All right, you’re done!
What? I don’t like the command line window, I want the GUI operation tool!!
All right, follow up.
Graphical interface tools
It is recommended to use the Robo3t tool, which can be downloaded by interested users. If you don’t want to download or don’t know what version to download, you can leave a message asking me to install the package.
This tool only needs to download down simple installation can be used, the author will not be introduced in detail.
The local mongodb connection succeeded. Procedure
Afterword.
All right! How to install mongodb on the MAC has been told the author, if there is a fallacy, please inform, thank you! If this article is of any help to you, please click on the “like” button. Your support is my motivation to keep updating!