Centos7 安装 redis

阅读 1583
 

查看redis的releases版本

首先安装redis之前,可以点击这里,查看最新的releases版本。

 

最上面的版本太久了,继续往下翻看看最新版本是多少。

 

下载redis的稳定版本

根据上面的页面,获取下载链接,进行下载。

wget http://download.redis.io/releases/redis-stable.tar.gz
 

解压redis安装包

[root@server01 ~]# tar -zxvf redis-stable.tar.gz 
 
 

yum安装gcc依赖

[root@server01 ~]# yum install gcc -y
查看gcc版本,新版本redis6.0以上不兼容老版本的gcc
升级gcc版本

[root@hadoop01 redis-6.0.5]# gcc -v # 查看gcc版本
[root@hadoop01 redis-6.0.5]# yum -y install centos-release-scl # 升级到9.1版本
[root@hadoop01 redis-6.0.5]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@hadoop01 redis-6.0.5]# scl enable devtoolset-9 bash
以上为临时启用,如果要长期使用gcc 9.1的话,使用如下命令,然后关闭服务器,重新连接,如果显示版本是9.1,就可以继续执行下面的操作了

[root@hadoop01 redis-6.0.5]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

 
 

编译安装

[root@server01 ~]# cd redis-stable
[root@server01 redis-stable]# ls
00-RELEASENOTES  CONTRIBUTING  deps     Makefile   README.md   runtest          runtest-sentinel  src    utils
BUGS             COPYING       INSTALL  MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests
[root@server01 redis-stable]# make MALLOC=libc 
 
 

生成了src目录文件之后,进入src(源文件目录)继续编译,如下:

[root@server01 src]# make install
    CC Makefile.dep

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

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
[root@server01 src]# 
 

测试是否安装成功

[root@server01 src]# pwd
/root/redis-stable/src
[root@server01 src]# ./redis-server 
 

 

 

配置redis

以后台进程方式启动:

1.修改/root/redis-stable/redis.conf: daemonize no 将值改为yes 保存退出

*daemonize是用来指定redis是否要用守护线程的方式启动。

当我们采用yes时,redis会在后台运行,此时redis将一直运行,除非手动kill该进程。同时将进程pid号写入至redis.conf选项pidfile设置的文件中,默认会生成在/var/run/redis.pid,也可以通过pidfile来指定pid文件生成的位置
pidfile /path/redis.pid
而采用no时,当前界面将进入redis的命令行界面,exit强制退出或者关闭连接工具(putty,xshell等)都会导致redis进程退出。

[root@server01 redis-stable]# pwd
/root/redis-stable
[root@server01 redis-stable]# vim redis.conf 
 

将daemonize no 改为 yes

 
 

指定redis.conf文件启动

[root@server01 src]# ./redis-server /root/redis-stable/redis.conf 
1335:C 05 Dec 2018 08:49:43.682 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1335:C 05 Dec 2018 08:49:43.682 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=1335, just started
1335:C 05 Dec 2018 08:49:43.682 # Configuration loaded
[root@server01 src]# 
 

这时候redis服务已经后台运行了,下一步就需要配置使用密码认证以及远程访问的配置。

[root@server01 src]# ps -ef | grep redis
root       1336      1  0 08:49 ?        00:00:00 ./redis-server 127.0.0.1:6379
root       1371   1313  0 09:00 pts/0    00:00:00 grep --color=auto redis
[root@server01 src]# 

设置redis远程连接

关闭selinux 以及 firewalld 或者 放行 6379 默认端口

[root@server01 selinux]# service firewalld stop
[root@server01 selinux]# systemctl disable firewalld 

配置redis.conf中将bind 127.0.0.1 改为bind 0.0.0.0或打开注释

bind 127.0.0.1 ::1 #表示redis服务程序会监听这台电脑上的本地环回地址(如果配置文件中,只bind了这一个ip,那么就不能远程访问这台服务器上的redis服务了)。

bind x.x.x.x ::1 #表示redis服务程序会监听我们指定的ip地址,当然这个ip地址必须是这台电脑的一个合法ip地址才行。

bind 0.0.0.0 ::1 #表示redis服务程序会监听任意一个合法的ip

如果连接redis提示:Could not connect to Redis at 127.0.0.1:6379: Connection refused错误解析

应该是在配置文件中,bind 的ip地址 不是 127.0.0.1 或者 0.0.0.0或者其他指定ip 

