安装前准备
下载安装包
安装依赖
yum -y install gcc automake autoconf libtool make
创建用户
useradd redis
passwd redis
[root@myhost redis]# mkdir /home/redis/6379
[root@myhost redis]# chown -R redis:redis /home/redis/6379/
tar -zxvf redis-4.0.10.tar.gz
ln -s redis-4.0.10 redis
cd redis
make
vim ~/.bash_profile
export PATH=/home/redis/redis/src:$PATH
source ~/.bash_profile
启动
redis-server &
连接
[redis@myhost ~]$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set num 100
OK
127.0.0.1:6379> get num
"100"
配置文件
cp redis/redis.conf 6379/redis.conf
修改配置文件
去掉注释,保存到6379.conf
grep -Ev "$^|#" 6379/redis.conf > 6379/6379.conf
[redis@myhost ~]$ cat 6379/6379.conf
bind 172.18.27.141 127.0.0.1 #修改
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes #修改
supervised no
pidfile "/home/redis/6379/redis.pid" #修改
loglevel notice
logfile "/home/redis/6379/redis.log" #修改
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir "/home/redis/6379" #修改
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
启动服务
redis-server /home/redis/6379/6379.conf
[redis@myhost ~]$ netstat -lntup|grep redis
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 27600/redis-server
tcp 0 0 172.18.27.141:6379 0.0.0.0:* LISTEN 27600/redis-server
[redis@myhost ~]$
root用户操作:
[root@myhost redis]# cp /home/redis/redis/utils/redis_init_script /etc/init.d/redis_6379
[root@myhost redis]#
[root@myhost redis]# ll /etc/init.d/redis_6379
-rwxr-xr-x 1 root root 1352 Nov 16 14:22 /etc/init.d/redis_6379
修改
vim /etc/init.d/redis_6379
#!/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=/home/redis/redis/src/redis-server
CLIEXEC=/home/redis/redis/src/redis-cli
PIDFILE=//home/redis/6379/redis.pid
CONF="/home/redis/6379/${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
redis 用户
启动服务,停止服务
/etc/init.d/redis_6379 start
/etc/init.d/redis_6379 stop
新增requirepass (password)
配置文件新增
requirepass 123456
重启服务
重启后连接操作,需要密码认证了
[redis@myhost 6379]$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379> get num
(error) NOAUTH Authentication required.
127.0.0.1:6379>
127.0.0.1:6379>
方式1
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get num
"100"
127.0.0.1:6379>
方式2
[redis@myhost 6379]$ redis-cli -a 123456
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379>
127.0.0.1:6379>
启动脚本中新增密码
root操作
$CLIEXEC -p $REDISPORT -a 123456 shutdown
停止服务 提示不安全
[redis@myhost ~]$ /etc/init.d/redis_6379 stop
Stopping ...
Warning: Using a password with '-a' option on the command line interface may not be safe.