Redis在linux系统上的安装与服务部署

Redis在linux系统上的安装与服务部署

1.下载软件的压缩包

建好下载目录后,使用wget官网下载:

# wget https://download.redis.io/releases/redis-6.2.5.tar.gz

使用在华为云的镜像下载(国内速度快):

# wget https://mirrors.huaweicloud.com/redis/redis-6.2.5.tar.gz

注意检查是否安装wget,安装命令为:

# yum install wget

2.解压下载的文件

# tar -zxvf redis-6.2.5.tar.gz

解压完成后:

drwxrwxr-x. 7 root root    4096 7月  22 02:06 redis-6.2.5
-rw-r--r--. 1 root root 2465302 7月  22 02:51 redis-6.2.5.tar.gz

进入redis-6.2.5目录

# cd redis-6.2.5
-rw-rw-r--.  1 root root 30644 7月  22 02:06 00-RELEASENOTES
-rw-rw-r--.  1 root root    51 7月  22 02:06 BUGS
-rw-rw-r--.  1 root root  5026 7月  22 02:06 CONDUCT
-rw-rw-r--.  1 root root  3384 7月  22 02:06 CONTRIBUTING
-rw-rw-r--.  1 root root  1487 7月  22 02:06 COPYING
drwxrwxr-x.  7 root root   213 8月  24 23:26 deps
-rw-rw-r--.  1 root root    11 7月  22 02:06 INSTALL
-rw-rw-r--.  1 root root   151 7月  22 02:06 Makefile
-rw-rw-r--.  1 root root  6888 7月  22 02:06 MANIFESTO
-rw-rw-r--.  1 root root 21567 7月  22 02:06 README.md
-rw-rw-r--.  1 root root 93724 7月  22 02:06 redis.conf
-rwxrwxr-x.  1 root root   275 7月  22 02:06 runtest
-rwxrwxr-x.  1 root root   279 7月  22 02:06 runtest-cluster
-rwxrwxr-x.  1 root root  1079 7月  22 02:06 runtest-moduleapi
-rwxrwxr-x.  1 root root   281 7月  22 02:06 runtest-sentinel
-rw-rw-r--.  1 root root 13768 7月  22 02:06 sentinel.conf
drwxrwxr-x.  3 root root 12288 8月  24 23:27 src
drwxrwxr-x. 11 root root   182 7月  22 02:06 tests
-rw-rw-r--.  1 root root  3055 7月  22 02:06 TLS.md
drwxrwxr-x.  9 root root  4096 7月  22 02:06 utils

(之后便可按照README.md中介绍的步骤进行部署,简化操作如下)

1)检查系统是否安装gcc编译器

若显示版本号等信息则为已安装

若显示找不到命令则为未安装

安装gcc:

# yum install gcc

2)安装完成后开始编译

执行命令:

# make

若中途安装报错,可能是由于之前在未安装gcc时进行过编译,须清理后执行编译命令,清理命令为:

# make distclean

3)检查安装成功与否

来到source目录下:

# cd src/

执行redis-server:

# ./redis-server

可以成功运行即可:

15304:C 24 Aug 2021 23:56:52.719 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15304:C 24 Aug 2021 23:56:52.719 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=15304, just started
15304:C 24 Aug 2021 23:56:52.719 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
15304:M 24 Aug 2021 23:56:52.719 * Increased maximum number of open files to 10032 (it was originally set to 1024).
15304:M 24 Aug 2021 23:56:52.719 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 15304
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

15304:M 24 Aug 2021 23:56:52.722 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
15304:M 24 Aug 2021 23:56:52.722 # Server initialized
15304:M 24 Aug 2021 23:56:52.722 # 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.
15304:M 24 Aug 2021 23:56:52.723 * Ready to accept connections

到这里redis已经可以正常使用了。

ctrl + c结束进程。

3.部署为服务

cd至软件的根目录:

# cd ..

执行:

# make install PREFIX=/opt/balabala/redis6

完成后cd至:

# cd /opt/balabala/redis6/bin
-rwxr-xr-x. 1 root root 4829552 8月  25 00:19 redis-benchmark
lrwxrwxrwx. 1 root root      12 8月  25 00:19 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root      12 8月  25 00:19 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root 5003168 8月  25 00:19 redis-cli
lrwxrwxrwx. 1 root root      12 8月  25 00:19 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 9491680 8月  25 00:19 redis-server

可执行程序迁出完毕。

添加PATH环境变量:

# vi /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

在文件最后添加:

export REDIS_HOME=/opt/balabala/redis6
export PATH=$PATH:$REDIS_HOME/bin

保存退出后

执行:

# source /etc/profile

可以执行命令:

# echo $PATH

来检查是否修改成功:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/balabala/redis6/bin

cd至源码的utils路径下:

# cd /soft/redis-6.2.5/utils/

如果发生报错:

This systems seems to use systemd.

Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

则修改install_server.sh

找到以下代码并且注释:

#bail if this system is managed by systemd
_pid_1_exe="$(readlink -f /proc/1/exe)"
if [ "${_pid_1_exe##*/}" = systemd ]
then
echo "This systems seems to use systemd."
echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
exit 1
fi

保存退出,重新执行

Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 

输入你想要配置的redis端口号(默认是6379)

然后一路回车,中间会生成配置文件,日志文件等:

Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/opt/zyydcb/redis6/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /opt/zyydcb/redis6/bin/redis-server
Cli Executable : /opt/zyydcb/redis6/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

一直到这里。完成了服务部署并且直接启动了,可以看一下:

# ps -ef | grep redis
root      15707      1  0 00:52 ?        00:00:00 /opt/balabala/redis6/bin/redis-server 127.0.0.1:6379

或者查看服务(注意服务名是redis_[刚才设定的端口号]):

# service redis_6379 status
Redis is running (15707)

大功告成!

posted @ 2021-08-25 01:04  飞杨煎生物  阅读(223)  评论(0)    收藏  举报