redis中redis.windows.conf和redis.windows-service.conf文件的区别

redis中redis.windows.conf和redis.windows-service.conf文件的区别

 

原文链接:https://blog.csdn.net/summer_style/java/article/details/106534996

 

 

Windows版的Redis有2个配置文件,一个是:redis.windows.conf,另一个是redis.windows-service.conf。

由于安装版的Redis服务自启动,是直接通过redis-server.exe启动的,但是,启动时并没有加载Redis的配置文件(redis.windows.conf),导致redis 中bind配置和密码设置不生效。
Redis自启动导致的常见的问题有:

1、在CMD命令加载配置文件(redis.windows.conf)进行启动是不成功的。提示如下:

D:\soft\Redis>redis-server.exe redis.windows.conf
[13760] 11 Jul 16:39:51.067 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
1
2
因为Redis服务已经自启动,这里是不会再新启动的,故加载配置文件是失败的。也没有出现Redis启动的小盒子(下面有图片,慢慢往下看),需要注意的是Windows版的Redis安装时,默认启动加载的配置文件是redis.windows-service.conf,如下图所示:

2、密码失效。虽然在配置文件(redis.windows.conf)设置了密码,密码为123456:

################################## SECURITY ###################################
……省略……
# requirepass foobared
requirepass 123456

但启动客户端进行Redis命令操作时,是不需要密码的,也没有提示无权限操作,这是一个严重的安全问题。

D:\soft\Redis>redis-cli.exe
127.0.0.1:6379> get name
"haha"
127.0.0.1:6379>

3、Redis访问IP绑定(bind)无效
Redis默认绑定的ip为127.0.0.1,但如果想内网的机器都能访问,则需要设置内网的ip地址,如192.168.100.66,然后redis.host则可以设置为192.168.100.66访问Redis。
Redis ip地址绑定默认说明:

################################## 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,只能通过绑定的地址访问Redis。

如果不设置bind,则所有地址都可以访问,如果在项目部署外网,所有人都可以访问到,所以这里也是个注意的地址,还是设置bind比较安全。

绑定多个ip地址:

bind 127.0.0.1 192.168.100.66
1
127.0.0.1和192.168.100.66之间通过空格分隔,不是逗号。
但如果Redis是自启动的,没有加载配置文件(redis.windows.conf)启动,这里的设置也是无效的。
如果不绑定ip地址(192.168.100.66),直接设置redis.host=192.168.100.66是访问不了的,出现以下的错误:
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

所以说,Redis由Windows自启动的,配置文件(redis.windows.conf)的设置都是无效的

五、解决方案:

1、禁用Redis的自启动,设置为手动
2、不要使用Redis安装版,使用压缩版
3、通过命令行CMD加载配置文件(redis.windows.conf)启动

D:\soft\Redis>redis-server.exe redis.windows.conf
1
4、再新打开一个cmd(不要关闭之前打的Cmd窗口),启动Redis客户端:

D:\soft\Redis>redis-cli.exe
1
5、获取Redis中某个key的值,提示无权限。

127.0.0.1:6379> get name
(error) NOAUTH Authentication required.
127.0.0.1:6379>
1
2
3
通过密码进入访问,使用 auth + 密码,如下:

127.0.0.1:6379> get name
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get name
"haha"
127.0.0.1:6379>

六、如果不是安装版的Redis,又想让Redis自启动的时候,可以向Windows添加自启动服务:
1、进入到Redis的安装目录

D:\soft\Redis>
1
2、执行命令:

redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis
1
完整实例指令:

D:\soft\Redis>redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis
1
–service-install redis.windows.conf 指定redis配置文件
–loglevel notice 指定日志级别
–service-name Redis 指定服务名称
运行结果如下( Redis successfully installed as a service.):

D:\soft\Redis>redis-server --service-install redis.windows.conf --loglevel notice --service-name Redis
[7176] 12 Jul 09:34:50.730 # Granting read/write access to 'NT AUTHORITY\NetworkService' on: "D:\soft\Redis" "D:\soft\Redis\"
[7176] 12 Jul 09:34:50.730 # Redis successfully installed as a service.
1

4、安装服务后,默认不是马上启动的,但启动类型是自启动,如果想马上启动,请执行命令:

redis-server --service-start
1
服务成功启动显示如下:
[9876] 12 Jul 09:57:41.251 # Redis service successfully started.
1
2
或者重启电脑。

5、删除Redis服务:
redis-server --service-uninstall

5:redis的配置文件讲解
redis.windows.conf文件中

#redis的配置

#Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
daemonize yes
#当Redis以守护进程方式运行时,Redis默认会把pid写入redis.pid文件,可以通过pidfile指定
pidfile 'E:/xxx/redis/redis_pid/redis.pid'
#端口
port 6379
#绑定主机的ip地址
bind 127.0.0.1
#当 客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
timeout 300
#指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose
loglevel notice
#日志记录方式,默认为标准输出,如果配置Redis为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送给/dev/null
logfile stdout
#设置数据库的数量,默认数据库为0,可以使用SELECT <dbid>命令在连接上指定数据库id
databases 16
#指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
#分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改
save 900 1
save 300 10
save 60 10000
#指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大
rdbcompression yes
#指定本地数据库文件名,默认值为dump.rdb
dbfilename dump.rdb
#指定本地数据库存放目录
dir 'D:/XXX/redis/redis_database'
#设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步
#slaveof 127.0.0.1 6379
#当master服务设置了密码保护时,slav服务连接master的密码
#masterauth 123456
#设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过AUTH <password>命令提供密码,默认关闭
#requirepass foobared
#设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max number of clients reached错误信息
maxclients 10000
#指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis新的vm机制,会把Key存放内存,Value会存放在swap区
maxmemory 300m
#指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no
appendonly yes
#指定更新日志文件名,默认为appendonly.aof
appendfilename 'appendonly.aof'
#指定更新日志条件,共有3个可选值
#no:表示等操作系统进行数据缓存同步到磁盘(快)
#always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全)
#everysec:表示每秒同步一次(折衷,默认值)
appendfsync everysec
————————————————

原文链接:https://blog.csdn.net/summer_style/java/article/details/106534996

posted @ 2020-06-27 14:10  未来全栈攻城狮  阅读(13372)  评论(1编辑  收藏  举报