Redis单机安装以及redis生产环境启动方案

发布 : 2017-06-09 分类 : 大数据 浏览 :

1.安装单机版redis

1
2
3
4
5
6
7
8
9
10
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
tar -xzvf tcl8.6.1-src.tar.gz
cd tcl8.6.1/unix
./configure
make && make install

使用redis-3.2.8.tar.gz(截止2017年4月的最新稳定版)
tar -zxvf redis-3.2.8.tar.gz
cd redis-3.2.8
make && make test && make install

2.redis的生产环境启动方案

1
要把redis作为一个系统的daemon进程去运行的,每次系统启动,redis进程一起启动

(1).redis utils目录下,有个redis_init_script脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@matrix-cache01 ~]# ll redis-3.2.8/utils/
总用量 72
-rw-rw-r-- 1 root root 593 2月 12 23:14 build-static-symbols.tcl
-rw-rw-r-- 1 root root 1303 2月 12 23:14 cluster_fail_time.tcl
-rw-rw-r-- 1 root root 1070 2月 12 23:14 corrupt_rdb.c
drwxrwxr-x 2 root root 4096 2月 12 23:14 create-cluster
-rwxrwxr-x 1 root root 2137 2月 12 23:14 generate-command-help.rb
drwxrwxr-x 2 root root 4096 2月 12 23:14 hashtable
drwxrwxr-x 2 root root 4096 2月 12 23:14 hyperloglog
-rwxrwxr-x 1 root root 8545 2月 12 23:14 install_server.sh
drwxrwxr-x 2 root root 4096 2月 12 23:14 lru
-rw-rw-r-- 1 root root 1277 2月 12 23:14 redis-copy.rb
-rwxrwxr-x 1 root root 1098 2月 12 23:14 redis_init_script
-rwxrwxr-x 1 root root 1047 2月 12 23:14 redis_init_script.tpl
-rw-rw-r-- 1 root root 1762 2月 12 23:14 redis-sha1.rb
drwxrwxr-x 2 root root 4096 2月 12 23:14 releasetools
-rwxrwxr-x 1 root root 3787 2月 12 23:14 speed-regression.tcl
-rwxrwxr-x 1 root root 693 2月 12 23:14 whatisdoing.sh

(2).将redis_init_script脚本拷贝到linux的/etc/init.d目录中,将redis_init_script重命名为redis_6379,6379是我们希望这个redis实例监听的端口号

1
2
3
[root@matrix-cache01 ~]# cp redis-3.2.8/utils/redis_init_script /etc/init.d
[root@matrix-cache01 ~]# cd /etc/init.d
[root@matrix-cache01 init.d]# mv redis_init_script redis_6379

(3).修改redis_6379脚本的第6行的REDISPORT,设置为相同的端口号(默认就是6379)

1
[root@matrix-cache01 init.d]# vi redis_6379

Markdown

(4).创建两个目录:/etc/redis(存放redis的配置文件),/var/redis/6379(存放redis的持久化文件)

1
2
[root@matrix-cache01 ~]# mkdir -p /etc/redis
[root@matrix-cache01 ~]# mkdir -p /var/redis/6379

(5).修改redis配置文件(默认在根目录下,redis.conf),拷贝到/etc/redis目录中,修改名称为6379.conf

1
2
3
[root@matrix-cache01 ~]# cp redis-3.2.8/redis.conf /etc/redis
[root@matrix-cache01 ~]# cd /etc/redis
[root@matrix-cache01 redis]# mv redis.conf 6379.conf

(6).修改redis.conf中的部分配置为生产环境

1
2
3
4
5
vi /etc/redis/6379.conf
daemonize yes 让redis以daemon进程运行
pidfile /var/run/redis_6379.pid 设置redis的pid文件位置
port 6379 设置redis的监听端口号
dir /var/redis/6379 设置持久化文件的存储位置

Markdown

(7).启动redis,执行cd /etc/init.d, chmod 777 redis_6379,./redis_6379 start

1
2
3
[root@matrix-cache01 ~]# cd /etc/init.d
[root@matrix-cache01 init.d]# chmod 777 redis_6379
[root@matrix-cache01 init.d]# ./redis_6379 start

Markdown

(8).确认redis进程是否启动

1
ps -ef | grep redis

Markdown

(9).让redis跟随系统启动自动启动

1
2
3
4
5
6
[root@matrix-cache01 ~]# cd /etc/init.d
[root@matrix-cache01 init.d]# vi redis_6379
# 在redis_6379文件中加入两行注释
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
[root@matrix-cache01 init.d]# chkconfig redis_6379 on

Markdown

3.redis cli的使用

1
redis-cli SHUTDOWN:连接本机的6379端口停止redis进程

Markdown

1
redis-cli -h 127.0.0.1 -p 6379 SHUTDOWN:制定要连接的ip和端口号
1
redis-cli PING:ping redis的端口,看是否正常

Markdown

1
2
3
redis-cli:进入交互式命令行
SET k1 v1
GET k1

Markdown

redis技术主要在以下方面

1
2
3
4
(1).redis各种数据结构和命令的使用,包括java api的使用
(2).redis一些特殊的解决方案的使用,pub/sub消息系统,分布式锁,输入的自动完成,等等
(3).redis日常的管理相关的命令
(4).redis企业级的集群部署和架构

redis持久化的意义

1
数据备份和数据恢复
本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/06/09/Redis单机安装以及redis生产环境启动方案/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