一、环境准备
Redis官网:https://redis.io/
历史版本:http://download.redis.io/releases/
1、安装依赖
yum -y install gcc gcc-c++ make cmake lrzsz
2、解压redis
tar zxvf redis-6.0.1.tar.gz
二、redis编译安装
1、编译redis
cd redis-6.0.1 && make
redis6需要较高版本的gcc编译
如果make失败,需要手动升级gcc版本(redis5以下的不需要升级)
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile ## gcc版本永久生效
2、安装redis
make install PREFIX=/usr/local/redis
3、拷贝redis.conf
mkdir /usr/local/redis/etc
cp redis.conf /usr/local/redis/etc
4、启动redis
nohup /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf 2>&1 &
三、redis环境配置
1、配置环境变量
## 在 /etc/profile 后添加
REDIS_HOME=/usr/local/redis/
PATH=$PATH:$REDIS_HOME/bin
2、开机自启配置
## 在 /etc/rc.local 下添加
echo never > /sys/kernel/mm/transparent_hugepage/enabled
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
3、其他配置
# 注释掉绑定ip,允许远程连接
bind 127.0.0.1 # 注释掉这句
# 允许后台运行
daemonize yes
# 关闭保护模式,否则外部ip无法连接
protected-mode no
# 防火墙开启redis端口,默认6379
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
# 内存设置
echo 8192 > /proc/sys/net/core/somaxconn ## 最大内存8G
sysctl vm.overcommit_memory=1 ## 内存超出时开辟内存
/etc/sysctl.conf 文件添加
vm.overcommit_memory = 1
# 密码设置 redis.conf 文件下添加
requirepass 123456
# 不重启设置密码(永久有效),登录之后输入
config set requirepass 123456
# 查询密码
config get requirepass
# 验证密码
auth 123456
# 登录时,验证登录输入一下
auth 123456
4、redis关闭方法
redis-cli shutdown
ps -ef | grep redis | grep -v grep | awk '{print $2}' | xargs kill -9
redis-cli -h 127.0.0.1 -p 6379 shutdown
四、redis系统服务
1、redis添加到系统服务
vi /lib/systemd/system/redis.service
[Unit]
Description=The redis-server Process Manager
Documentation=https://redis.io/
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown
[Install]
WantedBy=multi-user.target
2、redis系统服务命令
systemctl enable redis
systemctl start redis
systemctl stop redis
systemctl status redis
五、redis JavaAPI
1、Maven配置
redis.clients
jedis
3.1.0
2、java连接 redis
import redis.clients.jedis.Jedis;
Jedis jedis = new Jedis("localhost");
jedis.auth("123456");
jedis.set("mmsi", "123456789");
//存储数据到列表中
jedis.lpush("site-list", "Runoob");
jedis.lpush("site-list", "Google");
jedis.lpush("site-list", "Taobao");
// 获取存储的数据并输出
List list = jedis.lrange("site-list", 0 ,2);
for(int i=0; i keys = jedis.keys("*");
Iterator it=keys.iterator() ;
while(it.hasNext()){
String key = it.next();
System.out.println(key);
}
3、redis连接池
package com.xtd.utils.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Set;
public class RedisUtil {
private static JedisPool jedisPool;
private String ip = "127.0.0.1";
private Integer port;
private Integer maxIdle;
private Long maxWait;
public RedisUtil(Integer port) {
this.port = port;
this.maxIdle = 10;
this.maxWait = -1L;
initJedisPool();
}
public RedisUtil(String ip, Integer port) {
this.ip = ip;
this.port = port;
this.maxIdle = 10;
this.maxWait = -1L;
initJedisPool();
}
public RedisUtil(String ip, Integer port, Integer maxIdle, Long maxWait) {
this.ip = ip;
this.port = port;
this.maxIdle = maxIdle;
this.maxWait = maxWait;
initJedisPool();
}
public Jedis getJedis() {
return jedisPool.getResource();
}
private void initJedisPool() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setMaxTotal(10000);
jedisPool = new JedisPool(config, ip, port, 60000);
}
public static void main(String[] args) {
// Jedis jedis = new RedisUtil("192.168.0.102", 6379).getJedis();
// Jedis jedis = new RedisUtil("127.0.0.1", 6379).getJedis();
Jedis jedis = new RedisUtil(6379).getJedis();
jedis.select(0);
Set set = jedis.keys("t_route_local-*");
jedis.set("station_v1_01","station_v1_01;station,\\`date\\`,\\`time\\`,lat,long,visibility,air_temperature,wind_direction,wind_speed,air_pressure,precipitaion,sea_temperature,sea_salt,wave_period,wave_height;station_par,month_par");
for (String s : set) {
// jedis.del(s);
System.out.println(s);
}
String value = jedis.get("station_v1_01");
System.out.println(value);
System.out.println(set.size());
jedis.close();
}
}
文章引自
https://cloud.tencent.com/developer/article/1818699
标签:redis,编译,复制,jedis,usr,6.0,local,port
From: https://www.cnblogs.com/Magiclala/p/17142868.html