redis安装

1、下载

http://redis.io/

2、安装

tar -zxvf redis-2.8.19.tar.gz

make

make install

3、修改配置

创建路径

mkdir -p /etc/redis

复制配置文件

cp redis.conf /etc/red

redis.conf

daemonize yes###后台启动

pidfile

当Redis在后台运行的时候,Redis默认会把pid文件放在/var/run/redis.pid,你可以配置到其他地址。当运行多个redis服务时,需要指定不同的pid文件和端口。

4、客户端连接

[root@rac1 ~]# redis-cli
127.0.0.1:6379>

远程连接:

redis-cli -h host -p port -a password

 例如:

redis-cli -h 127.0.0.1 -p 6379 -a "mypass"

5、客户端测试

服务器启动
[root@rac1 redis-2.8.19]# redis-server  /etc/redis/redis.conf

客户端连接
[root@rac1 ~]# redis-cli 
127.0.0.1:6379> set name zhaoja
OK
127.0.0.1:6379> get name
"zhaoja"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> exists name
(integer) 0

  

  

  

 6、c接口

下载

https://github.com/redis/hiredis

解压

unzip hiredis-master.zip 

安装

make

make install

例子:

#include <stdio.h> 
#include "hiredis.h"
int main() 
{ 
    //连接hiredis服务器
     redisContext *conn  = redisConnect("127.0.0.1",6379); 
     if(conn != NULL && conn->err) 
     {   
         printf("connection error: %s
",conn->errstr); 
         return 0; 
     }   
    //发送命令至服务器
     redisReply *reply = (redisReply*)redisCommand(conn,"set foo 1234"); 
     //关闭Reply对象
     freeReplyObject(reply); 
             
     reply = redisCommand(conn,"get foo"); 
     printf("%s
",reply->str); 
     freeReplyObject(reply); 
     //关闭连接      
     redisFree(conn); 
     return 0; 
}

  

  

编译:

gcc  test.c -o Test -lhiredis -I/usr/local/soft/hiredis-master

7、问题:

1、报错:

/usr/local/soft/hiredis-master/hiredis.h:158: error: expected identifier or ‘(’ before numeric constant

这个文件是源代码里的,查看代码为:

struct {
char *path;
} unix;

看着也没错,但是unix这个词是不是关键词,就把这个词给改了,结果错误消失。

2、

[root@rac1 redis-test]# ./Test
./Test: error while loading shared libraries: libhiredis.so.0.13: cannot open shared object file: No such file or directory

原因是:是动态库的问题,是系统找不到这个动态库,此时记住sudo /sbin/ldconfig以下,更新一下系统动态库配置。

原文地址:https://www.cnblogs.com/huanhuanang/p/4468428.html