redis 连接数据库_如何连接到Redis数据库

redis 连接数据库

介绍 (Introduction)

Redis is an open-source, in-memory key-value data store. Whether you’ve installed Redis locally or you’re working with a remote instance, you need to connect to it in order to perform most operations. In this tutorial we will go over how to connect to Redis from the command line, how to authenticate and test your connection, as well as how to close a Redis connection.

Redis是一个开源的内存中键值数据存储。 无论您是在本地安装Redis还是使用远程实例,都需要连接到它才能执行大多数操作。 在本教程中,我们将介绍如何从命令行连接到Redis,如何验证和测试您的连接以及如何关闭Redis连接。

如何使用本指南 (How To Use This Guide)

This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.

本指南以备有完整示例的备忘单形式编写。 我们鼓励您跳至与您要完成的任务相关的任何部分。

The commands shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, you can follow Step 1 of our guide on How To Install and Secure Redis on Ubuntu 18.04. We will demonstrate how these commands behave by running them with redis-cli, the Redis command line interface. Note that if you’re using a different Redis interface — Redli, for example — the exact output of certain commands may differ.

本指南中显示的命令已在运行Redis版本4.0.9的Ubuntu 18.04服务器上进行了测试。 要设置类似的环境,您可以按照我们的指南如何在Ubuntu 18.04上安装和保护Redis的 步骤1进行操作。 我们将通过使用Redis命令行界面redis-cli运行它们来演示这些命令的行为。 请注意,如果您使用其他Redis界面(例如Redli) ,则某些命令的确切输出可能会有所不同。

Alternatively, you could provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our Managed Databases product documentation. Then, you must either install Redli or set up a TLS tunnel in order to connect to the Managed Database over TLS.

另外,您可以提供一个托管的Redis数据库实例来测试这些命令,但是请注意,根据数据库提供者所允许的控制级别,本指南中的某些命令可能无法按所述方式工作。 要配置DigitalOcean托管数据库,请遵循我们的托管数据库产品文档 。 然后, 您必须 安装Redli 设置TLS隧道才能通过TLS连接到托管数据库。

连接到Redis (Connecting to Redis)

If you have redis-server installed locally, you can connect to the Redis instance with the redis-cli command:

如果您在本地安装了 redis-server ,则可以使用redis-cli命令连接到Redis实例:

  • redis-cli

    redis-cli

This will take you into redis-cli’s interactive mode which presents you with a read-eval-print loop (REPL) where you can run Redis’s built-in commands and receive replies.

这将带您进入redis-cli的_交互模式_ ,该_模式_为您提供一个read-eval-print循环 (REPL),您可以在其中运行Redis的内置命令并接收答复。

In interactive mode, your command line prompt will change to reflect your connection. In this example and others throughout this guide, the prompt indicates a connection to a Redis instance hosted locally (127.0.0.1) and accessed over Redis’s default port (6379):

在交互模式下,命令行提示符将更改以反映您的连接。 在本示例以及本指南中的其他示例中,提示指示与本地托管的Redis实例( 127.0.0.1 )的连接,并通过Redis的默认端口( 6379 )进行访问:

The alternative to running Redis commands in interactive mode is to run them as arguments to the redis-cli command, like so:

在交互方式下运行Redis命令的替代方法是将它们作为redis-cli命令的参数运行,如下所示:

  • redis-cli redis_command

    redis-cli redis_command

If you want to connect to a remote Redis datastore, you can specify its host and port numbers with the -h and -p flags, respectively. Also, if you’ve configured your Redis database to require a password, you can include the -a flag followed by your password in order to authenticate:

如果要连接到远程 Redis数据存储,可以分别使用-h-p标志指定其主机和端口号。 另外,如果您已将Redis数据库配置为要求输入密码,则可以在密码后面加上-a标志,以进行身份验证:

  • redis-cli -h host -p port_number -a password

    redis-cli -h 主机 -p port_number -a 密码

If you’ve set a Redis password, clients will be able to connect to Redis even if they don’t include the -a flag in their redis-cli command. However, they won’t be able to add, change, or query data until they authenticate. To authenticate after connecting, use the auth command followed by the password:

如果您设置了Redis密码,则即使客户端的redis-cli命令中未包含-a标志,它们也将能够连接到Redis。 但是,他们只有在通过身份验证后才能添加,更改或查询数据。 要在连接后进行身份验证,请使用auth命令以及密码:

  • auth password

    验证密码

If the password passed to auth is valid, the command will return OK. Otherwise, it will return an error.