设置redis连接密码

redis.conf中搜索requirepass这一行,然后在合适的位置添加配置

 

更新redis配置

再次执行启动加载配置的命令即可。

[root@server01 src]# ./redis-server /root/redis-stable/redis.conf 
1182:C 05 Dec 2018 09:09:58.721 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1182:C 05 Dec 2018 09:09:58.721 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=1182, just started
1182:C 05 Dec 2018 09:09:58.721 # Configuration loaded
[root@server01 src]# 
[root@server01 src]# ps -ef | grep redis
root       1177      1  0 09:09 ?        00:00:00 ./redis-server 0.0.0.0:6379
root       1185   1147  0 09:11 pts/0    00:00:00 grep --color=auto redis
[root@server01 src]# 

设置开机自启动

关闭redis服务

[root@server01 src]# ps -ef | grep redis
root       1177      1  0 09:09 ?        00:00:00 ./redis-server 0.0.0.0:6379
root       1185   1147  0 09:11 pts/0    00:00:00 grep --color=auto redis
[root@server01 src]# 
[root@server01 src]# ps -aux | grep redis
root       1177  0.0  0.2 144008  2028 ?        Ssl  09:09   0:00 ./redis-server 0.0.0.0:6379
root       1187  0.0  0.0 112708   976 pts/0    R+   09:11   0:00 grep --color=auto redis
[root@server01 src]# 
[root@server01 src]# kill -9 1177
[root@server01 src]# 
[root@server01 src]# ps -aux | grep redis
root       1189  0.0  0.0 112708   980 pts/0    R+   09:12   0:00 grep --color=auto redis
[root@server01 src]# 
 

查看redis-stable/utils目录下的启动脚本

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

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

查看 /usr/local/bin 目录是否存在redis执行文件

 

那么剩下就是需要设置配置文件了。

在/etc目录下新建redis目录

[root@server01 utils]# mkdir -p /etc/redis
[root@server01 utils]# 
[root@server01 utils]# ls /etc/redis/
[root@server01 utils]# 

将/redis-stable/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf

[root@server01 redis-stable]# pwd
/root/redis-stable
[root@server01 redis-stable]# 
[root@server01 redis-stable]# cp redis.conf /etc/redis/6379.conf
[root@server01 redis-stable]# 
[root@server01 redis-stable]# ls -ll /etc/redis/
total 64
-rw-r--r-- 1 root root 62203 Dec  5 09:32 6379.conf
[root@server01 redis-stable]# 
 

将redis的启动脚本复制一份放到/etc/init.d目录下

[root@server01 utils]# cp redis_init_script /etc/init.d/redisd
[root@server01 utils]# 
[root@server01 utils]# ls -ll /etc/init.d/redisd 
-rwxr-xr-x 1 root root 1352 Dec  5 09:34 /etc/init.d/redisd
[root@server01 utils]# 
 

设置redis开机自启动

[root@server01 utils]# cd /etc/init.d/
[root@server01 init.d]# ls
functions  netconsole  network  README  redisd
[root@server01 init.d]# chkconfig redisd on
[root@server01 init.d]# 
 

使用vim编辑redisd文件,在第一行加入如下两行注释,保存退出

#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

 

启动以及关闭redis

[root@server01 init.d]# service redisd start
/var/run/redis_6379.pid exists, process is already running or crashed
[root@server01 init.d]# 
[root@server01 init.d]# service redisd stop
Stopping ...
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
^C
[root@server01 init.d]
 

可以从上面看出,当配置了redis的密码之后,停止redis的时候是需要认证的。

配置启动脚本使用密码,解决停止(NOAUTH Authentication required)的问题

修改redis启动脚本

[root@server01 ~]# vim /etc/init.d/redisd
# 修改添加 -a "密码"
#$CLIEXEC -p $REDISPORT shutdown
$CLIEXEC -a "newpwd"  -p $REDISPORT shutdown
 

 

 

参考文献

CentOS7安装Redis4.0.2
redis服务启动和停止(NOAUTH Authentication required)
Redis服务停止报错解决方案[NOAUTH Authentication required]

 

 

关注微信公众号,回复【资料】、Python、PHP、JAVA、web,则可获得Python、PHP、JAVA、前端等视频资料。

posted @ 2019-09-07 14:26  落华  阅读(52)  评论(0)    收藏  举报