Redis配置(未完待续6/15)

一、redis安装

Redis 官网:https://redis.io/

1.1下载 redis

直接下载目前最新的 6.0.3 版本,下载地址:http://download.redis.io/releases/redis-6.0.3.tar.gz
$ wget http://download.redis.io/releases/redis-6.0.3.tar.gz
$ tar zxvf redis-6.0.3.tar.gz

1.2 编译安装 redis

$ cd redis-6.0.3
$ make
make 之后就编译完成了。有时间还可以 make test
$ sudo make install
默认安装到/usr/local/bin/目录,对应的命令
redis-server 是服务器程序
redis-cli 是客户端程序
查看版本命令:
$ redis-server -v
显示:Redis server v=6.0.3 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=77053994c60ea3c2

1.3 启动 redis

1) 直接启动

$ redis-server
在这里插入图片描述
上图这样表示已经打开redis

2) 以后台进程方式启动 redis

在/etc 目录创建 redis 目录
$ sudo mdkir /etc/redis
将编译目录(redis-6.0.3)下的 redis.conf 拷贝到/etc/redis 目录

$ sudo cp redis.conf /etc/redis/6379.conf
修改/etc/redis/6379.conf 文件 将
daemonize no
改为
daemonize yes
在这里插入图片描述
指定 6379.conf 文件启动
$ redis-server /etc/redis/6379.conf
启动后的打印
在这里插入图片描述
查看 redis 的进程 id,ps -ef | grep redis
在这里插入图片描述

3) 设置 redis 开机自启动

(1)将 将 redis- -3 6.0.3 中 的 启动脚本(在 redis-6.0.3/utils 目录)复制一份放到/etc/init.d 目录下
$ sudo cp utils/redis_init_script /etc/init.d/redis_6379
(2) 修改文档 redis_6379
在这里插入图片描述
(3) 添加到开机启动
$ sudo update-rc.d -f redis_6379 defaults
如果要禁止开机启动
$ sudo update-rc.d -f redis_6379 remove
(4) 验证是否加入到开机启动
$ sudo sysv-rc-conf --list redis_6379
查验结果,出现下图所示内容,代表设置成功:
在这里插入图片描述
或者
$ sudo sysv-rc-conf
看到 init.d 目录下的所有自启动。

( 5) 重启验证
$ sudo reboot
重新开机后
查看 redis 的进程,ps -ef | grep redis
在这里插入图片描述

1 .4 redis- -i cli 使用

