8.Redis的安装及启动停止

 

Redis的安装及启动停止

1. 下载Redis

下载地址: https://redis.io/download

redis下载版本: 6.0.9

2. 上传安装包

redis-6.0.9.tar.gz上传至/usr/local/download目录下

1
2
3
4
[root@localhost download]# pwd
/usr/local/download
[root@localhost download]# ls -l
-rw-r--r--. 1 root root 2261418 Nov 29 11:21 redis-6.0.9.tar.gz

3. 开始安装

3.1 安装gcc,目的是编译软件

1
yum install gcc-c++

注意:在CentOS 7下编译Redis 6.0版本,如果出现以下错误

1
2
3
4
In file included from server.c:30:0:
server.h:1022:5: error: expected specifier-qualifier-list before ‘_Atomic’
_Atomic unsigned int lruclock; /* Clock for LRU eviction */
...

请先检查gcc的版本是否低于5。

1
2
3
4
5
6
7
8
[root@localhost ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

如果gcc的版本低于5请先升级,可以使用以下命令:

1
[root@localhost ~]# yum install centos-release-scl
1
[root@localhost ~]# yum install devtoolset-7-gcc*
1
[root@localhost ~]# scl enable devtoolset-7 bash
1
2
3
4
5
6
7
8
[root@localhost ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-7/root/usr --mandir=/opt/rh/devtoolset-7/root/usr/share/man --infodir=/opt/rh/devtoolset-7/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-7.3.1-20180303/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)

3.2 解压安装包

1
2
3
4
[root@localhost download]# tar -zxvf redis-6.0.9
[root@localhost download]# ls -l
drwxrwxr-x. 7 root root 4096 Oct 27 15:12 redis-6.0.9
-rw-r--r--. 1 root root 2261418 Nov 29 11:21 redis-6.0.9.tar.gz

3.3 把解压的文件copy到/usr/local/src里面

1
[root@localhost download]# cp -r redis-6.0.9 /usr/local/src/

3.4 进入/usr/local/src/redis-6.0.9/目录进行编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost download]# cd /usr/local/src/redis-6.0.9/
[root@localhost redis-6.0.9]# ls -l
total 292
-rw-r--r--. 1 root root 108806 Nov 29 11:38 00-RELEASENOTES
-rw-r--r--. 1 root root 51 Nov 29 11:38 BUGS
-rw-r--r--. 1 root root 2499 Nov 29 11:38 CONTRIBUTING
-rw-r--r--. 1 root root 1487 Nov 29 11:38 COPYING
drwxr-xr-x. 6 root root 192 Nov 29 11:38 deps
-rw-r--r--. 1 root root 11 Nov 29 11:38 INSTALL
-rw-r--r--. 1 root root 151 Nov 29 11:38 Makefile
-rw-r--r--. 1 root root 6888 Nov 29 11:38 MANIFESTO
-rw-r--r--. 1 root root 21099 Nov 29 11:38 README.md
-rw-r--r--. 1 root root 84841 Nov 29 11:38 redis.conf
-rwxr-xr-x. 1 root root 275 Nov 29 11:38 runtest
-rwxr-xr-x. 1 root root 280 Nov 29 11:38 runtest-cluster
-rwxr-xr-x. 1 root root 795 Nov 29 11:38 runtest-moduleapi
-rwxr-xr-x. 1 root root 281 Nov 29 11:38 runtest-sentinel
-rw-r--r--. 1 root root 10744 Nov 29 11:38 sentinel.conf
drwxr-xr-x. 3 root root 8192 Nov 29 11:45 src
drwxr-xr-x. 11 root root 182 Nov 29 11:38 tests
-rw-r--r--. 1 root root 3055 Nov 29 11:38 TLS.md
drwxr-xr-x. 9 root root 4096 Nov 29 11:38 utils
1
[root@localhost redis-6.0.9]# make

3.5 在上面的Redis目录安装把它安装到/usr/local/redis-6.0.9里面

