源码安装
参考Redis官网https://redis.io/download
我的Linux环境是一个云服务器,配置如下:
Linux VM-0-16-ubuntu 4.4.0-157-generic #185-Ubuntu SMP Tue Jul 23 09:17:01 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
下载
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
出现如下信息表示下载成功
--2022-03-15 22:17:08-- https://download.redis.io/releases/redis-6.2.6.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2476542 (2.4M) [application/octet-stream]
Saving to: ‘redis-6.2.6.tar.gz’
redis-6.2.6.tar.gz 100%[===========================================================================================================================================>] 2.36M 121KB/s in 11s
2022-03-15 22:17:20 (216 KB/s) - ‘redis-6.2.6.tar.gz’ saved [2476542/2476542]
解压与安装
解压
tar xzf redis-6.2.6.tar.gz
进入redis-6.2.6目录
cd redis-6.2.6/
执行make命令进行编译
make
当看到如下信息,表示编译成功
...
CC expire.o
CC geohash.o
CC geohash_helper.o
CC childinfo.o
CC defrag.o
CC siphash.o
CC rax.o
CC t_stream.o
CC listpack.o
CC localtime.o
CC lolwut.o
CC lolwut5.o
CC lolwut6.o
CC acl.o
CC gopher.o
CC tracking.o
CC connection.o
CC tls.o
CC sha256.o
CC timeout.o
CC setcpuaffinity.o
CC monotonic.o
CC mt19937-64.o
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
CC cli_common.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory '/home/ubuntu/tmm/redis-6.2.6/src'
提示我们最好执行一下
make test
来自测一下
不幸的是,没有成功,出现下面错误
cd src && make test
make[1]: Entering directory '/home/ubuntu/tmm/redis-6.2.6/src'
/bin/sh: 1: pkg-config: not found
You need tcl 8.5 or newer in order to run the Redis test
Makefile:391: recipe for target 'test' failed
make[1]: *** [test] Error 1
make[1]: Leaving directory '/home/ubuntu/tmm/redis-6.2.6/src'
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 2
提示没有安装pkg-config,下面来安装
sudo apt-get install pkg-config
还提示You need tcl 8.5 or newer in order to run the Redis test
下面还需要安装tcl
sudo apt-get install tcl-dev
此时执行make test,就可以测试了,部分信息如下:
[ok]: Test replication partial resync: backlog expired (diskless: no, swapdb, reconnect: 1)
[ok]: Memory efficiency with values in range 1024
[exception]: Executing test client: couldn't fork child process: not enough memory.
提示内存不足,因为我只有1G内存。
这个测试就先不测试了。
官网安装
From the official Ubuntu PPA
官网安装就相对来说简单了
首先,添加下载源
sudo add-apt-repository ppa:redislabs/redis
输出信息如下:
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.
It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams.
Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
More info: https://launchpad.net/~redislabs/+archive/ubuntu/redis
Press [ENTER] to continue or ctrl-c to cancel adding it
gpg: keyring `/tmp/tmp2af61qh_/secring.gpg' created
gpg: keyring `/tmp/tmp2af61qh_/pubring.gpg' created
gpg: requesting key 3FA6E3CA from hkp server keyserver.ubuntu.com
gpg: /tmp/tmp2af61qh_/trustdb.gpg: trustdb created
gpg: key 3FA6E3CA: public key "Launchpad PPA for Redis Labs" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
OK
更新源
sudo apt-get update
输出如下信息
ubuntu@VM-0-16-ubuntu:~/tmm$ sudo apt-get update
Hit:1 http://mirrors.tencentyun.com/ubuntu xenial InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu xenial-security InRelease
Hit:3 http://mirrors.tencentyun.com/ubuntu xenial-updates InRelease
Get:4 http://ppa.launchpad.net/redislabs/redis/ubuntu xenial InRelease [18.0 kB]
Get:5 http://ppa.launchpad.net/redislabs/redis/ubuntu xenial/main amd64 Packages [1,024 B]
Get:6 http://ppa.launchpad.net/redislabs/redis/ubuntu xenial/main i386 Packages [1,020 B]
Get:7 http://ppa.launchpad.net/redislabs/redis/ubuntu xenial/main Translation-en [584 B]
Fetched 20.7 kB in 6s (3,129 B/s)
Reading package lists... Done
安装
sudo apt-get install redis
信息如下:
ubuntu@VM-0-16-ubuntu:~/tmm$ sudo apt-get install redis
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
fontconfig-config fonts-dejavu-core i965-va-driver libaacs0 libasound2 libasound2-data libass5 libasyncns0 libavc1394-0 libavcodec-ffmpeg56 libavdevice-ffmpeg56 libavfilter-ffmpeg5 libavformat-ffmpeg56 libavresample-ffmpeg2
libavutil-ffmpeg54 libbdplus0 libbluray1 libbs2b0 libcaca0 libcdio-cdda1 libcdio-paranoia1 libcdio13 libcrystalhd3 libdc1394-22 libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libflac8 libflite1 libfontconfig1 libgl1-mesa-dri
libgl1-mesa-glx libglapi-mesa libgme0 libgraphite2-3 libgsm1 libharfbuzz0b libiec61883-0 libjack-jackd2-0 libllvm6.0 libmodplug1 libmp3lame0 libogg0 libopenal-data libopenal1 libopencv-core2.4v5 libopencv-imgproc2.4v5 libopenjpeg5
libopus0 liborc-0.4-0 libpciaccess0 libpostproc-ffmpeg53 libpulse0 libraw1394-11 libsamplerate0 libschroedinger-1.0-0 libsdl1.2debian libsensors4 libshine3 libsnappy1v5 libsndfile1 libsodium18 libsoxr0 libspeex1 libssh-gcrypt-4
libswresample-ffmpeg1 libswscale-ffmpeg3 libtbb2 libtheora0 libtwolame0 libtxc-dxtn-s2tc0 libva1 libvdpau1 libvorbis0a libvorbisenc2 libvpx3 libwavpack1 libwebp5 libx11-xcb1 libx264-148 libx265-79 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0
libxcb-present0 libxcb-shape0 libxcb-shm0 libxcb-sync1 libxcb-xfixes0 libxdamage1 libxfixes3 libxshmfence1 libxv1 libxvidcore4 libxxf86vm1 libzmq5 libzvbi-common libzvbi0 mesa-va-drivers mesa-vdpau-drivers va-driver-all vdpau-driver-all
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
redis-server redis-tools
Suggested packages:
ruby-redis
The following NEW packages will be installed:
redis redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 267 not upgraded.
Need to get 1,202 kB of archives.
After this operation, 6,863 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ppa.launchpad.net/redislabs/redis/ubuntu xenial/main amd64 redis-tools amd64 6:6.2.6-2rl1~xenial1 [1,080 kB]
Get:2 http://ppa.launchpad.net/redislabs/redis/ubuntu xenial/main amd64 redis-server amd64 6:6.2.6-2rl1~xenial1 [82.5 kB]
Get:3 http://ppa.launchpad.net/redislabs/redis/ubuntu xenial/main amd64 redis all 6:6.2.6-2rl1~xenial1 [39.5 kB]
Fetched 1,202 kB in 5s (217 kB/s)
Selecting previously unselected package redis-tools.
(Reading database ... 104612 files and directories currently installed.)
Preparing to unpack .../redis-tools_6%3a6.2.6-2rl1~xenial1_amd64.deb ...
Unpacking redis-tools (6:6.2.6-2rl1~xenial1) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_6%3a6.2.6-2rl1~xenial1_amd64.deb ...
Unpacking redis-server (6:6.2.6-2rl1~xenial1) ...
Selecting previously unselected package redis.
Preparing to unpack .../redis_6%3a6.2.6-2rl1~xenial1_all.deb ...
Unpacking redis (6:6.2.6-2rl1~xenial1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu21.22) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up redis-tools (6:6.2.6-2rl1~xenial1) ...
Setting up redis-server (6:6.2.6-2rl1~xenial1) ...
Setting up redis (6:6.2.6-2rl1~xenial1) ...
Processing triggers for systemd (229-4ubuntu21.22) ...
Processing triggers for ureadahead (0.100.0-19) ...
此时可以看到和Redis相关的命令
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server
安装hiredis
为了使用C++操作Redis,需要安装hiredis库,这是官方推荐的库。
终端执行下面几个命令
git clone https://github.com/redis/hiredis
cd hiredis
make
sudo make install
sudo ldconfig /usr/local/lib
C++操作Redis
先上代码吧
写一个main.cpp文件
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <hiredis/hiredis.h>
int main()
{
//doTest();
redisContext *conn = redisConnect("127.0.0.1",6379);
if(conn != NULL && conn->err)
{
printf("connection error: %s\n",conn->errstr);
return 0;
}
redisReply *reply = (redisReply*)redisCommand(conn,"set foo 1234");
freeReplyObject(reply);
reply = (redisReply*)redisCommand(conn,"get foo");
printf("%s\n",reply->str);
freeReplyObject(reply);
redisFree(conn);
return 0;
}
编译
g++ main.cpp -L/usr/local/lib/ -lhiredis
执行
./a.out
输出
1234
表示执行成功。