Redis introduction and visualization tool installation details and redis basic instructions (Xiaobai++)

1. Redis introduction and installation

1.1 Introduction to Redis

Redis (Remote Dictionary Server), that is, remote dictionary service, is an open source written in ANSI C language, supports the network, can be memory-based or persistent log type, Key-Value database, and provides APIs in multiple languages. Since March 15, 2010, the development of Redis has been hosted by VMware, and since May 2013, the development of Redis has been sponsored by Pivotal.

What can Redis do?

  1. Memory storage, persistence, lost when power off, so persistence is very important, Redis uses two mechanisms (RDB and AOF)
  2. High efficiency, can be used for high speed efficiency
  3. publish subscribe system
  4. Map information analysis
  5. counters, etc., website views

Redis features/characteristics

  1. various data types
  2. Persistence
  3. cluster
  4. Affairs etc.

1.2 Install Redis in CentOS7

1.2.1 Install gcc

gcc in linux is a powerful multi-platform compiler with superior performance launched by GNU. The gcc compiler can compile and link C and C ++ language source programs and object programs into executable files. gcc in linux is a powerful multi-platform compiler with superior performance launched by GNU. The gcc compiler can compile and link C and C++ language source programs and object programs into executable files.The gcc in linux is a powerful multi-platform compiler with superior performance launched by GNU . The g cc compiler can convert C , C++ Language source program and target program are compiled and linked into executable files.

Because Redis depends on the c++ environment, we need to install c++

yum install gcc-c++

You can check the version with gcc -v

gcc -v

Upgrade the version because redis7.* requires the support of a higher version of gcc to execute 4 separately

yum -y install centos-release-scl 
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils #修改使用版本
scl enable devtoolset-9 bash 
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

Continue to check the gcc version upgrade to 9.* to complete the installation

1.2.2 Download and install Redis

redis can be installed and used under windows and linux, but the windows system version has been stopped for a long time, so it is not recommended to use it.

Official website https://redis.io/ Chinese website http://www.redis.cn/

Put the installation file under the opt folder through the tool:

Note: If the system application is java, we will install it under /usr/local, if it is a user-level application, we will put it under opt

Unzip the folder:

tar  -zxvf  redis-7.0.5 .tar.gz

After decompression, enter the redis folder to view the file:

[root@lixp ~]#   cd redis-7.0.5

[root@lixp redis-7.0.5]#  ls

Compile: It will help us configure the required configuration

make #注意需要在解压后的redis文件夹中执行
这一步就是编译,大多数的源代码包都经过这一步进行编译(当然有些perl或python编写的软件需要 调用perl或python来进行编译)。如果 在 make 过程中出现 error ,就要记下错误代码(注意不仅仅 是最后一行),然后可以向开发者提交 bugreport(一般在 INSTALL 里有提交地址)。或者系统少了 一些依赖库等,这些需要自己仔细研究错误代码。make 的作用是开始进行源代码编译,以及一些功能 的提供,这些功能由他的 Makefile 设置文件提供相关的功能。比如 make install 一般表示进行安装, make uninstall 是卸载,不加参数就是默认的进行源代码编译。make 是 Linux 开发套件里面自动化 编译的一个控制程序,他通过借助 Makefile 里面编写的编译规范进行自动化的调用 gcc 、ld 以及运行 某些需要的程序进行编译的程序。


install redis

make install

This command is used to install (of course, some software needs to run make check or make test for some tests first), this step generally requires you to have root privileges (because you need to write files to the system).

Command Expansion - Software Installation Process

1、解zhi包软件 tar zxf xxxx.tgz 

2、配置 cd xxxx ./configure 

3、编译 make 

4、安装 make install 

5、卸载 make uninstall

Check where the redis environment service is located

start test

redis-server
redis-server redis-config #后面是配置文件路径

1.2.3 configure redis

install vim-editor

yum  -y  install vim

backup file

Create a folder at the same level as the redis configuration file and cp a redis.conf file to the new folder. We will use this cp file for operations in the future, and the original original will not change.

#进入到redis目录下
[root@lixp redis-7.0.5]# cd /opt/redis-7.0.5
#复制一份redis.conf文件,防止源文件损坏,备份。
[root@lixp redis-7.0.5]# cp redis.conf ./redis0.conf 
[root@lixp redis-7.0.5]# ll

Modify background startup

vim redis-conf

Restart redis and see the test

[root@lixp redis-7.0.5]# redis-server /opt/redis-6.0.6/it-sunwz/redis.conf

view progress

ps -aux | grep redis

ps -ef | grep redis

Client connection test

[root@lixp redis-7.0.5]#  redis-cli -h 127.0.0.1 -p 6379 

[root@lixp redis-7.0.5]#  redis-cli -h 127.0.0.1 -p 6379 --raw #支持中文显示

Client IO test

set name 张三

get name

keys *

Exit the client and end the service

 # 退出客户端 
  exit 
 # 结束服务方式1 客户端内直接输入 
 shutdown shutdown 
 # 结束服务方式2 客户端外直接输入命令 
 redis-cli shutdown

Firewall Command Expansion

启动防火墙:systemctl start firewalld 
关闭防火墙:systemctl stop firewalld 
检查防火墙状态:systemctl status firewalld 
设置开机启用防火墙:systemctl enable firewalld.service 
设置开机禁用防火墙:systemctl disable firewalld.service · 
关闭防火墙:systemctl stop firewalld 
设置开机禁用防火墙:systemctl disable firewalld.service

