SQL (关系型数据库)VS NoSQL(非关系型数据库)
SQL | NoSQL |
---|---|
RELATIONAL DATABASE MANAGEMENT SYSTEM (RDBMS) | Non-relational or distributed database system. |
These databases have fixed or static or predefined schema | They have dynamic schema |
These databases are not suited for hierarchical data storage. | These databases are best suited for hierarchical data storage. |
These databases are best suited for complex queries | These databases are not so good for complex queries |
Vertically Scalable | Horizontally scalable |
Follows ACID property | Follows CAP(consistency, availability, partition tolerance) |
Redis=Remote Dictionary Server 远程词典服务器
2009年开发的,键值型NoSQL数据库.
特征:
-
键值型
-
单线程,每个命令具有原子性
-
低延迟,速度快(原因:基于内存,IO多路复用,良好的编码)
-
支持数据持久化
-
支持主从集群,分片集群
-
支持多语言客户端
Installation(base on Linux System):
yum install -y gcc tcl
上传/usr/local/src目录
- 解压
tar -zxvf redis.tar.gz
- 进入redis目录:
cd redies
- 运行编译命令:
make && make install
- 运行
redis-server
- 设置后台启动需配置redis.conf
backup conf file to avoid any accident:
cp redis.conf redis.conf.bck
修改如下参数:
#default 127.0.0.1 can only allow localhost access,use 0.0.0.0 can allow any other IP wants to access
bind 0.0.0.0
# yes to make background service avaiable
daemonize yes
#password for access to Redis
requirepass *******
#easy to understand.___why6379:6379 在是手机按键上字母 MERZ 对应的号码。 而 MERZ 取自意大利歌女 Alessia Merz 的名字。 MERZ长期以来被 Redis 作者 antirez 及其朋友当作愚蠢的代名词。 后来 Redis 作者在开发 Redis 时就选用了这个端口~~
port 6379
#workspace,default current catlogs you use command redis-server,will restore logs and persitence files here.
dir .
# database qty, setting to "1" only use only 1 base.By default,there are 16 pcs of bases marking from 0 to 15.
databses 1
#The max memory to allow redis to use
maxmemory 512mb
#log names,defult as null and not recording the logs.Can record once set a name
logfile "redis.log"
- quick to find config name:
#/configname
#eg:
/daemonize
- to start Redis:
# enter the root of redis
cd /user/local/src/redis
# start
redis-server redis.conf
- to see the status of redis
ps -ef | grep redis
- to kill/end the process
kill -9 PID
kill -9中,9代表的就是9号信号,带有强制执行的意思,它告诉进程:“无论你现在在做什么,立刻停止”。
Make redis start with System
- create a service file firstly
vi /etc/systemd/system/redis.service
- add those content to the file
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server/usr/local/src/redis-6.2.6/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- reload the system service
#make background service reload
systemctl daemon-reload
#then to start
systemctl start redis
#to check the status of redis
systemctl status redis
#to stop redis
systemctl stop redis
- finally to enable redis to start with system
systemctl enable redis
#then to start redis
systemctl start redis
Redis命令行客户端(example:login)
#for login:
redis-cli [options][commands]
#redis-cli -h -p
#redis-cli -h 127.0.0.1 -p 6379 -a ******
COMMAND
- SELECT——Select database
SELECT 0~15
- GET——To get value via KEY
GET $KEY
- set——Set KEY-VALUE:
set $KEY $VALUE
#set name jack
# it means that key is "name", and value is "jack"
- help——get help gude,we can use this command to know how to use the command
help [@command]
#to add a # to see details
help @**
- KEYS——search by KEYS name
KEYS *
KEYS a*
KEYS *a*
#it's not recommended to use KEYS to query in production environment.
- DEL——to delet a key
DEL $KEY
- MSET——to muti-add keys
MSET $KEY1 $VALUE1 $KEY2 $VALUE2 $KEY3 $VALUE3
#this command will reture the qty of successfull result.
- MGET——to muti-get keys
MGET $KEY1 $KEY2 $KEY3
- EXISTS——to judge if the key exist
EXISTS $KEY
#will return 1 or 0 ,0 represents $KEY not exists, 1 represents $KEY exists actually
- EXPIRE——to set a timelimit to make a KEY out of date and removed. we usually use “TTL” command to see the rest lifetime for a key.
EXPIRE $KEY $SECONDS
#EXPIRE name 20
TTL $KEY
#to see the key's life time. "-1" means forever exist;"-2" means removed already;
- INCR & INCRBY——defaultly increase 1, use incrby to set value to incrase
#age will increase 1
incr age
#age will increase 2
incrby age 2
- **SETNX **&SETX ——set a key-value if only when the key doesn’t exist,SETX means set a key-value with lifetime
SETNX $KEY $VALUE
SETEX $KEY $SECONDS $VALUE
#SETEX name 20 KKK ,means a key"name",value"KKK",expire 20s
==> set $KEY $VALUE ex $SECONDS
PS: 以·tar.gz为后缀的文件是一种压缩文件,在Linux和macOS下常见,Linux和macOS都可以直接解压使用这种压缩文件。eg: tar -zxf redis-6.0.8.tar.gz