Redis 连接

Redis 连接命令主要是用于连接 Redis 服务、密码验证、检测 Redis 服务是否运行、以及数据库切换。

实例

下面实例演示了客户端如何通过密码验证连接到 redis 服务,并检测服务是否在运行。如下:

127.0.0.1:6379> AUTH "password"
OK
127.0.0.1:6379> PING
PONG

Redis 连接命令

下表列出了 redis 连接的基本命令。

AUTH命令

该命令用于验证客户端连接的 Redis 服务器密码是否正确。语法如下:

AUTH password

实例:

首先,我们需要设置 redis 的密码,如下:

################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared 下面配置密码
requirepass  123456

然后,通过 auth 命令去验证密码,如下:

C:\Users\Administrator>redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379>

ECHO命令

打印字符串。语法如下:

ECHO message

实例:

127.0.0.1:6379> echo hello
"hello"
127.0.0.1:6379>

PING命令

该命令使用客户端向 Redis 服务器发送一个 PING,如果服务器运作正常的话,会返回一个 PONG。通常用于测试与服务器的连接是否仍然生效,或者用于测量延迟值。语法如下:

PING [message]

实例:

# 客户端和服务器连接正常
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> ping hello
"hello"
# 客户端和服务器连接不正常(网络不正常或服务器未能正常运行)
127.0.0.1:6379> ping
Could not connect to Redis at 127.0.0.1:6379: 由于目标计算机积极拒绝,无法连接。
(2.03s)

QUIT命令

该命令用于关闭当前客户端与 redis 服务的连接。一旦所有等待中的回复(如果有的话)顺利写入到客户端,连接就会被关闭。语法如下:

QUIT

实例:

127.0.0.1:6379> quit

SELECT命令

用于切换到指定的数据库,数据库索引号 index 用数字值指定,以 0 作为起始索引值。默认情况下,redis 有十六个数据库,分别为 0~15。可以通过如下配置修改数据库个数,如下:

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16

语法如下:

SELECT index

实例:

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 0
OK
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号