注意:部分内容转载来自网络~
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安装完成,接着配置。
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
##############################################################################################################
打开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