赞
踩
brew install redis
1
1.启动redis服务
brew services start redis
1
2.关闭redis服务
brew services stop redis
1
3.重启redis服务
brew services restart redis
1
4.打开图形化界面
redis-cli
redis-cli --raw # 解决中文乱码
12
5.开机启动redis命令
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
1
6.使用配置文件启动redis-server
redis-server /usr/local/etc/redis.conf
1
7.停止redis服务
redis-cli shutdown
1
/usr/local/etc/redis.conf
1
9.卸载redis
brew uninstall redis rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
1
10.允许远程访问
vim /usr/local/etc/redis.conf
1
注释bind,默认情况下 redis不允许远程访问,只允许本机访问。
#bind 127.0.0.1
1
注:在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,需要把protected-mode yes改为protected-mode no
下载hredis
解压之后,同样的
make
sudo make install
新建一个临时目录
创建新文件redis.h
/************************************************************************* > File Name: redis.h > Author: > Mail: > Created Time: 三 11/11 21:23:11 2020 ************************************************************************/ #ifndef _REDIS_H #define _REDIS_H #include <iostream> #include <cstring> #include <string> #include <stdio.h> #include <hiredis/hiredis.h> class Redis { public: Redis() {} ~Redis() { this->_connect = NULL; this->_reply = NULL; } bool connect(std::string host, int port) { this->_connect = redisConnect(host.c_str(), port); if (this->_connect != NULL && this->_connect->err) { printf("connect error: %s\n", this->_connect->errstr); return 0; } return 1; } std::string get(std::string key) { this->_reply = (redisReply*)redisCommand(this->_connect, "GET %s", key.c_str()); std::string str = this->_reply->str; freeReplyObject(this->_reply); return str; } void set(std::string key, std::string value) { redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str()); } private: redisContext* _connect; redisReply* _reply; }; #endif
创建redis.cpp
/************************************************************************* > File Name: redis.cpp > Author: > Mail: > Created Time: 三 11/11 21:23:36 2020 ************************************************************************/ #include "redis.h" int main() { Redis *r = new Redis(); if (!r->connect("127.0.0.1", 6379)) { printf("connect error!\n"); return 0; } r->set("name", "Andy"); printf("Get the name is %s\n", r->get("name").c_str()); delete r; return 0; }
编写Makefile文件
CCX=g++ CXXFLAGS= -std=c++11 -Wall -g -lhiredis OBJS=redis.o .PHONY: clean run redis: $(OBJS) $(CCX) $(CXXFLAGS) $(OBJS) -o redis redis.o: redis.cpp redis.h $(CCX) -c $(CXXFLAGS) redis.cpp run: ./redis clean: rm -rf *.o, redis
进行编译
make
make run
make clean
或者命令行执行
g++ redis.cpp -o redis -lhiredis
运行如果出现找不到动态链接库
在/etc/ld.so.conf.d/目录下新建文件usr-libs.conf,内容是:/usr/local/lib
最后执行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。