centOS下安装redis

CentOS6.8 安装redis过程记录

1. 安装

官网下载地址 https://redis.io/download

linux下分别输入

1
2
3
4
5
$ wget http://download.redis.io/releases/redis-5.0.3.tar.gz
$ tar xzf redis-5.0.3.tar.gz
$ cd redis-5.0.3
$ make
$ make install

2. 编辑redis.conf配置文件

由于redis默认在前台运行,我们希望它能够以守护进程的形式在后台运行,需要配置一下redis.conf

1
2
$ cd redis-5.0.3
$ vim redis.conf

找到daemonize,修改为yes,:wq保存

1
2
3
4
5
6
################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

3. 启动redis

1
2
# 启动并加载配置文件
$ redis-server redis.conf
1
2
3
4
5
6
7
8
9
10
11
# 启动详情
[lefter@shen redis-5.0.3]$ redis-server redis.conf
8256:C 05 Mar 2019 14:40:56.851 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8256:C 05 Mar 2019 14:40:56.851 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8256, just started
8256:C 05 Mar 2019 14:40:56.851 # Configuration loaded

# 查看进程
[lefter@shen redis-5.0.3]$ ps aux | grep redis
lefter 8257 0.6 0.4 152416 7804 ? Ssl 14:40 0:00 redis-server 127.0.0.1:6379
lefter 8262 0.0 0.0 103328 856 pts/0 S+ 14:41 0:00 grep redis

1
2
3
4
5
# 启动redis命令行
$ redis-cli

# 关闭redis
$ redis-cli shutdown

4.简化配置

redis/utils下,使用install_server.sh可以简化配置,并设置redis为开机启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[lefter@shen utils]$ sudo ./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/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis/redis.log
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/data
Please select the redis executable path [] /usr/local/bin/redis-server
Selected config:
Port : 6379
Config file : /usr/local/redis/redis.conf
Log file : /usr/local/redis/redis.log
Data dir : /usr/local/redis/data
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli

检查启动配置,已经变为开机启动

1
2
[lefter@shen redis]$ chkconfig --list | grep redis
redis_6379 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

可以通过service直接启动redis

1
2
3
4
5
6
7
[lefter@shen redis]$ service redis_6379 status
Redis is running (8105)
[lefter@shen redis]$ service redis_6379 stop
Stopping ...
Redis stopped
[lefter@shen redis]$ sudo service redis_6379 start
Starting Redis server...

5. 关于jedis访问的配置

Redis默认配置只绑定了本地访问,bind 127.0.0.1

由于我的Redis装在虚拟机中,需要在redis.conf中添加绑定虚拟机地址,如下:

bind 192.168.163.129

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1
bind 192.168.163.129 # 在这里添加新的绑定