Redis基础知识与安装

7/18/2022 缓存框架

# 基础的知识

# 1、Redis默认有16个数据库,可以使用select进行切换

[root@localhost bin]# redis-cli -p 6379
127.0.0.1:6379> select 3  # 切换数据库
OK
127.0.0.1:6379[3]> DBSIZE # 数据库大小
(integer) 0
127.0.0.1:6379[3]> 
1
2
3
4
5
6

# 2、清空

FLUSHDB:当前数据库 FLUSHALL:清空全部数据库内容

127.0.0.1:6379> select 3
OK
127.0.0.1:6379[3]> DBSIZE
(integer) 0
127.0.0.1:6379[3]> set name zhangsan
OK
127.0.0.1:6379[3]> DBSIZE
(integer) 1
127.0.0.1:6379[3]> FLUSHDB
OK
127.0.0.1:6379[3]> keys *
(empty array)
127.0.0.1:6379[3]> 
1
2
3
4
5
6
7
8
9
10
11
12
13

# 3、Redis是单线程

官方表示,Redis是基于内存操作,CPU不是Redis性能瓶颈,Redis的瓶颈是根据机器的内存和网络带宽,既然可以使用单线程来实现,就使用单线程了!

核心

redis是将所有的数据全部放在内存中的,所以说使用单线程去操作效率就是最高的;

多线程:CPU上下文会切换:耗时的操作;

对于内存系统来说,如果没有上下文切换效率就是最高的!多次读写都是在一个CPU上的,在内存情况下,这个就是最佳的方案!

# Linux安装以及出现错误

# 一、安装

首先下载 redis官方下载地址 (opens new window)

# 正常安装步骤如下

[root@localhost ~]# yum install gcc
[root@localhost ~]# cd /usr/local
[root@localhost local]# wget http://download.redis.io/releases/redis-6.0.1.tar.gz
[root@localhost local]# tar -xvf redis-6.0.1.tar.gz
[root@localhost local]# cd /usr/local/redis-6.0.1/
[root@localhost redis-6.0.1]# make PREFIX=/usr/local/redis install
1
2
3
4
5
6

# 可能会出现的错误

server.c:2402:11: 错误:‘struct redisServer’没有名为‘assert_file’的成员
     server.assert_file = "<no file>";
           ^
server.c:2403:11: 错误:‘struct redisServer’没有名为‘assert_line’的成员
     server.assert_line = 0;
           ^
server.c:2404:11: 错误:‘struct redisServer’没有名为‘bug_report_start’的成员
     server.bug_report_start = 0;
           ^
server.c:2405:11: 错误:‘struct redisServer’没有名为‘watchdog_period’的成员
     server.watchdog_period = 0;
           ^
server.c:2411:11: 错误:‘struct redisServer’没有名为‘lua_always_replicate_commands’的成员
     server.lua_always_replicate_commands = 1;
           ^
server.c: 在函数‘restartServer’中:
server.c:2464:32: 错误:‘struct redisServer’没有名为‘maxclients’的成员
     for (j = 3; j < (int)server.maxclients + 1024; j++) {
                                ^
server.c: 在函数‘adjustOpenFilesLimit’中:
server.c:2491:29: 错误:‘struct redisServer’没有名为‘maxclients’的成员
     rlim_t maxfiles = server.maxclients+CONFIG_MIN_RESERVED_FDS;
                             ^
server.c:2497:15: 错误:‘struct redisServer’没有名为‘maxclients’的成员
         server.maxclients = 1024-CONFIG_MIN_RESERVED_FDS;
               ^
server.c:2529:53: 错误:‘struct redisServer’没有名为‘maxclients’的成员
                 unsigned int old_maxclients = server.maxclients;
                                                     ^
server.c:2530:23: 错误:‘struct redisServer’没有名为‘maxclients’的成员
                 server.maxclients = bestlimit-CONFIG_MIN_RESERVED_FDS;
                       ^
server.c:2554:59: 错误:‘struct redisServer’没有名为‘maxclients’的成员
                     (unsigned long long) bestlimit, server.maxclients);
                                                           ^
