欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

windows下scrapy-redis如何为redis配置密码

程序员文章站 2022-03-13 21:20:25
...

windows下scrapy-redis如何为redis配置密码

1. 环境

  • 系统:win7
  • scrapy-redis
  • redis 3.0.5
  • python 3.6.1

2. 为redis-server配置密码并启动

2.1. 修改配置文件

  • 找到文件redis.windows.conf,对其进行如下设置:
    windows下scrapy-redis如何为redis配置密码
################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
# 
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
requirepass redisPasswordTest666666
  • 一定要注意:requirepass redisPasswordTest666666 前面不能有任何空格和字符。

2.2. 启动redis服务

  • 启动redis-server时,一定要加上 redis.windows.conf 这个参数,否则redis会使用默认的参数进行启动,这些配置信息都不会生效……
redis-server.exe redis.windows.conf

windows下scrapy-redis如何为redis配置密码

2.3. 使用Redis Desktop Manager进行验证

  • 不使用密码,无法登录
    windows下scrapy-redis如何为redis配置密码
2017-12-27 10:52:43 : Init app log
2017-12-27 10:52:45 : Connection: AUTH
2017-12-27 10:52:45 : Connection: localRedisTestPwd > connected
2017-12-27 10:52:45 : Connection: localRedisTestPwd > [runCommand] PING
2017-12-27 10:52:45 : Connection: localRedisTestPwd > Response received : -NOAUTH Authentication required.

2017-12-27 10:52:45 : Connection: AUTH ERROR
2017-12-27 10:52:45 : Connection: AUTH
2017-12-27 10:52:45 : Connection: localRedisTestPwd > connected
2017-12-27 10:52:45 : Connection: localRedisTestPwd > [runCommand] PING
2017-12-27 10:52:45 : Connection: localRedisTestPwd > Response received : -NOAUTH Authentication required.

2017-12-27 10:52:45 : Connection: AUTH ERROR
  • 使用密码,顺利登录
    windows下scrapy-redis如何为redis配置密码

3. scrapy-redis分布式爬虫使用密码进行redis连接

# 使用 redis 密码
class MySpider(RedisSpider):
    """Spider that reads urls from redis queue (myspider:start_urls)."""
    name = 'xxxx'
    redis_key = 'xxxx:start_urls'

    # ……
    custom_settings = {
        'LOG_LEVEL': 'DEBUG',
        'DOWNLOAD_DELAY': 0,

        # 指定redis数据库的连接参数
        'REDIS_HOST': '192.168.1.99',
        'REDIS_PORT': 6379,

        # 指定 redis链接密码,和使用哪一个数据库
        'REDIS_PARAMS' : {
            'password': 'redisPasswordTest666666',
        },
    }

4. 注意事项

  • 在windows下,安装方式有两种:程序安装和压缩包解压。上面提到的方法是压缩包解压的方式。而安装版的Redis服务是自启动的,直接通过redis-server.exe启动,但是,启动时并没有加载Redis的配置文件redis.windows.conf,这样就导致 redis 中的密码设置不生效。