1. cd /usr/local/redis/bin 進入到bin目錄,找到redis.conf文件,爲啓動redis作準備java
2. ./redis-server redis.conf 開啓redis服務器redis
3. ./redis-cli -h 192.168.248.59 開啓redis客戶單,host是本身虛擬機的ip地址spring
4.有密碼時,須要輸入密碼 auth 密碼;沒有密碼時直接進入成功shell
5.若想去掉密碼,先執行命令shutdown關閉服務器,再執行quit命令退出客戶端vim
6.執行vim redis.conf命令,進入文件中,將設置密碼行用#註釋掉,從新啓動服務器便可。 springboot
<!--springboot整合redis相關依賴引入--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
#Redis
spring:
redis: host: 192.168.248.59 port: 6379
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootdemoApplication.class)
class SpringbootdemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void add(){
redisTemplate.opsForValue().set("name","zhangsan");
String name = (String)redisTemplate.opsForValue().get("name");
System.out.println(name);
MUser user = new MUser();
user.setId(1);
user.setUsername("admin");
user.setPassword("123");
user.setName("超人");
redisTemplate.opsForValue().set("user",user);
MUser user1 = (MUser)redisTemplate.opsForValue().get("user");
System.out.println(user1);
}
}