如果传递给auth的密码有效,则命令将返回OK 。 否则,它将返回错误。

If you’re working with a managed Redis database, your cloud provider may give you a URI that begins with redis:// or rediss:// which you can use to access your datastore. If the connection string begins with redis://, you can include it as an argument to redis-cli to connect.

如果您使用的是托管Redis数据库,则云提供商可能会为您提供以redis://rediss://开头的URI,您可以使用它来访问数据存储。 如果连接字符串以redis://开头,则可以将其包含为redis-cli进行连接的参数。

However, if you have a connection string that begins with rediss://, that means your managed database requires connections over TLS/SSL. redis-cli does not support TLS connections, so you’ll need to use a different tool that supports the rediss protocol in order to connect with the URI. For DigitalOcean Managed Databases, which require connections to be made over TLS, we recommend using Redli to access the Redis instance.

但是,如果您有一个以rediss://开头的连接字符串,则意味着您的托管数据库需要通过TLS / SSL进行连接。 redis-cli不支持TLS连接,因此您需要使用支持rediss协议的其他工具才能与URI连接。 对于需要通过TLS建立连接的DigitalOcean托管数据库,我们建议使用Redli访问Redis实例。

Use the following syntax to connect to a database with Redli. Note that this example includes the --tls option, which specifies that the connection should be made over TLS, and the -u flag, which declares that the following argument will be a connection URI:

使用以下语法通过Redli连接到数据库。 请注意,此示例包括--tls选项和-u标志,该选项指定应通过TLS建立连接,该标志声明以下参数为连接URI:

  • redli --tls -u rediss://connection_URI

    redli --tls -u rediss:// 连接 _URI

If you’ve attempted to connect to an unavailable instance, redis-cli will go into disconnected mode. The prompt will reflect this:

如果您尝试连接到不可用的实例,则redis-cli将进入_断开连接模式_ 。 提示将反映以下内容:

Redis will attempt to reestablish the connection every time you run a command when it’s in a disconnected state.

每次在断开连接状态下运行命令时,Redis都会尝试重新建立连接。

测试连接 (Testing Connections)

The ping command is useful for testing whether the connection to a database is alive. Note that this is a Redis-specific command and is different from the [这里是代码052] networking utility. However, the two share a similar function in that they’re both used to check a connection between two machines.

ping命令对于测试与数据库的连接是否有效非常有用。 请注意,这是Redis专用的命令,与[这里是代码054]网络实用程序不同 。 但是,两者共享相似的功能,因为它们都用于检查两台计算机之间的连接。

If the connection is up and no arguments are included, the ping command will return PONG:

如果连接建立并且不包含任何参数,则ping命令将返回PONG

  • ping

    ping

    Output
    

    PONG

If you provide an argument to the ping command, it will return that argument instead of PONG if the connection is successful:

如果为ping命令提供参数,则如果连接成功,它将返回该参数而不是PONG

  • ping “hello Redis!”

    ping“你好Redis!”

    Output
    

    “hello Redis!”

If you run ping or any other command in disconnected mode, you will see an output like this:

如果在断开连接的模式下运行ping或任何其他命令,将看到类似以下的输出:

  • ping

    ping

    Output
    

    Could not connect to Redis at host:port: Connection refused

Note that ping is also used by Redis internally to measure latency.

请注意,Redis在内部也使用ping 来测量延迟 。

与Redis断开连接 (Disconnecting from Redis)

To disconnect from a Redis instance, use the quit command:

要与Redis实例断开连接,请使用quit命令:

  • quit

    退出

Running exit will also exit the connection:

运行exit还将退出连接:

  • exit

    出口

Both quit and exit will close the connection, but only as soon as all pending replies have been written to clients.

quitexit都将关闭连接,但是只有将所有未决的回复写到客户端后,才能关闭连接。

结论 (Conclusion)

This guide details a number of commands used to establish, test, and close connections to a Redis server. If there are other related commands, arguments, or procedures you’d like to see in this guide, please ask or make suggestions in the comments below.

本指南详细介绍了许多用于建立,测试和关闭与Redis服务器的连接的命令。 如果您想在本指南中看到其他相关的命令,参数或过程,请在下面的评论中提出疑问或提出建议。

For more information on Redis commands, see our tutorial series on How to Manage a Redis Database.

有关Redis命令的更多信息,请参阅关于如何管理Redis数据库的系列教程。

翻译自: https://www.digitalocean.com/community/cheatsheets/how-to-connect-to-a-redis-database

redis 连接数据库

你可能感兴趣的:(java,java,后端)