redis 简单部署

redis 官网
官网地址
redis 部署概括
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
$ tar xzf redis-4.0.8.tar.gz
$ cd redis-4.0.8
$ make

<!-- more -->

## redis-server前端启动
$ src/redis-server

## redis-cli 启动
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
redis 实际部署
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
[root@iZuf6iq8e7ya9v3ix71k0pZ redis]# pwd
/usr/local/redis

## 下载redis解压包
[root@iZuf6iq8e7ya9v3ix71k0pZ redis]# wget http://download.redis.io/releases/redis-4.0.8.tar.gz
--2018-02-13 14:12:36-- http://download.redis.io/releases/redis-4.0.8.tar.gz
正在解析主机 download.redis.io (download.redis.io)... 109.74.203.151
正在连接 download.redis.io (download.redis.io)|109.74.203.151|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1729973 (1.6M) [application/x-gzip]
正在保存至: “redis-4.0.8.tar.gz”

100%[=================================================================================================================================>] 1,729,973 973KB/s 用时 1.7s

2018-02-13 14:12:39 (973 KB/s) - 已保存 “redis-4.0.8.tar.gz” [1729973/1729973])

[root@iZuf6iq8e7ya9v3ix71k0pZ redis]# ll
总用量 1692
-rw-r--r-- 1 root root 1729973 2月 3 00:40 redis-4.0.8.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ redis]# tar xzf redis-4.0.8.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ redis]# ll
总用量 1696
drwxrwxr-x 6 root root 4096 2月 3 00:39 redis-4.0.8
-rw-r--r-- 1 root root 1729973 2月 3 00:40 redis-4.0.8.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ redis]# cd redis-4.0.8

## 自动配置
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-4.0.8]# make
cd src && make all
make[1]: 进入目录“/usr/local/redis/redis-4.0.8/src”
​ CC Makefile.dep
make[1]: 离开目录“/usr/local/redis/redis-4.0.8/src”
make[1]: 进入目录“/usr/local/redis/redis-4.0.8/src”
​ ...
​ ...
​ ...
​ CC redis-benchmark.o
​ LINK redis-benchmark
​ INSTALL redis-check-rdb
​ INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: 离开目录“/usr/local/redis/redis-4.0.8/src”
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-4.0.8]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-4.0.8]# ll
总用量 296
-rw-rw-r-- 1 root root 150927 2月 3 00:39 00-RELEASENOTES
-rw-rw-r-- 1 root root 53 2月 3 00:39 BUGS
-rw-rw-r-- 1 root root 1815 2月 3 00:39 CONTRIBUTING
-rw-rw-r-- 1 root root 1487 2月 3 00:39 COPYING
drwxrwxr-x 6 root root 4096 2月 13 14:14 deps
-rw-rw-r-- 1 root root 11 2月 3 00:39 INSTALL
-rw-rw-r-- 1 root root 151 2月 3 00:39 Makefile
-rw-rw-r-- 1 root root 4223 2月 3 00:39 MANIFESTO
-rw-rw-r-- 1 root root 20543 2月 3 00:39 README.md
-rw-rw-r-- 1 root root 58353 2月 3 00:39 redis.conf
-rwxrwxr-x 1 root root 271 2月 3 00:39 runtest
-rwxrwxr-x 1 root root 280 2月 3 00:39 runtest-cluster
-rwxrwxr-x 1 root root 281 2月 3 00:39 runtest-sentinel
-rw-rw-r-- 1 root root 7606 2月 3 00:39 sentinel.conf
drwxrwxr-x 3 root root 4096 2月 13 14:15 src
drwxrwxr-x 10 root root 4096 2月 3 00:39 tests
drwxrwxr-x 8 root root 4096 2月 3 00:39 utils

## 将启动目录配置到父目录同级下
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-4.0.8]# make PREFIX=/usr/local/redis install
cd src && make install
make[1]: 进入目录“/usr/local/redis/redis-4.0.8/src”
​ CC Makefile.dep
make[1]: 离开目录“/usr/local/redis/redis-4.0.8/src”
make[1]: 进入目录“/usr/local/redis/redis-4.0.8/src”

