Redis安装和开发(Centos)

注意:部分内容转载来自网络~

一.安装redis

1)下载redis安装包

可去官网http://redis.io,也可通过wget命令,

wget

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


2)解压

tar–zxvf redis-stable.tar.gz


3)编译、安装

cd redis-stable


make

如果提示gcc command不识别,请自行安装gcc;

如果提示couldn’t execute tcl : no such file or dicrectory,请自行安装tcl;

如果提示


请执行make distclean,然后再make


Make成功之后,会在src目录下多出一些文件,如下


可手动拷贝redis-server、redis-cli、redis-check-aof、redis-check-dump等至/usr/local/bin目录下,也可执行make install,此处执行make install


可查看,/usr/local/bin下已有这些文件。

注意:若此时执行redis-server–v

(查看版本命令),若提示redis-server command

not found,则需要将/usr/local/bin目录加到环境变量,如何添加,此处不做详细介绍,可查看修改/etc/profile,(查看环境变量命令:echo $PATH)

正常如下


至此,redis安装完成,接着配置。

二.修改配置文件.conf

1)创建配置文件目录,dump file目录,进程pid目录,log目录等

配置文件一般放在/etc/下,创建redis目录

cd /etc/

mkdir redis

ll查看创建的redis目录

~

dump file、进程pid、log目录等,一般放在/var/目录下,

cd /var/

mkdir redis


cd redis

mkdir data log run


至此,目录创建完毕

2)修改配置文件,配置参数

首先拷贝解压包下的redis.conf文件至/etc/redis


查看/etc/redis/redis.conf

cd /etc/redis/

ll


打开redis.conf文件

修改端口(默认6379)


修改pid目录为新建目录


修改dump目录为新建目录


修改log存储目录为新建目录


3)持久化

默认rdb,可选择是否开启aof,若开启,修改配置文件appendonly

4)启动redis,查看各目录下文件


查看进程


redis已启动

查看dump, log, pid等


发现只有日志,没有dump和pid信息,是因为当前redis服务仍然是console模式运行的,且没有数据存储操作

停止redis服务,修改配置文件使得redis在background运行


改成yes,保存,重启redis服务

查看pid信息,如下


查看dump信息


若配置了aof持久化方式,data目录下还会有aof的相关文件

5)客户端连接redis


默认端口6379

6)至此,redis基础配置完毕,若有其他相关配置调整,可查找文档再修改

三.服务及开机自启动

1)创建redis启动脚本

拷贝解压包下utils下redis启动脚本至/etc/init.d/

cp redis_init_script /etc/init.d/

修改脚本名称(也可不修改)为redis

查看ll


修改脚本pid及conf路径为实际路径


生产环境下,配置时,配置文件、pid等最好加上端口标识,以便区分,如


保存

退出

至此,在/etc/init.d/目录下,已经可以通过service redis start/stop命令启动和关闭redis


若在其他目录下,不能够使用这2个命令,请继续配置2),添加权限

2)给启动脚本添加权限

chmod +x /etc/init.d/redis


实际命令,根据目录的不同,会不一样

相应的删除权限是

chmod–x /etc/init.d/redis

如果需要在开机的时候,redis服务自动启动,可继续3)

3)设置自启动

chkconfig redis on

如果运行报错,提示


是因为没有在启动脚本里加入redis启动优先级信息,可添加如下


再次执行chkconfig redis on,成功


至此,自启动配置完毕


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

补充一些细节和使用方法

》》service redis does not support chkconfig的解决办法

问题解决办法如下:

必须把下面两行注释放在/etc/init.d/Redis文件靠前的注释中:

# chkconfig: 2345 90 10

# description: Redis is a persistent key-value database

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

》》Redis默认只允许本地访问,要使Redis可以远程访问可以修改redis.conf

打开redis.conf文件在NETWORK部分有说明

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens

# for connections from all the network interfaces available on the server.

# It is possible to listen to just one or multiple selected interfaces using

# the "bind" configuration directive, followed by one or more IP addresses.

#

# Examples:

#

# bind 192.168.1.100 10.0.0.1

# bind 127.0.0.1 ::1

#

# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the

# internet, binding to all the interfaces is dangerous and will expose the

# instance to everybody on the internet. So by default we uncomment the

# following bind directive, that will force Redis to listen only into

# the IPv4 lookback interface address (this means Redis will be able to

# accept connections only from clients running into the same computer it

# is running).

#

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES

# JUST COMMENT THE FOLLOWING LINE.

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bind 127.0.0.1

解决办法:注释掉bind 127.0.0.1可以使所有的ip访问redis

若是想指定多个ip访问,但并不是全部的ip访问,可以bind

注意

下面还有个说明

# Protected mode is a layer of security protection, in order to avoid that

# Redis instances left open on the internet are accessed and exploited.

#

# When protected mode is on and if:

#

# 1) The server is not binding explicitly to a set of addresses using the

# "bind" directive.

# 2) No password is configured.

#

# The server only accepts connections from clients connecting from the

# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain

# sockets.

#

# By default protected mode is enabled. You should disable it only if

# you are sure you want clients from other hosts to connect to Redis

# even if no authentication is configured, nor a specific set of interfaces

# are explicitly listed using the "bind" directive.

protected-mode yes

在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,如下

(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

修改办法:protected-mode no

##############################################################################################################

》》Linux Redis 配置认证密码

打开Redis客户端

开始是没有配置认证信息的

***:6379> configgetrequirepass1)"requirepass"2)""

比如设置认证为1234

***:6379> configsetrequirepass1234

OK

如果返回OK正面修改成功

***:6379> keys *

(error) NOAUTH Authentication required.

此时就得需要输入认证操作redis了

***:6379> auth 1234

OK***:6379> keys *

1)*****

2)*****

······信息

注意修改认证密码后,就得重新输入认证信息操作redis。

注意:只有配置文件修改,才能重启后继续生效


》》重启redis,发现一直报:Waiting for Redis to shutdown

service redis_6379 restart

Stopping ...

OK

(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 ...

因为配置了密码验证,而在restart的时候并没有配置密码。

解决方法:

1.修改redis服务脚本,加入如下所示的信息即可:

vi /etc/init.d/redis

$CLIEXEC -a "password" -p $REDISPORT shutdown

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

推荐阅读更多精彩内容