Linux 安装Redis

安装Redis

直接安装

1
yum install -y redis

没有源 下载并安装

1
2
yum install -y epel-release
yum install -y redis

开启服务

开启方式一

1
service redis start

开启方式二

1
redis-server /etc/redis.conf

关闭服务

1
redis-cli  shutdown

设置开机自启

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 myRedis

重启redis

1
service redis restart

带密码连接

1
redis-cli -h 127.0.0.1 -p 6379 -a myRedis

设置连接数

带密码连接

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
2
set psvmc 123456
get psvmc

清空Redis

清除当前数据库缓存

1
2
3
select 0
dbsize
flushdb

清除整个redis所有缓存

1
flushall

获取KEY

表达式

1
SCAN cursor [MATCH pattern] [COUNT count]

示例

1
scan 0 MATCH *AA* COUNT 10

其中

cursor 为 0 的时候是进行新的搜索,如果想接着检索 输入上次返回的值

image-20220726173313938

如图想接着检索就要输入

1
scan 92 MATCH *AA* COUNT 10

查询所有KEY

1
2
3
keys *
keys AAAAAA*
keys *_p_web*

防火墙设置

开放端口6379、6380的防火墙

1
2
/sbin/iptables -I INPUT -p tcp --dport 6379  -j ACCEPT   开启6379
/sbin/iptables -I INPUT -p tcp --dport 6380 -j ACCEPT 开启6380

保存

1
/etc/rc.d/init.d/iptables save

客户端下载

使用Redis Desktop Manager连接Redis

推荐 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日志的详细错误信息

解决方式

  1. 连接redis

  2. 执行2条命令

    1
    2
    config set stop-writes-on-bgsave-error no
    lpush myColour "red"

    过程如下

    1
    2
    3
    4
    127.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