Tutorial on Basic installation and configuration of Redis database under CentOS _redis

Source: Internet
Author: User
Tags benchmark mkdir redis centos port number

Redis is an open source multi-platform data storage software that uses ANSI C to write directly in memory using datasets, which makes it very efficient. Redis supports a variety of programming languages, including Lua, C, Java, Python, Perl, PHP, and many other languages. Redis's code is very small, only about 30,000 lines, it only do "very little" things, but do a good job. While working in memory, data persistence is still available, and redis reliability is high, and clustering is also supported, which can be a good guarantee for your data security.

Building Redis
Redis currently does not have an official RPM installation package, we need to compile from source code, and in order to compile we need to install make and GCC.

If GCC and make are not installed, use the Yum installation.

Yum Install gcc make

Download the Tar compress package from the official website.

Curl Http://download.redis.io/releases/redis-3.0.4.tar.gz-o redis-3.0.4.tar.gz

Uncompressed.

Tar zxvf redis-3.0.4.tar.gz

Enter the unpacked directory.

CD redis-3.0.4

Compile the source file with make.

Make

Installation
Enter the directory for the source file.

CD src

Replicate Redis servers and clients to/usr/local/bin.

CP Redis-server Redis-cli/usr/local/bin

It's also best to copy Sentinel,benchmark and check over.

CP Redis-sentinel Redis-benchmark redis-check-aof Redis-check-dump/usr/local/bin

Create the Redis configuration folder.

Mkdir/etc/redis

Create a valid directory for saving data under/var/lib/redis

Mkdir-p/var/lib/redis/6379

System parameters
Some kernel parameters need to be configured in order for Redis to work properly.

The configuration vm.overcommit_memory is 1, which prevents data from being truncated, see this for details.

Sysctl-w Vm.overcommit_memory=1

The maximum number of modified backlog connections exceeds the Tcp-backlog value in Redis.conf, which is the default value of 511. You can find more information on SYSCTL IP network tunnels in kernel.org.

Sysctl-w net.core.somaxconn=512

Suppresses support for transparent macro-page memory (transparent huge pages) because it causes latency and memory-access problems when redis use occurs.

echo Never >/sys/kernel/mm/transparent_hugepage/enabled

Redis.conf
redis.conf is a redis configuration file, but you will see that the name of the file is 6379.conf, and this number is the network port that Redis listens to. If you want to run more than one instance of Redis, recommend using this name.

Copy the redis.conf of the sample to/etc/redis/6379.conf.

CP redis.conf/etc/redis/6379.conf

Now edit this file and configure the parameters.

Vi/etc/redis/6379.conf

Daemonize
set daemonize for NO,SYSTEMD need it to run in the foreground, otherwise Redis will suddenly hang up.

Daemonize No

Pidfile
set Pidfile to/var/run/redis_6379.pid.

Pidfile/var/run/redis_6379.pid

Port
If you are not ready to use the default port, you can modify it.

Port 6379

LogLevel
Set the logging level.

LogLevel Notice

LogFile
Modify the log file path.

Logfile/var/log/redis_6379.log

Dir
Set Directory to/var/lib/redis/6379

dir/var/lib/redis/6379

Safety
Here are a few things you can do to improve your security.

Unix sockets
In many cases, client programs and server-side programs run on the same machine, so there is no need to listen for sockets on the network. If this is similar to your usage, you can use a UNIX socket instead of a network socket, so you need to configure Port 0, and then configure the options below to enable the UNIX socket.

Set up socket files for UNIX sockets.

 Unixsocket/tmp/redis.sock

Restrict permissions on the socket file.

Unixsocketperm 700

Now in order for REDIS-CLI to be accessible, you should point to the socket file with the-s argument.

Redis-cli-s/tmp/redis.sock

Requirepass
You may need remote access, and if so, you should set a password, which requires a password to be entered before each operation.

Requirepass "BTFBX1NYYWRMTUEYNHHSCG"

Rename-command
Imagine the output of the following instruction. Yes, this will output the configuration of the server, so you should deny this access whenever possible.

CONFIG Get *
In order to restrict or even prohibit this or other instructions, you can use the Rename-command command. You must provide an order name and an alternative name. To prohibit it you need to set an alternate name for an empty string, which prevents anyone from guessing that the name of the command will be safe.

Rename-command flushdb "flushdb_my_salt_g0es_here09u09u"
rename-command flushall "
rename-command CONFIG" Config_my_s4lt_go3s_here09u09u "

Snapshot
by default, Redis periodically dumps the dataset to the Dump.rdb file in the directory that we set up. You can configure the dump frequency using the Save command, whose first argument is a time frame in seconds, and the second parameter is the number of modifications on the data file.

Keys are modified at least once every 15 minutes.

Save 900 1

The keys are modified at least 10 times every 5 minutes.

Save 300 10

The keys are modified at least 10,000 times every 1 minutes.

Save 60 10000

File/var/lib/redis/6379/dump.rdb contains dump data from the last saved data set in memory. Because it first creates a temporary file and then replaces the dump file, there is no problem with data destruction, so you don't have to worry about copying the file directly.

Start at boot time
you can use SYSTEMD to add Redis to the system boot list.

Copy the Init_script file of the sample to/ETC/INIT.D, and note the port number that the script name represents.

CP utils/redis_init_script/etc/init.d/redis_6379

Now we're going to use SYSTEMD, so create a unit file under/etc/systems/system named Redis_6379.service.

Vi/etc/systemd/system/redis_6379.service

Fill in the following content, the details can be seen systemd.service.

[Unit]
Description=redis on port 6379
[Service]
type=forking
execstart=/etc/init.d/redis_6379 start
execstop=/etc/init.d/redis_6379 stop
[Install]
Wantedby=multi-user.target

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.