Example of using PHP to access Redis method with password

黄舟
Release: 2023-03-06 12:24:01
Original
1964 people have browsed it

This article mainly introduces how to use PHP to access Redis with password. The article starts by introducing how to set the Redis password and the method of Redis with password, which is convenient for everyone to learn and understand. Friends in need can refer to it. Below Let’s take a look together.

1. First set the Redis password to provide remote login

Open the redis.conf configuration file, find requirepass, and then modify it as follows:

requirepass yourpassword
Copy after login

yourpassword is the redis verification password. After setting the password, I found that I can log in, but I cannot execute the command.

The command is as follows:

redis-cli -h 127.0.0.1 -p 6379//启动redis客户端,并连接服务器
Copy after login

keys * //输出服务器中的所有key
Copy after login

The error is reported as follows

(error) ERR operation not permitted
Copy after login

At this time you can use the authorization command to authorize, and no error will be reported

The command is as follows:

auth youpassword
Copy after login

2. PHP access Redis

$redis = new Redis();
$conn = $redis->connect('localhost', 6379);
$auth = $redis->auth('20160601'); //设置密码
var_dump($auth);
$redis->set('access_token', "123213213213213213");
$redis->set('expired_time', 1464344863);

var_dump($redis->get("access_token"));
var_dump($redis->get("expired_time"));
Copy after login

Summary

The above is an example of using PHP to access Redis with password. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!