CentOS安装Redis

·  阅读 3226

一、安装Redis

1、下载、解压、编译

shell> wget http://download.redis.io/releases/redis-5.0.8.tar.gz
shell> cd /usr/local
shell> tar -zxvf /path/to/redis-5.0.8.tar.gz
shell> cd redis-5.0.8
shell> make
复制代码

2、启动

shell> src/redis-server
26997:C 21 Mar 2020 12:37:20.174 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26997:C 21 Mar 2020 12:37:20.174 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=26997, just started
26997:C 21 Mar 2020 12:37:20.174 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 26997
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

26997:M 21 Mar 2020 12:37:20.175 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
26997:M 21 Mar 2020 12:37:20.175 # Server initialized
26997:M 21 Mar 2020 12:37:20.175 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
26997:M 21 Mar 2020 12:37:20.175 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
26997:M 21 Mar 2020 12:37:20.175 * Ready to accept connections
复制代码

3、测试(使用内置的客户端与Redis进行交互)

shell> src/redis-cli
127.0.0.1:6379> PING
PONG
复制代码

以上就是官网的安装步骤了,比较简单,但实际应用中往往不止如此,下面就介绍下实战中常用的一些配设置

二、添加到/user/local/bin

在解压后的Redis目录执行以下命令,可以把redis命令安装到/user/local/bin目录,由于/user/local/bin默认是在环境变量PATH中的,所以安装后就无需在解压后的redis源码目录中执行相关命令了,可以在任何地方执行,比较方便

shell> make install
cd src && make install
make[1]: Entering directory `/usr/local/redis-5.0.8/src'
    CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis-5.0.8/src'
make[1]: Entering directory `/usr/local/redis-5.0.8/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/redis-5.0.8/src'
shell> ll /usr/local/bin/
total 32772
-rwxr-xr-x 1 root root 4366808 Mar 21 19:21 redis-benchmark
-rwxr-xr-x 1 root root 8125000 Mar 21 19:21 redis-check-aof
-rwxr-xr-x 1 root root 8125000 Mar 21 19:21 redis-check-rdb
-rwxr-xr-x 1 root root 4807792 Mar 21 19:21 redis-cli
lrwxrwxrwx 1 root root      12 Mar 21 19:21 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8125000 Mar 21 19:21 redis-server
复制代码

如果不想安装到默认路径/user/local/bin,可以通过PREFIX选项指定其他路径

shell> make PREFIX=/some/other/directory instal
复制代码

三、常用配置

官方文档中的src/redis-server是直接使用默认配置启动,我们可以通过以下命令指定配置文件

shell> src/redis-server /path/to/redis.conf
复制代码

当然,也可以使用命令行参数来指定配置项

shell> src/redis-server /path/to/redis.conf --port 9999 --loglevel debug
复制代码

Redis目录中会有一个配置文件redis.conf,我们可以基于这个文件进行修改,启动时只需指定该配置文件即可

shell> src/redis-server redis.conf
复制代码

更为常见的一种做法可能是把该配置文件拷贝至/etc目录,如:

shell> mkdir /etc/redis
shell> cp redis.conf /etc/redis/6379.conf
shell> src/redis-server /etc/redis/6379.conf
复制代码

下面开始编辑常用配置项

shell> vim /etc/redis/6379.conf
复制代码

1、以守护进程模式运行(后台运行)

daemonize no
复制代码

修改为

daemonize yes
复制代码

2、允许远程访问

关闭保护模式

protected-mode yes
复制代码

修改为

protected-mode no
复制代码

取消主机绑定,注释掉以下配置

# bind 127.0.0.1
复制代码

3、设置密码

# requirepass foobared
复制代码

打开注释,修改为自己的密码

requirepass 123456
复制代码

注意,如果是以服务方式启动(下面有介绍),设置密码后还需要修改脚本/etc/init.d/reds_6379,否则停止服务时会报“无权限”错误

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
# 新增下面一行
PASSWORD=123456
...

复制代码

找到shutdown命令

$CLIEXEC -p $REDISPORT shutdown
复制代码

修改为:

$CLIEXEC -a $PASSWORD -p $REDISPORT shutdown
复制代码

四、设置开机启动

1、设置为服务

shell> mkdir /etc/redis
shell> cp /usr/local/redis-5.0.8/redis.conf /etc/redis/6379.conf
shell> cp /usr/local/redis-5.0.8/utils/redis_init_script /etc/init.d/redis_6379
复制代码

2、启动&停止

shell> systemctl start redis_6379.service
复制代码
shell> systemctl stop redis_6379.service
复制代码

CentOS 6:

shell> service redis_6379 start
复制代码
shell> service redis_6379 stop
复制代码

3、设置开机启动

shell> chkconfig redis_6379 on
shell> chkconfig --list
redis              0:关    1:关    2:开    3:开    4:开    5:开    6:关
复制代码

以上步骤还可以通过一种更简便的方式实现,在redis/utils目录下有个install_server.sh脚本,执行下即可完成安装服务、设置开机启动以及配置文件位置等相关设置

shell> utils/install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
复制代码

细心的小伙伴可能已经发现了,官方文档的安装教程虽然简单,但是解压后的Redis目录中其实是有个README.md帮助文档的,基本的配置介绍的还是很详细的,上面的配置过程也主要是来自该文档:

Building Redis

Redis can be compiled and used on Linux, OSX, OpenBSD, NetBSD, FreeBSD. We support big endian and little endian architectures, and both 32 bit and 64 bit systems.

It may compile on Solaris derived systems (for instance SmartOS) but our support for this platform is best effort and Redis is not guaranteed to work as well as in Linux, OSX, and *BSD there.

It is as simple as:

% make
复制代码

You can run a 32 bit Redis binary using:

% make 32bit
复制代码

After building Redis, it is a good idea to test it using:

% make test
复制代码

Running Redis

To run Redis with the default configuration just type:

% cd src
% ./redis-server
复制代码

If you want to provide your redis.conf, you have to run it using an additional parameter (the path of the configuration file):

% cd src
% ./redis-server /path/to/redis.conf
复制代码

It is possible to alter the Redis configuration by passing parameters directly as options using the command line. Examples:

% ./redis-server --port 9999 --replicaof 127.0.0.1 6379
% ./redis-server /etc/redis/6379.conf --loglevel debug
复制代码

All the options in redis.conf are also supported as options using the command line, with exactly the same name.

Installing Redis

In order to install Redis binaries into /usr/local/bin just use:

% make install
复制代码

You can use make PREFIX=/some/other/directory install if you wish to use a different destination.

Make install will just install binaries in your system, but will not configure init scripts and configuration files in the appropriate place. This is not needed if you want just to play a bit with Redis, but if you are installing it the proper way for a production system, we have a script doing this for Ubuntu and Debian systems:

% cd utils
% ./install_server.sh
复制代码

The script will ask you a few questions and will setup everything you need to run Redis properly as a background daemon that will start again on system reboots.

You'll be able to stop and start Redis using the script named /etc/init.d/redis_<portnumber>, for instance /etc/init.d/redis_6379.

分类:
后端
标签:
分类:
后端
标签: