Redis

相关概念

数据库类型:

  • 关系型数据库RDBMS:按照预先设置的组织结构,将数据存储在物理介质上,数据之间可以做关联操作

  • 非关系型数据库NOSQL:

    1. Not Only SQL
    2. 不需要预先定义数据存储结构
    3. 每条记录可以有不同的数据类型和字段个数
    4. 主流软件:redis/memcached/mongodb

部署Redis

  • Remote Dictionary Server(远程字典服务器)
  • 高性能的(Key/Values)分布式内存数据库
  • 支持数据持久化(定期把数据存储到硬盘)
  • 支持多种数据类型string/list/hash
  • 支持master-slave模式数据备份
  • 中文官网www.redis.cn

1)安装软件

[root@redis ~] yum -y install gcc 
[root@redis ~] tar -xvf redis-4.0.8.tar.gz
[root@redis redis-4.0.8] cd redis-4.0.8
[root@redis redis-4.0.8] make
[root@redis redis-4.0.8] make install

2)初始化配置

[root@redis redis-4.0.8] ./utils/install_server.sh  #根据提示完成初始化
信息说明
默认端口6379
主配置文件/etc/redis/6379.conf
日志文件/var/log/redis_6379.log
数据库目录/var/lib/redis/6379/
服务启动程序/usr/local/bin/redis-server
命令行连接命令/usr/local/bin/redis-cli

3)管理服务

/etc/init.d/redis_6379:开发者提供的shell脚本

] /etc/init.d/redis_6379 stop  #停止服务
] /etc/init.d/redis_6379 start #启动服务
] /etc/init.d/redis_6379 status #查看服务状态
] ps -C redis-server  #查看进程
] ss -antup |grep 6379  #查看端口
] redis-cli 	 #连接redis
> ping
PONG
> set KEY VALUES     #存储数据(straing类型)
> mset KEY1 VALUES1 KEY2 VALUES2 ...   #存储多个key值
> get KEY    		 #获取key值(string类型)
> mget KEY1 KEY2 ...    	#获取多个key值
> select 数据库编号0-15  		#切换库
> keys *    		 #显示所有key名
> keys a??|age 		 #显示指定key名(?匹配任意一个字符)
> exists KEY  	  	 #检测key是否存在(返回值为0代表不存在,返回值为1代表存在)
> ttl KEY  		  	 #查看key生存时间(返回值为-1代表永不过期,-2代表已过期)
> type KEY   		 #查看key类型
> move KEY 库编号     #移动key到指定库
> expire KEY 数字  	 #设置key有效时间(单位:s)
> del KEY   	     #删除指定key
> flushall			 #删除内存里的所有key
> flushdb   		 #删除所在库的所有key
> save				 #保存所有key到磁盘(阻塞写)
> bgsave 	     	 #不阻塞写存盘
> shutdown           #停止服务

4)配置文件

/etc/redis/6379.conf

分类:

名称说明
NETWORK网络
GENERAL常规
SNAPSHOTTING快照
REPLICATION复制
SECURITY安全
CLIENTS客户端
MEMORY MANAGEMENT内存管理

数据单位:

  12 # 1k => 1000 bytes
  13 # 1kb => 1024 bytes
  14 # 1m => 1000000 bytes
  15 # 1mb => 1024*1024 bytes
  16 # 1g => 1000000000 bytes
  17 # 1gb => 1024*1024*1024 bytes
  18 #
  19 # units are case insensitive so 1GB 1Gb 1gB are all the same.

常用配置:

  70   bind 127.0.0.1   #ip地址
  93   port 6350        #端口
  137  daemonize yes   #以守护进程方式运行
  172  logfile /var/log/redis_6379.log   #日志文件
  187  databases 16    #数据库数量
  264  dir /var/lib/redis/6379  #数据库目录
  501  requirepass 123456   #登录密码
  533  # maxclients 10000    #并发连接数

内存管理:

  • MEMORY MANAGEMENT 内存清除策略
 565 # volatile-lru -> 删除最近最少使用(针对设置了TTL的key)
 566 # allkeys-lru -> 删除最少使用的key(针对所有key)
 567 # volatile-lfu -> 从所有配置了过期时间的key中清除使用频率最少的
 568 # allkeys-lfu -> 从所有key中清除使用频率最少的key
 569 # volatile-random -> 在设置了TTL的key里随机移除
 570 # allkeys-random -> 随机移除key
 571 # volatile-ttl -> 移除最近过期的key
 572 # noeviction -> 不删除
 573 #
 574 # LRU means Least Recently Used
 575 # LFU means Least Frequently Used
  • 优化
 560  maxmemory <bytes>   	#最大内存
 591  maxmemory-policy volatile-lru    #定义使用的策略
 602  maxmemory-samples 5	 #选取key模板的个数(针对lru和ttl策略)
  • 持久化:
 206 #   In the example below the behaviour will be to save:
 207 #   after 900 sec (15 min) if at least 1 key changed
 208 #   after 300 sec (5 min) if at least 10 keys changed
 209 #   after 60 sec if at least 10000 keys changed
 219 save 900 1
 220 save 300 10
 221 save 60 10000

设置密码/ip/端口:

] vim /etc/redis/6379.conf
requirepass 123456
bind 192.168.4.50
port 6350
] /etc/init.d/redis_6379 stop
] /etc/init.d/redis_6379 start

] redis-cli -h 192.168.4.50 -p 6350 -a 123456  #连接
] redis-cli -h 192.168.4.50 -p 6350 -a 123456 stop  #停止服务
  • 修改了密码/ip/端口,/etc/init.d/redis_6379脚本将不能正常执行stop,可以修改脚本内容,也可使用redis-cli实现服务的停止操作

LNMP+Redis

[root@lnmp60] yum -y install gcc pcre-devel zlib-devel   #安装依赖
[root@lnmp60] tar -zxvf nginx-1.12.2.tar.gz   #解压
[root@lnmp60] cd nginx-1.12.2  #进源码目录
[root@lnmp60] ./configure  #配置
[root@lnmp60] make && make install
[root@lnmp60] yum -y install php php-fpm
[root@lnmp60] vim +65 /usr/local/nginx/conf/nginx.conf
:65,71s/#//
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             include        fastcgi.conf;   
 70         }
[root@lnmp60] /usr/local/nginx/sbin/nginx -t  #测试配置文件格式
[root@lnmp60] systemctl start php-fpm
[root@lnmp60] /usr/local/nginx/sbin/nginx

配置支持Redis

[root@redis50] 部署redis,启动服务
[root@lnmp60] yum -y install php php-devel
[root@lnmp60] tar -xvf php-redis-2.2.4.tar.gz
[root@lnmp60] cd phpredis-2.2.4
[root@lnmp60] phpize   #生成配置文件php-config及configure命令
[root@lnmp60] ./configure --with-php-config=/usr/bin/php-config
[root@lnmp60] make && make install

[root@lnmp60] vim /etc/php.ini
 728 extension_dir = "/usr/lib64/php/modules/"  #模块目录
 729 ; On windows:
 730 extension = "redis.so"  #模块名
[root@lnmp60] systemctl restart php-fpm
[root@lnmp60] vim /usr/local/nginx/html/test.php  #测试脚本
<?php
$redis = new redis();
$redis->connect( '192.168.4.50','6350' );  
$redis->auth( '123456' );   #ip/端口/密码与redis的配置要一致
$redis->set( 'address','xian' );
echo 'OK';
echo $redis->get( 'address' );
?>
[root@lnmp60] curl localhost/test.php
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值