redis安装与配置
2019-06-27 本文已影响0人
荒天帝886
https://www.runoob.com/redis/redis-install.html
服务启动
cd到redis的src目录下
执行./redis-server
打开redis客户端
./redis-cli
解决中文乱码
./redis-cli --raw
密码验证
auth password
服务关闭
./redis-cli shutdown
更多用法 http://www.ttlsa.com/redis/the-reids-client-redis-cli-using/
解决其他主机可访问
- 打开配置文件把下面对应的注释掉
bind 127.0.0.1
- Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程,设置为no
daemonize no
- 保护模式
protected-mode no
参考链接
Jedis
package com.fzy.javastudy.java.day_0626;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import redis.clients.jedis.*;
import java.io.IOException;
public class RedisTest {
//新建jedis对象
private final static Jedis jedis = new Jedis("47.93.35.143");
//初始化密码
static {
jedis.auth("redis123456");
}
public static void main(String[] args) {
jedis.set("username", "fdsfdsfdsfsdfdsf分行第三");
System.out.println(jedis.get("username"));
}
@Test
public void test() {
Animal animal = new Animal();
animal.setName("dog");
animal.setColor("black");
animal.setAge(3);
animal.setLeg(4);
try {
String animalJon = new ObjectMapper().writeValueAsString(animal);
String s = jedis.set("animal", animalJon);
System.out.println(s);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
@Test
public void test2() {
String animalJson = jedis.get("animal");
System.out.println(animalJson);
try {
Animal animal = new ObjectMapper().readValue(animalJson, Animal.class);
System.out.println(animal);
} catch (IOException e) {
e.printStackTrace();
}
}
}