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

《CentOS 6.3 安装配置 Redis-4.0.2》 Make生成的文件

测试 ( 选做步骤 )
[root@iZ2864f6btwZ redis-4.0.2]# make test

《CentOS 6.3 安装配置 Redis-4.0.2》 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
...

《CentOS 6.3 安装配置 Redis-4.0.2》 添加了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的密码,注意是明文存储的

《CentOS 6.3 安装配置 Redis-4.0.2》 设置密码

测试密码是否配置正确,使用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 配置的文章稍后推出,喜欢就关注一下。

    原文作者:pijh
    原文地址: https://www.jianshu.com/p/300b75cb1232
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