linux下安装redis与启动,及后台启动redis

linux下安装redis及其中遇到的问题的解决方法

1.将下载好的压缩包放到/usr/local目录下
# tar xzf redis-3.0.2.tar.gz
# cd redis-3.0.2
# make
提示错误 make: cc: Command not found make: *** [adlist.o] Error 127
没有安装gcc环境,需要安装gcc
# yum install gcc
安装后检查是否安装成功
# rpm -qa |grep gcc
之后重新make
2.编译完成后,在Src目录下,有四个可执行文件redis-server、redis-benchmark、redis-cli和redis.conf将其拷贝到一个目录下。
# mkdir /usr/redis
# cp redis-server  /usr/redis
# cp redis-benchmark /usr/redis
# cp redis-cli  /usr/redis
# cp redis.conf  /usr/redis
# cd /usr/redis

3.启动服务
# ./redis-server   redis.conf
提示错误 -bash :redis-server:command not found

建立软连接
# ln -s /usr/redis/redis-server /usr/bin/redis-server
# ln -s /usr/redis/redis-cli /usr/bin/redis-cli
重新启动
# redis-server /usr/redis/redis.conf

启动成功

4.设置开机自启动

 (1)先将redis.conf中daemonize设置为yes,确保守护进程开启。

 (2)编写开机自启动脚本

# vi /etc/init.d/redis

PATH=/usr/local/bin:/sbin:/usr/bin:/bin   
REDISPORT=6379  
EXEC=/usr/redis/redis-server   
REDIS_CLI=/usr/redis/redis-cli   
 
PIDFILE=/var/run/redis.pid   
CONF="/usr/redis/redis.conf"  
AUTH="1234"  

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   
                if [ "$?"="0" ]   
                then   
                        echo "Redis is running..."  
                fi   
                ;;   
        stop)   
                if [ ! -f $PIDFILE ]   
                then   
                        echo "$PIDFILE exists, process is not running."  
                else  
                        PID=$(cat $PIDFILE)   
                        echo "Stopping..."  
                       $REDIS_CLI -p $REDISPORT  SHUTDOWN    
                        sleep 2  
                       while [ -x $PIDFILE ]   
                       do  
                                echo "Waiting for Redis to shutdown..."  
                               sleep 1  
                        done   
                        echo "Redis stopped"  
                fi   
                ;;   
        restart|force-reload)   
                ${0} stop   
                ${0} start   
                ;;   
        *)   
               echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2  
                exit 1  
esac
注: redis设置AUTH命令
redis 127.0.0.1:6379> AUTH PASSWORD
(error) ERR Client sent AUTH, but no password is set
redis 127.0.0.1:6379> CONFIG SET requirepass "1234"
OK
redis 127.0.0.1:6379> AUTH 1234
Ok

(3)设置权限
# chmod 755 redis
(4)启动
# /etc/init.d/redis start  
提示错误 /etc/init.d/redis:permission denied

# chmod a+x /etc/init.d/redis
a+x 是给所有人加上可执行权限,包括所有者,所属组,和其他人
o+x 只是给其他人加上可执行权限

重新启动后显示
Starting Redis server... 
Redis is running ...

(5)设置开机自启动
# chkconfig redis on
提示错误 service redis does not support chkconfig

把下面两行注释放在/etc/init.d/redis文件靠前的注释中:

# chkconfig:   2345 90 10

# description:  Redis is a persistent key-value database

(6)关机重启测试
5.设置启动redis为后台启动
平时我们启动是这样的:
 

linux下安装redis与启动,及后台启动redis_第1张图片

如果按ctrl+c退出后,则关闭了.

如果要设置redis后台启动,则需要修改redis.config文件,修改这里

linux下安装redis与启动,及后台启动redis_第2张图片

英文大概意思默认情况下复述,不作为守护进程运行。如果你需要使用“是”。

再次启动OK

 

原文地址:https://blog.csdn.net/q1035331653/article/details/79077260

 

你可能感兴趣的:(Redis)