概述
1. 下载Redis安装包
2. 安装Redis
3. 启动Redis
4. 相关配置
5. 随系统自动启动
6. 防火墙配置
1.下载Redis安装包
[root@Sunzh etc]# cd /usr/local/
[root@Sunzh local]# mkdir software //在/usr/local/下创建software文件夹
[root@Sunzh local]# ll
total 48
drwxr-xr-x 6 root root 4096 Aug 3 10:08 aegis
drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 6 root root 4096 Aug 3 10:07 share
drwxr-xr-x 2 root root 4096 Aug 3 11:09 software
drwxr-xr-x. 2 root root 4096 Sep 23 2011 src
[root@Sunzh software]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz //wget下载redis
--2018-08-03 11:36:59-- http://download.redis.io/releases/redis-4.0.10.tar.gz
Resolving download.redis.io... 109.74.203.151
Connecting to download.redis.io|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1738465 (1.7M) [application/x-gzip]
Saving to: “redis-4.0.10.tar.gz”
100%[===============================>] 1,738,465 809K/s in 2.1s
2018-08-03 11:37:02 (809 KB/s) - “redis-4.0.10.tar.gz” saved [1738465/1738465]
[root@Sunzh software]# ll
total 1700
-rw-r--r-- 1 root root 1738465 Jun 14 00:51 redis-4.0.10.tar.gz
2.安装Redis
[root@Sunzh software]# tar -zxvf redis-4.0.10.tar.gz //解压
[root@Sunzh software]# cd redis-4.0.10
[root@Sunzh redis-4.0.10]# make //编译
[root@Sunzh redis-4.0.10]# make install //安装
安装完后,在/usr/local/bin目录中可以找到刚刚安装的redis可执行文件。
[root@Sunzh bin]# pwd
/usr/local/bin
[root@Sunzh bin]# ll
total 35500
-rwxr-xr-x 1 root root 5599918 Aug 3 11:41 redis-benchmark
-rwxr-xr-x 1 root root 8332229 Aug 3 11:41 redis-check-aof
-rwxr-xr-x 1 root root 8332229 Aug 3 11:41 redis-check-rdb
-rwxr-xr-x 1 root root 5740282 Aug 3 11:41 redis-cli
lrwxrwxrwx 1 root root 12 Aug 3 11:41 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8332229 Aug 3 11:41 redis-server
3.启动Redis
[root@Sunzh bin]# redis-server //前台启动redis服务端
11334:C 03 Aug 11:50:15.819 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11334:C 03 Aug 11:50:15.819 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=11334, just started
11334:C 03 Aug 11:50:15.819 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
11334:M 03 Aug 11:50:15.821 # Not listening to IPv6: unsupproted
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 11334
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[root@Sunzh bin]# redis-cli //前台启动客户端
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> get a
"1"
127.0.0.1:6379>
4.相关配置
在Redis源代码目录的utils文件夹中有一个名为redis_init_script的初始化脚本文件。需要配置Redis的运行方式和持久化文件、日志文件的存储位置。步骤如下:
- 配置初始化脚本(首先将初始化脚本复制到/etc/init.d 目录中,文件名为 redis_端口号,其中端口号表示要让Redis监听的端口号,客户端通过该端口连接Redis。然后修改脚本第6行的REDISPORT变量的值为同样的端口号。)
[root@Sunzh utils]# pwd
/usr/local/software/redis-4.0.10/utils
[root@Sunzh utils]# cp redis_init_script /etc/init.d/redis_6379
- 建立以下需要的文件夹。
目录名 | Value |
---|---|
/etc/redis | 存放Redis的配置文件 |
/var/redis/端口号 | 存放Redis的持久化文件 |
[root@Sunzh bin]# mkdir /etc/redis
[root@Sunzh var]# mkdir /var/redis
[root@Sunzh var]# mkdir /var/redis/6379
- 修改配置文件
首先将配置文件模板(redis-4.0.2/redis.conf)复制到/etc/redis 目录中,以端口号命名(如“6379.conf”),然后按照下表对其中的部分参数进行编辑
参数 | 值 | 说明 |
---|---|---|
daemonize | yes | 使Redis以守护进程模式运行 |
pidfile | /var/run/redis_端口号.pid | 设置Redis的PID文件位置 |
port | 端口号 | 设置Redis监听的端口号 |
dir | /var/redis/端口号 | 设置持久化文件存放位置 |
[root@Sunzh redis]# pwd
/etc/redis
[root@Sunzh redis]# ll //复制配置文件
total 60
-rw-r--r-- 1 root root 58766 Aug 3 13:04 6379.conf
################################# 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
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/redis/6379
- 此时启动方式
[root@Sunzh redis]# /etc/init.d/redis_6379 start //启动
Starting Redis server...
11504:C 03 Aug 13:10:31.190 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11504:C 03 Aug 13:10:31.190 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=11504, just started
11504:C 03 Aug 13:10:31.190 # Configuration loaded
[root@Sunzh redis]# /etc/init.d/redis_6379 stop //停止
Stopping ...
Redis stopped
5.随系统自动启动
[root@Sunzh redis]# vi /etc/init.d/redis_6379 //添加第4,5行
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# as it does use of the /proc filesystem.
[root@Sunzh redis]# chkconfig redis_6379 on 开机执行脚本
- 此时启动方式
[root@Sunzh redis]# service redis_6379 start //开启服务端
Starting Redis server...
11535:C 03 Aug 13:28:26.372 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11535:C 03 Aug 13:28:26.372 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=11535, just started
11535:C 03 Aug 13:28:26.372 # Configuration loaded
[root@Sunzh redis]# service redis_6379 stop //停止服务端
Stopping ...
Redis stopped
[root@Sunzh ~]# redis-cli //任意目录下开启客户端
127.0.0.1:6379> get a
"1"
6.防火墙配置
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1 //注释掉
################################# 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 //后台运行
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no //改成no
- 测试连接
注意:
服务需要开放3306端口,阿里云和腾讯云是在控制台添加防火墙规则的