Centos7下安装redis6

一、 安装gcc依赖

由于 redis 是用 C 语言开发,安装之前必先确认是否安装 gcc 环境(gcc -v),如果没有安装,执行以下命令进行安装

 [root@localhost local]# yum install -y gcc 
 [root@localhost local]# yum -y install epel-release

二、下载并解压安装包

 [root@localhost local]# wget http://download.redis.io/releases/redis-6.0.8.tar.gz
 [root@localhost local]#  tar -zxvf redis-6.0.8.tar.gz

三、cd切换到redis解压目录下,执行编译

注意:编译前先升级gcc否则编译过程中报错

[root@localhost local]#  cd redis-6.0.8/ 
[root@localhost redis-6.0.8]# make

Centos7 安装redis6以上版本make时候可能会有问题

# 查看gcc版本是否在5.3以上,centos7.6默认安装4.8.5
gcc -v
# 升级gcc到5.3及以上,如下:
升级到gcc 9.3:
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
如果要长期使用gcc 9.3的话:
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
这样退出shell重新打开就是新版的gcc了
以下其他版本同理,修改devtoolset版本号即可。

四、测试

[root@localhost redis-6.0.8]# make test

五、安装并指定安装目录

[root@localhost redis-6.0.8]# make install PREFIX=/usr/local/redis-6.0.8

六、启动服务

5.1 bin下文件说明

[root@localhos bin]# ls
dump.rdb         redis-check-aof  redis-cli   redis-sentinel
redis-benchmark  redis-check-rdb  redis.conf  redis-server

  • redis-server redis服务器启动命令
  • redis-cli redis命令行客户端
  • redis-benchmark redis性能测试工具
  • redis-check-aof AOF文件修复工具
  • redis-check-rdb RDB文件检索工具(快照持久化文件)

5.2. 前台启动

[root@localhost redis-6.0.8]# cd /usr/local/redis-6.0.8/bin
[root@localhost bin]# ./redis-server
14783:C 19 Oct 2020 16:29:30.629 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14783:C 19 Oct 2020 16:29:30.629 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=14783, just started
14783:C 19 Oct 2020 16:29:30.629 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 14783
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

14783:M 19 Oct 2020 16:29:30.631 # Server initialized
14783:M 19 Oct 2020 16:29:30.631 # 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.
14783:M 19 Oct 2020 16:29:30.631 # 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').
14783:M 19 Oct 2020 16:29:30.631 * Loading RDB produced by version 6.0.8
14783:M 19 Oct 2020 16:29:30.631 * RDB age 1330 seconds
14783:M 19 Oct 2020 16:29:30.631 * RDB memory usage when created 0.76 Mb
14783:M 19 Oct 2020 16:29:30.631 * DB loaded from disk: 0.000 seconds
14783:M 19 Oct 2020 16:29:30.631 * Ready to accept connections

5.3. 后台启动配置
从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录

 [root@localhost bin]# cp /root/redis-6.0.8/redis.conf /usr/local/redis-6.0.8/bin/

修改 redis.conf 文件,把 daemonize no 改为 daemonize yes

[root@localhost bin]# vim redis.conf 
 221 ################################# GENERAL #####################################
 222 
 223 # By default Redis does not run as a daemon. Use 'yes' if you need it.
 224 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
 225 daemonize yes

5.4 后台启动

[root@localhost bin]# ./redis-server redis.conf 
[root@localhost bin]# ps aux |grep redis
root      15059  0.2  0.5 170632  9968 ?        Ssl  16:34   0:00 ./redis-server 0.0.0.0:6379
root      15069  0.0  0.1 112828  2320 pts/0    R+   16:34   0:00 grep --color=auto redis

七、设置开机启动

  1. 添加开机启动服务
[root@localhost bin]# vim /etc/systemd/system/redis.service
  1. 粘贴以下代码
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis-6.0.8/bin/redis-server /usr/local/redis-6.0.8/bin/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target

    注意:ExecStart要配置成自己的路径

  1. 设置开机启动
设置开机启动
[root@localhost bin]# systemctl daemon-reload
[root@localhost bin]# systemctl start redis.service
[root@localhost bin]# systemctl enable redis.service
  1. 设置redis软连接
[root@localhost ~]# ln -s /usr/local/redis-6.0.8/bin/redis-cli /usr/bin/redis
  1. 测试redis连接
[root@localhost ~]# redis
127.0.0.1:6379> 

八、服务操作命令

systemctl start redis.service    #启动redis服务
systemctl stop redis.service     #停止redis服务
systemctl restart redis.service  #重新启动服务
systemctl status redis.service   #查看服务当前状态
systemctl enable redis.service   #设置开机自启动
systemctl disable redis.service  #停止开机自启动

九、redis核心配置文件介绍

  1. 本文的Redis的配置信息在/usr/local/redis-6.0.8/bin/redis.conf 下。
  2. 绑定ip:如果需要远程访问,可将此⾏注释,或绑定⼀个真实ip
bind 127.0.0.1
  1. 端⼝,默认为6379
port 6379
  1. 是否以守护进程运⾏
  • 如果以守护进程运⾏,则不会在命令⾏阻塞,类似于服务
  • 如果以⾮守护进程运⾏,则当前终端被阻塞
  • 设置为yes表示守护进程,设置为no表示⾮守护进程,推荐设置为yes
daemonize yes
  1. 数据⽂件
dbfilename dump.rdb
  1. 数据⽂件存储路径
dir /var/lib/redis
  1. ⽇志⽂件
logfile "/var/log/redis/redis-server.log"
  1. 数据库,默认有16个
database 16
  1. 主从复制,类似于双机备份
slaveof
  1. 进程的pid文件
pidfile /var/run/redis_6379.pid

十 、参考配置

详细的redis配置信息

十一、服务器端和客户端命令

  • 服务器端的命令为 redis-server
  • 客户端的命令为 redis-cli
  • 可以使⽤help查看帮助⽂档
redis-server --help
redis-cli --help

# 启动
ps aux | grep redis 查看redis服务器进程
sudo kill -9 pid 杀死redis服务器
sudo redis-server /usr/local/redis-6.0.8/bin/redis.conf  指定加载的配置文件

猜你喜欢

转载自blog.csdn.net/m0_46090675/article/details/109158849