MongoDB is a document-oriented database that is free and open-source. It is classified as a NoSQL database because it does not rely on a traditional table-based relational database structure. Instead, it uses JSON-like documents with dynamic schemas. Unlike relational databases, MongoDB does not require a predefined schema before you add data to a database. You can alter the schema at any time and as often as is necessary without having to setup a new database with an updated schema.
This tutorial guides you through installing MongoDB Community Edition on a CentOS 7 server.
Before following this tutorial, make sure you have a regular, non-root user with
sudo privileges. You can learn more about how to set up a user with these privileges from our guide, How To Create a Sudo User on CentOS.
mongodb-org package does not exist within the default repositories for CentOS. However, MongoDB maintains a dedicated repository. Let's add it to our server.
vi editor, create a
.repo file for
yum, the package management utility for CentOS:
sudo vi /etc/yum.repos.d/mongodb-org.repo
Then, visit the Install on Red Hat section of MongoDB’s documentation and add the repository information for the latest stable release to the file:
[mongodb-org-3.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
Save and close the file.
Before we move on, we should verify that the MongoDB repository exists within the
yum utility. The
repolist command displays a list of enabled repositories:
MongoDB Repository in place, let's proceed with the installation.
We can install the
mongodb-org package from the third-party repository using the
sudo yum install mongodb-org
There are two
Is this ok [y/N]: prompts. The first one permits the installation of the MongoDB packages and the second one imports a GPG key. The publisher of MongoDB signs their software and
yum uses a key to confirm the integrity of the downloaded packages. At each prompt, type
Y and then press the
Next, start the MongoDB service with the
sudo systemctl start mongod
Although we will not use them in this tutorial, you can also change the state of the MongoDB service with the
reload command requests that the
mongod process reads the configuration file,
/etc/mongod.conf, and applies any changes without requiring a restart.
sudo systemctl reload mongod
stop command halts all running
sudo systemctl stop mongod
systemctl utility did not provide a result after executing the
start command, but we can check that the service started by viewing the end of the
mongod.log file with the
sudo tail /var/log/mongodb/mongod.log
An output of waiting for a connection confirms that MongoDB has started successfully and we can access the database server with the MongoDB Shell:
Note: When you launched the MongoDB Shell you may have seen a warning like this:
** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
MongoDB is a threaded application. It can launch additional processes to handle its workload. The warning states that for MongoDB to be most effective the number of processes that it is authorized to spin up should be half that of the number of files that it can have open at any given time. To resolve the warning, alter the
processes soft rlimit value for
mongod by editing the
sudo vi /etc/security/limits.d/20-nproc.conf
Add the following line to the end of file:
. . . mongod soft nproc 32000
For the new limit to be available to MongoDB, restart it using the
sudo systemctl restart mongod
Afterward, when you connect to the MongoDB Shell, the warning should cease to exist.
To learn how to interact with MongoDB from the shell, you can review the output of the
db.help()method which provides a list of methods for the db object.
mongod process running in the background, but quit the shell with the
Because a database-driven application cannot function without a database, we’ll make sure that the MongoDB daemon,
mongod, will start with the system.
systemctl utility to check its startup status:
systemctl is-enabled mongod; echo $?
An output of zero confirms an enabled daemon, which we want. A one, however, confirms a disabled daemon that will not start.
In the event of a disabled daemon, use the
systemctl utility to enable it:
sudo systemctl enable mongod
We now have a running instance of MongoDB that will automatically start following a system reboot.
Unlike other database servers, MongoDB does not come with data in its
test database. Since we don’t want to experiment with new software using production data, we will download a sample dataset from the “Import Example Dataset” section of the “Getting Started with MongoDB” documentation. The JSON document contains a collection of restaurants, which we’ll use to practice interacting with MongoDB and avoid causing harm to sensitive data.
Start by moving into a writable directory:
curl command and the link from MongoDB to download the JSON file:
curl -LO https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json
mongoimport command will insert the data into the test database. The
--db flag defines which database to use while the
--collection flag specifies where in the database the information will be stored, and the
--file flag tells the command which file to perform the import action on:
mongoimport --db test --collection restaurants --file /tmp/primer-dataset.json
The output confirms the importing of the data from the
With the sample dataset in place, we'll perform a query against it.
Relaunch the MongoDB Shell:
The shell selects the
test database by default, which is where we imported our data.
Query the restaurants collection with the
find() method to display a list of all the restuarants in the dataset. Since the collection contains over 25,000 entries, use the optional
limit() method to reduce the output of the query to a specified number. Additionally, the
pretty() method makes the information more human-readable with newlines and indentations.
db.restaurants.find().limit( 1 ).pretty()
You can continue using the sample dataset to familiarize yourself with MongoDB or delete it with the
Lastly, quit the shell with the
In this tutorial, we covered adding a third-party repository to
yum, installing the MongoDB database server, importing a sample dataset, and performing a simple query. We barely scratched the surface of the capabilities of MongoDB. You can create your own database with several collections, fill them with many documents and start building a robust application.
Redis is an open-source in-memory data structure store. It can be used as a database, cache and message broker and supports various data structures such as Strings, Hashes, Lists, Sets, etc. Redis provides high availability via Redis Sentinel including monitoring, notifications Automatic failover. It also provides automatic partitioning across multiple Redis nodes with Redis Cluster.
This tutorial explains how to install and configure Redis on a CentOS 7 server.
Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.
Redis package is not included in the default CentOS repositories. We will be installing Redis version 5.0.2 from the Remi repository.
The installation is pretty straightforward, just follow the steps below:
Redis service will fail to start if IPv6 is disabled on your server.
Congratulations, at this point you have Redis installed and running on your CentOS 7 server.
By default, Redis doesn’t allow remote connections. You can connect to the Redis server only from 127.0.0.1 (localhost) - the machine where Redis is running.
Perform the following steps only if you want to connect to your Redis server from remote hosts. If you are using a single server setup, where the application and Redis are running on the same machine then you should not enable remote access.
To configure Redis to accept remote connections open the Redis configuration file with your text editor:
sudo nano /etc/redis.conf
Locate the line that begins with
bind 127.0.0.1 and add your server private IP address after
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bind 127.0.0.1 192.168.121.233
Make sure you replace
192.168.121.233 with your IP address. Save the file and close the editor.
Restart the Redis service for changes to take effect:
sudo systemctl restart redis
Use the following
ss command to verify that the Redis server is listening on your private interface on port
ss -an | grep 6379
You should see something like below:
tcp LISTEN 0 128 192.168.121.233:6379 *:* tcp LISTEN 0 128 127.0.0.1:6379 *:*
Next, you’ll need to add a firewall rule that enables traffic from your remote machines on TCP port
Assuming you are using
FirewallD to manage your firewall and you want to allow access from the
192.168.121.0/24 subnet you would run the following commands:
sudo firewall-cmd --new-zone=redis --permanentsudo firewall-cmd --zone=redis --add-port=6379/tcp --permanentsudo firewall-cmd --zone=redis --add-source=192.168.121.0/24 --permanentsudo firewall-cmd --reload
The commands above create a new zone named
redis, opens the port
6379 and allows access from the private network.
At this point, Redis server will accept remote connections on TCP port 6379.
Make sure your firewall is configured to accept connections only from trusted IP ranges.
To verify that everything is set up properly, you can try to ping the Redis server from your remote machine using the
redis-cli utility which provides a command-line interface to a Redis server:
redis-cli -h <REDIS_IP_ADDRESS> ping
The command should return a response of
本文分享自微信公众号 - 黑洞日志（heidcloud）
原文出处及转载信息见文内详细说明，如有侵权，请联系 email@example.com 删除。