Linux (十)Redis 简介与安装

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

一、从NoSQL说起

NoSQL是Not only SQL的缩写,大意为“不只是SQL”,说明这项技术是传统关系型数据库的补充而非替代。在整个NoSQL技术栈中MemCacheRedisMongoDB被称为NoSQL三剑客。

那么时代为什么需要NoSQL数据库呢?我们来做个对比:

 关系型数据库NoSQL数据库
数据存储位置硬盘内存
数据结构高度组织化结构化数据没有预定义的模式
数据操作方式SQL所有数据都是键值对,没有声明性查询语言
事务控制严格的基础事务ACID原则CAP定理

所以NoSQL数据库的最大优势体现为:高性能、高可用性和可伸缩性。

 二、Redis 简介

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

三、Redis 安装1、

1、上传并解压

redis-4.0.2.tar.gz

2.安装C语言编译环境

[建议先拍快照]

yum install -y gcc-c++

3.修改安装位置

vim redis解压目录/src/Makefile

PREFIX?=/usr/local/redis

就Redis自身而言是不需要修改的,这里修改的目的是让Redis的运行程序不要和其他文件混杂在一起。

4.编译安装

编译:进入Redis解压目录执行make命令

[建议先拍快照]

安装:make install

5.启动Redis服务器

①默认启动

[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"
dirRedis工作目录/usr/local/redis

注意:/var/logs目录需要我们提前创建好

[3]让Redis根据指定的配置文件启动

格式

redis-server文件路径 redis.conf文件路径

举例

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

6.客户端登录

  /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

四、Jedis

1、对比

 MySQLRedis
连接ConnectionJedis
连接池C3P0等等JedisPool
操作完成关闭连接关闭连接

2.Redis准备

在Redis配置文件中关闭bind配置,设置密码。

#bind 127.0.0.1    //关闭或允许其他ip请求通过

#requirepass root    //配置密码

protected-mode no  //登录认证,yes 需要密码认证,no 不需要密码认证

3.Jedis

                <dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>
        @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();

	}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值