1
[root@localhost redis-6.0.9]# mkdir /usr/local/redis-6.0.9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost redis-6.0.9]# pwd
/usr/local/src/redis-6.0.9
[root@localhost redis-6.0.9]# ls -l
total 292
-rw-r--r--. 1 root root 108806 Nov 29 11:38 00-RELEASENOTES
-rw-r--r--. 1 root root 51 Nov 29 11:38 BUGS
-rw-r--r--. 1 root root 2499 Nov 29 11:38 CONTRIBUTING
-rw-r--r--. 1 root root 1487 Nov 29 11:38 COPYING
drwxr-xr-x. 6 root root 192 Nov 29 11:38 deps
-rw-r--r--. 1 root root 11 Nov 29 11:38 INSTALL
-rw-r--r--. 1 root root 151 Nov 29 11:38 Makefile
-rw-r--r--. 1 root root 6888 Nov 29 11:38 MANIFESTO
-rw-r--r--. 1 root root 21099 Nov 29 11:38 README.md
-rw-r--r--. 1 root root 84841 Nov 29 11:38 redis.conf
-rwxr-xr-x. 1 root root 275 Nov 29 11:38 runtest
-rwxr-xr-x. 1 root root 280 Nov 29 11:38 runtest-cluster
-rwxr-xr-x. 1 root root 795 Nov 29 11:38 runtest-moduleapi
-rwxr-xr-x. 1 root root 281 Nov 29 11:38 runtest-sentinel
-rw-r--r--. 1 root root 10744 Nov 29 11:38 sentinel.conf
drwxr-xr-x. 3 root root 8192 Nov 29 11:45 src
drwxr-xr-x. 11 root root 182 Nov 29 11:38 tests
-rw-r--r--. 1 root root 3055 Nov 29 11:38 TLS.md
drwxr-xr-x. 9 root root 4096 Nov 29 11:38 utils
[root@localhost redis-6.0.9]# make install PREFIX=/usr/local/redis-6.0.9

3.6 验证安装是否成功

看到如下启动文件就可以了

1
2
3
4
5
6
7
8
9
10
[root@localhost bin]# pwd
/usr/local/redis-6.0.9/bin
[root@localhost bin]# ls -l
total 36272
-rwxr-xr-x. 1 root root 4796896 Nov 29 11:49 redis-benchmark
-rwxr-xr-x. 1 root root 9094264 Nov 29 11:49 redis-check-aof
-rwxr-xr-x. 1 root root 9094264 Nov 29 11:49 redis-check-rdb
-rwxr-xr-x. 1 root root 5049408 Nov 29 11:49 redis-cli
lrwxrwxrwx. 1 root root 12 Nov 29 11:49 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 9094264 Nov 29 11:49 redis-server

3.7 把配置文件复制到自定义目录

1
2
3
4
[root@localhost my_redis_conf]# pwd
/usr/local/redis-6.0.9/my_redis_conf
[root@localhost my_redis_conf]# ls
redis.conf

3.8 启动Redis

1
2
3
4
5
6
7
8
9
10
[root@localhost bin]# pwd
/usr/local/redis-6.0.9/bin
[root@localhost bin]# ls -l
total 36272
-rwxr-xr-x. 1 root root 4796896 Nov 29 11:49 redis-benchmark
-rwxr-xr-x. 1 root root 9094264 Nov 29 11:49 redis-check-aof
-rwxr-xr-x. 1 root root 9094264 Nov 29 11:49 redis-check-rdb
-rwxr-xr-x. 1 root root 5049408 Nov 29 11:49 redis-cli
lrwxrwxrwx. 1 root root 12 Nov 29 11:49 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 9094264 Nov 29 11:49 redis-server
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
[root@localhost bin]# ./redis-server /usr/local/redis-6.0.9/my_redis_conf/redis.conf
29150:C 29 Nov 2020 11:51:59.193 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29150:C 29 Nov 2020 11:51:59.193 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=29150, just started
29150:C 29 Nov 2020 11:51:59.193 # Configuration loaded
29150:M 29 Nov 2020 11:51:59.195 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 29150
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

29150:M 29 Nov 2020 11:51:59.198 # 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 29 Nov 2020 11:51:59.198 # Server initialized
29150:M 29 Nov 2020 11:51:59.198 # 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 29 Nov 2020 11:51:59.198 # 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 madvise > /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 (set to 'madvise' or 'never').
29150:M 29 Nov 2020 11:51:59.198 * Ready to accept connections

4. 基本设置和使用

4.1 默认情况,Redis不是在后台运行,可以把redis放在后台运行

1
[root@localhost ~]# vim /usr/local/redis-6.0.9/my_redis_conf/redis.conf

daemonize no改为daemonize yes

1
2
3
4
5
6
7
################################# GENERAL #####################################

# 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 no
daemonize yes

再次启动查看进程  

1
2
3
4
[root@localhost bin]# ./redis-server /usr/local/redis-6.0.9/my_redis_conf/redis.conf
[root@localhost bin]# ps -ef | grep redis
root 29606 1 0 13:05 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 29612 29253 0 13:05 pts/3 00:00:00 grep --color=auto redis

可以看到在6379端口号已启动了redis

