• 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    尝试用密码登录并执行具体的命令看到可以成功执行

    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"  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

    这说明目前处在保护模式上,查看Redis的注释可以了解,连接Redis只能通过本地(127.0.0.1)来连接,而不能使用网络IP(192.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认证密码
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4、demo

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

    错误提示:

    thinkphp使用redis缓存的时候无法使用认证
    我在使用thinkphp的时候 发现如果是使用redis缓存 设置了认证的redis能连接成功 却无法 set 操作 ,检查发现是没有认证导致的 $redis->auth这一步没有,那么官方给出的 Redis.class.php没有的话,我们可以自己加上,在构造函数第29行 将以前的代码改为:
    以前代码如下:

    o p t i o n s = a r r a y _ m e r g e ( a r r a y ( ′ h o s t ′ = > C ( ′ R E D I S _ H O S T ′ ) : ′ 127.0.0. 1 ′ , ′ p o r t ′ = > C ( ′ R E D I S _ P O R T ′ ) : 6379 , ′ t i m e o u t ′ = > C ( ′ D A T A _ C A C H E _ T I M E O U T ′ ) : f a l s e , ′ p e r s i s t e n t ′ = > f a l s e , ) , 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=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认证的密码 ,改为这样

    o p t i o n s = a r r a y _ m e r g e ( a r r a y ( ′ h o s t ′ = > C ( ′ R E D I S _ H O S T ′ ) : ′ 127.0.0. 1 ′ , ′ p o r t ′ = > C ( ′ R E D I S _ P O R T ′ ) : 6379 , ′ t i m e o u t ′ = > C ( ′ D A T A _ C A C H E _ T I M E O U T ′ ) : f a l s e , ′ a u t h ′ = > C ( ′ R E D I S _ A U T H _ P A S S W O R D ′ ) C ( ′ R E D I S _ A U T H _ P A S S W O R D ′ ) : n u l l , / / a u t h 认证的密 码 ′ p e r s i s t e n t ′ = > f a l s e , ) , 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=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
    t h i s − > h a n d l e r − > this->handler-> this>handler>func($options[‘host’], $options[‘port’]) :
    t h i s − > h a n d l e r − > this->handler-> this>handler>func($options[‘host’], $options[‘port’], KaTeX parse error: Undefined control sequence:

    atposition8:options[̲timeout" role="presentation">atposition8:options[̲timeout
    ); …this->options[‘auth’]!==null)
    {
    t h i s − > h a n d l e r − > a u t h ( this->handler->auth( this>handler>auth(this->options[‘auth’]); //说明有配置redis的认证配置密码 需要认证一下
    }
    总体来看,构造方法被改为了如下:

    public function __construct($options=array()) {
    if ( !extension_loaded(‘redis’) ) {
    E(L(‘_NOT_SUPPORT_’).‘:redis’);
    }
    o p t i o n s = a r r a y _ m e r g e ( a r r a y ( ′ h o s t ′ = > C ( ′ R E D I S _ H O S T ′ ) : ′ 127.0.0. 1 ′ , ′ p o r t ′ = > C ( ′ R E D I S _ P O R T ′ ) : 6379 , ′ t i m e o u t ′ = > C ( ′ D A T A _ C A C H E _ T I M E O U T ′ ) : f a l s e , ′ a u t h ′ = > C ( ′ R E D I S _ A U T H _ P A S S W O R D ′ ) C ( ′ R E D I S _ A U T H _ P A S S W O R D ′ ) : n u l l , / / a u t h 认证的密 码 ′ p e r s i s t e n t ′ = > f a l s e , ) , 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=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;
    KaTeX parse error: Undefined control sequence:

    atposition14:this>options[̲expire" role="presentation" style="text-align: center; position: relative;">atposition14:this>options[̲expire
    = is…options[‘expire’]) $options[‘expire’] : C(‘DATA_CACHE_TIME’);
    KaTeX parse error: Undefined control sequence:
    atposition14:this>options[̲prefix" role="presentation" style="text-align: center; position: relative;">atposition14:this>options[̲prefix
    = is…
    options[‘prefix’]) $options[‘prefix’] : C(‘DATA_CACHE_PREFIX’);
    KaTeX parse error: Undefined control sequence:
    atposition14:this>options[̲length" role="presentation" style="text-align: center; position: relative;">atposition14:this>options[̲length
    = is…
    options[‘length’]) $options[‘length’] : 0;
    $func = $options[‘persistent’] ‘pconnect’ : ‘connect’;
    $this->handler = new Redis;
    $options[‘timeout’] === false
    t h i s − > h a n d l e r − > this->handler-> this>handler>func($options[‘host’], $options[‘port’]) :
    t h i s − > h a n d l e r − > this->handler-> this>handler>func($options[‘host’], $options[‘port’], KaTeX parse error: Undefined control sequence:
    atposition8:options[̲timeout" role="presentation" style="text-align: center; position: relative;">atposition8:options[̲timeout
    ); …
    this->options[‘auth’]!=null)
    {
    t h i s − > h a n d l e r − > a u t h ( this->handler->auth( this>handler>auth(this->options[‘auth’]); //说明有配置redis的认证配置密码 需要认证一下
    }
    }

    然后配置文件里面加上 “REDIS_AUTH_PASSWORD”=>“redis认证密码” 即可

  • 相关阅读:
    使用EF 连接 数据库 SQLserver、MySql 实现 CodeFirst
    NAS 初始化设置
    [iOS界面切换- Present And Push]
    【浅学Java】JVM面试必备
    2242902-55-0_Desthiobiotin-phenol_脱硫生物素价格
    请求跨域问题
    2023国考证件照要求什么底色?证件照换背景底色的方法
    Python之高阶函数
    yolov5 C3改进|深度可分离卷积轻量化主干
    如何配置React-Router?
  • 原文地址:https://blog.csdn.net/m0_67394230/article/details/126495666