Ubuntu上安装、使用Redis的详细教程

这篇具有很好参考价值的文章主要介绍了Ubuntu上安装、使用Redis的详细教程。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

这篇文章简单地介绍一下怎么在linux虚拟机上完成redis的安装及使用。

目录

1、安装redis

2、使用redis

3、启动/关闭redis

启动redis

启动方式一

启动方式二

启动方式三

重启redis

关闭redis

查看redis状态

4、在宿主机连接redis

5、通过java连接redis

创建maven项目

添加jedis的依赖

jedis的案例代码

关闭保护模式

重新运行代码


1、安装redis

首先,访问Redis官网,点击首页的【Get Started】,然后点击Install Redis on Linux

ubuntu安装redis,linux,redis,ubuntu

然后按照页面内容提示,在Ubuntu上安装redis

ubuntu安装redis,linux,redis,ubuntu

只需要在终端依次输入以下命令,如果过程中没有错误提示,则redis安装完成。

sudo apt install lsb-release curl gpg
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis

2、使用redis

在终端输入redis-cli,会进入到redis的命令行模式,这时候就可以愉快地使用redis的各种命令了。

输入exit退出redis-cli。

ubuntu安装redis,linux,redis,ubuntu

3、启动/关闭redis

启动redis

启动方式一

/etc/init.d/redis-server start

ubuntu安装redis,linux,redis,ubuntu

启动方式二

systemctl start redis-server

启动方式三

service redis-server start

重启redis

service redis-server restart

关闭redis

service redis-server stop

查看redis状态

service redis-server status

4、在宿主机连接redis

根据以上步骤安装启动redis后,默认只能在虚拟机内访问redis,如果在其他机器上访问,需要修改配置文件。

默认情况下,redis的配置文件在/etc/redis/redis.conf,打开这个文件,注释掉下面的内容。

bind 127.0.0.1 -::1

然后就可以在windows上通过Another Redis Desktop Manager连接ubuntu上的redis了,当然了,如果你希望给redis设置一个密码,可以在配置文件中加上以下的配置。

requirepass 你的密码

ubuntu安装redis,linux,redis,ubuntu

设置完之后,不输入密码连接时会提示

ubuntu安装redis,linux,redis,ubuntu

5、通过java连接redis

redis官方推荐通过jedis来操作redis,jedis是一个专门设计用于操作和快速使用redis的Java客户端。

Jedis is a Java client for Redis designed for performance and ease of use.

如图,点击Client quikstart,在点击展开的Java。

ubuntu安装redis,linux,redis,ubuntu

如图,官网已经给出了操作redis的方法

ubuntu安装redis,linux,redis,ubuntu

创建maven项目

在IntelliJ IDEA创建一个maven项目:依次File -> New -> Project...

然后在左侧选择Maven,然后点击Next

ubuntu安装redis,linux,redis,ubuntu

然后填写项目名和包名

ubuntu安装redis,linux,redis,ubuntu

最后点击Finish就完成Maven项目的创建了。

添加jedis的依赖

在pom.xml中添加jedis客户端依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.3.1</version>
</dependency>

完整的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>redis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.3.1</version>
        </dependency>
    </dependencies>
</project>

jedis的案例代码

package com.example.redis;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class RedisTests {

    @Test
    void contextLoads() {
        JedisPool pool = new JedisPool("192.168.254.128", 6379);

        try (Jedis jedis = pool.getResource()) {
            jedis.set("foo", "bar");
            System.out.println(jedis.get("foo"));

            Map<String, String> hash = new HashMap<>();

            hash.put("name", "John");
            hash.put("surname", "Smith");
            hash.put("company", "Redis");
            hash.put("age", "29");

            jedis.hset("user-session:123", hash);

            System.out.println(jedis.hgetAll("user-session:123"));
        }
    }

}

关闭保护模式

运行上面测试类,正常来说会看到绿条,但是运行的时候发生了异常

ubuntu安装redis,linux,redis,ubuntu

以下是控制台打印的异常信息

redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only 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 server 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 mode 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) Set up an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

	at redis.clients.jedis.Protocol.processError(Protocol.java:96)
	at redis.clients.jedis.Protocol.process(Protocol.java:137)
	at redis.clients.jedis.Protocol.read(Protocol.java:192)
	at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:316)
	at redis.clients.jedis.Connection.getOne(Connection.java:298)
	at redis.clients.jedis.Connection.executeCommand(Connection.java:123)
	at redis.clients.jedis.Jedis.set(Jedis.java:4880)
	at com.example.redis.RedisTests.contextLoads(RedisTests.java:19)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

抓到其中的一个关键词:protected-mode,很显然,这应该是一个配置

'CONFIG SET protected-mode no'

于是我们到redis.conf下搜索一下这个设置,果然找到了,先把这个设置yes改成no

ubuntu安装redis,linux,redis,ubuntu

修改为no

ubuntu安装redis,linux,redis,ubuntu

然后重启一下redis

service redis-server restart

重新运行代码

再次运行之前的代码,这一次成功了

ubuntu安装redis,linux,redis,ubuntu

然后接着往下看,redis还提供了不用写try...catch块的方式操作redis,这样代码看起来更简洁了

ubuntu安装redis,linux,redis,ubuntu

同样,复制代码运行一次,也运行成功了

ubuntu安装redis,linux,redis,ubuntu

