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
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客户端,并连接服务器
keys * //输出服务器中的所有key
The error is reported as follows
(error) ERR operation not permitted
At this time you can use the authorization command to authorize, and no error will be reported
The command is as follows:
auth youpassword
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"));
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)!