Redis是一种高级key-value数据库。它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富。有字符串,链表,集 合和有序集合。支持在服务器端计算集合的并,交和补集(difference)等,还支持多种排序功能。所以Redis也可以被看成是一个数据结构服务 器。
Redis的所有数据都是保存在内存中,然后不定期的通过异步方式保存到磁盘上(这称为“半持久化模式”);也可以把每一次数据变化都写入到一个append only file(aof)里面(这称为“全持久化模式”)。
I.快速运行Redis
一、下载安装
进入redis.io官方网站:
- $ wget http://redis.googlecode.com/files/redis-2.4.15.tar.gz
- $ tar xzf redis-2.4.5.tar.gz //这里假设解压缩到/usr/local/redis
- $ cd redis-2.4.5
- $ make
- $ make install
- $ cd utils
- $./install_server
$ wget http://redis.googlecode.com/files/redis-2.4.15.tar.gz $ tar xzf redis-2.4.5.tar.gz //这里假设解压缩到/usr/local/redis $ cd redis-2.4.5 $ make $ make install $ cd utils $./install_server
就会自动安装到/usr/local/bin目录下。在该目录下生成几个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-stat、redis-check-aof,它们的作用如下:
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况
redis-check-aof:
二.启动服务器
安装时的最后一步install_server脚本会生成启动命令文件(试试就知道),下面就是一个执行例子
- Welcome to the redis service installer
- This script will help you easily set up a running redis server
- Please select the redis port for this instance: [6379]
- Selecting default: 6379
- Please select the redis config file name [/etc/redis/6379.conf]
- Selected default - /etc/redis/6379.conf
- Please select the redis log file name [/var/log/redis_6379.log]
- Selected default - /var/log/redis_6379.log
- Please select the data directory for this instance [/var/lib/redis/6379]
- Selected default - /var/lib/redis/6379
- Please select the redis executable path [/usr/local/bin/redis-server]
- Copied /tmp/6379.conf => /etc/init.d/redis_6379
- Installing service...
- Successfully added to chkconfig!
- Successfully added to runlevels 345!
- Starting Redis server...
- Installation successful!
Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379 Please select the redis executable path [/usr/local/bin/redis-server] Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
/etc/init.d/redis_6379 start
将启动服务到默认端口6379
三.客户端访问
- $ redis-cli
- redis> set foo bar
- OK
- redis> get foo
- "bar"
$ redis-cli redis> set foo bar OK redis> get foo "bar"
四.关闭服务器
- $ /etc/init.d/redis_6379 stop
$ /etc/init.d/redis_6379 stop
以上摘官方文档http://redis.io/download。如果想使用Windows版的Redis请去http://code.google.com/p/servicestack/wiki/RedisWindowsDownload下载(其版本滞后于官方版本,不建议在生产环境使用Win32.)。
II. 定制服务器启动参数
在我们成功安装Redis后,我们直接执行redis-server即可运行Redis,此时它是按照默认配置来运行的(默认配置甚至不是后台运行)。我们希望Redis按我们的要求运行,则我们需要修改配置文件(在redis解压缩目录下有一个redis.con可以作为范本),下面是redis.conf的主要配置参数的意义:
pidfile:pid文件位置
port:监听的端口号
timeout:请求超时时间
loglevel:log信息级别
logfile:log文件位置
databases:开启数据库的数量
save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。
rdbcompression:是否使用压缩
dbfilename:数据快照文件名(只是文件名,不包括目录)
dir:数据快照的保存目录(这个是目录)
appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。
appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)
下面是一个略做修改后的配置文件内容:
- daemonize yes
- pidfile /usr/local/redis/var/redis.pid
- port 6000
- timeout 300
- loglevel debug
- logfile /usr/local/redis/var/redis.log
- databases 16
- save 900 1
- save 300 10
- save 60 10000
- rdbcompression yes
- dbfilename dump.rdb
- dir /usr/local/redis/var/
- appendonly no
- appendfsync always
- glueoutputbuf yes
- shareobjects no
- shareobjectspoolsize 1024
daemonize yes pidfile /usr/local/redis/var/redis.pid port 6000 timeout 300 loglevel debug logfile /usr/local/redis/var/redis.log databases 16 save 900 1 save 300 10 save 60 10000 rdbcompression yes dbfilename dump.rdb dir /usr/local/redis/var/ appendonly no appendfsync always glueoutputbuf yes shareobjects no shareobjectspoolsize 1024
重启服务器
- redis-server /usr/local/redis/redis.conf
redis-server /usr/local/redis/redis.conf
试试看读写是否有问题.如果服务器启动到了指定非默认端口,那么客户端连接则需要-p参数
如:
- $redis-cli -p 6380
$redis-cli -p 6380
* 开放服务器端口供其他主机连接
vi /etc/sysconfig/iptables #需要具备其修改权限
可能需要增加一行:
# redis
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
保存后重启iptables:
service iptables restart (PATH中加入了/sbin/)或者 /etc/init.d/iptables restart
III. PHP客户端
Redis的php客户端库非常之多,code.google.com上就有:
* phpredis
http://code.google.com/p/phpredis/ 是C客户端作为php模块
* php-predis
http://code.google.com/p/php-redis/ 纯php客户端
以上两个都不在Redis官方推荐推荐之列。
Redis推荐客户端链接是:http://redis.io/clients,如下图:
纯php库Predis(便于hack),但是性能不高。https://github.com/nrk/predis
下载该库文件后运行bin/createSingleFile.php可以生成一个类库文件Predis.php,非常方便使用。下面是最简单的一个Hello World应用:
- <?php
- require('Predis.php');
- $single_server = array(
- 'host' => '192.168.1.101',
- 'port' => 6379,
- 'database' => 15
- );
- $client = new Predis\Client($single_server);
- $client->set('library', 'predis');
- $retval = $client->get('allen ');
- var_dump($retval);
<?php require('Predis.php'); $single_server = array( 'host' => '192.168.1.101', 'port' => 6379, 'database' => 15 ); $client = new Predis\Client($single_server); $client->set('library', 'predis'); $retval = $client->get('allen '); var_dump($retval);
我推荐使用的客户端是:phpredis https://github.com/nicolasff/phpredis
windows版的dll从这里下载https://github.com/char101/phpredis/downloads
IV . Redis管理工具
1. phpRedisAdmin
https://github.com/ErikDubbelboer/phpRedisAdmin/
要求PHP C Module : https://github.com/nicolasff/phpredis
比较弱
2. redis-admin
http://code.google.com/p/redis-admin/
更弱
选phpRedisAdmin是不得不的做法.
相关推荐
Redis入门&配置教程整理
RT,Redis配置及使用资料整理 PDF文档
超级全面的redis入门文档,动力节点redis教程课件。从redis下载,到安装,到reids的数据类型,到redis的操作和使用。文档内有java代码使用jedis连接redis,演示对redis的各种操作。
安装步骤:https://blog.csdn.net/weixin_38642130/article/details/86596979 查阅了一些资料进行整理,记录学习之旅,分享给大家共同学习进步!
自己整理一些关于redis的安装与使用说明文档,包括后续使用工具查看redis的存的数据
这个是绝对超值的,本人收集了在windows下架设redis数据库的所需软件,以及在实践中应该注意的...这是本人经过长时间收集整理出来的,里面所有软件都带有安装方法,以及安装使用过程中注意的事项,有需要的就下载吧。
phpredis是个人觉得最好的一个php-redis客户端,因为其提供的function与redis的命令基本一致,降低的了学习成本,...1. windows下redis的客户端,可直接安装redis; 2. php5.5 的redis扩展(64位和32位的dll文件);
redis2.6.14 linux安装包,内含自己整理的两种安装模式,及操作说明文档,并在文档最后附上了配置文件说明
windows中使用Redis 里面包含Redis在页面中的使用说明和dll的代码引用说明
个人的学习笔记,五大基本数据类型,相关配置,redis事务,主从同步,集群,持久化,Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径。Redis的数据类型都是基于基本...
主要介绍了redis安装和配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
下载的朋友反映所有类都在一个文件看着有点乱,现重新整理了一下, 做成jar包使用 使用方法: 引用jar包,添加配置文件redisservice.properties, #redis服务地址,连接ip格式,默认第一个为主服务器,后面的为从服务器,...
菜鸟上整理的Redis思维导图,整理了关于Redis的一些服务配置还有数据类型的细节,很详细!
培训文档,整理redis的安装和部署,redis配置文档和有关方面。
Redis集群化部署方案以及遇到的问题整理,Ruby及其gem安装。
0103 Redis组件安装 0104 Redis服务配置 0105 Redis基础认证 0106 redis-benchmark 0201 Redis多业务管理 0202 文本数据类型 0203 keys命令 0204 Hash数据类型 0205【 数字操作 0206 List数据类型 0207 Set数据类型 ...
Nosql (1)redis安置 (2)RedisAdminUI.zip (3)redis安装部署 (4)redis技术深层剖析及应用实践经验(sina微博) (5)Redis内存存储结构分析 ...(11)Redis 安装及配置 (转整理) Openstack资源: 盛大云平台架构设计和实现
涉及知识:虚拟机中CentOS7 系统安装,Xshell及Winscp的使用,Redis单节点安装部署,Redis五种类型命令练习,服务器的配置,Redis集群部署全过程,Redis高可用性的测试,过程详细,本人自己学习的时候整理的。...
Redis 6.0.6 已发布,此版本修复了一些 bug,其中主要内容包括: 修复启用带前缀的 CLIENT TRACKING 时崩溃的问题 EXEC 始终会因 EXECABORT 而失败,并且已清除多状态 ...TLS:支持会话缓存配置