官网下载安装包
1.安装部分
编译安装
tar -zxvf /usr/redis
cd /usr/redis/redis-4.0.1
make
启动redis-server
cd /usr/redis/redis-4.0.1/src
./redis-server
测试是否正常启动
ps -ef | grep redis
netstat -tupln
一个简单的演示
cd /usr/redis/redis-4.0.1/src
./redis-cli
set name mayun
get name
已守护线程启动服务
修改配置文件(/usr/redis/redis-4.0.1/redis.conf)
参数:daemonize no改为yes
redis自带性能测试工具:
cd /usr/redis/redis-4.0.1/src
./redis-benchmark
关闭:
不要用kill -9 pid:这样可能会丢失数据
cd /usr/redis/redis-4.0.1/src
./redis-cli
shutdown
2.使用部分(java)
要在Java项目中使用,首先要确保已下载redis的java客户端
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
例子:
package com.itao.study.study2;
import redis.clients.jedis.Jedis;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
Jedis jedis = new Jedis("***.***.***.***");
jedis.set("foo", "bar");
String value = jedis.get("foo");
System.out.println(jedis.get("foo"));
}
}
默认情况下,redis是没有密码的,是不能远程链接的,我们需要设置密码
cd /usr/redis/redis-4.0.1/src
./redis-cli
CONFIG set requirepass "123456"
AUTH 123456
CONFIG get requireass
java代码修改如下:
package com.itao.study.study2;
import redis.clients.jedis.Jedis;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
Jedis jedis = new Jedis("***.***.***.***");
jedis.auth("123456");
jedis.set("foo", "bar");
String value = jedis.get("foo");
System.out.println(jedis.get("foo"));
}
}
可以参看教程:
http://www.yiibai.com/redis/redis_quick_guide.html