环境介绍
Ubantu 16.04
Redis5.0.3
下载地址(安装路径:/usr/local/redis):
http://download.redis.io/releases/redis-5.0.3.tar.gz
mkdir /usr/local/redis
cd /usr/local/redis
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
tar -xvf **redis-5.0.3.tar.gz**
mv -f redis-5.0.3 redis-single
cd /redis-single
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
src/redis-server
src/redis-cli
set sex 1
get sex
"1"
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
修改以上保存即可
#开放6379端口
ufw allow 6379
#关闭防火墙
ufw disable
#打开防火墙
ufw enable
复制到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
vim ~/.bashrc
alias REDIS_HOME='cd /usr/local/redis/'
source ~/.bashrc
Tips:
任意终端输入
REDIS_HOME
进入Redis目录
可以自行添加比较顺手的命令去执行redis服务
2018即将过去了,我不想去怀念它
2018已经过去了,我决定去怀念它
不管过得像一首诗还是像一摊狗屎