Centos7搭建单机Redis

Centos7搭建单机版Redis

工作需要,需要一个redis服务器来做缓存数据库,作为一个当下风靡的工具,至少还是应该知道怎么搭建
本次介绍内容为单机版搭建过程,集群部分,还没实践过,,

工具准备

首先Centos7服务器,然后装个wget,开始

安装包下载

https://redis.io/download官网下有很多版本,正常下载稳定版即可,当然,Beta版可能也有很多优化可以装了试试。

1
wget http://download.redis.io/releases/redis-4.0.11.tar.gz

编译安装

1
2
3
4
5
6
7
tar -zxvf redis-4.0.11.tar.gz

cd redis-4.0.11

make

make install

安装完之后,根据内部既定的shell脚本,会在/usr/bin目录下自动创建好redis的可执行文件:

1
2
3
4
5
6
-rwxr-xr-x. 1 root root 2450840 Sep 26 16:04 redis-benchmark
-rwxr-xr-x. 1 root root 5742360 Sep 26 16:04 redis-check-aof
-rwxr-xr-x. 1 root root 5742360 Sep 26 16:04 redis-check-rdb
-rwxr-xr-x. 1 root root 2605120 Sep 26 16:04 redis-cli
lrwxrwxrwx. 1 root root 12 Sep 26 16:04 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5742360 Sep 26 16:04 redis-server

redis的启动和很简单,在/usr/bin目录下执行redis-server即可启动;redis-cli SHUTDOWN即可停止

服务化

通常在Linux下启动都原因是使用服务的方式启动,比如启动nginx和MySQL:systemctl start nginxsystemctl start mysql那也可以把redis配置到服务中,使用类似的方式来启动redis

chkconfig

linux的服务通过chkconfig来管理,可以先执行下chkconfig来查看当前系统已有的服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@izuf65tvx7it01x88bzbp5z ~]# chkconfig

Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.

If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.

aegis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
agentwatch 0:off 1:off 2:on 3:on 4:on 5:on 6:off
cloudmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off

redis服务化

想要将redis服务化,就需要将redis 的启动脚本加到系统服务中去,而系统服务的脚本存储路径是/etc/init.d,也就是,只需要将redis的启动脚本加到/etc/init.d文件夹下,再添加到chkconfig即可。

在之前下载的redis源码下有个初始化脚本文件redis_init_script(路径:路径/redis-4.0.11/utils),将这个文件拷贝到/etc/init.d下并且命名为redis_自定义的端口(例如redis_6379)

1
cp /路径/redis-4.0.11/utils/redis_init_script /etc/init.d/redis_6379

然后进入/etc/init.d目录,修改初始化脚本vim redis_6379

1
2
3
4
5
6
7
8
增加(头部,第3行后增加即可)

# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database

修改

REDISPORT=6379 (大约第六行)

效果差不多是这样:

1
2
3
4
5
6
7
8
9

#!/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.

REDISPORT=6379

修改配置

首先创建放置配置文件的文件夹:

1
2
3
4
mkdir /etc/redis

cd /var
mkdir -p ./redis/6379(端口号)

将源文件下配置文件模板路径/redis-4.0.11/redis.conf复制到/etc/redis目录下,并且以端口号重命名:

1
cp 路径/redis-4.0.11/redis.conf /etc/redis/6379.conf

修改配置文件:

1
2
3
4
port 6636  (第92行)
daemonize yes (第136行)
pidfile /var/run/redis_6636.pid (第158行)
dir /var/redis/6379 (第263行)

保存退出。

添加服务

一条命令即可:

1
chkconfig redis_6379 on

这样就可以使用service redis_6379 start来启动redis了,同样service redis_6379 stop

效果:

1
2
3
4
5
[root@localhost redis]# service redis_6636 start
Starting Redis server...
120037:C 26 Sep 16:18:04.701 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
120037:C 26 Sep 16:18:04.701 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=120037, just started
120037:C 26 Sep 16:18:04.701 # Configuration loaded

原理

其实,用服务启动redis,就是使用将启动脚本加到chkconfig,启动/etc/init.d下的redis_6379脚本,根据脚本中的配置

1
2
3
4
5
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

真正的server启动目录:/usr/local/bin/redis-server
配置文件路径:/etc/redis/${REDISPORT}.conf
再根据/etc/redis/6379.conf内的配置,启动相应的端口,保存到相应的目录等

参考文件

Redis集群搭建
Centos下redis搭建

文章目录
  1. 工具准备
    1. 安装包下载
    2. 编译安装
  2. 服务化
    1. chkconfig
    2. redis服务化
    3. 修改配置
    4. 添加服务
    5. 原理
  3. 参考文件
|