Loading... ## 1. Redis安装 #### 1. 下载 ``` # 下载Redis包 wget -P /opt http://download.redis.io/releases/redis-6.2.3.tar.gz # 解压 cd /opt tar -zxvf redis-*.tar.gz # 重命名 mv /opt/redis-6.2.3 /opt/redis # 删除 rm redis-*.tar.gz ``` #### 2. 安装gcc依赖 ``` yum install gcc ``` #### 3. 编译 ``` make ```  #### 4. 安装 ``` make PREFIX=/opt/redis install ```  ## 2. 配置Redis -> redis.conf 1. 允许后台 ``` # 将 daemonize 的值改成 yes 代表redis可以后台运行 # daemonize no daemonize yes ```  2. 增加注释 ``` # 将 bind 的值改成 * -::* 代表redis可以被所有ip端口访问 # bind 127.0.0.1 -::1 bind * -::* ```  3. 关闭保护模式 ``` # 将 protected-mode 的值改为 no 代表外网可以访问到redis # protected-mode yes protected-mode no ```  4. 设置密码 ``` # redis默认没有设置密码 如果要设置密码 #修改 requirepass 将注释去掉并将后面对应的字段设置成自己想要的密码 # requirepass foobared requirepass password ```  这里列举下比较重要的配置项 | 配置项名称 | 配置项值范围 | 说明 | | ---------------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | daemonize | yes、no | yes表示启用守护进程,默认是no即不以守护进程方式运行。其中Windows系统下不支持启用守护进程方式运行 | | port | | 指定 Redis 监听端口,默认端口为 6379 | | bind | | 绑定的主机地址,如果需要设置远程访问则直接将这个属性备注下或者改为bind * 即可,这个属性和下面的protected-mode控制了是否可以远程访问 。 | | protected-mode | yes 、no | 保护模式,该模式控制外部网是否可以连接redis服务,默认是yes,所以默认我们外网是无法访问的,如需外网连接rendis服务则需要将此属性改为no。 | | timeout | 300 | 当客户端闲置多长时间后关闭连接,如果指定为 0,表示关闭该功能 | | loglevel | debug、verbose、notice、warning | 日志级别,默认为 notice | | databases | 16 | 设置数据库的数量,默认的数据库是0。整个通过客户端工具可以看得到 | | rdbcompression | yes、no | 指定存储至本地数据库时是否压缩数据,默认为 yes,Redis 采用 LZF 压缩,如果为了节省 CPU 时间,可以关闭该选项,但会导致数据库文件变的巨大。 | | dbfilename | dump.rdb | 指定本地数据库文件名,默认值为 dump.rdb | | dir | | 指定本地数据库存放目录 | | requirepass | | 设置 Redis 连接密码,如果配置了连接密码,客户端在连接 Redis 时需要通过 AUTH `` 命令提供密码,默认关闭 | | maxclients | 0 | 设置同一时间最大客户端连接数,默认无限制,Redis 可以同时打开的客户端连接数为 Redis 进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息。 | | maxmemory | XXX `` | 指定 Redis 最大内存限制,Redis 在启动时会把数据加载到内存中,达到最大内存后,Redis 会先尝试清除已到期或即将到期的 Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis 新的 vm 机制,会把 Key 存放内存,Value 会存放在 swap 区。配置项值范围列里XXX为数值。 | ## 3. 设置开机自启动 1. 新建配置文件 ``` vim /etc/init.d/redis ``` 修改参数 REDISPORT:redis启动端口,默认6379 EXEC:redis文件夹所在的redis-server所在路径 CLIEXEC:redis文件夹所在的redis-cli所在路径 CONF:redis启动所用的配置文件 ``` #!/bin/sh # chkconfig: 2345 10 90 # description: Start and Stop redis REDISPORT=6379 EXEC=/opt/redis/src/redis-server CLIEXEC=/opt/redis/src/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/opt/redis/redis.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF & fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart) "$0" stop sleep 3 "$0" start ;; *) echo "Please use start or stop or restart as first argument" ;; esac ```  2. 添加权限 ``` chmod 777 /etc/init.d/redis ``` 3. 设置开机自动启动 ``` chkconfig redis on ``` 4. 启动redis命令 ``` service redis start ```   ## 4. 遇到的问题  Centos中无法使用make, make install 命令 一般出现这个-bash: make: command not found提示, 是因为安装系统的时候使用的是最小化mini安装 系统没有安装make, vim等常用命令, 直接yum安装下即可 ``` # 安装 yum -y install gcc automake autoconf libtool make ``` 安装完即可使用make命令  Last modification:May 28, 2021 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 感谢大佬投喂 啾咪~