4.2 客户端链接和退出

连接: ./redis-cli,默认是-h 127.0.0.1 -p 6379

退出: quit

测试: ping,返回PONG

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost bin]# ps -ef | grep redis
root 29606 1 0 13:05 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 29612 29253 0 13:05 pts/3 00:00:00 grep --color=auto redis
[root@localhost bin]# pwd
/usr/local/redis-6.0.9/bin
[root@localhost bin]# ./redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> quit
[root@localhost bin]# ps -ef | grep redis
root 29606 1 0 13:05 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 29616 29253 0 13:07 pts/3 00:00:00 grep --color=auto redis

4.3 停止redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost bin]# ps -ef | grep redis
root 29643 1 0 13:14 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 29652 1 0 13:14 ? 00:00:00 ./redis-server 127.0.0.1:6380
root 29661 1 0 13:15 ? 00:00:00 ./redis-server 127.0.0.1:6381
root 29672 1 5 13:15 ? 00:00:00 ./redis-server 127.0.0.1:6382
root 29678 29253 0 13:15 pts/3 00:00:00 grep --color=auto redis

[root@localhost bin]# ./redis-cli shutdown

[root@localhost bin]# ps -ef | grep redis
root 29652 1 0 13:14 ? 00:00:00 ./redis-server 127.0.0.1:6380
root 29661 1 0 13:15 ? 00:00:00 ./redis-server 127.0.0.1:6381
root 29672 1 0 13:15 ? 00:00:00 ./redis-server 127.0.0.1:6382
root 29681 29253 0 13:16 pts/3 00:00:00 grep --color=auto redis
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost bin]# ps -ef | grep redis
root 29652 1 0 13:14 ? 00:00:00 ./redis-server 127.0.0.1:6380
root 29661 1 0 13:15 ? 00:00:00 ./redis-server 127.0.0.1:6381
root 29672 1 0 13:15 ? 00:00:00 ./redis-server 127.0.0.1:6382
root 29681 29253 0 13:16 pts/3 00:00:00 grep --color=auto redis

[root@localhost bin]# kill 29652

[root@localhost bin]# ps -ef | grep redis
root 29661 1 0 13:15 ? 00:00:00 ./redis-server 127.0.0.1:6381
root 29672 1 0 13:15 ? 00:00:00 ./redis-server 127.0.0.1:6382
root 29684 29253 0 13:18 pts/3 00:00:00 grep --color=auto redis

5. bin目录的文件说明

 

  • redis-benchmark:redis性能测试工具
  • redis-check-aof:检查aof日志的工具
  • redis-check-rdb:检查rdb日志的工具
  • redis-cli:连接用的客户端
  • redis-server:redis服务进程

6. redis配置

redis.conf配置文件  

  • daemonize:如需要在后台运行,把该项的值改为yes
  • pdifile:把pid文件放在/var/run/redis.pid,可以配置到其他地址
  • bind:指定redis只接收来自该IP的请求,如果不设置,那么将处理所有请求,在生产环节中最好设置该项
  • port:监听端口,默认为6379
  • timeout:设置客户端连接时的超时时间,单位为秒
  • loglevel:等级分为4级,debug,revbose,notice和warning。生产环境下一般开启notice
  • logfile:配置log文件地址,默认使用标准输出,即打印在命令行终端的端口上
  • database:设置数据库的个数,默认使用的数据库是0
  • save:设置redis进行数据库镜像的频率
  • rdbcompression:在进行镜像备份时,是否进行压缩
  • dbfilename:镜像备份文件的文件名
  • dir:数据库镜像备份的文件放置的路径
  • slaveof:设置该数据库为其他数据库的从数据库
  • masterauth:当主数据库连接需要密码验证时,在这里设定
  • requirepass:设置客户端连接后进行任何其他指定前需要使用的密码
  • maxclients:限制同时连接的客户端数量
  • maxmemory:设置redis能够使用的最大内存
  • appendonly:开启appendonly模式后,redis会把每一次所接收到的写操作都追加到appendonly.aof文件中,当redis重新启动时,会从该文件恢复出之前的状态
  • appendfsync:设置appendonly.aof文件进行同步的频率
  • vm_enabled:是否开启虚拟内存支持
  • vm_swap_file:设置虚拟内存的交换文件的路径
  • vm_max_momery:设置开启虚拟内存后,redis将使用的最大物理内存的大小,默认为0
  • vm_page_size:设置虚拟内存页的大小
  • vm_pages:设置交换文件的总的page数量
  • vm_max_thrrads:设置vm IO同时使用的线程数量