Detailed description of redis self-starting configuration and redis STARTUP configuration

Source: Internet
Author: User
Tags redis server

Detailed description of redis self-starting configuration and redis STARTUP configuration


I. Overview 1.1 Principles

How does redis auto-start work? After the Linux system is started, a program will scan files under a specific directory and execute these files, which can be called scripts. Therefore, you can write your work as a script and put it in the specified path (etc/init. d ). Therefore, all you need to do is compile the execution of redis instance startup into a script so that the Linux system can execute it for you.

1.2 Brief Introduction

It takes about three steps to Configure Automatic startup.

1. Edit the configuration file corresponding to the instance. On the same machine, multiple redis instances can be started, so multiple configuration files are matched. At the same time, each instance has its own working path. If the shared working path is used, many project items in the configuration file must be configured differently to avoid conflicts. Therefore, we recommend that you set different working paths.

2. edit a self-starting script and copy the script to the etc/init. d directory. After the Linux system is started, a program will automatically scan the files in this path and then execute them.

3. Execute the command to take effect and test.

Ii. Edit The Name Of The redis configuration file 2.1

There is a redis under the redis directory. conf file. Copy one copy. We recommend that you name it by port name, for example, 6379. conf. The rule is: first, it must be consistent with the configuration in the script in Chapter 3. Second, different instances correspond to different configuration files and cannot be mixed up. Then, you can use vi to edit the files.

2.2 edit the configuration file

Note the following items:

 

First, configure redis as the on-duty process. After redis runs, it will run in the background as follows:
daemonize yes
 
Redis sever writes its pid to the default file "/var/run/redis. "pid", here, you need to customize the file name, several redis instances, cannot be the same, the Linux system uses this file to determine whether a process is running and closed, as follows:
pidfile /var/run/redis_6379.pid
 
The default TCP listening port used by the Redis instance to receive client connections is 6379. If it is set to 0, the TCP listening service is not enabled. You can customize the port here, which must be consistent with the relevant items configured in subsequent scripts.
port 6379
 
By default, the Redis server receives requests from all the NICS of the current server. However, you can also specify an IP address to bind the NIC. Multiple NICs can be bound, which are separated by spaces, as shown below:
bind 192.168.1.100 10.0.0.1
bind 127.0.0.1
 
The name of the file that stores the snapshot file. The default value is dump. rdb. You can customize the file name. Do not change the extension. If you need to start multiple redis instances and multiple instances share the same working path, the file name must be different.
dbfilename dump.rdb
 

The above (section 4th) is only the set file name, not the path. You can specify the path through the command dir, and the file will be stored in the specified path and the specified file name. AOF (Append Only File) also references this path. Multiple instances can share this path. If the shared working path is used, other configurations must be configured differently to prevent conflicts. Therefore, we recommend that you set different working paths to avoid these problems.

dir ./

 

Specifies the file that inputs log information. If it is null, the file is output from the standard output window, And the daemon mode is set, the log is recorded to/dev/null. It is only a file name and does not contain a path.
logfile ""

 

3. Process script 3.1 and copy the script

There is a redis_init_script file under the redis/utils directory, copy it to etc/init. in the d path, we recommend that you change the file name to be related to the redis instance port. For example, the Script Name of port 6379 is in the etc/init. change the path to 6379 redisd.

3.2 edit the script

Edit the script copied from section 2.1 to the/etc/init. d path as follows:

#! /Bin/sh
#
# Simple Redis init. d script conceived to work on Linux systems
# As it does use of the/proc filesystem.


# Chkconfig: 2345 90 10
# Description: Redis is a persistent key-value database


REDISIP = localhost
REDISPORT = 31001
EXEC =/usr/local/bin/redis-server
CLIEXEC =/usr/local/bin/redis-cli


PIDFILE =/var/run/redis _ $ {REDISPORT}. pid
CONF = "/etc/redis/$ {REDISPORT}. conf"


Case "$1" in
Start)
If [-f $ PIDFILE]
Then
Echo "$ PIDFILE exists, process is already running or crashed"
Else
Echo "Starting Redis server ..."
$ EXEC $ CONF
Fi
;;
Stop)
If [! -F $ PIDFILE]
Then
Echo "$ PIDFILE does not exist, process is not running"
Else
PID = $ (cat $ PIDFILE)
Echo "Stopping ..."
$ CLIEXEC-h $ REDISIP-p $ REDISPORT shutdown
While [-x/proc/$ {PID}]
Do
Echo "Waiting for Redis to shutdown ..."
Sleep 1
Done
Echo "Redis stopped"
Fi
;;
*)
Echo "Please use start or stop as first argument"
;;
Esac


The above script is described below

REDISPORT = 6379

This should be just a defined variable. Now it is installed. We recommend that you configure the port for the redis instance and the port for the local configuration. If there is more than one server, this must be configured differently.

 

EXEC =/usr/local/bin/redis-server

Your redis-server path. If you run make install after the redis source code is compiled and successful, redis-server will have a connection to redis-server under the/usr/local/bin/path. Alternatively, you need to directly configure the link to the actual path of your redis-server.

 

CLIEXEC =/usr/local/bin/redis-cli

The path of redis-cli is the same as that of redis-server. If you have compiled the redis source code and executed make install successfully, redis-cli will have a connection to redis-cli under the/usr/local/bin/path. Alternatively, you need to directly configure the link to the actual path where your redis-cli is located. In this way, you can directly run the redis-cli command in linux without specifying a path. You can use it as a common command for execution.

 

PIDFILE =/var/run/redis _ $ {REDISPORT}. pid

This is the pid file path. When the apsaradb for redis instance is set to daemonize, a pid file is generated after startup, where the file is generated, and what the file name is, you need to configure it in the config file of redis. Therefore, when you have multiple redis instances in one server, the file name must be different, otherwise it will cause trouble. Therefore, this configuration item must be consistent with the configuration item in the redis configuration file. For details about the configuration items in the redis configuration file, refer to the subsequent sections.

 

CONF = "/usr/local/src/redis/$ {REDISPORT}. conf"

Specify the path of the configuration file of the redis instance. Different redis instances on the same server need different configuration files. Note that the configuration files cannot be the same, otherwise the startup will succeed.

The subsequent part of the script does not need to be modified.


Add item:

# Chkconfig: 2345 90 10

# Description: Redis is a persistent key-value database

The fourth line at the beginning of the script, that is,"# As it does use of the/proc filesystem.Insert the above two segments of code in one line. Otherwise, a permission error will be prompted when the command is executed later.

 

4. Start and take effect

# Set the redisd to the self-starting server (the redisd is the name of the script just created in the/etc/init. d/directory, the same below)

Chkconfig redisd on

# Open the service to test whether the service is valid

Service redisd start

# Close the service

Service redisd stop

# Restart the system and test whether the system is effective

Reboot

After the system restarts, check whether the redis instance is started as expected.

Redis-cli-h the IP address you set-p the port you set

 

 

Copyright Disclaimer: This article is an original article by the blogger. If you want to reprint it, contact the blogger. QQ: 1148824289.

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.