Redis服务及客户操作
2017-01-21 23:29
363 查看
配置说明:
Redis 服务端安装在ubuntu上
客户端通过C++远程访问Redis服务
ubuntu上安装方法:
//在终端中安装Redis服务器端
sudo apt-get install redis-server
安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序
可以看到:
2
1
2
显示: tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
2
1
2
显示: redis-server is running
默认服务端只开启了本地回路访问127.0.0.1,想要远程访问,必须修改绑定地址为外网ip
1、 使用Redis的访问账号
默认情况下,访问Redis服务器是不需要密码的,为了增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为redis。
用vi打开Redis服务器的配置文件redis.conf
2
3
4
1
2
3
4
2、 让Redis服务器被远程访问
默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。
用vi打开Redis服务器的配置文件redis.conf
重启命令:
service redis-server restart
windows平台远程访问该redis数据服务端代码如下:
void doTest()
{
//redis默认监听端口为6387 可以再配置文件中修改
redisContext* c = redisConnect("127.0.0.1", 6379);
if ( c->err)
{
redisFree(c);
printf("Connect to redisServer faile\n");
return ;
}
printf("Connect to redisServer Success\n");
const char* command1 = "set aaa 12345";
redisReply* r = (redisReply*)redisCommand(c, command1);
if( NULL == r)
{
printf("Execut command1 failure\n");
redisFree(c);
return;
}
if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0))
{
printf("Failed to execute command[%s]\n",command1);
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command1);
const char* command2 = "strlen stest1";
r = (redisReply*)redisCommand(c, command2);
if ( r->type != REDIS_REPLY_INTEGER)
{
printf("Failed to execute command[%s]\n",command2);
freeReplyObject(r);
redisFree(c);
return;
}
int length = r->integer;
freeReplyObject(r);
printf("The length of 'stest1' is %d.\n", length);
printf("Succeed to execute command[%s]\n", command2);
const char* command3 = "get stest1";
r = (redisReply*)redisCommand(c, command3);
if ( r->type != REDIS_REPLY_STRING)
{
printf("Failed to execute command[%s]\n",command3);
freeReplyObject(r);
redisFree(c);
return;
}
printf("The value of 'stest1' is %s\n", r->str);
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command3);
const char* command4 = "get stest2";
r = (redisReply*)redisCommand(c, command4);
if ( r->type != REDIS_REPLY_NIL)
{
printf("Failed to execute command[%s]\n",command4);
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command4);
redisFree(c);
}
Redis 服务端安装在ubuntu上
客户端通过C++远程访问Redis服务
ubuntu上安装方法:
//在终端中安装Redis服务器端
sudo apt-get install redis-server
安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序
//在终端中检查Redis服务器系统进程 ps -aux|grep redis
可以看到:
//在终端中通过启动命令检查Redis服务器状态 netstat -nlt|grep 63791
2
1
2
显示: tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
//通过启动命令检查Redis服务器状态 sudo /etc/init.d/redis-server status1
2
1
2
显示: redis-server is running
Redis的配置
默认服务端只开启了本地回路访问127.0.0.1,想要远程访问,必须修改绑定地址为外网ip1、 使用Redis的访问账号
默认情况下,访问Redis服务器是不需要密码的,为了增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为redis。
用vi打开Redis服务器的配置文件redis.conf
~ sudo vi /etc/redis/redis.conf #取消注释requirepass requirepass redis1
2
3
4
1
2
3
4
2、 让Redis服务器被远程访问
默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。
用vi打开Redis服务器的配置文件redis.conf
~ sudo vi /etc/redis/redis.conf #注释bind #bind 127.0.0.1修改完以上配置,重启该redis服务即可让设置生效。
重启命令:
service redis-server restart
windows平台远程访问该redis数据服务端代码如下:
void doTest()
{
//redis默认监听端口为6387 可以再配置文件中修改
redisContext* c = redisConnect("127.0.0.1", 6379);
if ( c->err)
{
redisFree(c);
printf("Connect to redisServer faile\n");
return ;
}
printf("Connect to redisServer Success\n");
const char* command1 = "set aaa 12345";
redisReply* r = (redisReply*)redisCommand(c, command1);
if( NULL == r)
{
printf("Execut command1 failure\n");
redisFree(c);
return;
}
if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0))
{
printf("Failed to execute command[%s]\n",command1);
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command1);
const char* command2 = "strlen stest1";
r = (redisReply*)redisCommand(c, command2);
if ( r->type != REDIS_REPLY_INTEGER)
{
printf("Failed to execute command[%s]\n",command2);
freeReplyObject(r);
redisFree(c);
return;
}
int length = r->integer;
freeReplyObject(r);
printf("The length of 'stest1' is %d.\n", length);
printf("Succeed to execute command[%s]\n", command2);
const char* command3 = "get stest1";
r = (redisReply*)redisCommand(c, command3);
if ( r->type != REDIS_REPLY_STRING)
{
printf("Failed to execute command[%s]\n",command3);
freeReplyObject(r);
redisFree(c);
return;
}
printf("The value of 'stest1' is %s\n", r->str);
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command3);
const char* command4 = "get stest2";
r = (redisReply*)redisCommand(c, command4);
if ( r->type != REDIS_REPLY_NIL)
{
printf("Failed to execute command[%s]\n",command4);
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command4);
redisFree(c);
}
相关文章推荐
- 操作redis,把redis做成一个服务(PATH操作)
- NoSQL之Redis技术(四):使用Java操作Redis服务
- CORBA技术系列一:JAVA客户程序与C++服务程序互操作
- Linux redis服务基本配置操作
- centos下部署redis服务环境的操作记录
- php操作redis服务之Predis
- redis 学习手册之redis服务操作命令
- Redis服务操作命令
- 供电公司客户服务系统(原创)
- 吴刚兴的毕业设计课题--校园网络打印服务系统---客户端子系统
- 【蛙蛙王子】wawa客户服务应该达到的功能,大家看看还有什么需要补充
- 第1位客户的保证金服务推介(防欺诈)——CSDN外包实践(6)
- 如何写CRM需求——客户服务管理篇
- 公司培训的作业——『全面客户服务』
- 第1位客户的保证金服务推介(防欺诈)——CSDN外包实践(6)
- 【蛙蛙王子】如何在CRM里提供客户的高度个性化的服务?都有哪些功能要实现个性化?
- 4.3 遇到自己的工作机IIS无法启动,提示World Wide Web Publishing 服务因下列错误而停止: 资源不足,无法完成该操作。
- D7中编写NT服务操作数据库的简单方法
- 远程操作服务元素协议(ISO ROSE)--网络大典
- 操作 NT 服务