linux环境下安装redis
环境
- 系统:Linux
- 版本: Red Hat 4.8.5-16
- 服务商:阿里云
- 内核:3.10.0-693.2.2.el7.x86_64
step 1: 在用户级程序目录下创建 /usr/local/redis
cd /usr/local/
mkdir redis
step 2: 获取redis资源,服务器下载源文件
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
step 3: 进行解压资源,并编译
tar -xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make
step 4:测试安装是否完成
./redis-server
// 重开一个窗口
./redis-cli
执行完以上步骤就安装成功了,但是只能是简单安装,革命尚未成功
配置
我们在项目中使用redis,可能要设置密码,访问端口等配置,这些配置是在redis.conf中配置的。所以,我们接下来就要进行配置工作。
- 我们从官网下载一份配置文件https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf,在该基础上进行配置工作
- 增加配置文件,redis支持启动时指定配置文件,同时也支持多个配置文件的方式。多配置文件通过我们启动时指定的配置文件来绑定。
# 启动时指定配置文件
./redis-server /path/to/redis.conf
# 如果你需要覆盖掉我们启动时配置文件的设置,你可以在下面进行指定配置文件路径来实现(最后一个配置会覆盖之前的),同时这也是多配置文件的实现方式
31 # If instead you are interested in using includes to override configuration
32 # options, it is better to use include as the last line.
33 #
34 # include /path/to/local.conf
35 # include /path/to/other.conf
- 访问控制,此处可以注释,注释后为全网访问,如果保持默认 127.0.0.1 则只能本机访问
65 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
66 # JUST COMMENT THE FOLLOWING LINE.
67 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68 bind 127.0.0.1
- 端口监听,系统默认为6379,再进行主从配置的时候需要修改此处配置,也可以通过启动时参数指定端口
89 # Accept connections on the specified port, default is 6379 (IANA #815344).
90 # If port 0 is specified Redis will not listen on a TCP socket.
91 port 6379
-
连接数,在高并发的环境下,你需要设置更高的backlog来避免客户端连接缓慢。
注意,Linux系统会自动的修改这个值为/proc/sys/net/core/somaxconn的值(这个值默认为128),所以请保证你同时修改了这两个值
93 # TCP listen() backlog.
94 #
95 # In high requests-per-second environments you need an high backlog in order
96 # to avoid slow clients connections issues. Note that the Linux kernel
97 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
98 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
99 # in order to get the desired effect.
100 tcp-backlog 511
- 超时断开连接,在客户端闲置 N秒时,主动关闭该连接。如果设置为0,则不主动关闭
111 # Close the connection after a client is idle for N seconds (0 to disable)
112 timeout 0
- 指定日志文件名称,在多节点时可以用于区分
167 # Specify the log file name. Also the empty string can be used to force
168 # Redis to log on the standard output. Note that if you use standard
169 # output for logging but daemonize, logs will be sent to /dev/null
170 logfile ""
- 指定databases,如果我们在不同的程序中使用相同的databases会造成脏数据的情况,同时也可能会串读。比如A系统有一个token,B系统也有一个,这个时候就不能区分了,会进行覆盖操作。不同的系统应该指定不同的databases,无上限,命令行可以通过select N 进行切换。注意,不同的databases并不是完全隔离,FLUSHALL 指令就会清除所有记录,而不是当前databases中的记录。建议使用时不同程序使用不同redis实例。
182 # Set the number of databases. The default database is DB 0, you can select
183 # a different one on a per-connection basis using SELECT <dbid> where
184 # dbid is a number between 0 and 'databases'-1
185 databases 16
- 持久化,这里可以设置我们持久化到硬盘的规则,因为redis是基于内存,在断电或者宕机的情况后如果没有持久化进行数据恢复,这就可能会造成数据的丢失。所以此处是持久化到硬盘,规则为900 秒中至少有一次修改,或者300秒有10次修改,或者60秒内有10000次修改
195 ################################ SNAPSHOTTING ################################
196 #
197 # Save the DB on disk:
198 #
199 # save <seconds> <changes>
200 #
201 # Will save the DB if both the given number of seconds and the given
202 # number of write operations against the DB occurred.
203 #
204 # In the example below the behaviour will be to save:
205 # after 900 sec (15 min) if at least 1 key changed
206 # after 300 sec (5 min) if at least 10 keys changed
207 # after 60 sec if at least 10000 keys changed
208 #
209 # Note: you can disable saving completely by commenting out all "save" lines.
210 #
211 # It is also possible to remove all the previously configured save
212 # points by adding a save directive with a single empty string argument
213 # like in the following example:
214 #
215 # save ""
216
217 save 900 1
218 save 300 10
219 save 60 10000
- 如果快照失败,则停止写入
221 # By default Redis will stop accepting writes if RDB snapshots are enabled
222 # (at least one save point) and the latest background save failed.
223 # This will make the user aware (in a hard way) that data is not persisting
224 # on disk properly, otherwise chances are that no one will notice and some
225 # disaster will happen.
226 #
227 # If the background saving process will start working again Redis will
228 # automatically allow writes again.
229 #
230 # However if you have setup your proper monitoring of the Redis server
231 # and persistence, you may want to disable this feature so that Redis will
232 # continue to work as usual even if there are problems with disk,
233 # permissions, and so forth.
234 stop-writes-on-bgsave-error yes
- 压缩字符串写入rdb,如果关闭RBD进程可能会快一些,但是最后的rdb文件可能会更大
236 # Compress string objects using LZF when dump .rdb databases?
237 # For default that's set to 'yes' as it's almost always a win.
238 # If you want to save some CPU in the saving child set it to 'no' but
239 # the dataset will likely be bigger if you have compressible values or keys.
240 rdbcompression yes
- 指定持久化文件写入的路径
254 # The working directory.
255 #
256 # The DB will be written inside this directory, with the filename specified
257 # above using the 'dbfilename' configuration directive.
258 #
259 # The Append Only File will also be created inside this directory.
260 #
261 # Note that you must specify a directory here, not a file name.
262 dir ./
- 主从节点配置,此处为从节点配置。配置后在启动子节点时会主动连接主节点进行数据同步
266 # Master-Slave replication. Use slaveof to make a Redis instance a copy of
267 # another Redis server. A few things to understand ASAP about Redis replication.
268 #
269 # 1) Redis replication is asynchronous, but you can configure a master to
270 # stop accepting writes if it appears to be not connected with at least
271 # a given number of slaves.
272 # 2) Redis slaves are able to perform a partial resynchronization with the
273 # master if the replication link is lost for a relatively small amount of
274 # time. You may want to configure the replication backlog size (see the next
275 # sections of this file) with a sensible value depending on your needs.
276 # 3) Replication is automatic and does not need user intervention. After a
277 # network partition slaves automatically try to reconnect to masters
278 # and resynchronize with them.
279 #
280 # slaveof <masterip> <masterport>
- 主从节点配置,主节点连接密码
282 # If the master is password protected (using the "requirepass" configuration
283 # directive below) it is possible to tell the slave to authenticate before
284 # starting the replication synchronization process, otherwise the master will
285 # refuse the slave request.
286 #
287 # masterauth <master-password>
- 子节点异常丢失,或者主从同步时,设置为yes,则表示子节点任然可以继续响应请求,如果为no则slave会回复"正在从master同步(SYNC with master in progress)"来处理各种请求,除了 INFO 和 SLAVEOF 命令。
289 # When a slave loses its connection with the master, or when the replication
290 # is still in progress, the slave can act in two different ways:
291 #
292 # 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
293 # still reply to client requests, possibly with out of date data, or the
294 # data set may just be empty if this is the first synchronization.
295 #
296 # 2) if slave-serve-stale-data is set to 'no' the slave will reply with
297 # an error "SYNC with master in progress" to all the kind of commands
298 # but to INFO and SLAVEOF.
299 #
300 slave-serve-stale-data yes
- 将内存中的数据写入磁盘,redis提供了三种策略,always:每次有修改新增时,everysec:每秒写入,no: 不主动写入。默认每秒,这样对系统资源使用较为友好,数据如果丢失也仅限于秒级数据。
681 # Redis supports three different modes:
682 #
683 # no: don't fsync, just let the OS flush the data when it wants. Faster.
684 # always: fsync after every write to the append only log. Slow, Safest.
685 # everysec: fsync only one time every second. Compromise.
686 #
687 # The default is "everysec", as that's usually the right compromise between
688 # speed and data safety. It's up to you to understand if you can relax this to
689 # "no" that will let the operating system flush the output buffer when
690 # it wants, for better performances (but if you can live with the idea of
691 # some data loss consider the default persistence mode that's snapshotting),
692 # or on the contrary, use "always" that's very slow but a bit safer than
693 # everysec.
694 #
695 # More details please check the following article:
696 # http://antirez.com/post/redis-persistence-demystified.html
697 #
698 # If unsure, use "everysec".
699
700 # appendfsync always
701 appendfsync everysec
702 # appendfsync no
- 重写本地文件,当新写入文件达到上一次写入大小的100%,或者内存为64mb时,重写
725 # Automatic rewrite of the append only file.
726 # Redis is able to automatically rewrite the log file implicitly calling
727 # BGREWRITEAOF when the AOF log size grows by the specified percentage.
728 #
729 # This is how it works: Redis remembers the size of the AOF file after the
730 # latest rewrite (if no rewrite has happened since the restart, the size of
731 # the AOF at startup is used).
732 #
733 # This base size is compared to the current size. If the current size is
734 # bigger than the specified percentage, the rewrite is triggered. Also
735 # you need to specify a minimal size for the AOF file to be rewritten, this
736 # is useful to avoid rewriting the AOF file even if the percentage increase
737 # is reached but it is still pretty small.
738 #
739 # Specify a percentage of zero in order to disable the automatic AOF
740 # rewrite feature.
741
742 auto-aof-rewrite-percentage 100
743 auto-aof-rewrite-min-size 64mb
以上只是对配置文件部分设置做了说明,其中还有很多未列出。详情请查看redis.conf 文件