redis 连接数据库_如何连接到Redis数据库_redis连接数据库-程序员宅基地

技术标签: java  后端  

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 连接数据库

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_67401761/article/details/123791527

智能推荐

什么是深度学习,深度学习和机器学习有什么关系?_深度学习,是机器学习的一种特定技术,称其为深度,是因为他有()结构。-程序员宅基地

文章浏览阅读3.2k次。深度学习的概念源于人工神经网络的研究。含多隐层的多层感知器就是一种深度学习结构。深度学习通过组合低层特征形成更加抽象的高层表示属性类别或特征,以发现数据的分布式特征表示。晦涩难懂的概念,略微有些难以理解,但是在其高冷的背后,却有深远的应用场景和未来。深度学习是实现机器学习的一种方式或一条路径。其动机在于建立、模拟人脑进行分析学习的神经网络,它模仿人脑的机制来解释数据。比如其按特定的物理距离连接;..._深度学习,是机器学习的一种特定技术,称其为深度,是因为他有()结构。

bootstrap tab切换后,刷新 页面 回到被选中的tab页签_bootstrap刷新页面-程序员宅基地

文章浏览阅读686次。描述:当使用bootstrap 的tab 进行页面切换后,一刷新页面,又回到了第一个初始页,从而又得重新切换,实现目标:例如当前切换到第三个页面后,刷新页面自动定位到第三个tab页面中。_bootstrap刷新页面

uniapp视频播放器(h5+app)

这是关于一篇在uniapp使用video视频播放器,支持自定义播放器样式,支持手势操作,选集、倍数和清晰度切换,支持SRT字幕格式。

uCosii从任务的建立到运行_ucosii用户任务中都有参数初始化,先执行哪个-程序员宅基地

文章浏览阅读823次。μC/OS-Ⅱ从任务的建立到运行提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、任务建立到运行整体流程二、分步功能实现1.μC/OS-Ⅱ初始化2.读入数据总结前言最近在学习ucosii实时操作系统,记录下学习过程,以待日后查阅.tips:本文所用ucosii版本为V2.86,硬件平台为STM32.一、任务建立到运行整体流程μC/OS-Ⅱ任务从建立到运行的整体流程如下所述:1. μC/OS-Ⅱ初始化: OSInit();2. 创建任务: Create_T_ucosii用户任务中都有参数初始化,先执行哪个

【燃料电池】燃料电池并网以最大额定功率运行研究(Simulink实现)_如何实现燃料电池大规模并网-程序员宅基地

文章浏览阅读260次。控制和电流谐振控制方法,增强了系统的稳定性,提高了供电质量。部分文献针对风力发电、太阳电池并网系统的小干扰稳定性进行研究,对燃料电池发电系统稳定性分析具有一定指导意义。文献[6,7]则分别建立太阳电池和风力发电系统的小信号模型,利用特征值分析法对系统的小干扰稳定性进行分析。文献[8]在光伏发电系统控制器参数进行全局优化以提高系统稳定性,但只考虑特征值作为单一优化目标,没有考虑阻尼比对系统稳定的影响,影响了控制参数的优化效果。在该模型中,燃料电池连接到电网,并由基于功率的控制器控制。行百里者,半于九十。_如何实现燃料电池大规模并网

AGI要闻:斯坦福李飞飞首次创业,瞄准“空间智能”;OpenAI下周发布搜索产品挑战谷歌;新的开源 AI 眼镜来了|钛媒体AGI | 最新快讯

她的专业领域是计算机视觉和认知神经科学。对于这家新的初创公司介绍,一位消息人士提到,上个月,李飞飞在温哥华 TED 大会上演讲中表示,最前沿的 AI 研究涉及一种算法,这种算法可以合理地推断出图像和文字在三维环境中的样子,并根据这些预测采取行动,这种算法使用的概念叫做“空间智能”。让机器狗在瑜伽球上保持平衡是很有挑战的任务,在没有大语言模型之前需要人去调整参数,但现在像 GPT-4 这样的大语言模型,因为本身就具备了丰富的物理知识,所以大语言模型可以代替人类去调整参数,并根据反馈的结果进行推理和参数调整。

随便推点

逆向_base64_rc4_----笔记_mdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmd-程序员宅基地

文章浏览阅读1k次。新人…学校比赛,没什么经验,上去见见世面某公司的月赛题 资源就不放了,只是当笔记Ida打开 有点小陷阱 巧妙的堆栈运用导致载入ida分析不了 得不到函数的边界比较幸运win32的程序od打开走一遍流程感受下00AD12E7 . 52 push edx00AD12E8 . 68 C821AD00 push 5ba358a4.00AD21C8 ..._mdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdawmdaw

致 Tapdata 开源贡献者:聊聊 2022 年的进展和新一年的共建计划-程序员宅基地

文章浏览阅读781次。在内、外部开发者的合力之下,过去一年 Tapdata 新增数据源近20个,实现了60+数据源的接入能力。一个人可以走得很快,一群人可以走得更远,2023年期待与您共创更受欢迎的开源项目。_tapdata

python列表添加元素的三种方法定义集合数据对象_python 学习第三天 可迭代对象(列表,字典,元组和集合)...-程序员宅基地

文章浏览阅读642次。列表,字典,元组和集合列表 list列表是由一系列特定元素组成的,元素和元素之间没有任何关联关系,但他们之间有先后顺序关系列表是一种容器列表是序列的一种列表是可以被改变的序列Python中的序列类型简介(sequence)字符串(str) 列表(list) 元组(tuple) 字节串(bytes) 字节数组(bytearray)创建空列表的字面值L = [ ] # L绑定空列表创建非空列表: L ..._python 集合 对象元素

第十五周 项目 1 - 验证算法_insertht(hashtable ha[], int& n, int m, int p, key-程序员宅基地

文章浏览阅读316次。/* *Copyright (c) 2016,烟台大学计算机学院 *All right reserved. *文件名称:test.cpp *作者:杨天瑞 *完成日期:2016年12月16日 *版本号:v1.7.5 * * 问题描述:验证算法。 * 程_insertht(hashtable ha[], int& n, int m, int p, keytype k)

react-native之项目结构分析_react native课程的项目分析-程序员宅基地

文章浏览阅读6k次,点赞5次,收藏10次。前言庖丁为文惠君解牛,手之所触,肩之所倚,足之所履,膝之所踦,砉然响然,奏刀騞然,莫不中音。合于桑林之舞,乃中经首之会。熟悉项目的结构,是开发的基本也是技术提升的一个重要途径,现总结下自己对react-native项目结构的分析与理解。正文项目结构init 的一个项目结构如图:tests:测试文件夹,执行命令 “npm test”会调用此文件夹,在文件夹中需要引入待测试文件。android:An_react native课程的项目分析

uniapp开发公众号,微信开发者工具进行本地调试_uniapp使用微信开发者工具 利用公众号调试的方法-程序员宅基地

文章浏览阅读584次。uniapp开发公众号,微信开发者工具进行本地调试_uniapp使用微信开发者工具 利用公众号调试的方法