Redis(二) redis基本应用

剛才我們已經介紹了Redis的一些基礎特點,接下來我們來繼續說Redis的應用,要想安裝配置使用Redis到官方站點去下載程序包就可以了,訪問"https://redis.io",各位看目前的最新版本是6.0.8:

image.png

不過呢我們說過很多次,如果對應的程序包隨系統發行光盤提供的有,而且那個對應的版本沒有顯著的漏洞,相比較新版本而言只是少了一些特性而已但足以滿足我們的需要,那麼很顯然就沒有太大的動力使用最新版,不必要去當小白鼠對吧?

好的,那麼對於我們這的Redis而言呢隨系統發行光盤它並沒有提供的話,就可以看一下在我們的EPEL(Extra Packages for Enterprise Linux)倉庫或者是在CentOS官方的其他的倉庫當中像Extra等等看看是否有,如果有的話建議各位去採用它們,那我們這呢就以node1為例,使用yum info redis告訴我們說在EPEL倉庫中存的有這麼一個對應的程序包,License是BSD的,版本是3.2.12,大小是544 k可以知道Redis其實是非常輕量的,那EPEL倉庫提供的比官方提供的是比較舊了:

[root@node1 ~]# yum info redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.mirror.hostinginside.com
 * epel: d2lzkl7pfhq30w.cloudfront.net
 * extras: centos.mirror.hostinginside.com
 * updates: centos.mirror.hostinginside.com
Available Packages
Name        : redis
Arch        : x86_64
Version     : 3.2.12
Release     : 2.el7
Size        : 544 k
Repo        : epel/x86_64
Summary     : A persistent key-value database
URL         : http://redis.io
License     : BSD
Description : Redis is an advanced key-value store. It is often referred to as a data
            : structure server since keys can contain strings, hashes, lists, sets and
            : sorted sets.
            : 
            : You can run atomic operations on these types, like appending to a string;
            : incrementing the value in a hash; pushing to a list; computing set
            : intersection, union and difference; or getting the member with highest
            : ranking in a sorted set.
            : 
            : In order to achieve its outstanding performance, Redis works with an
            : in-memory dataset. Depending on your use case, you can persist it either
            : by dumping the dataset to disk every once in a while, or by appending
            : each command to a log.
            : 
            : Redis also supports trivial-to-setup master-slave replication, with very
            : fast non-blocking first synchronization, auto-reconnection on net split
            : and so forth.
            : 
            : Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
            : limited time-to-live, and configuration settings to make Redis behave like
            : a cache.
            : 
            : You can use Redis from most programming languages also.

我們這裡使用EPEL倉庫提供的,執行yum install redis -y:

[root@node1 ~]# yum install redis -y

安裝完以後執行rpm -ql redis來看生成了那些文件,各位看文件其實非常少。主程序文件叫/usr/bin/redis-server,關閉Redis的叫/usr/bin/redis-shutdown,如果我們打算為Redis的主從複製集群提供一個類似MHA一樣的解決方案,這裡有/usr/bin/redis-sentinel,Redis的客戶端程序,像MySQL客戶端一樣叫/usr/bin/redis-cli,Redis有兩個檢測工具叫/usr/bin/redis-check-aof、/usr/bin/redis-check-rdb但我們很少手動去執行它們,而評估Redis的性能則使用/usr/bin/redis-benchmark。主配置文件叫/etc/redis.conf,如果這個Redis運行的是redis-sentinel,那麼它的配置文件是/etc/redis-sentinel.conf,如果要啟動服務因為我們這是CentOS 7,所以要用到的是Unit File叫/usr/lib/systemd/system/redis.service,如果是哨兵服務則用到的是/usr/lib/systemd/system/redis-sentinel.service。注意,Redis的數據存在哪?因為它是個持久存儲,大家知道rpm包安裝的CentOS系列的風格當中數據一般在哪阿?/var/lib目錄,所以這的數據存放也是一樣在/var/lib/redis,而且日誌文件在/var/lob/redis。rpm包安裝這種組織管理方式易於部署,但是我們將來在遷移不太方便,至少你遷移程序不方便,遷移數據還算可以。好,我們也說過很多次,將來真正用的時候你不可能也不應該把數據放在默認的/var/lib/redis路徑下對不對?我們應該去修改它們的,那怎麼去修改呢?只需要去編輯配置文件即可:

