Redis-1- Introduction and Installation

1.Redis Profile

Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。

1.1 Redis and other key - value caching product has the following three characteristics:

1.Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
2.Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
3.Redis支持数据的备份,即master-slave模式的数据备份。

1.2 Redis advantage

1.性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
2.丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
3.原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。
4.丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。

1.3 Redis and other key-value store What is the difference?

Redis有着更为复杂的数据结构并且提供对他们的原子性操作,这是一个不同于其他数据库的进化路径。Redis的数据类型都是基于基本数据结构的同时对程序员透明,无需进行额外的抽象。

Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集进行高速读写时需要权衡内存,因为数据量不能大于硬件内存。在内存数据库方面的另一个优点是,相比在磁盘上相同的复杂的数据结构,在内存中操作起来非常简单,这样Redis可以做很多内部复杂性很强的事情。同时,在磁盘格式方面他们是紧凑的以追加的方式产生的,因为他们并不需要进行随机访问。

2. Install Redis

Redis choose to install on Linux, now using centos7 virtual machine installation of the installation of

1. Install gcc redis is written in c language

yum install gcc-c++

2. Download the installation package redis executed in the root directory

wget http://download.redis.io/releases/redis-5.0.7.tar.gz

3. Extract installation package redis

tar -zxvf redis-5.0.7.tar.gz

Note: We put the redis file / directory under the root, do not accidentally deleted. Also you can extract it elsewhere.

4. Go to the directory redis

cd redis-5.0.7

5. compile and install

make
make install

After make will be compiled redis redis-server service program under redis-2.8.17 / src directory, as well as the client program for testing redis-cli
after make install we can in / usr / local / bin to see these command.

6. edit the configuration file (not choose to modify, using the default configuration)

在解压后的redis-5.0.7目录下有redis的配置文件redis.conf,我们可以根据需要修改配置文件,最好复制一份该文件出来做修改。
mkdir myconf
cp redis.conf myconf/
vim myconf/redis.conf
1.以守护进程的方式运行(后台运行),daemonize yes

2.绑定端口,port 6379 默认是6379 需要安全组开放端口

3.绑定IP,bind 127.0.0.1

4.指定数据存放路径,dir /usr/local/redis/log rdb存放的路径

5.指定持久化方式,appendonly yes

更多配置及其说明参考:[https://www.runoob.com/redis/redis-conf.html](https://www.runoob.com/redis/redis-conf.html)

7. redis added to the boot

vim /etc/rc.local
在里面添加内容:
/usr/local/bin/redis-server /root/redis-5.0.7/myconf/redis.conf

8. Start redis

启动redis:redis-server
上面是使用redis默认配置启动的,如果我们需要使用我们刚刚自定义后的配置文件则使用命令:redis-server /root/redis-5.0.7/myconf/redis.conf
[root@aliyun ~]# redis-server 
30123:C 26 Nov 2019 14:03:51.237 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30123:C 26 Nov 2019 14:03:51.237 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=30123, just started
30123:C 26 Nov 2019 14:03:51.237 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 30123
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

30123:M 26 Nov 2019 14:03:51.239 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30123:M 26 Nov 2019 14:03:51.239 # Server initialized
30123:M 26 Nov 2019 14:03:51.239 # 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.
30123:M 26 Nov 2019 14:03:51.239 * Ready to accept connections

9. By connecting the client to enter redis

如果我们直接输入命令redis-cli提示(redis-cli: command not found)的话,去/usr/local/bin目录下 执行./redis-cli

这种命令看起来不太舒服,或者我们直接安装一个redis客户端吧(在其他机器上连接redis服务器就是安装这个客户端):
# 从redis官网下载redis-cli的压缩包
wget http://download.redis.io/redis-stable.tar.gz 

# 解压下载下来的压缩包
tar xvzf redis-stable.tar.gz

# 进入redis-stable目录
cd redis-stable

# 安装
make

# 将redis-cli拷贝到/usr/local/bin/下,让redis-cli指令可以在任意目录下直接使用
cp src/redis-cli /usr/local/bin/

After our normal execution redis-cli you can enter the redis this machine. If you want to enter the remote redis services are: redis-cli -h host -p port -a password

# 这是我使用自己修改过的配置文件运行redis,并使用redis-cli进入了本机redis

[root@aliyun ~]# redis-server redis-5.0.7/myconf/redis.conf 
7240:C 26 Nov 2019 14:28:02.815 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7240:C 26 Nov 2019 14:28:02.815 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=7240, just started
7240:C 26 Nov 2019 14:28:02.815 # Configuration loaded
[root@aliyun ~]# redis-cli
127.0.0.1:6379> 

10. Stop redis

考虑到redis可能正在进行数据持久化,所以不能强行终止redis进程。正确的做法是发送shutdown命令。
    [root@aliyun ~]# redis-cli shutdown

It can also be performed directly at the redis client: shutdown

Another stop command:

    [root@aliyun ~]# pkill redis

redis can properly handle the SIGTERM signal, use the "kill redis process pid" can also be redis normal end, the same effect as shutdown.

3.Redis basic key commands

前面提到,Redis是一个键值(key-value)数据库,所以有很多键命令,键命令用于管理redis的键(key)。

Commonly used commands:

1   DEL key   # 该命令用于在 key 存在时删除 key。
2   DUMP key    # 序列化给定 key ,并返回被序列化的值。
3   EXISTS key  # 检查给定 key 是否存在。
4   EXPIRE key seconds  # 为给定 key 设置过期时间,以秒计。
5   EXPIREAT key timestamp  # EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)。
6   PEXPIRE key milliseconds    # 设置 key 的过期时间以毫秒计。
7   PEXPIREAT key milliseconds-timestamp    # 设置 key 过期时间的时间戳(unix timestamp) 以毫秒计
8   KEYS pattern    # 查找所有符合给定模式( pattern)的 key 。
9   MOVE key db     # 将当前数据库的 key 移动到给定的数据库 db 当中。
10  PERSIST key     # 移除 key 的过期时间,key 将持久保持。
11  PTTL key    # 以毫秒为单位返回 key 的剩余的过期时间。
12  TTL key     # 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
13  RANDOMKEY   # 从当前数据库中随机返回一个 key 。
14  RENAME key newkey   # 修改 key 的名称
15  RENAMENX key newkey # 仅当 newkey 不存在时,将 key 改名为 newkey 。
16  TYPE key    # 返回 key 所储存的值的类型。

See more commands examiner Net: https://redis.io/commands

Guess you like

Origin www.cnblogs.com/ChangAn223/p/11958856.html