该文使用 centos6.5 64 位 redis-3.2.8
NSQL:Not Only SQL 以 key-value 形式存储和传统的关系型数据库不一样,不一定遵循传统数据库的一些基本要求,比如说遵循 SQL 标准、ACID 属性、表结构等,这类数据库主要具有以下特点:非关系型的、分布式的、开源的、水平可扩展的。
名称解释:
1、key-value :js 中的对象,json 数据格式、java 中的 map,java 中的 javabean
2、SQL 标准:insert、delete、update、select 等标准的 SQL 语句
3、ACID 属性:事务 ,redis 中也存在事务,但很简单。
4、表结构:通过已经建立的表向表中添加数据,redis 可以在表不存在的情况下直接添加数据
NSQL 特点:
1、处理超大量数据能力强
2、可以运行在便宜的 PC 服务器集群上
4、对数据的高并发读写能力强(mysql 在上万数据的写入对磁盘 IO 消耗很大)
5、对海量数据的高效存储和访问。
6、对数据的高可靠扩展性和高可用性
Redis 是一个开源的,先进的 key-value 存储。它通常被称为数据结构服务器,因为键可以包含字符串、哈希、链表、集合和有序集合。
支持的数据类型:string(字符串)、list(集合)、set(集合)、zset(有序集合)。
支持的操作:这些数据类型支持 push/pop、add/remove 等丰富的数据操作。支持不同方式的排序。
缓存:redis 为了保证效率数据都是缓存在内存中的,为了防止系统突然崩溃从而导致内存中的数据丢失,它也可以周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件。
redis 用户:新浪微博是 redis 最大的用户,200 多台物理机。
redis 在新浪微博中的使用场景:
1、应用程序直接访问 redis 数据库
该方式与传统应用程序访问 mysql 类似,但该方式不安全。
2、应用程序直接访问 Redis,只有当 Redis 访问失败后才访问 Mysql
a、应用程序先访问 redis server,redis 与 mysql 集群进行数据同步
b、如果 redis 集群宕机后应用程序直接访问 mysql 集群
3、redis 使用场景:
a、区最新 N 个数据的操作
b、排行榜应用、取 TOP N 操作
c、需要精确设定过期时间的应用
d、计数器应用
e、uniq 操作、获取某段时间所有数据排重值
f、实时系统、反垃圾系统
g、Pub/Sub 构建实时消息系统
h、构建队列系统
i、redis 缓存
1、下载 https://redis.io/download 稳定版(stable)
a、解压:[root@localhost tools]# tar -zxvf redis-3.2.8.tar.gz
b、编译:[root@localhost redis-3.2.8]# make
如果出现错误 gcc: 命令未找到 则安装 gcc yum install -y gcc g++ gcc-c++ make
如果出现 tcl 问题 则安装 tcl yum install tcl
c、安装 [root@localhost src]# make install
在 src 下将出现 redis 的相关命令
-rwxr-xr-x. 1 root root 5707211 2 月 17 22:33 redis-cli 进入 redis 客户端命令
-rwxr-xr-x. 1 root root 7827978 2 月 17 22:33 redis-server 启动 redis 服务命令
2、配置 redis
(1)[root@localhost src]# mkdir -p /usr/local/redis/bin 创建文件夹用户存储 redis 命令
(2)[root@localhost src]# mkdir -p /usr/local/redis/etc 创建文件夹用户存储 redis 配置文件
(3)将 / tools/redis-3.2.8 下的 redis.conf 移动到 / usr/local/redis/etc 下
[root@localhost redis-3.2.8]# mv ./redis.conf /usr/local/redis/etc
(4)将 / tools/redis-3.2.8/src 下的 mkreleasehdr.sh、redis-benchmark、redis-check-aof、redis-check-rdb、redis-cli、redis-server 移动到 / usr/local/redis/bin 下
(5)启动 redis 服务:进入 redis bin 目录下 /usr/local/redis/bin
[root@localhost bin]# ./redis-server
如果出现 -bash: ./redis-server: 权限不够
查看权限:[root@localhost bin]# ls -l | grep -i redis-server
赋予 xr 权限 [root@localhost bin]# chmod 755 redis-server
- [root@localhost bin]# ls -l | grep -i redis-server
- -rw-rw-r--. 1 root root 7827978 2月 17 22:52 redis-server
- [root@localhost bin]# chmod 755 redis-server
- [root@localhost bin]# ls -l | grep -i redis-server
- -rwxr-xr-x. 1 root root 7827978 2月 17 22:52 redis-server
提示信息
- [root@localhost bin]# ./redis-server
- 5322:C 17 Feb 23:18:39.086 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
- 5322:M 17 Feb 23:18:39.087 * Increased maximum number of open files to 10032 (it was originally set to 1024).
- _._
- _.-``__ ''-._
- _.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
- .-`` .-```. ```\/ _.,_ ''-._
- ( ' , .-` | `, ) Running in standalone mode
- |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
- | `-._ `._ / _.-' | PID: 5322
- `-._ `-._ `-./ _.-' _.-'
- |`-._`-._ `-.__.-' _.-'_.-'|
- | `-._`-._ _.-'_.-' | http://redis.io
- `-._ `-._`-.__.-'_.-' _.-'
- |`-._`-._ `-.__.-' _.-'_.-'|
- | `-._`-._ _.-'_.-' |
- `-._ `-._`-.__.-'_.-' _.-'
- `-._ `-.__.-' _.-'
- `-._ _.-'
- `-.__.-'
- 5322:M 17 Feb 23:18:39.113 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
- 5322:M 17 Feb 23:18:39.113 # Server started, Redis version 3.2.8
- 5322:M 17 Feb 23:18:39.114 # 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.
- 5322:M 17 Feb 23:18:39.114 # 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.
- 5322:M 17 Feb 23:18:39.114 * The server is now ready to accept connections on port 6379
安装成功端口 6379
使用指定的配置文件启动 redis 服务器
[root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no
daemonize no:默认启动方式该方式占用一个窗口,一旦 ctrl+c redis 服务器就关闭
daemonize yes: 将 redis 的启动方式改为后台运行
进入:redis 客户端
[root@localhost bin]# pwd
/usr/local/redis/bin
[root@localhost bin]# ./redis-cli
127.0.0.1:6379>
关闭 redis 服务器:
[root@localhost bin]# pkill redis-serve (通过进程的方式)
[root@localhost bin]# ./redis-cli shutdown(通过 redis 命令)
来源: http://www.cnblogs.com/jalja/p/6409680.html