server.c: 在函数‘checkTcpBacklogSettings’中:
server.c:2574:48: 错误:‘struct redisServer’没有名为‘tcp_backlog’的成员
         if (somaxconn > 0 && somaxconn < server.tcp_backlog) {
                                                ^
server.c:2575:171: 错误:‘struct redisServer’没有名为‘tcp_backlog’的成员
             serverLog(LL_WARNING,"WARNING: The TCP backlog setting of %d cannot be enforced because /proc/sys/net            /core/somaxconn is set to the lower value of %d.", server.tcp_backlog, somaxconn);
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

解决办法:升级 gcc 版本

[root@localhost redis-6.0.1]# gcc -v                             # 查看gcc版本
[root@localhost redis-6.0.1]# yum -y install centos-release-scl  # 升级到9.1版本
[root@localhost redis-6.0.1]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@localhost redis-6.0.1]# scl enable devtoolset-9 bash
以上为临时启用,如果要长期使用gcc 9.1的话:
[root@localhost redis-6.0.1]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
1
2
3
4
5
6

再执行编译:

[root@mmjredis redis-6.0.1]# make PREFIX=/usr/local/redis install
1

安装完成后我们使用make test来进行测试一下,可能会出现了如下错误

[root@localhost redis-6.0.6]# make test
cd src && make test
make[1]: 进入目录“/usr/redis-6.0.1/src”
    CC Makefile.dep
make[1]: 离开目录“/usr/redis-6.0.1/src”
make[1]: 进入目录“/usr/redis-6.0.1/src”
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] 错误 1
make[1]: 离开目录“/usr/redis-6.0.1/src”
make: *** [test] 错误 2
1
2
3
4
5
6
7
8
9
10

解决办法

[root@localhost redis-6.0.1]# yum install tcl
[root@localhost redis-6.0.1]# make test
1
2

运行 redis 试一下:

[root@localhost redis-6.0.1]# /usr/local/redis-6.0.1/src/redis-server /usr/local/redis-6.0.1/redis.conf
32101:C 18 July 2022 12:01:48.248 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32101:C 18 July 2022 12:01:48.248 # Redis version=6.0.1, bits=64, commit=00000000, modified=0, pid=32101, just started
32101:C 18 July 2022 12:01:48.248 # Configuration loaded
32101:M 18 July 2022 12:01:48.249 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 6.0.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 32101
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'


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.1]# vim redis.conf

#  daemonize no 改成 daemonize yes
1
2
3
[root@localhost redis-6.0.1]# /usr/redis-6.0.1/src/redis-server /usr/redis-6.0.1/redis.conf
30385:C 13 May 2020 16:57:58.918 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30385:C 13 May 2020 16:57:58.918 # Redis version=6.0.1, bits=64, commit=00000000, modified=0, pid=30385, just started
30385:C 13 May 2020 16:57:58.918 # Configuration loaded
1
2
3
4

后台运行成功,我们查看一下,并终止程序

[root@localhost redis-6.0.1]# ps -aux|grep redis| grep -v grep
root     29836  0.0  0.0 162416  7912 ?        Ssl  16:56   0:00 /usr/redis-6.0.1/src/redis-server 127.0.0.1:6379
[root@localhost redis-6.0.1]# kill -9 29836
1
2
3

# 使用systemctl启动redis

首先把 redis 加入 service 服务

vim /lib/systemd/system/redis.service
1
[Unit]
Description=redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis-6.0.1/src/redis-server /usr/local/redis-6.0.1/etc/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

1、注意路径问题,以及 PIDFile 的路径对应的是 redis-6.0.1/redis.conf 里面的 PIDFile

2、wq 保存退出即可

3、接着运行以下命令

[root@localhost redis-6.0.1]# systemctl enable redis.service                   # 加入开机启动

[root@localhost redis-6.0.1]# systemctl is-enabled redis.service               # 查看开机是否启动成功
enabled

[root@localhost redis-6.0.1]# systemctl start redis                            #开启redis服务


[root@localhost redis-6.0.1]# systemctl status redis //查看redis运行状态
1
2
3
4
5
6
7
8
9

如果启动出现问题,可以通过 systemctl status redis 来查看日志修复