前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Centos7 安装 redis

Centos7 安装 redis

作者头像
Devops海洋的渔夫
发布2019-06-02 14:00:49
9130
发布2019-06-02 14:00:49
举报
文章被收录于专栏:Devops专栏Devops专栏

查看redis的releases版本

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

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

下载redis的稳定版本

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

代码语言:javascript
复制
wget http://download.redis.io/releases/redis-stable.tar.gz

解压redis安装包

代码语言:javascript
复制
[root@server01 ~]# tar -zxvf redis-stable.tar.gz 

yum安装gcc依赖

代码语言:javascript
复制
[root@server01 ~]# yum install gcc -y

编译安装

代码语言:javascript
复制
[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(源文件目录)继续编译,如下:

代码语言:javascript
复制
[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]# 

测试是否安装成功

代码语言:javascript
复制
[root@server01 src]# pwd
/root/redis-stable/src
[root@server01 src]# ./redis-server 

配置redis

以后台进程方式启动:

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

代码语言:javascript
复制
[root@server01 redis-stable]# pwd
/root/redis-stable
[root@server01 redis-stable]# vim redis.conf 

将daemonize no 改为 yes

指定redis.conf文件启动

代码语言:javascript
复制
[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服务已经后台运行了,下一步就需要配置使用密码认证以及远程访问的配置。

代码语言:javascript
复制
[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 默认端口

代码语言:javascript
复制
[root@server01 selinux]# service firewalld stop
[root@server01 selinux]# systemctl disable firewalld 

配置redis.conf中将bind 127.0.0.1 改为bind 0.0.0.0或者注释该行

设置redis连接密码

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

更新redis配置

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

代码语言:javascript
复制
[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服务

代码语言:javascript
复制
[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目录下的启动脚本

代码语言:javascript
复制
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目录

代码语言:javascript
复制
[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

代码语言:javascript
复制
[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目录下

代码语言:javascript
复制
[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开机自启动

代码语言:javascript
复制
[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文件,在第一行加入如下两行注释,保存退出

代码语言:javascript
复制
#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

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

启动以及关闭redis

代码语言:javascript
复制
[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启动脚本

代码语言:javascript
复制
[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]

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.12.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 下载redis的稳定版本
  • 解压redis安装包
  • yum安装gcc依赖
  • 编译安装
  • 测试是否安装成功
  • 配置redis
  • 设置redis远程连接
  • 设置redis连接密码
  • 设置开机自启动
  • 参考文献
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档