安装与启动
安装
直接安装
1 | yum install -y redis |
没有源 下载并安装
1 | yum install -y epel-release |
开启服务
开启方式一
1 | service redis start |
关闭服务
1 | service redis stop |
或者
1 | redis-cli shutdown |
如果启动报错可以使用下面的方式启动查看错误信息
1 | redis-server /etc/redis.conf |
设置开机自启
1 | chkconfig redis on |
查看运行状态
1 | ps -ef | grep redis |
Redis设置
允许远程连接
yum方式安装的redis配置文件通常在/etc/redis.conf
中,打开配置文件找到
1 | vi /etc/redis.conf |
注释bind 127.0.0.1
就可以远程访问 如下
1 | bind 127.0.0.1 |
改为
1 | # bind 127.0.0.1 |
Redis3.2之后还要修改protected-mode
默认的yes
改为no
1 | protected-mode no |
重启1
service redis restart
设置连接密码
yum方式安装的redis配置文件通常在/etc/redis.conf
中,打开配置文件找到
1 | vim /etc/redis.conf |
找到
1 | # requirepass foobared |
去掉行前的注释,并修改密码为所需的密码,保存文件
1 | requirepass 1qaz2wsxasd |
重启redis
1 | service redis restart |
带密码连接
1 | redis-cli -h 127.0.0.1 -p 6379 -a 1qaz2wsxasd |
设置连接数
带密码连接
1 | redis-cli -h 127.0.0.1 -p 6379 -a myRedis |
连接后
查看现在连接数
1 | info clients |
查看最大连接数
1 | config get maxclients |
具体的客户端连接
1 | client list |
修改最大连接数
1 | config set maxclients 10208 |
永久生效
在2.6之后版本,可以修改最大连接数配置,默认10000
可以在/etc/redis.conf
配置文件中修改
取消注释 修改值
1 | maxclients 10208 |
重启
1 | service redis rstart |
Redis操作
赋值取值
进入redis服务
1 | redis-cli |
赋值取值
1 | set psvmc 123456 |
清空Redis
清除当前数据库缓存
1 | select 0 |
清除整个redis所有缓存
1 | flushall |
获取KEY
表达式
1 | SCAN cursor [MATCH pattern] [COUNT count] |
示例
1 | scan 0 MATCH *AA* COUNT 10 |
其中
cursor 为 0 的时候是进行新的搜索,如果想接着检索 输入上次返回的值
如图想接着检索就要输入
1 | scan 92 MATCH *AA* COUNT 10 |
查询所有KEY
1 | keys * |
防火墙设置
开放端口6379、6380的防火墙
1 | /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT 开启6379 |
保存
1 | /etc/rc.d/init.d/iptables save |
客户端下载
使用Redis Desktop Manager
连接Redis
- 下载地址:
Redis Desktop Manager
- 密码:
h13m
推荐 AnotherRedisDesktopManager
https://github.com/qishibo/AnotherRedisDesktopManager/releases
常见问题
问题描述:
1 | MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error. |
翻译:Redis被配置为保存数据库快照,但它目前不能持久化到硬盘。用来修改集合数据的命令不能用。请查看Redis日志的详细错误信息
解决方式
连接redis
执行2条命令
1
2config set stop-writes-on-bgsave-error no
lpush myColour "red"过程如下
1
2
3
4127.0.0.1:6379> config set stop-writes-on-bgsave-error no
127.0.0.1:6379> OK
127.0.0.1:6379> lpush myColour "red"
127.0.0.1:6379> (integer) 1
或者直接修改 默认配置
1 | vi /etc/redis.conf |
stop-writes-on-bgsave-error
的值从yes
设置为no
1 | stop-writes-on-bgsave-error no |
重启redis
1 | service redis restart |