Hint: It's a good idea to run 'make test' ;)

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/usr/local/redis/redis-4.0.8/src”
[root@iZuf6iq8e7ya9v3ix71k0pZ redis-4.0.8]# cd ..
[root@iZuf6iq8e7ya9v3ix71k0pZ redis]# ll
总用量 1700
drwxr-xr-x 2 root root 4096 2月 13 14:23 bin
drwxrwxr-x 6 root root 4096 2月 3 00:39 redis-4.0.8
-rw-r--r-- 1 root root 1729973 2月 3 00:40 redis-4.0.8.tar.gz
[root@iZuf6iq8e7ya9v3ix71k0pZ redis]# cd bin
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ll
总用量 21836
-rwxr-xr-x 1 root root 2450960 2月 13 14:23 redis-benchmark
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-check-aof
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-check-rdb
-rwxr-xr-x 1 root root 2616128 2月 13 14:23 redis-cli
lrwxrwxrwx 1 root root 12 2月 13 14:23 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-server

## 前端启动(关闭窗口,则服务关闭)
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ./redis-server
21156:C 13 Feb 14:25:25.281 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
21156:C 13 Feb 14:25:25.281 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=21156, just started
21156:C 13 Feb 14:25:25.281 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
​ _._
​ _.-``__ ''-._
​ _.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 21156
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
​ `-._ `-.__.-' _.-'
​ `-._ _.-'
​ `-.__.-'

21156:M 13 Feb 14:25:25.283 # Server initialized
21156:M 13 Feb 14:25:25.283 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
21156:M 13 Feb 14:25:25.283 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
21156:M 13 Feb 14:25:25.283 * Ready to accept connections
^C21156:signal-handler (1518503420) Received SIGINT scheduling shutdown...
21156:M 13 Feb 14:30:20.926 # User requested shutdown...
21156:M 13 Feb 14:30:20.926 * Saving the final RDB snapshot before exiting.
21156:M 13 Feb 14:30:20.928 * DB saved on disk
21156:M 13 Feb 14:30:20.929 # Redis is now ready to exit, bye bye...

## 配置后端启动(可以关闭窗口)
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# cp ../redis-4.0.8/redis.conf
cp: 在"../redis-4.0.8/redis.conf" 后缺少了要操作的目标文件
Try 'cp --help' for more information.
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# cp ../redis-4.0.8/redis.conf /usr/local/redis/bin/
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ll
总用量 21900
-rw-r--r-- 1 root root 92 2月 13 14:30 dump.rdb
-rwxr-xr-x 1 root root 2450960 2月 13 14:23 redis-benchmark
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-check-aof
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-check-rdb
-rwxr-xr-x 1 root root 2616128 2月 13 14:23 redis-cli
-rw-r--r-- 1 root root 58353 2月 13 14:31 redis.conf
lrwxrwxrwx 1 root root 12 2月 13 14:23 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-server
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# chmod 777 redis.conf
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ll
总用量 21900
-rw-r--r-- 1 root root 92 2月 13 14:30 dump.rdb
-rwxr-xr-x 1 root root 2450960 2月 13 14:23 redis-benchmark
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-check-aof
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-check-rdb
-rwxr-xr-x 1 root root 2616128 2月 13 14:23 redis-cli
-rwxrwxrwx 1 root root 58353 2月 13 14:31 redis.conf
lrwxrwxrwx 1 root root 12 2月 13 14:23 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-server
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# vim redis.conf
...
...
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
...
...
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ./redis-server redis.conf
21167:C 13 Feb 14:34:45.321 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
21167:C 13 Feb 14:34:45.321 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=21167, just started
21167:C 13 Feb 14:34:45.321 # Configuration loaded
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ps aux|grep redis
root 21168 0.0 0.1 145252 7556 ? Ssl 14:34 0:00 ./redis-server 127.0.0.1:6379
root 21173 0.0 0.0 112664 976 pts/0 S+ 14:35 0:00 grep --color=auto redis