最后,通过Another Redis Desktop Manager连接虚拟机上的redis,能看到刚刚运行设置的key

ubuntu安装redis,linux,redis,ubuntu

没有用过Another Redis Desktop Manager的童鞋,可以通过以下文章了解一下:

redis桌面连接工具Another Redis Desktop Manager使用介绍https://blog.csdn.net/heyl163_/article/details/133007448

好了,关于安装和使用redis的介绍就到这里了,看完不要忘了点赞+收藏哦~文章来源地址https://www.toymoban.com/news/detail-752191.html

到了这里,关于Ubuntu上安装、使用Redis的详细教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • linux(ubuntu)安装Docker教程-超详细超简单

    1、首先进入管理员权限。 2、更新软件源。 3、安装一些依赖 4、为系统添加Docker的密钥 5、添加Docker源,这里我们选择的是stable稳定版 6、再次更新源 7、查看有哪些Docker版本可以安装 8、这里我们安装安装Docker的社区版 9、输入 docker ,即可检查是否安装成功。 10、启动Docke

    2024年02月16日
    浏览(32)
  • Linux系统 Ubuntu18.04安装的详细教程(提供18.04ubuntu镜像)

    镜像文件下载: 链接:https://pan.baidu.com/s/12bEdRBwO1YbLt23QKnrSrA 提取码:h7as 关于全名、用户名区别可先看第四部分 处理器和内核数量,根据需要配置就行。我是8核16线程,配置2,2。小白学习的话,配置低一点没关系。如果你只是为了学一些基础命令,配置成1,1应该也没问题

    2024年02月01日
    浏览(48)
  • 【Redis】Ubuntu22.04安装Redis

    前言:最近想要学习用Python控制Redis的方法,但是Redis官网是不支持Windows直接安装的,各种大佬的Windows移植版本也比较老,虽然够用,但是也希望使用官网版本。网上的各种安装教程或多或少都存在一点问题,这里我针对我所使用的服务器版本安装Redis服务进行整理,若与我

    2024年02月06日
    浏览(26)
  • 如何在Ubuntu 22.04上安装Linux 内核 详细教程!

    在Ubuntu 22.04上安装Linux内核可以按照以下步骤进行操作: 更新系统:首先,确保你的Ubuntu系统是最新的,执行以下命令更新系统软件包: 下载内核文件:访问Linux内核官方网站(https://www.kernel.org)下载所需的内核版本。选择一个稳定版本并下载源代码文件(以.tar.gz或.tar.xz为

    2024年02月07日
    浏览(48)
  • Windows10系统安装Linux虚拟机(Ubuntu)详细图文教程

    在学习Linux系统编程时由于没有多余的电脑于是想到了使用虚拟机来安装Liunx系统环境。在翻阅了诸多教程后,选择了免费的VM VirtualBox虚拟机。顺便写了一下虚拟机使用流程以及系统安装教程供初学者参考 虚机机Oracle VM VirtualBox链接: Oracle VM VirtualBox https://www.virtualbox.org/ Ub

    2023年04月13日
    浏览(40)
  • Redis安装与配置指南:适用于Windows、Mac和Linux系统的详细教程

    🌷🍁 博主 libin9iOak带您 Go to New World.✨🍁 🦄 个人主页——libin9iOak的博客🎐 🐳 《面试题大全》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍》学会IDEA常用操作,工作效率翻倍~💐 🪁🍁 希望本文能够给您带来一定的帮助🌸文章粗浅,敬

    2024年02月16日
    浏览(42)
  • Ubuntu系统环境搭建(六)——Ubuntu安装redis

    ubuntu环境搭建专栏🔗点击跳转 更新 安装 查看是否在运行 编辑redis配置 允许远程访问 将bind 127.0.0.1 ::1注释掉 设置密码 找到 requirepass foobared ,将foobared修改为你的redis密码 至此,保存 redis.conf ,配置工作已经完成。 重启redis

    2024年02月10日
    浏览(31)
  • Ubuntu下安装Redis

    Redis是基于C语言编写的,因此首先需要安装Redis所需要的gcc依赖: 利用xftp传输工具将redis安装包上传到linux上,最好是放在/usr/local/src下 解压缩: 解压后,进入redis目录下 安装make: 安装完成后,进入/usr/local/bin 使用\\\"ll\\\"命令得到如下结果即是安装成功 常见查看命令: redis的启

    2024年02月05日
    浏览(18)
  • Ubuntu上安装、使用MongoDB详细教程

    MongoDB是所有非关系型数据库中最像关系型数据库的一种存储技术,MongoDB中的数据结构是类似于JSON的BSON(Binary Json),这篇文章就详细介绍如何安装和使用MongoDB。 MongoDB和MySQL中的概念对应关系 MongoDB MySQL 数据库 数据库 表/集合 表 索引 索引 文档 表的行数据 在MongoDB官网下载

    2024年02月05日
    浏览(26)
  • ubuntu安装单个redis服务

    1.apt-get install redis-server 使用lighthouse用户这样操作会报与权限有关的错误,   改成使用root账号操作     2.安装完成后,Redis服务器会自动启动,查看进程是否正常启动 ps -axu|grep redis redis    18689  0.1  0.4  40136  6860 ?        Ssl  09:12   0:01 /usr/bin/redis-server 127.0.0.1:6379 root  

    2024年02月16日
    浏览(20)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包