Home > Database > Redis > How to install and run Redis as a non-root user under CentOS7

How to install and run Redis as a non-root user under CentOS7

PHPz
Release: 2023-05-27 14:51:26
forward
1902 people have browsed it

1、基本环境信息

1.1、环境信息

序号 ID 版本
1 操作系统 CentOS7
2 redis 5.0.12



1.2、redis下载地址

redis下载地址:

我们这里使用的是5.0.12


2、安装

2.1、新建redis普通用户

1)、使用root登录我们的系统

2)、新建组:groupadd redisgroup

3)、新建用户:useradd -g redisgroup redis

4)、设置密码:passwd redis 自己输入密码即可

groupadd redisgroupuseradd -g redisgroup redispasswd redis
Copy after login

How to install and run Redis as a non-root user under CentOS7

2.2、上传安装包并解压

1)、使用root用户,新建目录:mkdir -p /usr/local/src/redis

2)、解压压缩包:tar -zxvf redis-5.0.12.tar.gz

如下:

mkdir -p /usr/local/src/redistar -zxvf redis-5.0.12.tar.gz
Copy after login

How to install and run Redis as a non-root user under CentOS7

2.3、新建目录

切换到redis用户并在其目录下创建一个名为redis的文件夹

mkdir rediscd redismkdir datamkdir etc
Copy after login

2.4、编译并安装

使用root用户操作

1)、编译:cd redis-5.0.12

make

2)、安装:

make install PREFIX=/home/redis/redis

How to install and run Redis as a non-root user under CentOS7

3、配置

3.1、将配置文件复制到redis用户下面

1)、将/usr/local/src/redis/redis-5.0.12下的redis.conf文件复制到/home/redis/redis/etc目录下

2)、如果复制不了就用root赋权限,chmod 777 redis.conf

3)、复制过来之后,改一下文件的所属组和用户

chown redis redis.confchgrp redisgroup redis.conf
Copy after login

3.2、配置Redis的配置文件

修改配置文件我们主要修改三个地方:

1)、修改为后台运行

2)、修改支持其他机器连接

下面的三个键值,直接在配置文件中修改即可

# yes-后台模式运行,no-前台模式运行,默认daemonize yes# 下面的这里给注释掉,默认是放开的#bind 127.0.0.1# 下面的这个值改为no,默认是yesprotected-mode no
Copy after login

4、编写启动脚本,并启动

4.1、编写管理脚本

1)、使用redis用户登录,我们在用户目录下新建一个目录bin

2)、在bin目录下新建一个redctl脚本,并授予可执行的权限,chmod a+x redisctl

3)、将~/bin目录加到用户的环境变量中去

修改用户目录下的.bash_profile文件,在倒数第二行增加

PATH=$PATH:~/bin

完整内容如下:

# .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then. ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/.local/bin:$HOME/bin

PATH=$PATH:~/binexport PATH
Copy after login

编写的redctl脚本,支持redis启动,停止,重启以及状态查看,详细内容如下:

  • redctl

#!/bin/bash#check paramif [ $# -lt 1 ]; thenecho "USAGE: redctl start|stop|restart|status"exit 8fipid=0
ACTION=$1REDIS_HOME=$HOME/redis# Here define some functiongetpid(){pid=`ps -ef | grep redis-server | grep -v grep |awk '{print $2}'`}start(){getpidif [ ! -n "$pid" ]; thencd $REDIS_HOME/bin
        ./redis-server ../etc/redis.conf
        getpidif [ ! -n "$pid" ]; thenecho "redis-server start failed, please check your commond"elseecho "redis-server start success, PID: $pid"fielseecho "redis-server is running PID: $pid"fi}stop(){getpidif [ ! -n "$pid" ]; thenecho "redis-server is not running"elsekill -9 $pidecho "redis-server has been stopped"fi}restart(){stopsleep 1s
    start}status(){getpidif [ ! -n "$pid" ]; thenecho "redis-server is not running"elseecho "redis-server is running PID: $pid"fi}case $ACTION instart) start;;stop) stop;;restart) restart;;status) status;;*) echo "require start|stop|restart|status" ;;esac
Copy after login

4.2、启动服务并验证

依次执行,启动,查看状态,重启,停止,命令如下:

redctl start

redctl status

redctl restart

redctl stop

How to install and run Redis as a non-root user under CentOS7

我们再次执行一下启动命令,将redis服务启动起来,

使用redis-cli连接上,尝试设置一些值,并查看所设置的值,如下:

表示成功

How to install and run Redis as a non-root user under CentOS7

5、可能遇到的问题

5.1、未安装gcc g++包

未安装gcc g++包的话,请自行安装

yum install -y gcc g++
Copy after login

The above is the detailed content of How to install and run Redis as a non-root user under CentOS7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template