For the complete documentation index, see llms.txt. This page is also available as Markdown.

35.5 MongoDB

MongoDB is a NoSQL document database that uses BSON format for storage, featuring dynamic schemas and horizontal scaling capabilities. This section covers the pkg installation, startup configuration, and basic operations of MongoDB 8.0 on FreeBSD.

Note:

Since October 16, 2018, the license for MongoDB Community Server has changed from AGPL v3 to SSPL (Server Side Public License). SSPL is modified from GPL v3 and requires third parties offering MongoDB as a service to open-source their entire service stack source code. SSPL has not been approved by the OSI (Open Source Initiative) as an open-source license, and MongoDB withdrew its OSI approval application in March 2019. Strictly speaking, MongoDB is "source-available" software, not open-source software as defined by the OSI. For specific permitted and prohibited uses under SSPL, please refer to the official MongoDB SSPL FAQ: https://www.mongodb.com/legal/licensing/server-side-public-license/faq.

Installation

Install MongoDB using pkg:

# pkg install mongodb80

Or install using Ports:

# cd /usr/ports/databases/mongodb80/
# make install clean

View the post-installation information for MongoDB:

# pkg info -D mongodb80

Service

After installation is complete, start the MongoDB service:

# service mongod enable   # Set the MongoDB service to start on boot
# service mongod start    # Start the MongoDB service

mongosh (MongoDB Official Shell CLI)

MongoDB 6.0 and later no longer include the legacy mongo command-line tool; it has been replaced by the official mongosh. The mongosh package is available in the FreeBSD Ports collection and requires no additional steps.

Installing mongosh

Install using pkg:

Or install using Ports:

Testing the MongoDB Connection

Use mongosh to connect to the local MongoDB service:

Configuration File

Related project file structure:

If you need to further configure MongoDB, you can modify its configuration file.

The MongoDB 8.0 configuration file is located at /usr/local/etc/mongodb.conf, and the configuration template file is located at /usr/local/etc/mongodb.conf.sample.

Creating Users and Passwords

MongoDB does not enable access control by default; anyone can connect to and operate the database. It is recommended to create users and set passwords.

Use mongosh to connect to the local MongoDB service:

You can copy the following text directly to the command line:

After creating the user, enable password authentication:

Edit the /usr/local/etc/mongodb.conf file:

Remove the comment symbol # before #security:, then add authorization: enabled on the next line, as shown below:

Restart the MongoDB service for the configuration to take effect:

Login Methods

After enabling access control, you need to log in using a username and password. The methods are as follows:

Login Method ①

Use mongosh to connect to the local MongoDB service:

Login Method ②

Log in to MongoDB using the username admin and password z:

References

Last updated