Redis系列三:thinkphp 使用 redis

1、redis服务端配置认证密码

(1)通过配置文件进行配置
            打开配置文件/usr/local/redis/etc/redis.conf找到
            #requirepass foobared  
           去掉行前的注释,并修改密码为所需的密码,保存文件
            requirepass myRedis  
           重启redis

这个时候尝试登录redis,发现可以登上,但是执行具体命令是提示操作不允许


1.  redis-cli -h 127.0.0.1 -p 6379  
2.  redis 127.0.0.1:6379>  
3.  redis 127.0.0.1:6379> keys *  
4.  (error) ERR operation not permitted  
5.  redis 127.0.0.1:6379> select 1  
6.  (error) ERR operation not permitted  
7.  redis 127.0.0.1:6379[1]>   

尝试用密码登录并执行具体的命令看到可以成功执行
1.  redis-cli -h 127.0.0.1 -p 6379 -a myRedis  
2.  redis 127.0.0.1:6379> keys *  
3.  1) "myset"  
4.  2) "mysortset"  
5.  redis 127.0.0.1:6379> select 1  
6.  OK  
7.  redis 127.0.0.1:6379[1]> config get requirepass  
8.  1) "requirepass"  
9.  2) "myRedis"  

2、redis服务端配置外部访问

Redis系列三:thinkphp 使用 redis_第1张图片

这说明目前处在保护模式上,查看Redis的注释可以了解,连接Redis只能通过本地(127.0.0.1)来连接,而不能使用网络IP192.168.1.x)来连接,如果需要请修改配置文件redis.conf

解决方案

进入Redis目录打开Redis.conf配置文件

1>注释掉bind

#bind 127.0.0.1

2>禁用保护模式

protected-mode no


3、配置thinkphp配置redis信息

	'DATA_CACHE_PREFIX' => 'Redis_',//缓存前缀
 	'DATA_CACHE_TYPE'=>'Redis',//默认动态缓存为Redis
 	'DATA_CACHE_TIMEOUT'=>'1000',
 	'REDIS_RW_SEPARATE' => true, //Redis读写分离 true 开启
 	'REDIS_HOST'=>'IP地址', //redis服务器ip,多台用逗号隔开;读写分离开启时,第一台负责写,其它[随机]负责读;
 	'REDIS_PORT'=>'6379',//端口号
 	'REDIS_TIMEOUT'=>'1000',//超时时间
 	'REDIS_PERSISTENT'=>false,//是否长连接 false=短连接
 	'REDIS_AUTH_PASSWORD'=>'password',//AUTH认证密码

4、demo

            $Cache = Cache::getInstance('Redis');
            $Cache->set('name2','easdfasd',6400);  // 缓存name数据
            $value = $Cache->get('name2');  // 获取缓存的name数据
            var_dump( $value);
            exit();

错误提示:

thinkphp使用redis缓存的时候无法使用认证
我在使用thinkphp的时候 发现如果是使用redis缓存 设置了认证的redis能连接成功 却无法 set 操作 ,检查发现是没有认证导致的  $redis->auth这一步没有,那么官方给出的 Redis.class.php没有的话,我们可以自己加上,在构造函数第29行 将以前的代码改为:
以前代码如下:
 
        $options = array_merge(array (
            'host'          => C('REDIS_HOST') ? : '127.0.0.1',
            'port'          => C('REDIS_PORT') ? : 6379,
            'timeout'       => C('DATA_CACHE_TIMEOUT') ? : false,
            'persistent'    => false,
        ),$options);
 
加一行 'auth' => C('REDIS_AUTH_PASSWORD') ? C('REDIS_AUTH_PASSWORD'):null,//auth认证的密码  ,改为这样
 
        $options = array_merge(array (
            'host'          => C('REDIS_HOST') ? : '127.0.0.1',
            'port'          => C('REDIS_PORT') ? : 6379,
            'timeout'       => C('DATA_CACHE_TIMEOUT') ? : false,
            'auth'            => C('REDIS_AUTH_PASSWORD') ? C('REDIS_AUTH_PASSWORD'):null,//auth认证的密码
            'persistent'    => false,
        ),$options);
 
这样就能在options中读取到是否启用认证的密码了,然后后面加一个判断
在这段代码后面
        $this->handler  = new \Redis;
        $options['timeout'] === false ?
            $this->handler->$func($options['host'], $options['port']) :
            $this->handler->$func($options['host'], $options['port'], $options['timeout']);
加上一个判断,判断是否启用了认证密码配置 启用了就去认证一下
        if($this->options['auth']!==null)
        {
            $this->handler->auth($this->options['auth']); //说明有配置redis的认证配置密码 需要认证一下
        }
总体来看,构造方法被改为了如下:
 
    public function __construct($options=array()) {
        if ( !extension_loaded('redis') ) {
            E(L('_NOT_SUPPORT_').':redis');
        }
        $options = array_merge(array (
            'host'          => C('REDIS_HOST') ? : '127.0.0.1',
            'port'          => C('REDIS_PORT') ? : 6379,
            'timeout'       => C('DATA_CACHE_TIMEOUT') ? : false,
            'auth'            => C('REDIS_AUTH_PASSWORD') ? C('REDIS_AUTH_PASSWORD'):null,//auth认证的密码
            'persistent'    => false,
        ),$options);

        $this->options =  $options;
        $this->options['expire'] =  isset($options['expire'])?  $options['expire']  :   C('DATA_CACHE_TIME');
        $this->options['prefix'] =  isset($options['prefix'])?  $options['prefix']  :   C('DATA_CACHE_PREFIX');        
        $this->options['length'] =  isset($options['length'])?  $options['length']  :   0;        
        $func = $options['persistent'] ? 'pconnect' : 'connect';
        $this->handler  = new \Redis;
        $options['timeout'] === false ?
            $this->handler->$func($options['host'], $options['port']) :
            $this->handler->$func($options['host'], $options['port'], $options['timeout']);
        if($this->options['auth']!=null)
        {
            $this->handler->auth($this->options['auth']); //说明有配置redis的认证配置密码 需要认证一下
        }
    }
 
然后配置文件里面加上 "REDIS_AUTH_PASSWORD"=>"redis认证密码" 即可

 
  


你可能感兴趣的:(Redis)