ubantu16.04安装redis5.0.3单机版开机启动

环境介绍

Ubantu 16.04

Redis5.0.3

下载地址(安装路径:/usr/local/redis):

http://download.redis.io/releases/redis-5.0.3.tar.gz


一、下载安装

1.1.创建目录
mkdir /usr/local/redis
1.2.进入redis目录
cd /usr/local/redis
1.3.下载
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
1.4.解压
tar -xvf **redis-5.0.3.tar.gz** 
1.5.文件夹重命名
mv -f redis-5.0.3 redis-single
1.6.进入redis-single目录
cd /redis-single
1.7.编译
make

make install

make test

This :输出下面代码则无报错安装成功**

All tests passed without errors


还有要注意的一点是记得安装gcc和gcc-c++,还要注意gcc的版本,用gcc -v来查看当前安装的gcc版本,版本过低(一般需要4.0以上)的话编译redis3.0以上的是会出错的。

apt-get install gcc

apt-get install gcc-c++

有可能还要安装tcl(如果make test出现You need tcl 8.5 or newer in order to run the Redis test的话)

apt-getinstall tcl

安装需要root权限 命令前加上sudo即可(当前root用户则不需要)

redis编译报致命错误:jemalloc/jemalloc.h没有那个文件或目录

分配器allocator, 如果有MALLOC 这个 环境变量, 会有用这个环境变量的 去建立Redis。

而且libc 并不是默认的 分配器, 默认的是 jemalloc, 因为 jemalloc 被证明 有更少的 fragmentation problems 比libc。

但是如果你又没有jemalloc 而只有 libc 当然 make 出错。 所以加这么一个参数,运行如下命令:

make MALLOC=libc

1.8.启动服务
src/redis-server
1.9.运行客户端
src/redis-cli
1.10测试
set sex 1
get sex
"1"

二、添加redis到服务设为开机启动

2.1.修改redis配置
vim redis.conf
#注释掉开启远程访问
#bind 127.0.0.1 
#关闭保护模式不然远程还是连接不了
protected-mode no 
#设为后台运行
deamonize yes
#添加redis密码
requirepass 1234
#修改为你的安装目录 redis_端口号 端口改为该redis服务端口
pidfile /usr/local/redis/redis-single/redis_6379.pid
#redis日志存放位置
logfile /usr/local/redis/redis-single/redis_log.log 
#redis位置
dir /usr/local/redis/redis-single

修改以上保存即可

2.2.开放redis端口
#开放6379端口
ufw allow 6379 
#关闭防火墙
ufw disable
#打开防火墙
 ufw enable
2.3.添加redis到服务

复制到redis_init_script 到init.d起个名字

cp utils/redis_init_script /etc/init.d/redis-single

编辑

vim /etc/init.d/redis-single

Tips:

注意头的修改

#! /bin/sh

### BEGIN INIT INFO

# Provides: redis-single

# Required-Start: $syslog $remote_fs

# Required-Stop: $syslog $remote_fs

# Should-Start: $local_fs

# Should-Stop: $local_fs

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: redis-single - Persistent key-value db

# Description: redis-single - Persistent key-value db

### END INIT INFO

原始文件内容:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

修改后内容

#! /bin/sh

### BEGIN INIT INFO
# Provides:             redis-single
# Required-Start:       $syslog $remote_fs
# Required-Stop:        $syslog $remote_fs
# Should-Start:         $local_fs
# Should-Stop:          $local_fs
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    redis-single - Persistent key-value db
# Description:          redis-single - Persistent key-value db
### END INIT INFO

#redis端口
REDISPORT=6379
#redis server目录
EXEC=/usr/local/redis/redis-single/src/redis-server
#redis cli
CLIEXEC=/usr/local/redis/redis-single/src/redis-cli
#这和redis.conf设置的一样
PIDFILE=/usr/local/redis/redis-single/redis_6379.pid
#redis.conf位置
CONF="/usr/local/redis/redis-single/redis.conf"

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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                # -a 'password' 这是关闭redis时候需要的密码 也就是我们上面设置的redis密码
                $CLIEXEC -a '1234' -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

设置服务脚本有执行权限

chmod +x /etc/init.d/redis-single

注册服务 开机启动

cd /etc/init.d
update-rc.d redis-single defaults

Tips:

启动redis

service redis-single start

关闭redis

service redis-single stop

重启系统测试是否成功

reboot

三、添加快速运行指令

3.1.编辑
vim ~/.bashrc
3.2.添加保存
alias REDIS_HOME='cd /usr/local/redis/'
3.3.立即生效
source ~/.bashrc

Tips:

任意终端输入

REDIS_HOME

进入Redis目录

可以自行添加比较顺手的命令去执行redis服务


2018即将过去了,我不想去怀念它
2018已经过去了,我决定去怀念它
不管过得像一首诗还是像一摊狗屎

你可能感兴趣的:(Linux学习之路,学习笔记,环境搭建)