首先,在redis官网上下载redis。
然后使用winScp将其上传到/usr/local文件夹下
[root@localhost local]# ll
总用量 1700
drwxr-xr-x. 2 root root 6 11月 5 2016 bin
drwxr-xr-x. 2 root root 6 11月 5 2016 etc
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 2 root root 6 11月 5 2016 include
drwxr-xr-x. 2 root root 6 11月 5 2016 lib
drwxr-xr-x. 2 root root 6 11月 5 2016 lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 libexec
-rw-r--r--. 1 root root 1737022 5月 12 14:10 redis-4.0.9.tar.gz
drwxr-xr-x. 2 root root 6 11月 5 2016 sbin
drwxr-xr-x. 5 root root 49 4月 22 16:27 share
drwxr-xr-x. 2 root root 6 11月 5 2016 src
[root@localhost local]#
然后进行解压:
[root@localhost local]# tar -zxvf redis-4.0.9.tar.gz
[root@localhost local]# cd redis-4.0.9/
[root@localhost redis-4.0.9]# make -j 4
使用4个线程进行编译,因为我的电脑是4核的。
[root@localhost redis-4.0.9]# make install
redis启动的时候,需要指定配置文件,redis.conf
[root@localhost redis-4.0.9]# ll
总用量 308
-rw-rw-r--. 1 root root 157632 3月 27 00:04 00-RELEASENOTES
-rw-rw-r--. 1 root root 53 3月 27 00:04 BUGS
-rw-rw-r--. 1 root root 1815 3月 27 00:04 CONTRIBUTING
-rw-rw-r--. 1 root root 1487 3月 27 00:04 COPYING
drwxrwxr-x. 6 root root 192 5月 12 14:19 deps
-rw-r--r--. 1 root root 92 5月 12 14:23 dump.rdb
-rw-rw-r--. 1 root root 11 3月 27 00:04 INSTALL
-rw-rw-r--. 1 root root 151 3月 27 00:04 Makefile
-rw-rw-r--. 1 root root 4223 3月 27 00:04 MANIFESTO
-rw-rw-r--. 1 root root 20543 3月 27 00:04 README.md
-rw-rw-r--. 1 root root 58766 3月 27 00:04 redis.conf
-rwxrwxr-x. 1 root root 271 3月 27 00:04 runtest
-rwxrwxr-x. 1 root root 280 3月 27 00:04 runtest-cluster
-rwxrwxr-x. 1 root root 281 3月 27 00:04 runtest-sentinel
-rw-rw-r--. 1 root root 7606 3月 27 00:04 sentinel.conf
drwxrwxr-x. 3 root root 8192 5月 12 14:21 src
drwxrwxr-x. 10 root root 167 3月 27 00:04 tests
drwxrwxr-x. 8 root root 4096 3月 27 00:04 utils
[root@localhost redis-4.0.9]# vi redis.conf
找到bind 127.0.0.1 ,表示只允许本机访问,要改为 bind 0.0.0.0 ,表示可以远程访问。
再找到daemonize no,改为 daemonize yes ,表示允许后台执行
然后 :wq 保存退出。
接着,指定redis.conf配置文件启动redis服务:
[root@localhost redis-4.0.9]# redis-server ./redis.conf
5161:C 12 May 14:34:40.842 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5161:C 12 May 14:34:40.842 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=5161, just started
5161:C 12 May 14:34:40.842 # Configuration loaded
[root@localhost redis-4.0.9]#
查看redis相关的进程:
[root@localhost redis-4.0.9]# ps -ef | grep redis
root 5162 1 0 14:34 ? 00:00:00 redis-server 0.0.0.0:6379
root 5181 1628 0 14:36 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-4.0.9]#
启动redis客户端:
[root@localhost redis-4.0.9]# redis-cli
127.0.0.1:6379> set key1 123456 [EX seconds] [PX milliseconds] [NX|XX
127.0.0.1:6379> set key1 123456
OK
127.0.0.1:6379> get key1
"123456"
为redis设置密码:
[root@localhost redis-4.0.9]# vi redis.conf
需要设置requirepass字段,因此首先进行查找。vi下进行查找需要输入 :/balabala ,如果查找下一个,输入n,查找上一个,输入shift n
现将其设置为:
,然后:wq保存退出。
然后,redis重新启动:
[root@localhost redis-4.0.9]# redis-cli
127.0.0.1:6379> shutdown save
not connected> exit
[root@localhost redis-4.0.9]# ps -ef | grep redis
root 5365 1628 0 14:51 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-4.0.9]# redis-server ./redis.conf
[root@localhost redis-4.0.9]# redis-cli
127.0.0.1:6379> get key1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get key1
"123456"
127.0.0.1:6379>
然后生成系统服务:
[root@localhost redis-4.0.9]# cd utils
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis-4.0.9/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis-4.0.9/redis.log
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis-4.0.9/data
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /usr/local/redis-4.0.9/redis.conf
Log file : /usr/local/redis-4.0.9/redis.log
Data dir : /usr/local/redis-4.0.9/data
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
[root@localhost utils]#
[root@localhost utils]# chkconfig --list | grep redis
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
redis_6379 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@localhost utils]# systemctl status redis_6379.service
● redis_6379.service - LSB: start and stop redis_6379
Loaded: loaded (/etc/rc.d/init.d/redis_6379; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@localhost utils]# systemctl stop redis_6379
[root@localhost utils]# systemctl start redis_6379
[root@localhost utils]# ps -ef | grep redis
root 5379 1 0 14:52 ? 00:00:01 redis-server 0.0.0.0:6379
root 5703 1628 0 15:06 pts/0 00:00:00 grep --color=auto redis
[root@localhost utils]#
说明服务已经安装好了。所谓的服务,就是系统帮我们执行的shell文件:
[root@localhost utils]# vi /etc/init.d/redis_6379
#!/bin/sh
#Configurations injected by install_server below....
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO
case "$1" in
start)
if [ -f $PIDFILE ]
then
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 ..."
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac
然后,需要防火墙永久开启 redis数据库用到的 6379端口,我使用的是centos7.4,只需要两条命令即可:
firewall-cmd --zone=public --add-port=6379/tcp --permanent #永久开启 6379端口
firewall-cmd --reload #重启防火墙