Linux(CentOS)中Redis介绍、安装、使用【一篇就够】

2018-05-13 13:36:16 sjmz30071360 阅读数 1590更多

分类专栏: redis

版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

一、介绍

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.

二、下载 redis版本包到 CentOS上

curl -O http://download.redis.io/releases/redis-4.0.9.tar.gz

三、安装

mkdir redis 创建redis存放目录

mv redis-4.0.9.tar.gz reids 将安装包移动到redis存放目录下

cd redis 进入目录

tar -xvf redis-4.0.9.tar.gz 解压安装包

ls -lrt

ls -lrt redis-4.0.9

cd redis-4.0.9 进入redis-4.0.9

make 运行make命令

如果运行出现make:gcc:Commond not found错误,一般出现这种错误是因为安装系统的时候使用的是最小化mini安装,系统没有安装make、vim等常用命令,直接yum安装下即可。

运行yum -y install gcc automake autoconf libtool make即可

make命令正常运行完毕后,在安装目录的src目录下可以看到服务程序redis-server和用于测试的客户端程序redis-cli等

四、使用

cd src 进入src目录

./redis-server 启动redis服务(这种方式使用的是默认配置,也可以通过启动参数告诉redis使用指定的配置文件)

如: ./redis-server redis.conf

启动后的界面如下:

通过另一台机器,登录客户端,使用客户端和redis服务交互

cd src

./redis-cli

setkey value

getkey

参考:http://www.runoob.com/redis/redis-install.html

五、windows端通过java程序访问虚拟机linux上的redis

1)修改redis.conf配置文件

vi redis.conf

注释掉 bind 127.0.0.1

bind的意思是绑定哪个ip地址能够访问服务 ,简单说bind指定的ip才可以访问redis server。

ps:

bind 127.0.0.1 //指定只有本机才能访问redis服务器

bind 0.0.0.0    // 所有的机子都可以访问到redis server

bind  192.168.1.253  //只有这个ip的机子才可以访问redis server

2)关闭linux防火墙

systemctl stop firewalld.service

3)java程序(虚拟机linux ip地址为:192.168.0.104)

import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.0.104"); System.out.println("连接成功"); //查看服务是否运行 System.out.println("服务正在运行:" + jedis.ping()); } }

执行时报下面错误:

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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 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)Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

修改redis.conf,去掉requirepass foobared前面的#,启用密码验证(密码为 foobared)

相应的在java程序中增加密码设置

import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.0.104"); jedis.auth("foobared"); //输入redis密码 System.out.println("连接成功"); //查看服务是否运行 System.out.println("服务正在运行:" + jedis.ping()); } }

重新启动redis,再次执行java程序,执行成功!

连接成功

服务正在运行:PONG

Redis Java String(字符串)实例

import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.0.104"); jedis.auth("foobared"); //输入redis密码 System.out.println("连接成功"); //查看服务是否运行 System.out.println("服务正在运行:" + jedis.ping()); //设置redis字符串数据 jedis.set("testK", "first redis value"); //获取存储的数据并输出 System.out.println("redis存储的字符串为:" + jedis.get("testK")); } }

执行结果:

连接成功

服务正在运行:PONG

redis存储的字符串为:first redis value

linux redis安装使用,linux安装redis相关推荐

  1. Redis 入门安装(Linux)

    Redis 入门安装(Linux) 备注:该案例讲解基于CentOS6.5.Reids3.2.8 Redis 官网 中文地址:http://www.redis.cn/ 英文地址:https://red ...

  2. linux 安装redis4.0.6,Redis(4.0.6)在Linux(CentOS7)下的安装

    构建 Redis redis 目前没有官方 RPM 安装包,需要从源代码编译,编译需要安装 Make 和 GCC. yum install gcc make 从官网下载 tar 压缩包. curl h ...

  3. linux下Redis以及phpredis扩展安装

    linux下Redis以及phpredis扩展安装 首先安装redis: 一.下载redis: wgethttp://download.redis.io/releases/redis-2.8.10.t ...

  4. linux redis数据库安装配置,Linux系统中redis的安装配置步骤

    Linux系统中redis的安装配置步骤 发布时间:2020-06-23 10:13:36 来源:亿速云 阅读:87 作者:Leah 这篇文章将为大家详细讲解有关Linux系统中redis的安装配置步 ...

  5. Linux redis安装教程,Linux 下redis5.0.0安装教程详解

    Linux redis5.0.0安装,教程如下所示: 1.从官网下载,然后传到服务器,tar -zxvf解压 2.进入redis ? 3.安装:make, (1)若提示:: gcc: Command ...

  6. 七、redis的安装(linux)

    1.系统环境 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core) 2.Linux安装yum ...

  7. Redis在Windows+linux平台下的安装配置(转)

    window平台Redis安装 下载地址: http://code.google.com/p/servicestack/wiki/RedisWindowsDownload Redis文件夹有以下几个文 ...

  8. linux redis-4.0,Linux Redis 4.0.2 安装部署

    Linux Redis 4.0.2 安装部署 01 安装GCC yum -y install gcc gcc-c++ libstdc++-devel tcl -y 02 下载安装包 cd /expor ...

  9. linux装redis环境变量,linux 怎样安装redis

    人到中年有点甜 获取Redis1.通过官网http://redis.io/获取稳定版源码包下载地址:2.通过wget http://download.redis.io/releases/redis-3 ...

最新文章

  1. Sangfor_AC用户不在线但在“在线用户管理”里有显示
  2. SSO单点登录之——JWT
  3. 第十七章 Python网络编程
  4. python写元旦快乐_用Python在00:00给微信好友发元旦祝福语
  5. python编写一个登陆验证程序_python项目实战:实现验证码登录网址实例
  6. iOS开发 AVAudioPlayer
  7. Unfair contest(个人做法)
  8. Hibernate的fetch
  9. HTML页面禁止选择、页面禁止复制、页面禁止右键
  10. is内存地址 id 地址比较 小数据池概念
  11. UrlRewrite的使用
  12. Tcl 语言 ——列表篇
  13. 有道词典pc离线包打包下载_【超福利】安卓手机上最好用的离线词典
  14. 什么是k近邻算法,K近邻算法:Fackbook最近入住预测
  15. PostgreSQL数据库网络层——libpq服务端顶层接口
  16. 揭秘中国商品期货市场的9大重要因子
  17. 被boss直聘转发过多而“封杀”的2021年全套java高级面试题有多牛
  18. 2022还在为怎样去提升自己Android技术而发愁吗?享学课堂是个不错的选择
  19. 专题总纲目录 PMP总纲
  20. 实现证件照APP(一)

热门文章

  1. 到底是32位系统运行快还是64位系统快
  2. java 导入导出txt文件_Java读取和写入txt文件
  3. 运行错误5无效的过程调用或参数_FANUC系统常用参数汇总
  4. VS2017无法启动
  5. 少儿编程100讲轻松学python(三)-python如何重命名文件
  6. seo 伪原创_胡子哥谈seo优化:那些不被了解的伪原创技巧
  7. linux c 11 运行库,11.1.3 运行库与I/O
  8. 正则表达式贪婪与非贪婪模式
  9. FormatJS – 让你的 Web 应用程序国际化
  10. web前端(12)—— 页面布局2