前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >redis 入门(一)——Linux环境安装测试以及基本命令演示

redis 入门(一)——Linux环境安装测试以及基本命令演示

作者头像
MickyInvQ
发布2020-09-27 17:18:07
4600
发布2020-09-27 17:18:07
举报
文章被收录于专栏:InvQ的专栏InvQ的专栏

redis概述

redis是一个开源的,先进的 key-value 存储可用于构建高性能的存储解决方案。它支持数据结构有字符串,哈希,列表,集合,带有范围查询的排序集,位图,超文本和具有半径查询的地理空间索引。 NoSQL,Not Only [SQL],泛指非关系型的数据库。所以redis是一种nosql。敲黑板画重点:redis是一种nosql.

redis的优点:

异常快速 支持丰富的数据类型 操作都是原子的

下载安装

代码语言:javascript
复制
$ wget http://download.redis.io/releases/redis-3.2.6.tar.gz 
代码语言:javascript
复制
/root/redis tar xzf redis−3.2.6.tar.gz
代码语言:javascript
复制
/root/redis/redis-3.2.6   make
代码语言:javascript
复制
启动服务器:

 ```
    $ src/redis-server
 ```

 启动客户端

 ```
 $ src/redis-cli
 ```

启动:

redis-server redis-cli

代码语言:javascript
复制
11645:C 23 Apr 14:48:49.784 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 11645
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

11645:M 23 Apr 14:48:49.788 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
11645:M 23 Apr 14:48:49.788 # Server started, Redis version 3.2.6
11645:M 23 Apr 14:48:49.788 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
11645:M 23 Apr 14:48:49.788 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
11645:M 23 Apr 14:48:49.788 * DB loaded from disk: 0.000 seconds
11645:M 23 Apr 14:48:49.788 * The server is now ready to accept connections on port 6379

redis 支持的数据类型

启动客户端 ,存储字符串到redis.

代码语言:javascript
复制
127.0.0.1:6379> set name wonder
OK

取字符串:

代码语言:javascript
复制
127.0.0.1:6379> get name
"wonder"

哈希值

代码语言:javascript
复制
127.0.0.1:6379> hmset king username wonder password root age 22
OK
代码语言:javascript
复制
127.0.0.1:6379> hgetall king
1) "username"
2) "wonder"
3) "password"
4) "root"
5) "age"
6) "22"

Lists - 列表

代码语言:javascript
复制
127.0.0.1:6379> lpush pricess yiyue
(integer) 1
127.0.0.1:6379> lpush pricess chen
(integer) 2
127.0.0.1:6379> lpush privess yinli
(integer) 1
127.0.0.1:6379> lpush pricess yinli
(integer) 3
127.0.0.1:6379> lrange pricess 0 10
1) "yinli"
2) "chen"
3) "yiyue"
127.0.0.1:6379> 

Redis 有序集合

Redis有序集合类似Redis集合存储在设定值唯一性。不同的是,一个有序集合的每个成员带有分数,用于以便采取有序set命令,从最小的到最大的分数有关。

代码语言:javascript
复制
127.0.0.1:6379> zadd kindom 1 redis
(integer) 1
127.0.0.1:6379> zadd kindom 2 mongodb
(integer) 1
127.0.0.1:6379> zadd kindom 3 mysql
(integer) 1
127.0.0.1:6379> zadd kindom 3 mysql
(integer) 0
127.0.0.1:6379> zadd kindom 4 mysql
(integer) 0
127.0.0.1:6379> zrange kindom 0 10 withscores
1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"

redis 发布订阅

开启客户端作为接受者

代码语言:javascript
复制
127.0.0.1:6379> subscribe myking messages
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "myking"
3) (integer) 1
1) "subscribe"
2) "messages"
3) (integer) 2

开启另一个客户端作为发送者:

代码语言:javascript
复制
127.0.0.1:6379> publish myking redis
(integer) 1
127.0.0.1:6379> publish myking "hello redis,you are a great caching technique"
(integer) 1

