为 Redis 配置密码
当我们想要对我们的 Redis 进行密码保护时,我们可以通过修改配置文件或者使用 config 命令进行修改。
配置文件方式
配置文件路径:/etc/redis.conf
[root@RLKJ-BT ~]# cat /etc/redis.conf | grep requirepass
# If the master is password protected (using the "requirepass" configuration
requirepass "pwd@123"
[root@RLKJ-BT ~]#
如上,将
requirepass字段的值改为你想设置的密码,重启redis服务即可。
连接 redis 测试

输入密码后测试

命令方式
配置命令:config set requirepass <pwd>
127.0.0.1:6379> config set requirepass pwd@321 OK 127.0.0.1:6379> exit [root@RLKJ-BT ~]# redis-cli 127.0.0.1:6379> set name phyger (error) NOAUTH Authentication required. 127.0.0.1:6379> auth pwd@321 OK 127.0.0.1:6379>
Redis 备份恢复
简单的,我们一般通过 save 命令将数据存盘进行备份,当我们需要恢复的时候只需要将安装目录备份的 dump.rdb 文件移动到需要恢复的 redis 实例的安装目录下,重启 redis 服务即可。当数据量比较大的时候,我们可以使用 bgsave 命令让 redis 在后台进行备份。
常用的 Redis 命令
| 命令 | 用途 |
|---|---|
| info | 查询 redis 的信息 |
| save | 数据保存到安装目录的 dump.rdb |
| bgsave | 数据保存操作后台执行 |
| lastsave | 返回最近一次存盘时间 |
| config get dir | 查询 redis 安装目录 |
| client list | 查询客户端列表 |
| command | 查询所有的命令及子命令 |
| command count | 查询命令总数 |
| dbsize | 查询所有的 key |
| flushall | 删除所有 db 的所有 key |
| flushdb | 删除当前 db 的所有 key |
| monitor | 实时打印出 Redis 服务器接收到的命令,调试用 |
| role | 返回主从实例所属的角色 |
| config set parameter value | 修改 redis 配置参数,无需重启 |
| time | 返回当前服务器时间 |
| select | db 切换 |
更多命令详见 redis 官方文档。
http://www.redis.cn/documentation.html
Python操作Redis点击这里查看详情。