CentOS 7 install mongo redis

mongo

Introduction

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.

Prerequisites

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.

Step 1 – Adding the MongoDB Repository

The 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.

With the 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:

/etc/yum.repos.d/mongodb-org.repo

[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:

yum repolist
Output

With the MongoDB Repository in place, let's proceed with the installation.

Step 2 – Installing MongoDB

We can install the mongodb-org package from the third-party repository using the yum utility.

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 ENTER key.

Next, start the MongoDB service with the systemctl utility:

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 and stop commands.

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

The stop command halts all running mongod processes.

sudo systemctl stop mongod

The 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 tail command:

sudo tail /var/log/mongodb/mongod.log
Output

An output of waiting for a connection confirms that MongoDB has started successfully and we can access the database server with the MongoDB Shell:

mongo

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 20-nproc.conf file:

sudo vi /etc/security/limits.d/20-nproc.conf

Add the following line to the end of file:

/etc/security/limits.d/20-nproc.conf

. . .
mongod soft nproc 32000

For the new limit to be available to MongoDB, restart it using the systemctl utility:

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.

db.help()
Output

Leave the mongod process running in the background, but quit the shell with the exit command:

exit
Output

Step 3 – Verifying Startup

Because a database-driven application cannot function without a database, we’ll make sure that the MongoDB daemon, mongod, will start with the system.

Use the 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.

Output

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.

Step 4 – Importing an Example Dataset (Optional)

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:

cd /tmp

Use the 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

The 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 primer-dataset.json file:

Output

With the sample dataset in place, we'll perform a query against it.

Relaunch the MongoDB Shell:

mongo

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()
Output

You can continue using the sample dataset to familiarize yourself with MongoDB or delete it with the db.restaurants.drop() method:

db.restaurants.drop()

Lastly, quit the shell with the exit command:

exit
Output

Conclusion

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

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.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.

Installing Redis on CentOS 7

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:

  1. Start by enabling the Remi repository by running the following commands in your SSH terminal: sudo yum install epel-release yum-utilssudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpmsudo yum-config-manager --enable remi
  2. Install the Redis package by typing: sudo yum install redis
  3. Once the installation is completed, start the Redis service and enable it to start automatically on boot with: sudo systemctl start redissudo systemctl enable redis Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service. To check the status of the service enter the following command: sudo systemctl status redis You should see something like the following: ● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Sat 2018-11-24 15:21:55 PST; 40s ago Main PID: 2157 (redis-server) CGroup: /system.slice/redis.service └─2157 /usr/bin/redis-server 127.0.0.1:6379

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.

Configure Redis Remote Access

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 127.0.0.1.

/etc/redis.conf

# 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 6379:

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 6379.

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

Copy

The command should return a response of PONG:

PONG

https://linuxize.com/post/how-to-install-and-configure-redis-on-centos-7/

https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-centos-7

本文分享自微信公众号 - 黑洞日志(heidcloud)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-03-30

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券