Centos7 安装 redis

下载redis的稳定版本

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

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

image

解压redis安装包

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

image
image

yum安装gcc依赖

[root@server01 ~]# yum install gcc -y

image

编译安装

[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 

image
image

生成了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]# 

image

测试是否安装成功

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

image
image

配置redis

以后台进程方式启动:

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

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

image

将daemonize no 改为 yes

image
image

指定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]# 

image

这时候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或者注释该行

image

设置redis连接密码

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

image

更新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]# 

image

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

image
image
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执行文件

image

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

在/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]# 

image

将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]# 

image

设置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]# 

image

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

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

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

image

启动以及关闭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]

image

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

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

修改redis启动脚本

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

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

推荐阅读更多精彩内容