( 1) 默认无权限控制
$ redis-cli -h 127.0.0.1 -p 6379
( ( 2) 服务停止
$ redis-cli -h 127.0.0.1 -p 6379 shutdown
( ( 3) 有权限控制时( ( 加上- - a 密码) )
需要配置密码的话就去/etc/redis/6379.conf 的配置文件中找到 requirepass 这个参
数,如下配置:
修改 redis.conf 配置文件
requirepass foobared
requirepass 0voice 指定密码 0voice
指定密码后需要重启 redis-server
$ redis-cli -h 127.0.0.1 -p 6379 -a 0voice
(4)s Redis 默认 启动
端口号为 127.0.0.1,端口号默认为:6379
$ redis-cli

1.5 关于 0 redis 6.0 的多线程

(1) 如需开启需要修改 redis.conf 配置文件:io-threads-do-reads yes。
在这里插入图片描述
(2) Redis 6.0 多线程开启时,线程数如何设置?
开启多线程后,还需要设置线程数,否则是不生效的。同样修改 redis.conf 配置文件:
在这里插入图片描述
关于线程数的设置,官方有一个建议:4 核的机器建议设置为 2 或 3 个线程,8 核的建议设置为
6 个线程,线程数一定要小于机器核数。

1 .6 抓包分析命令

sudo tcpdump -i any dst host 127.0.0.1 and port 6379
更详细的显示加上 -XX 输出包的头部数据,会以 16 进制和 ASCII 两种方式同时输出,这样更方便
分析 redis 的协议
比如:sudo tcpdump -i any dst host 127.0.0.1 and port 6379 -XX
比如设置命令:set teacher king
在这里插入图片描述
服务器返回
在这里插入图片描述

二、基本数据结构

redis基本数据结构我另外一篇文章有将

三、操作命令

四、C语言使用redis

在这里插入图片描述
1.第一步

  • 看返回值redisReply *reply这个指针是不是空的
    redisReply *reply = (redisReply *)redisCommand…

2.第二步

若读到的是数组,用下面图片取
在这里插入图片描述
//链接不上需要更新系统动态库配置
//sudo /sbin/ldconfig

#include <hiredis/hiredis.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// gcc -o redis-test redis-test.c -I /usr/local/include/hiredis -L /usr/local/lib -lhiredis 

int main() {

    // 连接Redis服务
    redisContext *context = redisConnect("127.0.0.1", 6379);
    if (context == NULL || context->err) {
        if (context) {
            printf("%s\n", context->errstr);
        } else {
            printf("redisConnect error\n");
        }
        exit(EXIT_FAILURE);
    }
    printf("-----------------connect success--------------------\n");

    //链接不上需要更新系统动态库配置
    //sudo /sbin/ldconfig

    redisReply *reply = redisCommand(context, "auth 0voice");
    printf("type : %d\n", reply->type);
    // // if (reply->type == REDIS_REPLY_STATUS) {
    // //     /*SET str Hello World*/
    // //     printf("auth ok\n");
    // // }
    // // freeReplyObject(reply);

    // // Set Key Value
    // char *key = "str";
    // char *val = "Hello World";
    // /*SET key value */
    // reply = redisCommand(context, "SET %s %s", key, val);
    // printf("type : %d\n", reply->type);
    // if (reply->type == REDIS_REPLY_STATUS) {//REDIS_REPLY_STATUS ==5:返回命令执行的 状态
    //     /*SET str Hello World*/
    //     printf("SET %s %s\n", key, val);
    // }
    // freeReplyObject(reply);

    // GET Key
    int key_1 = 1;
    reply = redisCommand(context, "GET %d", key_1);
    if (reply->type == REDIS_REPLY_STRING) {//REDIS_REPLY_STRING == 1:返回值是 字符串
        /*GET str Hello World*/
        printf("GET str %s\n", reply->str); //字符串储存在 redis->str 当中
        /*GET len 11*/
        printf("GET len %ld\n", reply->len);//字符串长度为 redis->len
    }
    freeReplyObject(reply);


    // APPEND key value
    // char *append = " I am your GOD";
    // //如果 key 已经存在并且是一个字符串, APPEND 命令将指定的 value 追加到该 key 原来值(value)的末尾
    // reply = redisCommand(context, "APPEND %s %s", key, append);
    // if (reply->type == REDIS_REPLY_INTEGER) {//REDIS_REPLY_INTEGER == 3:返回值为 整数 long long。
    //     printf("APPEND %s %s \n", key, append);
    // }
    // freeReplyObject(reply);
    // /*GET key*/
    // reply = redisCommand(context, "GET %s", key);
    // if (reply->type == REDIS_REPLY_STRING) {//REDIS_REPLY_STRING == 1:返回值是 字符串
    //     //GET Hello World I am your GOD
    //     printf("GET %s\n", reply->str);//string str(reply->str);
    // }
    // freeReplyObject(reply);

    // // INCR key
    // reply = redisCommand(context, "INCR counter");//将 key 中储存的数字值增一
    // if (reply->type == REDIS_REPLY_INTEGER) {//REDIS_REPLY_INTEGER == 3:返回值为 整数 long long
    //     printf("INCR counter %lld\n", reply->integer);
    // }
    // freeReplyObject(reply);
    // reply = redisCommand(context, "INCR counter");
    // if (reply->type == REDIS_REPLY_INTEGER) {
    //     printf("INCR counter %lld\n", reply->integer);
    // }
    // freeReplyObject(reply);

    // // DECR key
    // reply = redisCommand(context, "DECR counter");
    // if (reply->type == REDIS_REPLY_INTEGER) {
    //     printf("DECR counter %lld\n", reply->integer);
    // }
    // freeReplyObject(reply);
    // reply = redisCommand(context, "DECR counter");
    // if (reply->type == REDIS_REPLY_INTEGER) {
    //     printf("DECR counter %lld\n", reply->integer);
    // }
    // freeReplyObject(reply);


    // // DECRBY key decrement
    // reply = redisCommand(context, "DECRBY counter 5");
    // if (reply->type == REDIS_REPLY_INTEGER) {
    //     printf("DECRBY counter %lld\n", reply->integer);
    // }
    // freeReplyObject(reply);
    // reply = redisCommand(context, "DECRBY counter 5");
    // if (reply->type == REDIS_REPLY_INTEGER) {
    //     printf("DECRBY counter %lld\n", reply->integer);
    // }
    // freeReplyObject(reply);

    // // INCRBY key increment
    // reply = redisCommand(context, "INCRBY counter 5");
    // if (reply->type == REDIS_REPLY_INTEGER) {
    //     printf("INCRBY counter %lld\n", reply->integer);
    // }
    // freeReplyObject(reply);

    // reply = redisCommand(context, "INCRBY counter 5");
    // if (reply->type == REDIS_REPLY_INTEGER) {
    //     printf("INCRBY counter %lld\n", reply->integer);
    // }
    // freeReplyObject(reply);

    // // GETRANGE key start end
    // reply = redisCommand(context, "GETRANGE str 0 5");
    // if (reply->type == REDIS_REPLY_STRING) {
    //     /*GETRANGE str Hello*/
    //     printf("GETRANGE %s %s\n", key, reply->str);
    // }
    // freeReplyObject(reply);

    // // GETSET key value
    // reply = redisCommand(context, "GETSET %s %s", key, val);
    // if (reply->type == REDIS_REPLY_STRING) {
    //     /*GETSET str Hello World I am your GOD*/
    //     printf("GETSET %s %s\n", key, reply->str);
    // }
    // freeReplyObject(reply);

    // /*INCRBYFLOAT key increment*/
    // reply = redisCommand(context, "INCRBYFLOAT f 2.1");
    // if (reply->type == REDIS_REPLY_STRING) {
    //     printf("INCRBYFLOAT counter %s\n", reply->str);
    // }
    // freeReplyObject(reply);

    // /*MSET key value [key value ...]*/
    // reply = redisCommand(context, "MSET k1 hello k2 world k3 good");
    // if (reply->type == REDIS_REPLY_STATUS) {
    //     printf("MSET k1 hello k2 world k3 good\n");
    // }
    // freeReplyObject(reply);

    // /*MGET key [key ...]*/
    // reply = redisCommand(context, "MGET k1 k2 k3");
    // if (reply->type == REDIS_REPLY_ARRAY) {
    //     printf("MGET k1  k2  k3 \n");
    //     redisReply **pReply = reply->element;
    //     int i = 0;
    //     size_t len = reply->elements;
    //     //hello world good
    //     for (; i < len; ++i) {
    //         printf("%s ", pReply[i]->str);
    //     }
    //     printf("\n");
    // }
    // freeReplyObject(reply);

    // /*STRLEN key*/
    // reply = redisCommand(context, "STRLEN str");
    // if (reply->type == REDIS_REPLY_INTEGER) {
    //     //1
    //     printf("STRLEN str %lld \n", reply->integer);
    // }
    // freeReplyObject(reply);

    // /*SETEX key seconds value*/
    // reply = redisCommand(context, "SETEX s 30 30seconds");
    // if (reply->type == REDIS_REPLY_STATUS) {
    //     printf("SETEX s 30 30seconds\n");
    //     freeReplyObject(reply);
    //     int i = 0;
    //     while (i++ < 32) {
    //         reply = redisCommand(context, "GET s");
    //         if (reply->type == REDIS_REPLY_STRING) {
    //             printf("%d s %s\n", i, reply->str);
    //         } else if (reply->type == REDIS_REPLY_NIL) {
    //             printf("%d s nil\n", i);
    //         }
    //         freeReplyObject(reply);
    //         sleep(1);
    //         /*
    //          * 29 s 30seconds
    //          * 30 s 30seconds
    //          * 31 s nil
    //          * 32 s nil
    //          */
    //     }
    // }

    redisFree(context);
    return EXIT_SUCCESS;
}



1.查看redis版本
redis-server -v

读到字符串流

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值