NoSQL是Not only SQL的缩写,大意为“不只是SQL”,说明这项技术是传统关系型数据库的补充而非替代。在整个NoSQL技术栈中MemCache、Redis、MongoDB被称为NoSQL三剑客。
那么时代为什么需要NoSQL数据库呢?我们来做个对比:
关系型数据库 | NoSQL数据库 | |
---|---|---|
数据存储位置 | 硬盘 | 内存 |
数据结构 | 高度组织化结构化数据 | 没有预定义的模式 |
数据操作方式 | SQL | 所有数据都是键值对,没有声明性查询语言 |
事务控制 | 严格的基础事务ACID原则 | CAP定理 |
所以NoSQL数据库的最大优势体现为:高性能、高可用性和可伸缩性。
Redis英文官网介绍:
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
Redis中文官网介绍:
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
Redis命令参考文档网址:http://redisdoc.com
1、上传并解压
redis-4.0.2.tar.gz
[建议先拍快照]
yum install -y gcc-c++
vim redis解压目录/src/Makefile
PREFIX?=/usr/local/redis
就Redis自身而言是不需要修改的,这里修改的目的是让Redis的运行程序不要和其他文件混杂在一起。
编译:进入Redis解压目录执行make命令
[建议先拍快照]
安装:make install
[root@rich redis-4.0.2]# /usr/local/redis/bin/redis-server 5052:C 25 Sep 12:48:39.760 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5052:C 25 Sep 12:48:39.760 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5052, just started 5052:C 25 Sep 12:48:39.760 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/local/redis/bin/redis-server /path/to/redis.conf 5052:M 25 Sep 12:48:39.761 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.2 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 5052 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 5052:M 25 Sep 12:48:39.765 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 5052:M 25 Sep 12:48:39.765 # Server initialized 5052:M 25 Sep 12:48:39.765 # 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. 5052:M 25 Sep 12:48:39.766 # 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. 5052:M 25 Sep 12:48:39.766 * Ready to accept connections
停止Redis服务器
/usr/local/redis/bin/redis-cli shutdown
5052:M 25 Sep 12:49:30.212 # User requested shutdown...
5052:M 25 Sep 12:49:30.212 * Saving the final RDB snapshot before exiting.
5052:M 25 Sep 12:49:30.216 * DB saved on disk
5052:M 25 Sep 12:49:30.216 # Redis is now ready to exit, bye bye...
[1]准备配置文件
cp /opt/redis-4.0.2/redis.conf /usr/local/redis/
[2]修改配置项
配置项名称 | 作用 | 取值 |
---|---|---|
daemonize | 控制是否以守护进程形式运行Redis服务器 | yes |
logfile | 指定日志文件位置 | "/var/logs/redis.log" |
dir | Redis工作目录 | /usr/local/redis |
注意:/var/logs目录需要我们提前创建好
[3]让Redis根据指定的配置文件启动
格式
redis-server文件路径 redis.conf文件路径
举例
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
/usr/local/redis/bin/redis-cli
[root@rich logs]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
MySQL | Redis | |
---|---|---|
连接 | Connection | Jedis |
连接池 | C3P0等等 | JedisPool |
操作完成 | 关闭连接 | 关闭连接 |
在Redis配置文件中关闭bind配置,设置密码。
#bind 127.0.0.1 //关闭或允许其他ip请求通过
#requirepass root //配置密码
protected-mode no //登录认证,yes 需要密码认证,no 不需要密码认证
redis.clients
jedis
2.9.0
@Test
public void contextLoads() {
//指定Redis服务器的IP地址和端口号
Jedis jedis = new Jedis("192.168.254.111", 6379,10000);
//执行ping命令
String ping = jedis.ping();
System.out.println(ping);
jedis.close();
}