CentOS 6.3 安装配置 Redis-4.0.2

Redis官网

进入/usr/local/src目录,使用wget下载
[root@iZ2864f6btwZ ~]# cd /usr/local/src/
[root@iZ2864f6btwZ src]# wget http://download.redis.io/releases/redis-4.0.2.tar.gz
解压 redis-4.0.2.tar.gz 并进入 redis-4.0.2 目录
[root@iZ2864f6btwZ src]# tar zxvf redis-4.0.2.tar.gz && cd redis-4.0.2
编译安装
[root@iZ2864f6btwZ redis-4.0.2]# make
Make生成的文件
测试 ( 选做步骤 )
[root@iZ2864f6btwZ redis-4.0.2]# make test
make test 结果

错误:
tcl 版本过低

cd src && make test
make[1]: Entering directory `/usr/local/src/redis-4.0.2/src'
   CC Makefile.dep
make[1]: Leaving directory `/usr/local/src/redis-4.0.2/src'
make[1]: Entering directory `/usr/local/src/redis-4.0.2/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/usr/local/src/redis-4.0.2/src'
make: *** [test] Error 2

解决:
安装 ctl 8.5 或更高版本

[root@iZ2864f6btwZ redis-4.0.2]# yum -y install tcl
上述步骤 make 已经生成了redis-benchmarkredis-check-aofredis-check-rdbredis-cliredis-sentinelredis-serverredis-trib.rb等文件,为了方便管理,我习惯将这些文件拷贝到/usr/local/redis目录,然后再将此路径加入环境变量,当然你也可以手动将这些文件拷贝到/usr/local/bin目录,或者执行make install进行安装
[root@iZ2864f6btwZ redis-4.0.2]# mkdir /usr/local/redis
[root@iZ2864f6btwZ redis-4.0.2]# cd src
[root@iZ2864f6btwZ src]# cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server /usr/local/redis/
[root@iZ2864f6btwZ src]# 
[root@iZ2864f6btwZ src]# ll !$
ll /usr/local/redis/
total 27376
-rwxr-xr-x 1 root root 2450936 Sep 26 15:22 redis-benchmark
-rwxr-xr-x 1 root root 5742456 Sep 26 15:22 redis-check-aof
-rwxr-xr-x 1 root root 5742456 Sep 26 15:22 redis-check-rdb
-rwxr-xr-x 1 root root 2605224 Sep 26 15:22 redis-cli
-rwxr-xr-x 1 root root 5742456 Sep 26 15:22 redis-sentinel
-rwxr-xr-x 1 root root 5742456 Sep 26 15:22 redis-server
[root@iZ2864f6btwZ src]# 

注意:
!$是指上一次最后使用的路径

/usr/local/redis目录加入环境变量
[root@iZ2864f6btwZ src]# vim /etc/profile.d/redis.sh
...
# redis.sh 的内容
export PATH=$PATH:/usr/local/redis/
...
[root@iZ2864f6btwZ src]# 
[root@iZ2864f6btwZ src]# source !$
source /etc/profile.d/redis.sh
[root@iZ2864f6btwZ src]# 
查看 redis 版本信息,请确保/usr/local/redis在环境变量中
[root@iZ2864f6btwZ src]# cd ..
[root@iZ2864f6btwZ redis-4.0.2]# redis-server -v
Redis server v=4.0.2 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=2bf9a60dbb8aa944
[root@iZ2864f6btwZ redis-4.0.2]# 
将redis配置文件拷贝到/usr/local/redis目录
[root@iZ2864f6btwZ redis-4.0.2]# cp redis.conf /usr/local/redis/
创建 redis 运行时所需目录
[root@iZ2864f6btwZ redis-4.0.2]# mkdir -p /var/run/redis/{data,log,run}
修改redis.conf,配置正确的dump、log、pid目录路径
[root@iZ2864f6btwZ redis-4.0.2]# vim /usr/local/redis/redis.conf

...
# 以后台守护进程运行
daemonize yes
...
pidfile /var/run/redis/run/redis.pid
...
logfile /var/run/redis/log/redis.log
...
dir /var/run/redis/data
...
启动 redis 并查看运行时目录
[root@iZ2864f6btwZ redis-4.0.2]# cd /var/run/redis
[root@iZ2864f6btwZ redis]# tree .
.
├── data
├── log
└── run

3 directories, 0 files
[root@iZ2864f6btwZ redis]# redis-server /usr/local/redis/redis.conf 
[root@iZ2864f6btwZ redis]# 
[root@iZ2864f6btwZ redis]# tree .
.
├── data
├── log
│   └── redis.log
└── run
    └── redis.pid

3 directories, 2 files
[root@iZ2864f6btwZ redis]# 

注意:
tree命令是查看目录结构,如果当前系统没有此命令,请使用yum -y install tree进行安装。

使用脚本启动 redis 服务器,请确保REDISPORTEXECCLIEXECPIDFILECONF配置路径正确,并给/etc/init.d/redis执行权限
[root@iZ2864f6btwZ redis]# cd /usr/local/src/redis-4.0.2/
[root@iZ2864f6btwZ redis-4.0.2]# cp utils/redis_init_script /etc/init.d/redis
[root@iZ2864f6btwZ redis-4.0.2]# vim !$

...
REDISPORT=6379
EXEC=/usr/local/redis/redis-server
CLIEXEC=/usr/local/redis/redis-cli

PIDFILE=/var/run/redis/run/redis.pid
CONF="/usr/local/redis/redis.conf"

redis_run_path="/var/run/redis/"
if [ ! -d $redis_run_path ];then
  mkdir -p ${redis_run_path}"data" ${redis_run_path}"log" ${redis_run_path}"run"
fi
...

[root@iZ2864f6btwZ redis-4.0.2]# chmod +x /etc/init.d/redis
加入开机自动启动
[root@iZ2864f6btwZ redis-4.0.2]# chkconfig --add /etc/init.d/redis 
service redis does not support chkconfig
[root@iZ2864f6btwZ redis-4.0.2]#

错误:
service redis does not support chkconfig

解决:
出现上述问题的原因是因为redis启动脚本没有添加chkconfig启动的优先级导致的,加入redis启动优先级信息# chkconfig: 2345 90 10

[root@iZ2864f6btwZ redis-4.0.2]# vim /etc/init.d/redis

...
#!/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
...
添加了redis启动优先级
再次执行上述步骤
[root@iZ2864f6btwZ redis-4.0.2]# chkconfig --add /etc/init.d/redis 
[root@iZ2864f6btwZ redis-4.0.2]# chkconfig redis on
[root@iZ2864f6btwZ redis-4.0.2]# chkconfig --list | grep redis

Note: This output shows SysV services only and does not include native
     systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

redis           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@iZ2864f6btwZ redis-4.0.2]#
用service启动redis服务器
[root@iZ2864f6btwZ redis-4.0.2]# service redis start
Starting Redis server...
[root@iZ2864f6btwZ redis-4.0.2]# ps -ef | grep redis
root     12505     1  0 17:06 ?        00:00:00 /usr/local/redis/redis-server 127.0.0.1:6379
root     12511  3790  0 17:06 pts/0    00:00:00 grep --color=auto redis
[root@iZ2864f6btwZ redis-4.0.2]# 

注意:
如果之前有启动过redis,请先停止redis服务器,如果是通过redis-server命令启动的,可使用redis-cli shutdown停止

[root@iZ2864f6btwZ redis-4.0.2]# ps -ef | grep redis
root     12151     1  0 16:18 ?        00:00:02 redis-server 127.0.0.1:6379
root     12479  3790  0 17:04 pts/0    00:00:00 grep --color=auto redis
[root@iZ2864f6btwZ redis-4.0.2]# 
[root@iZ2864f6btwZ redis-4.0.2]# redis-cli shutdown
[root@iZ2864f6btwZ redis-4.0.2]# 
[root@iZ2864f6btwZ redis-4.0.2]# ps -ef | grep redis
root     12483  3790  0 17:04 pts/0    00:00:00 grep --color=auto redis
[root@iZ2864f6btwZ redis-4.0.2]# 
测试redis服务器是否可以正常读写
[root@iZ2864f6btwZ redis-4.0.2]# redis-cli
127.0.0.1:6379> set foo test
OK
127.0.0.1:6379> get foo
"test"
127.0.0.1:6379> 

以下为补充内容

给redis设置密码,编辑/usr/local/reids/redis.conf,大概在500行左右找到requirepass项,后面输入访问redis的密码,注意是明文存储的
设置密码
测试密码是否配置正确,使用auth命令 + 密码进行授权
[root@iZ2864f6btwZ redis-4.0.2]# redis-cli 
127.0.0.1:6379> get foo
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth newpassword
OK
127.0.0.1:6379> get foo
"test"
127.0.0.1:6379> 

结尾:
Redis安装与基本配置已经完成,后续更多关于 redis 配置的文章稍后推出,喜欢就关注一下。

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

推荐阅读更多精彩内容