大家好,我是Tester1991,一枚普通测试工程师,热爱生活,热爱分享!
Redis官网
https://redis.io/download
阿里云服务器安装Redis
$ wget http://download.redis.io/releases/redis-6.0.8.tar.gz
$ tar xzf redis-6.0.8.tar.gz
$ cd redis-6.0.8
$ make
安装过程中如果出现如下错误:
serverLog(LL_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket);
^
server.c:5103:19: error: ‘struct redisServer’ has no member named ‘supervised_mode’
if (server.supervised_mode == SUPERVISED_SYSTEMD) {
^
server.c:5104:24: error: ‘struct redisServer’ has no member named ‘masterhost’
if (!server.masterhost) {
^
server.c:5117:15: error: ‘struct redisServer’ has no member named ‘maxmemory’
if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
^
server.c:5117:39: error: ‘struct redisServer’ has no member named ‘maxmemory’
if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
^
server.c:5118:176: error: ‘struct redisServer’ has no member named ‘maxmemory’
serverLog(LL_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory);
^
server.c: In function ‘hasActiveChildProcess’:
server.c:1476:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘allPersistenceDisabled’:
server.c:1482:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘writeCommandsDeniedByDiskError’:
server.c:3747:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
server.c: In function ‘iAmMaster’:
server.c:4914:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
make[1]: *** [server.o] Error 1
make[1]: Leaving directory `/usr/src/redis-6.0.1/src'
make: *** [install] Error 2
出现上面错误的原因是redis6.0+以上安装需要gcc版本5.3及以上。
查看当前gcc版本
gcc -v
我们发现阿里云服务器上面,centos7系统gcc版本默认为4.8.5。
所以我们需要升级gcc版本。
//升级gcc到9以上
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
还需要执行下面命令,二者任选其一
//临时将此时的gcc版本改为9
scl enable devtoolset-9 bash
//或永久改变
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
执行完上述操作后,执行gcc -v命令,我们会发现gcc版本成功升级到了9.1.1。接下来我们继续执行make安装redis6.0+,应该就不会出现什么问题。
redis6.0+安装成功后,我们如何验证redis服务是否正常?
启动服务端
src/redis-server
如上图,我们发现redis服务已经正常启动了。
redis服务端已经启动成功,我们如何进行客户端连接呢?
Redis配置文件
位置:redis安装路径下面redis.conf文件
修改redis.conf配置文件
1.daemonize设置成yes
2.注释掉bind 127.0.0.1 添加bind 0.0.0.0
3.protected-mode yes 改为 protected-mode no
配置阿里云安全组规则
经过上述的配置,我们现在完全可以使用redis客户端进行连接我们的阿里云上面的redis服务了。
Redis客户端
Redis客户端我推荐大家使用AnotherRedisDesktopManager这个开源工具。
AnotherRedisDesktopManager地址
https://github.com/qishibo/AnotherRedisDesktopManager
AnotherRedisDesktopManager支持通过工具直接打开redis命令客户端。
Python连接Redis
import redis
pool = redis.ConnectionPool(host='xxx', port='6379', decode_responses=True)
r = redis.Redis(host='xxx', port='6379', decode_responses=True, password='xxx')
print(r.get('name'))
Java连接Redis
import redis.clients.jedis.Jedis;
public class RedisTest {
public static void main(String[] args) {
Jedis jedis = new Jedis("xxx", 6379);
jedis.auth("root");
System.out.println("连接成功");
//查看服务是否运行
System.out.println("服务正在运行: " + jedis.ping());
System.out.println("redis 存储的字符串为: " + jedis.get("name"));
}
}
如果您觉得我的文章帮助了您,后续我会持续分享软件测试相关知识,本篇博客实战视频后续我录制好会上传,欢迎大家一起学习测试知识,一起分享!