1.2.4 redis switch

The first type: after exiting the client

[root@lixp redis-7.0.5]#  redis-cli shutdown

The second: directly in the client (recommended)

127.0.0.1:6379>shutdown

1.3 redis visualization tool installation

Redis Desktop Manager (Redis visualization tool) installation and use

Cannot connect by default

Configure the firewall to open ports

firewall-cmd --add-port=6379/tcp --permanent

firewall-cmd --remove-port=8888/tcp --permanent

firewall-cmd --list-ports

The redis service has a protection mechanism by default. The default mechanism rule is that only the computer with the redis service currently installed is allowed to access 127.0.0.1

By default, redis has an IP address bound to 127.0.0.1 in the configuration file. We need to comment out this address to support remote access.

Restart the redis service


insert image description here

1.4 getting started with redis

1.4.1 About redis thread problem

Redis is single-threaded, so why is single-threaded so fast?

The data structure of Redis is not all simple Key-Value, but also complex structures such as list and hash. These structures may perform very fine-grained operations, such as adding an element to the end of a long list, adding or removing an object in a hash. These operations may require a lot of locks, resulting in a greatly increased synchronization overhead. In short, in the case of a single thread, the code is clearer, the processing logic is simpler, there is no need to consider various locks, there is no lock release operation, there is no performance consumption due to possible deadlocks, and there is no multiple locks. Switching caused by processes or multithreading consumes CPU. Single-threaded multi-process cluster solution The power of a single thread is actually very powerful, and the efficiency per core is also very high. Multi-threading can naturally have a higher performance limit than single-threading, but in today's computing environment, even the upper limit of single-machine multi-threading often cannot meet the needs. What needs to be further explored is the solution of multi-server clustering. The multi-threading technology in the plan is still not available. So a single-threaded, multi-process cluster is a fashionable solution. CPU consumption is single-threaded, which avoids unnecessary context switching and race conditions, and does not consume CPU due to switching caused by multi-process or multi-threading. But what if the CPU becomes the bottleneck of Redis, or if you don't want to idle the other CPU cores of the server? Several Redis processes can be considered. Redis is a key-value database, not a relational database, and there is no constraint between data. As long as the client can distinguish which keys are placed in which Redis process.

1.4.3 redis basic commands

Database switching operation

127.0.0.1:6379> PING #检查连接 
PONG 
127.0.0.1:6379> SELECT 15
OK 
127.0.0.1:6379[15]> SELECT 16 #一共16个库 0-15
(error) ERR DB index is out of range 
127.0.0.1:6379[15]> SELECT 0 #默认是0 不显示索引
OK 
127.0.0.1:6379>

view current database

127.0.0.1:6379> SET student zhangsan #增加一个数据
OK
127.0.0.1:6379> SET student lisi #相同key增加一个数据 会覆盖掉
OK
127.0.0.1:6379> GET student # 取值
"lisi"
127.0.0.1:6379> KEYS * # 查看所有的key
1) "list11"
2) "student"
3) "user:1:age"
4) "user:1:name"
127.0.0.1:6379>

Clear the data in the database

flushdb deletes the current database – the premise is to enter the current database

flushall removes all libraries

127.0.0.1:6379> SET student zhangsan #在索引0库加信息
OK
127.0.0.1:6379> SET teacher lisi #在索引0库加信息
OK
127.0.0.1:6379> select 5 #切换索引为5的库
OK
127.0.0.1:6379[5]> SET user wangwu #在索引5库加信息
OK
127.0.0.1:6379[5]> KEYS *
1) "user"
127.0.0.1:6379[5]> FLUSHDB #清除当前库
OK
127.0.0.1:6379[5]> keys *
(empty array)
127.0.0.1:6379[5]> SELECT 0
OK
127.0.0.1:6379> KEYS *
4.2.4 设置过期时间
4.2.5 查看key类型
4.2.5 查看key是否存在
5. redis数据类型
redis存在5种基本数据类型和三种特殊类型
1) "teacher"
2) "student"
127.0.0.1:6379> SELECT 5
OK
127.0.0.1:6379[5]> FLUSHALL #清空所有的库信息
OK
127.0.0.1:6379[5]> SELECT 0
OK
127.0.0.1:6379> KEYS *
(empty array)
127.0.0.1:6379>

set expiration time

127.0.0.1:6379> set student jiazong
OK
127.0.0.1:6379> EXPIRE student 30 #设置过期 秒数
(integer) 1
127.0.0.1:6379> TTL student
(integer) 18
127.0.0.1:6379> TTL student
(integer) 16
127.0.0.1:6379> TTL student #查看过期时间
(integer) 14
127.0.0.1:6379> TTL student
(integer) 7
127.0.0.1:6379> TTL student
(integer) -2
127.0.0.1:6379> get student
(nil)
127.0.0.1:6379>

View key type

type key

Check if the key exists

EXISTS key

(integer) 1
127.0.0.1:6379> TTL student
(integer) 18
127.0.0.1:6379> TTL student
(integer) 16
127.0.0.1:6379> TTL student #查看过期时间
(integer) 14
127.0.0.1:6379> TTL student
(integer) 7
127.0.0.1:6379> TTL student
(integer) -2
127.0.0.1:6379> get student
(nil)
127.0.0.1:6379>


 查看key类型

```shell
type key

Check if the key exists

EXISTS key

Guess you like

Origin blog.csdn.net/weixin_52859229/article/details/129757611