【Redis】RCMD 01 访问 & 键命令

 

客户端本地访问Redis:

redis-Cli

客户端远程访问Redis:

redis-cli -h host -p port -a password

这里我没有对服务器的Redis设置密码,就没有 -a 密码参数了,这里回到redis.conf进行配置

requirepass 你设置的密码

但是服务器的Redis能被远程访问,需要对Redis.conf配置文件的BIND进行设置,直接注释掉bind配置即可

# bind 127.0.0.1

 

除了这些,再远程这端操作命令发现被Redis拒绝了,原因是因为没有解除保护模式:

redis在启动的时候默认会启动一个保护模式,只有同一个服务器可以连接上redis。别的服务器连接不上这个redis

(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address
 was specified, no authentication password is requested to clients. In this mode connections are onl
y accepted from the loopback interface. If you want to connect from external computers to Redis you
may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG
 SET protected-mode no' from the loopback interface by connecting to Redis from the same host the se
rver is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use
CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mo
de by editing the Redis configuration file, and setting the protected mode option to 'no', and then
restarting the server. 3) If you started the server manually just for testing, restart it with the '
--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only n
eed to do one of the above things in order for the server to start accepting connections from the ou
tside.

设置了密码之后重启,使用密码登录远程访问不再有任何问题了。。。

参考的解决办法:

https://blog.csdn.net/persistencegoing/article/details/88890402

无密码访问,操作会有授权要求:

redis-cli -h 49.234.116.100 -p 6379 -a redis.conf中设置的密码

 

检测 redis 服务是否启动

ping

关闭redis服务:

redis-cli -h 127.0.0.1 -p 6379 shutdown

简单的概念:

一个redis中共有16个数据库,【0 - 15】

清除当前库数据:

flushdb

清除所有库:

flushall

切换【选择】库:

SELECT ? (0 ~ 15

查看当前库有多少键:

DBSIZE

 

随机返回一个在库中存在的键:

RANDOMKEY

查看指定的键的剩余时间:

TTL key-name

 

移动键到指定的库:

MOVE key-name DB-index (0 ~ 15)

键命令:KEY

命令格式:操作命令 键的名称

创建一个键并且绑定一个值

SET key-name value-name

指令不区分大小写,但是键和值区分

 

 

通过键获取值:

GET key-name

 

检查是否存在这个键:

存在返回1,否则返回0

EXISTS key-name

 

将这个键进行序列化,返回被序列化的值:

DUMP key-name

 

将已经写入的键,设定过期时间,超过时间销毁此键:

EXPIRE key-name seconds

或者可以在创建键的时候,就指定过期时间:

SET key-name value-name EX seconds

 

将当前Redis库中的键移动到指定的Redis库中:

MOVE key db-name

 

将键进行持久化,移除过期时间:

PERSIST key-name

 

更改键的名称:

RENAME key-name new-key-name

先判断新的键名是否存在再执行更改:

RENAMENX key-name newkey-name

判断有,返回0,不更改。

反之为1,更改

 

查看该键所存储的值的数据类型:[这里纠正,是存储类型]

TYPE key-name

 

查看当前库的所有键:

keys *

 

参考地址:

https://www.runoob.com/redis/redis-keys.html

 

posted @ 2020-09-09 10:39  emdzz  阅读(404)  评论(0)    收藏  举报