CentOS安装Redis

一、安装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.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,723评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,003评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,512评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,825评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,874评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,841评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,812评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,582评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,033评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,309评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,450评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,158评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,789评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,409评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,609评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,440评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,357评论 2 352

推荐阅读更多精彩内容