关键字:Linux, CentOS, Redis server
1. 安装redis
1.1 下载redis
根据需要的版本下载redis,这里以4.0.6版本为例。执行以下命令
这里我直接下载到了/usr/local中,因为我们即将在这个位置安装redis。
1.2 解压缩
执行以下命令
tar -zxvf redis-4.0.6.tar.gz
解压之后会生成一个redis-4.0.6的文件夹
1.3 源码安装
进入该目录后,执行以下命令进行编译
cd redis-4.0.6 && make MALLOC=libc
编译完成后,会自动建立一个src的目录,里面有所有用到的.c和.h文件以及生成的.o文件等
(venv) [root@localhost redis-4.0.6]# cd src
(venv) [root@localhost src]# ls
adlist.c bio.c crc64.c evict.c intset.c memtest.o pubsub.o redis-benchmark.o replication.c setproctitle.o testhelp.h ziplist.h
adlist.h bio.h crc64.h evict.o intset.h mkreleasehdr.sh quicklist.c redis-check-aof replication.o sha1.c t_hash.c ziplist.o
adlist.o bio.o crc64.o expire.c intset.o module.c quicklist.h redis-check-aof.c rio.c sha1.h t_hash.o zipmap.c
ae.c bitops.c db.c expire.o latency.c module.o quicklist.o redis-check-aof.o rio.h sha1.o t_list.c zipmap.h
ae_epoll.c bitops.o db.o fmacros.h latency.h modules rand.c redis-check-rdb rio.o siphash.c t_list.o zipmap.o
ae_evport.c blocked.c debug.c geo.c latency.o multi.c rand.h redis-check-rdb.c scripting.c siphash.o t_set.c zmalloc.c
ae.h blocked.o debugmacro.h geo.h lazyfree.c multi.o rand.o redis-check-rdb.o scripting.o slowlog.c t_set.o zmalloc.h
ae_kqueue.c childinfo.c debug.o geohash.c lazyfree.o networking.c rax.c redis-cli sdsalloc.h slowlog.h t_string.c zmalloc.o
ae.o childinfo.o defrag.c geohash.h lzf_c.c networking.o rax.h redis-cli.c sds.c slowlog.o t_string.o
ae_select.c cluster.c defrag.o geohash_helper.c lzf_c.o notify.c rax_malloc.h redis-cli.o sds.h solarisfixes.h t_zset.c
anet.c cluster.h dict.c geohash_helper.h lzf_d.c notify.o rax.o redismodule.h sds.o sort.c t_zset.o
anet.h cluster.o dict.h geohash_helper.o lzf_d.o object.c rdb.c redis-sentinel sentinel.c sort.o util.c
anet.o config.c dict.o geohash.o lzf.h object.o rdb.h redis-server sentinel.o sparkline.c util.h
aof.c config.h dump.rdb geo.o lzfP.h pqsort.c rdb.o redis-trib.rb server.c sparkline.h util.o
aof.o config.o endianconv.c help.h Makefile pqsort.h redisassert.h release.c server.h sparkline.o valgrind.sup
asciilogo.h crc16.c endianconv.h hyperloglog.c Makefile.dep pqsort.o redis-benchmark release.h server.o syncio.c version.h
atomicvar.h crc16.o endianconv.o hyperloglog.o memtest.c pubsub.c redis-benchmark.c release.o setproctitle.c syncio.o ziplist.c
接下来将/usr/local/redis-4.0.6/src目录下的文件加到/usr/local/bin下,执行命令
cd src && make install
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
(base) [root@localhost ~]# ll /usr/local/bin
总用量 9428
-rwxr-xr-x. 1 root root 290422 12月 30 17:03 redis-benchmark
-rwxr-xr-x. 1 root root 2977570 12月 30 17:03 redis-check-aof
-rwxr-xr-x. 1 root root 2977570 12月 30 17:03 redis-check-rdb
-rwxr-xr-x. 1 root root 428943 12月 30 17:03 redis-cli
lrwxrwxrwx. 1 root root 12 12月 30 17:03 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 2977570 12月 30 17:03 redis-server
(base) [root@localhost ~]#
2. 运行redis server
2.1 直接启动
直接在/usr/local/redis-4.0.6/src目录下执行下面的命令
./redis-server
(venv) [root@localhost src]# ./redis-server
29150:C 30 Dec 17:05:30.652 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29150:C 30 Dec 17:05:30.652 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=29150, just started
29150:C 30 Dec 17:05:30.652 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
29150:M 30 Dec 17:05:30.654 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 29150
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
29150:M 30 Dec 17:05:30.659 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
29150:M 30 Dec 17:05:30.659 # Server initialized
29150:M 30 Dec 17:05:30.659 # 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.
29150:M 30 Dec 17:05:30.659 # 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.
29150:M 30 Dec 17:05:30.659 * Ready to accept connections
2.2 设置为守护进程
出现这个界面就说明我们的redis server已经成功运行了,但是有时候我们希望redis作为一个后台进程来执行,这样我们还可以在这个终端做其他的工作,那么可以配置redis为守护进程。具体操作如下:
退回上一级目录,也就是/usr/local/redis-4.0.6,用vim打开该目录下的配置文件redis.conf
vi redis.conf
133 ################################# GENERAL #####################################
134
135 # By default Redis does not run as a daemon. Use 'yes' if you need it.
136 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
137 daemonize yes
找到并将daeminize这个参数设置为yes就可以了,默认是no
保存退出后再次运行,就可以看到变成这样:
(venv) [root@localhost src]# ./redis-server /usr/local/redis-4.0.6/redis.conf
29528:C 30 Dec 18:39:25.065 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29528:C 30 Dec 18:39:25.065 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=29528, just started
29528:C 30 Dec 18:39:25.065 # Configuration loaded
上面返回了一个pid=29528,我们可以查一下redis进程
(venv) [root@localhost src]# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 29529 0.2 0.2 140420 2060 ? Ssl 18:39 0:00 ./redis-server 127.0.0.1:6379
root 29536 0.0 0.0 103336 868 pts/1 S+ 18:39 0:00 grep --color=auto redis
可以看到实际上的pid是29529,这里很奇怪,查到的pid总是比redis返回的pid大1,ps命令的pid才是真正的pid,这点儿可以用kill命令测试就知道了
(venv) [root@localhost src]# kill 29529
(venv) [root@localhost src]# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 29543 0.0 0.0 103336 868 pts/1 S+ 18:40 0:00 grep --color=auto redis
2.3 设置为系统服务
为了方便我们使用,可以把redis作为系统服务随着系统自动启动,这样我们就不用每次手动启动了。
- 进入/etc目录并在该目录下创建一个名为redis的文件夹,然后将安装目录下的配置文件复制到该文件夹中并以6379.conf命名
(venv) [root@localhost etc]# mkdir redis
(venv) [root@localhost etc]# cd redis
(venv) [root@localhost redis]# cp /usr/local/redis-4.0.6/redis.conf /etc/redis/6379.conf
- 将redis的启动脚本复制一份到/etc/init.d下面并重命名为redisd
(venv) [root@localhost init.d]# cp /usr/local/redis-4.0.6/utils/redis_init_script /etc/init.d/redisd
- 将redis服务设置为开机启动
如果直接使用chkconfig命令会有问题
(venv) [root@localhost init.d]# chkconfig redisd on
redisd 服务不支持 chkconfig
解决方法也很简单,只需要在刚刚复制的那个文件开头加上下面两行就可以了
#!/bin/sh
# chkconfig: 2345 90 10
# discription:Redis is a persistent key-value database
注意这虽然是注释,但是也是有意义的,和上面的#!/bin/sh一样
- chkconfig:redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10
- discription:仅仅是一个描述信息
再次执行上面的指令就成功了。
接下来就可以使用service命令启动redis server了, 同样可以用ps指令看到已经在运行了。
(venv) [root@localhost init.d]# service redisd start
Starting Redis server...
29609:C 30 Dec 18:53:28.256 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29609:C 30 Dec 18:53:28.257 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=29609, just started
29609:C 30 Dec 18:53:28.257 # Configuration loaded
(venv) [root@localhost init.d]# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 29610 0.1 0.1 43560 2020 ? Ssl 18:53 0:11 /usr/local/bin/redis-server 127.0.0.1:6379
root 30361 0.0 0.0 103336 864 pts/1 S+ 21:40 0:00 grep --color=auto redis
停止服务可以用下面的指令
(venv) [root@localhost init.d]# service redisd stop
Stopping ...
Redis stopped
当然也可以使用
redis-cli SHUTDOWN
来停止redis