一.安装redis
1.安装
1、推荐进入到linux路径/usr/local
2、$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
3、$ tar xzf redis-4.0.10.tar.gz
4、$ cd redis-4.0.10/
5、$ make
至此,redis安装完毕,我们可以利用以下两个命令测试
二进制文件是编译完成后在src目录下,通过下面的命令启动Redis服务
$ src/redis-server
验证启动是否成功
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
2.修改redis.conf配置文件
(1)、#bind 127.0.0.1 将这里前面加上#否则远程无法连接redis或者只能连接ip为127.0.0.1的本地回环地址,无法连接真实的ip.
(2)、daemonize yes (这里讲原来的no改为yes,目的是为了设置后台运行)
(3)、protected-mode no (这里讲原来的yes改为no,目的是为了解决安全模式引起的报错)
3、移动redis的配置文件
1、在etc里面建立一个文件夹
mkdir /etc/redis
注意:权限问题777
2、移动配置文件到新建的文件夹下
cp redis.conf /etc/redis/
4、杀死redis并重新后台开启redis
pkill -9 redis-server
src/redis-server /etc/redis/redis.conf
二.设置Redis开机自启动
设置条件:
-Ubuntu16.04
-Redis-4.0.11
在redis目录下找到 utils/redis_init_script 复制到 /etc/init.d/redis 打开文件进行修改
步骤:(主要Linux命令)
whereis redis # 查找redis目录
sudo cp redis_init_script /etc/init.d/redis # 复制文件
cd /etc/init.d/ # 进入文件目录 sudo vim redis # 编辑配置文件redis
原配置文件
#!/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
#
# chkconfig: 2345 10 90 修改
# description: Start and Stop redis 修改
### 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/redis/src/redis-server 修改
CLIEXEC=/usr/local/redis/src/redis-cli 修改
PIDFILE=/var/run/redis_${REDISPORT}.pid 修改
CONF="/etc/redis/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 ..."
$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
修改完配置文件后执行下面两条命令
sudo chmod +x /etc/init.d/redis # 取得权限
sudo update-rc.d redis defaults # 加载到系统自启动文件
测试命令:
kady@ubuntu:/etc/init.d$ sudo update-rc.d redis defaults
kady@ubuntu:/etc/init.d$ service redis start # 启动redis服务
kady@ubuntu:/etc/init.d$ redis-cli
127.0.0.1:6379> # 设置成功
其他命令:
启动服务: service redis start
停止服务: service redis stop
重启服务: service redis restart
三.作者在配置中遇到的问题
原因
.conf的路径不对,改成/etc/redis/redis.conf即可