## 启动 redis 实例,并简单set,get
[root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ./redis-cli
127.0.0.1:6379> set name niuhe
OK
127.0.0.1:6379> get name
"niuhe"
127.0.0.1:6379>
jedis 客户端操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Test
public void testJedis() {

/**
* redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)
at redis.clients.jedis.Connection.connect(Connection.java:207)
解决:
vim redis.conf
# bind 127.0.0.0.1

redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because
protected mode is enabled, no bind address was specified, no authentication password is requested to clients.
In this mode connections are only accepted from the loopback interface. If you want to connect from external
computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the
command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host
the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use
CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by
editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the
server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option
. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order
for the server to start accepting connections from the outside.
解决:
vim redis.conf
protected-mode yes 改成
protected-mode no
*/
Jedis jedis = new Jedis("smniuhe.com", 6379);
jedis.set("name", "smniuhe");
System.out.println(jedis.get("name"));
jedis.close();
}
两种持久化方案
  • rdb(默认)
    • 可以设置间隔多长时间保存一次(Redis不用任何配置默认的持久化方案)
      有点:让redis的数据存取速度变快
      缺点:服务器断电时会丢失部分数据(数据的完整性得不到保
  • aop
    • 可以设置实时保存
      优点:持久化良好,能包装数据的完整性
      缺点:大大降低了redis系统的存取速度
    • 设置为 aop 策略
      修改 redis.conf 配置文件 appendonly yes
主从复制
  • 模拟 redis 集群(从服务器只能读取)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ll
    总用量 21900
    -rw-r--r-- 1 root root 197 2月 14 18:19 dump.rdb
    -rwxr-xr-x 1 root root 2450960 2月 13 14:23 redis-benchmark
    -rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-check-aof
    -rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-check-rdb
    -rwxr-xr-x 1 root root 2616128 2月 13 14:23 redis-cli
    -rwxrwxrwx 1 root root 58356 2月 13 16:04 redis.conf
    lrwxrwxrwx 1 root root 12 2月 13 14:23 redis-sentinel -> redis-server
    -rwxr-xr-x 1 root root 5762432 2月 13 14:23 redis-server
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin]# rm -rf dump.rdb
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ../
    [root@iZuf6iq8e7ya9v3ix71k0pZ redis]# mkdir bin2
    [root@iZuf6iq8e7ya9v3ix71k0pZ redis]# cp bin/* bin2/
    [root@iZuf6iq8e7ya9v3ix71k0pZ redis]# cp bin2
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin2]# vim redis.conf
    修改 redis-cli 启动的端口号
    port = 6380
    # 设置主的端口 ip port
    # slaveof <masterip> <masterport>
    slaveof 106.15.191.27 6379
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ./redis-server redis.conf
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin2]# ./redis-server redis.conf
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin2]# ./redis-server redis.conf
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin2]# ps -aux|grep redis
    root 21266 0.0 0.2 147300 9768 ? Ssl 2月13 1:10 ./redis-server *:6379
    root 21847 0.0 0.2 147300 9688 ? Ssl 18:16 0:00 ./redis-server *:6380
    root 21853 0.0 0.0 112664 976 pts/0 S+ 18:16 0:00 grep --color=auto redis
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin2]# ./redis-cli
    127.0.0.1:6379> set name smniuhe79
    OK
    127.0.0.1:6379> get name
    "smniuhe79"
    127.0.0.1:6379>
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin]# cd ../bin
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin]# ./redis-cli shutdown
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin2]# ./redis-cli -p 6380
    127.0.0.1:6380> get name
    "smniuhe79"
    127.0.0.1:6380>

    # 指定ip地址和port
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin2]# ./redis-cli -h ip -p 6380
    106.15.191.27:6380> get name

    # 指定校验密码
    [root@iZuf6iq8e7ya9v3ix71k0pZ bin2]# ./redis-cli -h ip -p port -a authpassword
    106.15.191.27:6380> get name

    # 正常关闭实例
    106.15.191.27:6380> shutdown