CentOS 7 完美安装redis-5.0.5

记录下自己安装redis过程,算是个笔记吧,以后会用到。

准备条件

因为我虚拟机环境没有联网,所以需要提前准备好redis包。
一开始用的是redis 6+版本,安装后解压发现系统的gcc版本太低,无法编译。而没有联网,升级gcc版本太麻烦,之前系统装过rabbitmq,有4.8+版本的gcc,此版本适用于redis 5,于是就选择了redis-5.0.5的版本。
提前将压缩包通过XFTP导入系统内。

解压,编译

首先解压redis包

[root@zbh redis]# tar -zxvf redis-5.0.5.tar.gz 

然后进入解压的redis文件夹,准备编译和安装

[root@zbh redis]# cd redis-5.0.5

编译

[root@zbh redis-5.0.5]# make

大概一分钟左右,编译完毕,生成了src目录。

    LINK redis-server
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb
    INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/opt/redis/redis-5.0.5/src'

安装到常用包路径下

[root@zbh redis-5.0.5]# make install PREFIX=/usr/local/redis

这时候,其实就安装完成了,可以启动了。可以尝试去启动一次。

[root@zbh redis-5.0.5]# /usr/local/redis/bin/redis-server

启动成功

6556:C 12 Oct 2020 23:11:18.369 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6556:C 12 Oct 2020 23:11:18.369 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=6556, just started
6556:C 12 Oct 2020 23:11:18.369 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/local/redis/bin/redis-server /path/to/redis.conf
6556:M 12 Oct 2020 23:11:18.369 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6556
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6556:M 12 Oct 2020 23:11:18.371 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6556:M 12 Oct 2020 23:11:18.371 # Server initialized
6556:M 12 Oct 2020 23:11:18.371 # 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.
6556:M 12 Oct 2020 23:11:18.371 # 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.
6556:M 12 Oct 2020 23:11:18.371 * Ready to accept connections

基础配置

这时候仅仅是能启动服务,但不是后台启动,且外部客户端无法连接到此redis,简单更改配置。
将原来文件夹的配置文件复制一份放到安装目录,然后修改配置。

[root@zbh redis-5.0.5]# cp redis.conf /usr/local/redis/bin/

进入安装目录,修改配置。

[root@zbh redis-5.0.5]# cd /usr/local/redis/bin
[root@zbh bin]# vi redis.conf

设置显示行号,方便修改

:set number

首先修改redis服务端监听请求的地址,注掉默认bind 127.0.0.1即可。此版本是在69行。

     48 # By default, if no "bind" configuration directive is specified, Redis listens
     49 # for connections from all the network interfaces available on the server.
     50 # It is possible to listen to just one or multiple selected interfaces using
     51 # the "bind" configuration directive, followed by one or more IP addresses.
     52 #
     53 # Examples:
     54 #
     55 # bind 192.168.1.100 10.0.0.1
     56 # bind 127.0.0.1 ::1
     57 #
     58 # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
     59 # internet, binding to all the interfaces is dangerous and will expose the
     60 # instance to everybody on the internet. So by default we uncomment the
     61 # following bind directive, that will force Redis to listen only into
     62 # the IPv4 loopback interface address (this means Redis will be able to
     63 # accept connections only from clients running into the same computer it
     64 # is running).
     65 #
     66 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
     67 # JUST COMMENT THE FOLLOWING LINE.
     68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     69 # bind 127.0.0.1

然后关闭保护模式,默认开启yes,在88行。

     71 # Protected mode is a layer of security protection, in order to avoid that
     72 # Redis instances left open on the internet are accessed and exploited.
     73 #
     74 # When protected mode is on and if:
     75 #
     76 # 1) The server is not binding explicitly to a set of addresses using the
     77 #    "bind" directive.
     78 # 2) No password is configured.
     79 #
     80 # The server only accepts connections from clients connecting from the
     81 # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
     82 # sockets.
     83 #
     84 # By default protected mode is enabled. You should disable it only if
     85 # you are sure you want clients from other hosts to connect to Redis
     86 # even if no authentication is configured, nor a specific set of interfaces
     87 # are explicitly listed using the "bind" directive.
     88 protected-mode no

最后开启后台启动。

    134 # By default Redis does not run as a daemon. Use 'yes' if you need it.
    135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
    136 daemonize yes

OK,暂时就先配置这些。保存退出。
试试能不能启动。

[root@zbh bin]# ./redis-server redis.conf
6578:C 12 Oct 2020 23:27:23.510 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6578:C 12 Oct 2020 23:27:23.510 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=6578, just started
6578:C 12 Oct 2020 23:27:23.510 # Configuration loaded

开机自启动

创建一个启动服务。

[root@zbh bin]# vi /etc/systemd/system/redis.service

添加启动服务内容

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意其中的 ExecStart项,是你redis编译安装指定的路径和所使用的配置文件的路径。

设置开机启动

[root@zbh bin]# systemctl daemon-reload
[root@zbh bin]# systemctl start redis.service
[root@zbh bin]# systemctl enable redis.service
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.

至此,就完成了开机启动。可以重启试一试。
查看进程。

[root@zbh ~]# ps -aux|grep redis
root       1584  0.1  0.7 154000  7624 ?        Rsl  00:04   0:00 /usr/local/redis/bin/redis-server *:6379
root       2243  0.0  0.0 112816   960 pts/0    R+   00:05   0:00 grep --color=auto redis

外部使用redis客户端连接试试。
在这里插入图片描述
OK,大功告成。开启redis之旅。

一般通过redis客户端命令行来输出redis命令。设置启动客户端软连接,生成一个redis命令,方便启动客户端。

[root@zbh ~]# ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis

试一试是否成功。

[root@zbh ~]# redis
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 

至此,启动客户端就简单多了。
本人对redis及linux不熟悉,参考文章:https://www.cnblogs.com/heqiuyong/p/10463334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值