注意订阅端的变化:

代码语言:javascript
复制
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "myking"
3) (integer) 1
1) "subscribe"
2) "messages"
3) (integer) 2
1) "message"
2) "myking"
3) "redis"
1) "message"
2) "myking"
3) "hello redis,you are a great caching technique"

其他的一些操作

1.获取所以的key KEYS *

代码语言:javascript
复制
127.0.0.1:6379> keys *
1) "kindom"
2) "name"
3) "privess"
4) "king"
5) "pricess"
2.判断key是否存在

EXISTS key 存在返回 1,不存在返回 0

代码语言:javascript
复制
127.0.0.1:6379> exists privess
(integer) 1
127.0.0.1:6379> exists privesssssss
(integer) 0
3.删除key

DEL key [key …]

代码语言:javascript
复制
127.0.0.1:6379> del privess
(integer) 1
127.0.0.1:6379> keys *
1) "kindom"
2) "name"
3) "king"
4) "pricess"
4.获取数据类型

TYPE key

代码语言:javascript
复制
127.0.0.1:6379> type kindom
zset
5.向尾部添加

APPEND key value

代码语言:javascript
复制
127.0.0.1:6379> append name qqq
(integer) 9
127.0.0.1:6379> get name
"wonderqqq"
6.获取字符串长度

strlen key

代码语言:javascript
复制
127.0.0.1:6379> strlen name
(integer) 9

当然这里只是介绍简单的一些操作,复杂的参考官方文档。

在java应用中使用redis—jedis

前提是redis 已经安装,并且已经开启服务。

jedis 下载地址 https://github.com/xetorthio/jedis

Jedis is a blazingly small and sane Redis java client. Jedis was conceived to be EASY to use. 翻译: jedis是一个非常小的java客户端,被认为是容易使用。

怎么使用?

代码语言:javascript
复制
import redis.clients.jedis.Jedis;

import java.util.List;

public class TestRedisClient {
    public static void main(String[] args){

        Jedis jedis = new Jedis("10.20.112.33");
        System.out.println("Connection to server sucessfully");
        //check whether server is running or not
        System.out.println("Server is running: "+jedis.ping());
        jedis.lpush("wonder-list", "Redis");
        jedis.lpush("wonder-list", "Mongodb");
        jedis.lpush("wonder-list", "Mysql");
        // Get the stored data and print it
        List<String> list = jedis.lrange("wonder-list", 0 ,5);
        for(int i=0; i<list.size(); i++) {
            System.out.println("Stored string in redis:: "+list.get(i));
        }

    }
}
代码语言:javascript
复制
Connection to server sucessfully
Server is running: PONG
Stored string in redis:: Mysql
Stored string in redis:: Mongodb
Stored string in redis:: Redis

以上均为自己安装测试通过。

如果未开启服务,那么则会拒绝连接:

代码语言:javascript
复制
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
    at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
    at TestRedisClient.main(TestRedisClient.java:11)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 6 more
其中遇到的问题有:
1.如何通过远程,Windows使用远程linux上的redis服务。

1.注释掉你的redis.conf 的bind ip 的配置 我这里将原来的127.0.0.1注释掉

2.设置 protect-mode no

3.关闭redis服务:redis-server stop 或者使用 ctrl + c

4.启动: redis-server 你的redis.conf 目录 如 /user/redis/src/redis-server /user/redis/redis.conf

启动的时候,可以指定配置文件启动

成功连接。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • redis概述
  • 下载安装
  • redis 支持的数据类型
  • 哈希值
  • Lists - 列表
  • Redis 有序集合
  • redis 发布订阅
  • 其他的一些操作
    • 2.判断key是否存在
      • 3.删除key
        • 4.获取数据类型
          • 5.向尾部添加
            • 6.获取字符串长度
            • 在java应用中使用redis—jedis
              • 其中遇到的问题有:
                • 1.如何通过远程,Windows使用远程linux上的redis服务。
                相关产品与服务
                云数据库 Redis
                腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档