Centos7安装Redis5.0.4笔记

前言

Redis是一个高性能的key-value数据库,现时越来越多企业与应用使用Redis作为缓存服务器。

搭建Redis

1. 下载并解压

首先从Redis官网下载Redis并解压,本人使用的是最新版本5.0.4。依次执行如下命令:

cd /usr/local/
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
tar -zxvf redis-5.0.4.tar.gz

检测gcc依赖包是否安装

gcc --version

如果出现

[root@localhost ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)

字样表示已经安装。

如果没有安装gcc依赖包,则安装对应依赖包

yum install -y gcc-c++ tcl

2. 编译并安装

下载并解压完毕后,则对源码包进行编译安装,楼主的Redis安装路径为/usr/local/redis,同学们可以自行修改语句:make install PREFIX=你想要安装的路径

cd /usr/local/redis-5.0.4/
make install PREFIX=/usr/local/redis

复制Redis相关命令到/usr/sbin目录下,这样就可以直接执行这些命令,不用写全路径

cd /usr/local/redis/bin/
sudo cp redis-cli  redis-server  redis-sentinel   /usr/sbin/

3. 建立Redis配置文件

安装完成之后将 Redis 配置文件拷贝到系统配置目录/etc/下,redis.conf 是 Redis 的配置文件,redis.conf 在 Redis 源码目录,port默认 6379。

cp /usr/local/redis-5.0.4/redis.conf  /etc/redis.conf

 到这里我们就可以启动Redis

启动命令如下

[root@localhost ~]# /usr/local/redis/bin/redis-server /etc/redis.conf

 显示以下内容表示已启动成功


7178:C 01 Jun 2019 07:22:30.548 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7178:C 01 Jun 2019 07:22:30.548 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=7178, just started
7178:C 01 Jun 2019 07:22:30.548 # Configuration loaded
7178:M 01 Jun 2019 07:22:30.549 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 7178
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

7178:M 01 Jun 2019 07:22:30.554 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7178:M 01 Jun 2019 07:22:30.554 # Server initialized
7178:M 01 Jun 2019 07:22:30.554 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
7178:M 01 Jun 2019 07:22:30.554 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
7178:M 01 Jun 2019 07:22:30.554 * DB loaded from disk: 0.000 seconds
7178:M 01 Jun 2019 07:22:30.554 * Ready to accept connections

Redis配置文件主要参数解析参考 

daemonize  no               #redis进程是否以守护进程的方式运行,yes为是,no为否(不以守护进程的方式运行会占用一个终端)
pidfile /var/run/redis.pid  #指定redis进程的PID文件存放位置
port 6379                   #redis进程的端口号
bind 127.0.0.1              #绑定的主机地址
timeout  300                #客户端闲置多长时间后关闭连接,默认此参数为0即关闭此功能
loglevel verbose            #redis日志级别,可用的级别有debug.verbose.notice.warning
logfile stdout              #log文件输出位置,如果进程以守护进程的方式运行,此处又将输出文件设置为stdout的话,就会将日志信息输出到/dev/null里面去了
databases 16                #设置数据库的数量,默认为0可以使用select <dbid>命令在连接上指定数据库id
save <seconds> <changes>    #指定在多少时间内刷新次数达到多少的时候会将数据同步到数据文件;
rdbcompression yes          #指定存储至本地数据库时是否压缩文件,默认为yes即启用存储;
dbfilename dump.db          #指定本地数据库文件名
dir ./                      #指定本地数据问就按存放位置;
slaveof <masterip> <masterport>     #指定当本机为slave服务时,设置master服务的IP地址及端口,在redis启动的时候他会自动跟master进行数据同步
masterauth <master-password>        #当master设置了密码保护时,slave服务连接master的密码;
requirepass footbared       #设置redis连接密码,如果配置了连接密码,客户端在连接redis是需要通过AUTH<password>命令提供密码,默认关闭
maxclients 128              #设置同一时间最大客户连接数,默认无限制;redis可以同时连接的客户端数为redis程序可以打开的最大文件描述符,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max number of clients reached错误信息
maxmemory<bytes>            #指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis新的vm机制,会把Key存放内存,Value会存放在swap区
appendonly no               #指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no
appendfilename appendonly.aof       #指定跟新日志文件名默认为appendonly.aof
appendfsync everysec         #指定更新日志的条件,有三个可选参数no:表示等操作系统进行数据缓存同步到磁盘(快),always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全), everysec:表示每秒同步一次(折衷,默认值);

3.1 设置后端启动:

由于Redis默认是前端启动,必须保持在当前的窗口中,如果使用ctrl + c退出,那么Redis也就退出,不建议使用。

vim /etc/redis.conf

修改Redis配置文件把旧值daemonize no 改为 新值daemonize yes

 

找到 bind 那行配置,默认是: # bind 127.0.0.1

去掉#注释并改为: bind 0.0.0.0 此设置会变成允许所有远程访问。如果想指定限制访问,可设置对应的IP。

4. Redis常用操作

4.1 启动

/usr/local/redis/bin/redis-server /etc/redis.conf

 4.2 关闭

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown

 4.3 查看Redis进程

ps -ef | grep redis

 4.4 进入客户端

redis-cli

 4.5 关闭客户端

redis-cli shutdown

 4.6 设置开机自动启动配置

echo "/usr/local/redis/bin/redis-server /etc/redis.conf" >> /etc/rc.local

 4.7 开放防火墙端口

添加规则:iptables -I INPUT -p tcp -m tcp --dport 6379 -j ACCEPT
保存规则:service iptables save
重启 iptables:service iptables restart

至此Redis已经安装完毕。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值