[root@node1 ~]# rpm -ql redis
/etc/logrotate.d/redis
/etc/redis-sentinel.conf
/etc/redis.conf
/etc/systemd/system/redis-sentinel.service.d
/etc/systemd/system/redis-sentinel.service.d/limit.conf
/etc/systemd/system/redis.service.d
/etc/systemd/system/redis.service.d/limit.conf
/usr/bin/redis-benchmark
/usr/bin/redis-check-aof
/usr/bin/redis-check-rdb
/usr/bin/redis-cli
/usr/bin/redis-sentinel
/usr/bin/redis-server
/usr/lib/systemd/system/redis-sentinel.service
/usr/lib/systemd/system/redis.service
/usr/libexec/redis-shutdown
/usr/share/doc/redis-3.2.12
/usr/share/doc/redis-3.2.12/00-RELEASENOTES
/usr/share/doc/redis-3.2.12/BUGS
/usr/share/doc/redis-3.2.12/CONTRIBUTING
/usr/share/doc/redis-3.2.12/MANIFESTO
/usr/share/doc/redis-3.2.12/README.md
/usr/share/licenses/redis-3.2.12
/usr/share/licenses/redis-3.2.12/COPYING
/usr/share/man/man1/redis-benchmark.1.gz
/usr/share/man/man1/redis-check-aof.1.gz
/usr/share/man/man1/redis-check-rdb.1.gz
/usr/share/man/man1/redis-cli.1.gz
/usr/share/man/man1/redis-sentinel.1.gz
/usr/share/man/man1/redis-server.1.gz
/usr/share/man/man5/redis-sentinel.conf.5.gz
/usr/share/man/man5/redis.conf.5.gz
/var/lib/redis
/var/log/redis
/var/run/redis

Redis的程序環境,這幾個核心大家要記清楚:

● 配置文件:/etc/redis.conf

● 主程序:/usr/bin/redis-server,tcp 6379 port

●客戶端:/usr/bin/redis-cli

● Unit File:/usr/lib/systemd/system/redis.service

● 數據目錄:/var/lib/redis

OK,安裝完以後我們就可以直接啟動它們,不過呢Redis的配置要略為麻煩一點點,它可能有很多與我們各種各樣的網路等相關的配置,但是在了解這些配置之前我們先簡單使用Redis,怎麼叫簡單使用?先了解Redis的基本的命令行接口有哪一些命令,它有哪些數據類型,我們如何在本機上簡單使用它們即可,只先了解它的接口,那因此我們這也可以直接啟動Redis,執行systemctl start redis.service,而後執行ss -tnl可以看到6379 port已經監聽了,只不過默認監聽的是本機地址,也就意謂著你的Redis服務無法向外部其他主機提供了,為什麼這麼幹呢?因為Redis此處沒有啟動任何認證功能,跟我們的MySQL很相似,root用戶是不允許遠程連接的:

[root@node1 ~]# ss -tnl
State      Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port              
LISTEN     0      128                                                 *:22                                                              *:*                  
LISTEN     0      100                                         127.0.0.1:25                                                              *:*                  
LISTEN     0      128                                         127.0.0.1:6379                                                            *:*                  
LISTEN     0      128                                              [::]:22                                                           [::]:*                  
LISTEN     0      100                                             [::1]:25                                                           [::]:*

那這裡沒有啟動認證你可以直接本機連接,執行redis-cli就連上來了:

[root@node1 ~]# redis-cli
127.0.0.1:6379>


猜你喜欢

转载自blog.51cto.com/9574347/2539794