Home >Backend Development >Python Tutorial >Problem with python accessing redis

Problem with python accessing redis

高洛峰
高洛峰Original
2017-02-15 14:59:141697browse

Today, there was an error when using python to access redis on this machine (windows). There are two types of errors:
If I configure sentinel, an error like unknown command 'SENTINEL' will be reported;
If When I configure StrictRedis, a NOAUTH Authentication required. error will be reported;

mark it and find a solution tomorrow. . . .

ps: It was finally discovered that the failure of the mongo connection was caused by the password not being set (actually because the online configuration was regarded as offline configuration...)
For the first error, the company's recent online A problem occurred, and I encountered such an error. The main reason was that sentinel did not start. However, the process was a bit tricky. The company has three redis sentinel machines. After one of the machines went down, the other two machines were unable to elect. One machine is used as the master, because I don’t know which temporary worker configured the election machine to 3 when doing redis configuration. As a result, as long as one machine is down, it cannot be elected. In fact, the configuration should be changed to n-m, n is the number of machines, and m is the number of machines allowed to fail.

Attached is the redis code as follows:

import redis
import ConfigParser
import redis.sentinel


class RedisConn:
    _rp = None

    def __init__(self):
        cp = ConfigParser.SafeConfigParser()
        cp.read('avatar_redis.conf')
        host = cp.get('redis', 'host')
        port = cp.get('redis', 'port')
        db_name = cp.get('redis', 'dbName')
        pass_word = cp.get('redis', 'passWord')
        self._rp = redis.StrictRedis(host=host, port=port, db=0, password=pass_word)
        # = redis.ConnectionPool(host=host, port=port, db=0, password='')
        #sentinel = redis.sentinel.Sentinel([(host, port)], socket_timeout=0.1)
        #self._rp = sentinel.master_for(db_name, socket_timeout=0.1)
        #self._rp = redis.Redis(connection_pool=conn_pool)

    def get_conn(self):
        return self._rp

    def set_value(self, key, value):
        self._rp.set(key, value)

    def get_value(self, key):
        return self._rp.get(key)

For more questions about python accessing redis, please pay attention to the PHP Chinese website for related articles!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn