当前位置:网站首页>Redis series 3: ThinkPHP uses redis
Redis series 3: ThinkPHP uses redis
2022-04-22 15:04:00 【m0_ fifty-four million eight hundred and fifty thousand four hu】
1、redis Server configuration authentication password
(1) Configure through the configuration file
Open profile /usr/local/redis/etc/redis.conf find
#requirepass foobared
Remove the comment before the line , And change the password to the desired one , Save the file
requirepass myRedis
restart redis
Try to login at this time redis, Find out you can get on , However, the execution of specific commands is a prompt operation, which is not allowed
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]>
Try to login with password and execute specific command. You can see that it can be executed successfully
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 The server configures external access

This shows that it is in the protection mode , see Redis You can understand , Connect Redis Only through local (127.0.0.1) To connect , Instead of using the Internet IP(192.168.1.x) To connect , If necessary, please modify the configuration file redis.conf
Solution
Get into Redis Directory open Redis.conf The configuration file
1> Comment out bind
#bind 127.0.0.1
2> Protection mode is disabled
protected-mode no
3、 To configure thinkphp To configure redis Information
'DATA_CACHE_PREFIX' => 'Redis_',// Cache prefix
'DATA_CACHE_TYPE'=>'Redis',// The default dynamic cache is Redis
'DATA_CACHE_TIMEOUT'=>'1000',
'REDIS_RW_SEPARATE' => true, //Redis Read / write separation true Turn on
'REDIS_HOST'=>'IP Address ', //redis The server ip, Multiple sets are separated by commas ; When read-write separation is on , The first one is responsible for writing , Other [ Random ] Responsible for reading ;
'REDIS_PORT'=>'6379',// Port number
'REDIS_TIMEOUT'=>'1000',// Timeout time
'REDIS_PERSISTENT'=>false,// Long connection or not false= Short connection
'REDIS_AUTH_PASSWORD'=>'password',//AUTH The authentication code
4、demo
$Cache = Cache::getInstance('Redis');
$Cache->set('name2','easdfasd',6400); // cache name data
$value = $Cache->get('name2'); // Get cached name data
var_dump( $value);
exit();
Error message :
thinkphp Use redis Authentication cannot be used when caching
I am using thinkphp When Find out if you are using redis cache Set up the authentication redis Can connect successfully But not set operation , The inspection found that there was no certification $redis->auth This step does not , So the official Redis.class.php If not , We can add it ourselves , In the constructor section 29 That's ok Change the previous code to :
The code is as follows :
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);
Add a line ‘auth’ => C(‘REDIS_AUTH_PASSWORD’) C(‘REDIS_AUTH_PASSWORD’):null,//auth Authenticated password , Instead
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 recognize Prove Of The secret code ′ 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 Authenticated password '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 recognize Prove Of The secret code ′persistent′=>false,),options);
So you can options Read the password of whether to enable authentication in , Then add a judgment
After this code
$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: \[ at position 8: 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’]); // Description configuration redis Authentication configuration password Need certification
}
overall , The construction method is changed as follows :
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 recognize Prove Of The secret code ′ 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 Authenticated password '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 recognize Prove Of The secret code ′persistent′=>false,),options);
$this->options = $options;
KaTeX parse error: Undefined control sequence: \[ at position 14: this->options\̲[̲'expire'\] = is…options[‘expire’]) $options[‘expire’] : C(‘DATA_CACHE_TIME’);
KaTeX parse error: Undefined control sequence: \[ at position 14: this->options\̲[̲'prefix'\] = is…options[‘prefix’]) $options[‘prefix’] : C(‘DATA_CACHE_PREFIX’);
KaTeX parse error: Undefined control sequence: \[ at position 14: 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: \[ at position 8: 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’]); // Description configuration redis Authentication configuration password Need certification
}
}
Then add... To the configuration file “REDIS_AUTH_PASSWORD”=>“redis The authentication code ” that will do
版权声明
本文为[m0_ fifty-four million eight hundred and fifty thousand four hu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221500049376.html
边栏推荐
猜你喜欢

Watch out for spies! Safeguard national security, you and I guard together

redis优化系列(一)基于docker搭建Redis主从

dried food! Reflection on the image migration of targeted confrontation

人脸识别 (4) 人脸对齐
![【深入理解TcaplusDB技术】扫描数据示例代码——[Generic表]](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
【深入理解TcaplusDB技术】扫描数据示例代码——[Generic表]

中国海油上交所上市:市值6515亿 年利润703亿

2021年漏洞利用状况:已公开的传统漏洞占95%

数学——贝塞尔曲线

Tencent cloud im integration (so easy)

2022年化工自动化控制仪表考试题库及在线模拟考试
随机推荐
Android eye protection function, double non undergraduate byte beating Android interview question sharing
[C language] basic concepts of C language
【C语言】C语言的基本概念
微信小程序----第四天
查看自己的系统激活类型
Leave a hole in postmass
升级了源端数据库到5.0版本,重启mongoshake服务,然后报错了
【深入理解TcaplusDB技术】批量删除列表指定位置数据接口说明——[List表]
SMB+MSSQL
攒机笔记二十:改造笔记本电脑(华硕A555L)
【朋友圈服务器架构设计】
这两种人是做不好自媒体的,一辈子都赚不到大钱
JVM garbage collection parnew and CMS introduction
2022茶艺师(中级)考试题及答案
爱站网关键词挖掘查询工具-批量网站关键词挖掘导出软件免费下载
带你了解极具弹性的Spark架构的原理
数据库操作
[ELT. Zip] openharmony paper club -- one article penetrates the forefront of Multimedia
PCBA/IPQ4019 /openWRT 2.4/5G dual bands IPQ4019 openWRT 2.4/5G dual bands
[ELT. Zip] openharmony paper club -- Interpretation of compressed coding from the perspective of overview