1.下载安装包
访问https://redis.io/download到官网进行下载。这里下载最新的4.0.14版本.
2.安装
2.1 通过远程管理工具,将压缩包拷贝到Linux服务器中,执行解压操作
[caimh@server01 software]$ tar -zxf redis-4.0.14.tar.gz -C /opt/module/
2.2 进入解压文件目录使用make对解压的Redis文件进行编译
[caimh@server01 redis-4.0.14]$ pwd
/opt/module/redis-4.0.14
[caimh@server01 redis-4.0.14]$ make
编译完成之后,可以看到解压文件redis-4.0.14中会有对应的src、conf等文件夹
2.3 编译成功后,进入src文件夹,执行make install进行Redis安装。
[caimh@server01 src]$ sudo make PREFIX=/usr/local/redis install
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
[caimh@server01 src]$ cd /usr/local/redis/ #查看
[caimh@server01 redis]$ pwd
/usr/local/redis
[caimh@server01 redis]$ ll
total 4
drwxr-xr-x 2 root root 4096 Dec 27 16:26 bin
[caimh@server01 redis]$ cd bin/
[caimh@server01 bin]$ ll #常用命令
total 21888
-rwxr-xr-x 1 root root 2452216 Dec 27 16:26 redis-benchmark
-rwxr-xr-x 1 root root 5777792 Dec 27 16:26 redis-check-aof
-rwxr-xr-x 1 root root 5777792 Dec 27 16:26 redis-check-rdb
-rwxr-xr-x 1 root root 2618240 Dec 27 16:26 redis-cli
lrwxrwxrwx 1 root root 12 Dec 27 16:26 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 5777792 Dec 27 16:26 redis-server
3.部署
3.1为了方便管理,将Redis文件中的conf配置文件和常用命令移动到统一文件中
将刚刚安装目录下的redis.conf复制移动到 /usr/local/redis下
[caimh@server01 redis]$ pwd
/usr/local/redis
[caimh@server01 redis]$ sudo cp /opt/module/redis-4.0.14/redis.conf /usr/local/redis/
[caimh@server01 redis]$ ll
total 64
drwxr-xr-x 2 root root 4096 Dec 27 16:26 bin
-rw-r--r-- 1 root root 58766 Dec 27 16:32 redis.conf
3.2 设置后端启动Redis
首先编辑conf文件,将daemonize属性改为yes(表明需要在后台运行)
[caimh@server01 redis]$ sudo vim redis.conf
daemonize yes
3.3 启动redis服务,并制定启动服务配置文件
/usr/local/redis/bin
[caimh@server01 bin]$ ./redis-server /usr/local/redis/redis.conf #启动Redis服务
27491:C 27 Dec 16:40:10.125 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
27491:C 27 Dec 16:40:10.125 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=27491, just started
27491:C 27 Dec 16:40:10.125 # Configuration loaded
[caimh@server01 bin]$ ./redis-cli #启动Redis客户端
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
[caimh@server01 bin]$ netstat -tunpl|grep 6379 #查看Redis进程状态
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 27492/./redis-serve
3.4 关闭redis服务
[caimh@server01 bin]$ ./redis-server ../redis.conf
[caimh@server01 bin]$ ps -ef|grep 6379
caimh 7060 1 0 17:46 ? 00:00:00 ./redis-server 127.0.0.1:6379
caimh 7118 27321 0 17:47 pts/0 00:00:00 grep --color=auto 6379
[caimh@server01 bin]$ ./redis-cli shutdown
[caimh@server01 bin]$ ps -ef|grep 6379
caimh 7167 27321 0 17:47 pts/0 00:00:00 grep --color=auto 6379
4 Jedis客户端操作
//1.获取连接池的配置对象
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//2.设置最大连接数
jedisPoolConfig.setMaxTotal(30);
//3.设置最大空闲连接数
jedisPoolConfig.setMaxIdle(10);
//4.获取连接池
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "111.229.85.165", 6379);
//5.获取核心对象
Jedis jedis = null;
try {
//通过连接池获取对象
jedis = jedisPool.getResource();
//6.保存数据
jedis.set("name", "caimh");
jedis.set("age", "30");
//7.获取数据
String name = jedis.get("name");
String age = jedis.get("age");
System.out.println("name:" + name + " age:" + age);
} finally {
//8.释放资源
if (jedis != null) {
jedis.close();
}
if (jedisPool != null) {
jedisPool.close();
}
}
5 问题
问题1:关闭Redis遇到问题
[caimh@server01 bin]$ ./redis-cli shutdown
(error) ERR Errors trying to SHUTDOWN. Check logs.
参考:https://blog.csdn.net/github_33809414/article/details/82531642
[caimh@server01 redis]$ pwd
/usr/local/red
[caimh@server01 redis]$ mkdir redis_dbfile/ redis_log/
[caimh@server01 redis]$ sudo chmod 777 redis_dbfile/
[caimh@server01 redis]$ sudo touch redis_log/redis_log.log
[caimh@server01 redis]$ sudo chmod -R 777 redis_log
[caimh@server01 redis]$ sudo vim redis.conf
logfile /usr/local/redis/redis_log/redis_log.log
dir /usr/local/redis/redis_dbfile/
问题2:Jedis客户端连接云服务器部署的Redis三种常见异常
解决方案:
参考https://blog.csdn.net/qq_38159800/article/details/79560413