<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • 聊一聊Centos7中的Redis(非關(guān)系型數(shù)據(jù)庫)

            博文目錄
            一、關(guān)系型數(shù)據(jù)庫與非關(guān)系型數(shù)據(jù)庫
            二、redis數(shù)據(jù)庫簡介
            三、Redis安裝部署
            四、Redis命令工具

            創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設計制作、網(wǎng)站設計與策劃設計,綏江網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十余年,網(wǎng)設計領(lǐng)域的專業(yè)建站公司;建站業(yè)務涵蓋:綏江等地區(qū)。綏江做網(wǎng)站價格咨詢:18982081108

            Redis數(shù)據(jù)庫是一個非關(guān)系型數(shù)據(jù)庫,在討論Redis之前,我們先了解關(guān)系型數(shù)據(jù)庫與非關(guān)系型數(shù)據(jù)庫的概念。

            一、關(guān)系型數(shù)據(jù)庫與非關(guān)系型數(shù)據(jù)庫

            數(shù)據(jù)庫按照其結(jié)構(gòu)可以分為關(guān)系型數(shù)據(jù)庫與其他數(shù)據(jù)庫,而這些其他數(shù)據(jù)庫我們將其統(tǒng)稱為非關(guān)系型數(shù)據(jù)庫。

            1、關(guān)系型數(shù)據(jù)庫

            關(guān)系型數(shù)據(jù)庫是一個結(jié)構(gòu)化的數(shù)據(jù)庫,創(chuàng)建在關(guān)系模型基礎(chǔ)上,一般面向記錄。它借助于集合代數(shù)等數(shù)學概念和方法來處理數(shù)據(jù)庫中的數(shù)據(jù)。關(guān)系模型指二維表格模型,因而一個關(guān)系型數(shù)據(jù)庫就由二維表及其之間的聯(lián)系組成的一個數(shù)據(jù)組織。實際中,各種實體與實體之間的各種聯(lián)系都可以用關(guān)系模型來表示。SQL語句就是一種基于關(guān)系型數(shù)據(jù)庫的語言,用于執(zhí)行對關(guān)系型數(shù)據(jù)庫中數(shù)據(jù)中數(shù)據(jù)的檢索和操作。

            主流的關(guān)系型數(shù)據(jù)庫包括Oracle、MySQL、SQL Server、Microsoft Access、DB2等。

            2、非關(guān)系型數(shù)據(jù)庫

            NOSQL(NoSQL = Not Only SQL),意思是“不僅僅是SQL”,是非關(guān)系型數(shù)據(jù)庫的總稱。主流的NoSql數(shù)據(jù)庫有Redis、MongBD、CouhDB等。以上這些數(shù)據(jù)庫,它們的存儲方式、存儲結(jié)構(gòu)以及使用的場景都是完全不同的。所有我們認為它是一個非關(guān)系型數(shù)據(jù)庫的集合,而不是像關(guān)系型數(shù)據(jù)庫一樣,是一個統(tǒng)稱。換言之,主流的關(guān)系型數(shù)據(jù)庫以外的數(shù)據(jù)庫,都是非關(guān)系型的。NoSQL數(shù)據(jù)庫憑借著其非關(guān)系型、分布式、開源和橫向擴展等優(yōu)勢,被認為是下一代數(shù)據(jù)庫產(chǎn)品。

            3、非關(guān)系型數(shù)據(jù)庫可以解決關(guān)系型數(shù)據(jù)庫產(chǎn)生的三高問題

            • High performance——對數(shù)據(jù)庫高并發(fā)讀寫需求;

            • Huge Storage——對海量數(shù)據(jù)高效存儲與訪問需求;

            • High Scalability && High Availability——對數(shù)據(jù)庫高可擴展性與高可用性需求;

            關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫都有各自的特點與應用場景,兩者精密結(jié)合將會給Web 2.0的數(shù)據(jù)庫發(fā)展帶來新的思路。讓關(guān)系型數(shù)據(jù)庫關(guān)注在關(guān)系上,非關(guān)系型數(shù)據(jù)庫關(guān)注在存儲上。

            二、Redis數(shù)據(jù)庫簡介

            Redis是一個開源的、使用C語言編寫、支持網(wǎng)絡、可基于內(nèi)存也可持久化的日志類型、key-value(鍵值對)數(shù)據(jù)庫,是目前分布式架構(gòu)中不可或缺的一環(huán)。

            Redis服務器是單進程模型,也就是在一臺服務器上可以同時啟動多個Redis進程,而Redis的實際處理速度則完全依靠主進程的執(zhí)行效率。若在服務器上只運行一個Redis進程,當多個客戶端同時訪問時,服務器的處理能力會有一定程度的下降;若在同一臺服務器上開啟多個Redis進程,Redis在提高并發(fā)處理能力的同時會給服務器的CPU造成很大的壓力。也就是說,在實際生產(chǎn)環(huán)境中,需要根據(jù)實際的需求來決定開啟多少個Redis進程。若對高并發(fā)要求更高些,可能會考慮在同一臺服務器上開啟多個進程;若CPU資源比較緊張,采用單進程即可。

            Redis具有以下幾個特點:

            • 具有極高的讀寫速度,數(shù)據(jù)讀取的速度最高可達到110000次/s,數(shù)據(jù)的寫入速度最高可以達到81000次/s;

            • 支持豐富的數(shù)據(jù)類型,不僅僅支持簡單的key-value數(shù)據(jù)類型,還支持strings、lists、hashes、sets及ordered sets等數(shù)據(jù)類型操作;

            • 支持數(shù)據(jù)的持久化,可以將內(nèi)存中的數(shù)據(jù)保存在磁盤中,重啟的時候可以再次加載進行使用;

            • 原子性,Redis所有操作都是原子性的;

            • 支持數(shù)據(jù)備份,即master-salve模式的數(shù)據(jù)備份;

            Redis作為基于內(nèi)存運行的數(shù)據(jù)庫,緩存是其比較常應用的場景之一,除此之外,Redis常見應用場景還包括獲取最新N個數(shù)據(jù)的操作、排行榜類應用、計數(shù)器應用、存儲關(guān)系、實時分析系統(tǒng)、日志記錄等。

            三、Redis安裝部署

            Redis官網(wǎng):https://www.redis.io 或者:https://pan.baidu.com/s/1xdDZzTGdOOWFwpJtZtwRaw
            提取碼:xuqg 下載相應的源碼軟件包,然后上傳至Linux系統(tǒng)的服務器中進行解壓、安裝。

            通常情況下在Linux系統(tǒng)中進行源碼編譯安裝,需要先執(zhí)行./configure進行環(huán)境檢查與配置,從而生成Makefile文件,再執(zhí)行make &&make install命令進行編譯安裝。而Redis源碼包中直接提供了Makefile文件,所以再解壓完軟件包后,可直接進入解壓縮后的軟件包目錄,執(zhí)行make與make install命令進行安裝即可。

            [root@centos01 ~]# tar zxvf redis-3.2.9.tar.gz -C /usr/src/<!--解包-->
            [root@centos01 ~]# mv /usr/src/redis-3.2.9/ /usr/src/redis/   
                      <!--將redis所有配置文件剪切到/usr/src/redis/目錄-->
            [root@centos01 ~]# cd /usr/src/redis/    <!--進入redis目錄-->
            [root@centos01 redis]# ls
            00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
            BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
            CONTRIBUTING     INSTALL  README.md  runtest-cluster  src
            [root@centos01 redis]# make && make install <!--編輯及安裝redis--> 
            <!--(若想更改安裝路徑,可以使用make PREFIX=安裝路徑 install命令格式來進行安裝)-->

            make install只是安裝了二進制文件到系統(tǒng)中,并沒有啟動腳本和配置文件。軟件包中默認提供了一個install_server.sh腳本文件,通過該腳本文件可以設置Redis服務所需要的相關(guān)配置文件。當腳本運行完畢,Redis服務就已經(jīng)啟動,默認偵聽端口為6379。

            [root@centos01 ~]# cd /usr/src/redis/utils/   <!--進入utils目錄-->
            [root@centos01 utils]# ./install_server.sh   <!--初始化redis-->
            Welcome to the redis service installer
            This script will help you easily set up a running redis server
            
            Please select the redis port for this instance: [6379]<!--回車鍵即可-->
            Selecting default: 6379
            Please select the redis config file name [/etc/redis/6379.conf] 
                                             <!--回車鍵即可-->
            Selected default - /etc/redis/6379.conf
            Please select the redis log file name [/var/log/redis_6379.log]   
                                            <!--回車鍵即可-->
            Selected default - /var/log/redis_6379.log
            Please select the data directory for this instance [/var/lib/redis/6379]            
                                             <!--回車鍵即可-->
            Selected default - /var/lib/redis/6379
            Please select the redis executable path [/usr/local/bin/redis-server]  
                                               <!--回車鍵即可-->
            Selected config:
            Port           : 6379       <!--端口號,默認允許6379-->
            Config file    : /etc/redis/6379.conf   <!--redis主配置文件-->
            Log file       : /var/log/redis_6379.log   <!--redis日志文件-->
            Data dir       : /var/lib/redis/6379       <!--設置數(shù)據(jù)目錄-->
            Executable     : /usr/local/bin/redis-server   <!--執(zhí)行命令-->
            Cli Executable : /usr/local/bin/redis-cli       <!--客戶端命令-->
            Is this ok? Then press ENTER to go on or Ctrl-C to abort.
            Copied /tmp/6379.conf => /etc/init.d/redis_6379
            Installing service...
            Successfully added to chkconfig!
            Successfully added to runlevels 345!
            Starting Redis server...
            Installation successful!
            [root@centos01 ~]# netstat -anptu | grep redis<!--監(jiān)聽redis是否啟動-->
            tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5141/redis-server 1

            安裝完成后,可通過Redis的服務控制腳本/etc/init.d/redis_6379來對Redis服務進行控制,如停止Redis服務、啟動Redis服務、重啟Redis服務、查看Redis運行狀態(tài)。

            [root@centos01 ~]# /etc/init.d/redis_6379 start   <!--啟動Refis服務-->
            [root@centos01 ~]# /etc/init.d/redis_6379 stop      <!--停止Redis服務-->
            [root@centos01 ~]# /etc/init.d/redis_6379 restart <!--重啟Redis服務-->
            [root@centos01 ~]# /etc/init.d/redis_6379 status    <!--查看Redis運行狀態(tài)-->

            1、配置參數(shù)

            Redis主配置文件為/etc/redis/6379.conf,由注釋行與設置行兩部分組成。與大多數(shù)Linux配置文件一樣,注釋性的文字以“#”開始,包含了對相關(guān)配置內(nèi)容進行的說明和解釋。注釋行與空行以外的內(nèi)容即為設置行。可根據(jù)生產(chǎn)環(huán)境的需求調(diào)整相關(guān)參數(shù)。如:

            [root@centos01 ~]# vim /etc/redis/6379.conf  
                                   <!--修改redis主配置文件啟動群集功能-->
            62   bind 192.168.100.10   <!--監(jiān)聽的主機地址-->
            85   port 6379         <!--監(jiān)聽端口號-->
            129  daemonize yes   <!--啟動守護進程-->
            pidfile /var/run/redis_6379.pid    <!--指定PID文件-->
            loglevel notice            <!--日志級別-->
            logfile "/var/log/redis_6379.log"    <!--指定日志文件-->
            [root@centos01 ~]# /etc/init.d/redis_6379 restart  <!--重啟redis服務-->

            除了上述配置參數(shù)外,Redis主配置文件中還包含很多其他的配置參數(shù),具體內(nèi)如下:
            聊一聊Centos 7中的Redis(非關(guān)系型數(shù)據(jù)庫)

            四、Redis命令工具

            Redis軟件提供了多個命令工具,當Redis安裝時,所包含的軟件工具會同時被安裝到系統(tǒng)中,在系統(tǒng)中可以直接使用。這些命令工具的作用分別如下所示:

            • redis-server:用于啟動Redis的工具;

            • redis-benchmark:用于檢測Redis在本機的運行效率;

            • redis-check-aof:修復AOF持久化文件;

            • redis-check-rdb:修復RDB持久化文件;

            • redis-cli:Redis命令行工具;

            • redis-setinel:redis-server文件的軟連接;

            1、redis-cli命令行工具

            Redis數(shù)據(jù)庫系統(tǒng)也是一個典型的C/S(客戶端/服務器端)架構(gòu)的應用,要訪問Redis數(shù)據(jù)庫需要使用專門的客戶端軟件。Redis服務的客戶端軟件就是其自帶的redis-cli命令行工具。使用redis-cli連接指定數(shù)據(jù)庫,連接成功后會進入提示符為“遠程主機IP地址:端口號>” 的數(shù)據(jù)庫操作環(huán)境。
            用戶可以輸入各種操作語句對數(shù)據(jù)庫進行管理,如執(zhí)行ping命令可以檢測redis服務是否啟動。

            [root@centos01 ~]# redis-cli     <!--   登錄本地redis數(shù)據(jù)庫-->
            192.168.100.10:6379> ping     <!--檢測redis服務是否啟動-->
            PONG
            192.168.100.10:6379> exit     <!--退出數(shù)據(jù)庫-->

            在進行數(shù)據(jù)庫連接操作時,可以通過選項來指定遠程主機上的Redis數(shù)據(jù)庫,命令語法為redis-cli -h host -p port -a password。其中,“-h”指定遠程主機;“-p指定服務的端口號”,若設置了密碼,可以用“-a”指定密碼,若沒設置連接密碼,則可以省略“-a”選項。

            [root@centos01 ~]# redis-cli -h 192.168.100.10 -p 6379 
                           <!--登錄遠程主機100.10的6379端口redis實例-->
            192.168.100.10:6379> info
            # Server
            redis_version:3.2.9
            redis_git_sha1:00000000
            redis_git_dirty:0
            redis_build_id:2cc10a3065b1ebc2
            ............      <!--此處省略部分內(nèi)容-->
            db0:keys=1,expires=0,avg_ttl=0
            192.168.100.10:6379> exit   <!--退出數(shù)數(shù)據(jù)庫-->

            在數(shù)據(jù)庫操作環(huán)境中,使用help命令可以獲取命令類型的幫助。其中,有三種獲取命令幫助的方式:

            • help @<group>:獲取<group>中的命令列表;

            • help <command>:獲取某個命令的幫助;

            • help <tab>:獲取可能幫助的主題列表;

            具體操作方式如下所示:

            [root@centos01 ~]# redis-cli 
            192.168.100.10:6379> help @list  <!--查看所有與List數(shù)據(jù)類型有關(guān)的命令-->
            
              BLPOP key [key ...] timeout
              summary: Remove and get the first element in a list, or block until one is available
              since: 2.0.0
            
              BRPOP key [key ...] timeout
              summary: Remove and get the last element in a list, or block until one is available
              since: 2.0.0
            
              BRPOPLPUSH source destination timeout
              summary: Pop a value from a list, push it to another list and return it; or block until one is available
              since: 2.2.0
            
              LINDEX key index
              summary: Get an element from a list by its index
              since: 1.0.0
            
              LINSERT key BEFORE|AFTER pivot value
              summary: Insert an element before or after another element in a list
              since: 2.2.0
            
              LLEN key
              summary: Get the length of a list
              since: 1.0.0
            
              LPOP key
              summary: Remove and get the first element in a list
              since: 1.0.0
            
              LPUSH key value [value ...]
              summary: Prepend one or multiple values to a list
              since: 1.0.0
            
              LPUSHX key value
              summary: Prepend a value to a list, only if the list exists
              since: 2.2.0
            
              LRANGE key start stop
              summary: Get a range of elements from a list
              since: 1.0.0
            
              LREM key count value
              summary: Remove elements from a list
              since: 1.0.0
            
              LSET key index value
              summary: Set the value of an element in a list by its index
              since: 1.0.0
            
              LTRIM key start stop
              summary: Trim a list to the specified range
              since: 1.0.0
            
              RPOP key
              summary: Remove and get the last element in a list
              since: 1.0.0
            
              RPOPLPUSH source destination
              summary: Remove the last element in a list, prepend it to another list and return it
              since: 1.2.0
            
              RPUSH key value [value ...]
              summary: Append one or multiple values to a list
              since: 1.0.0
            
              RPUSHX key value
              summary: Append a value to a list, only if the list exists
              since: 2.2.0
            
            127.0.0.1:6379> help set   <!--查看set命令的命令幫助-->
            
              SET key value [EX seconds] [PX milliseconds] [NX|XX]
              summary: Set the string value of a key
              since: 1.0.0
              group: string
            
            127.0.0.1:6379> help get  <!--查看get命令的命令幫助-->
            
              GET key
              summary: Get the value of a key
              since: 1.0.0
              group: string

            2、redis-benchmark測試工具

            redis-benchmark是官方自帶的Redis性能測試工具,可以有效地測試Redis服務的性能。基本的測試語法為redis-benchmark [option] [option value]。常用選項如下所示:

            • -h:指定服務器主機名;

            • -p:指定服務器端口;

            • -s:指定服務器socket;

            • -c:指定并發(fā)連接數(shù);

            • -n:指定請求數(shù);

            • -d:以字節(jié)(B)的形式指定SET/GET值的數(shù)據(jù)大??;

            • -k:1=keep alive 0=reconnect;

            • -r:SET/GET/INCR 使用隨機key,SADD使用隨機值;

            • -p:通過管道傳輸<numreq>請求;

            • -q:強制退出redis。僅顯示query/sec值;

            • --csv:以CSV格式輸出;

            • -l:生成循環(huán),永久執(zhí)行測試;

            • -t:僅運行以逗號分隔的測試命令列表;

            • -i:idle模式。僅打開N個idle連接并等待;

            結(jié)合上述選項,可以針對某臺Redis服務器進行性能檢測,如執(zhí)行redis-benchmark -h 192.168.100.10 -p 6379 -c 100 -n 10000命令即可向IP地址192.168.100.10、端口為6379的Redis服務器發(fā)送100個并發(fā)連接與10000個請求測試性能:

            [root@centos01 ~]# redis-benchmark -h 192.168.100.10 -p 6379 -c 100 -n 10000
            …………      <!--此處省略部分內(nèi)容-->
            5165.29 requests per second
            
            ====== MSET (10 keys) ======
              10000 requests completed in 0.18 seconds
              100 parallel clients
              3 bytes payload
              keep alive: 1
            
            0.01% <= 1 milliseconds
            98.93% <= 2 milliseconds
            100.00% <= 2 milliseconds
            55555.55 requests per second

            執(zhí)行redis-benchmark -h 192.168.100.10 -p 6379 -q -d 100命令的作用是測試存取大小為100B的數(shù)據(jù)包性能:

            [root@centos01 ~]# redis-benchmark -h 192.168.100.10 -p 6379 -q -d 100
            PING_INLINE: 100603.62 requests per second
            PING_BULK: 98231.83 requests per second
            SET: 87412.59 requests per second
            GET: 90991.81 requests per second
            INCR: 94786.73 requests per second
            LPUSH: 86206.90 requests per second
            RPUSH: 82987.55 requests per second
            LPOP: 84245.99 requests per second
            RPOP: 87108.02 requests per second
            SADD: 90744.10 requests per second
            SPOP: 90661.83 requests per second
            LPUSH (needed to benchmark LRANGE): 85251.49 requests per second
            LRANGE_100 (first 100 elements): 23912.00 requests per second
            LRANGE_300 (first 300 elements): 6502.37 requests per second
            LRANGE_500 (first 450 elements): 3816.07 requests per second
            LRANGE_600 (first 600 elements): 2778.09 requests per second
            MSET (10 keys): 54141.85 requests per second

            還可以測試某些操作的性能,例如,執(zhí)行redis-benchmark -t set,lpush -n 100000 -q命令的作用是測試本機上Redis服務在運行set與lpush操作時的性能:

            [root@centos01 ~]# redis-benchmark -t set,lpush -n 100000 -q
            SET: 81037.28 requests per second
            LPUSH: 88652.48 requests per second

            五、Redis數(shù)據(jù)庫常用命令

            Redis數(shù)據(jù)庫采用key-value(鍵值對)的數(shù)據(jù)存儲形式,所使用的命令是set和get。

            • set:存放數(shù)據(jù),命令格式為set key value;

            • get:獲取數(shù)據(jù),命令格式為get key;

            例如,在Redis的命令行模式下執(zhí)行“set xingming liyanxin”,表示在當前數(shù)據(jù)庫下存放一個key為xingming,value為liyanxin的數(shù)據(jù),而執(zhí)行“get xingming”命令即可查看存放的數(shù)據(jù)。

            [root@centos01 ~]# redis-cli 
            192.168.100.10:6379> set xingming liyanxin
            OK
            192.168.100.10:6379> set xingbie nan
            OK
            192.168.100.10:6379> get xingming
            "liyanxin"
            192.168.100.10:6379> get xingbie
            "nan"

            1、Key相關(guān)命令

            1)keys

            *使用keys命令可以獲取符合規(guī)則的鍵值列表,通常情況可以結(jié)合、?等選項來使用。**

            192.168.100.10:6379> set k1 1
            OK
            192.168.100.10:6379> set k2 2
            OK
            192.168.100.10:6379> set v1 3
            OK
            192.168.100.10:6379> set v2 4
            OK
            192.168.100.10:6379> set v33 6
            OK
            192.168.100.10:6379> KEYS *  
                       <!--查看當前數(shù)據(jù)庫中所有鍵-->
            1) "xingming"
            2) "xingbie"
            3) "k1"
            4) "k2"
            5) "v1"
            6) "v2"
            7) "v33"
            192.168.100.10:6379> KEYS v*  
                   <!--查看當前數(shù)據(jù)庫中以v開頭的所有數(shù)據(jù)-->
            1) "v1"
            2) "v2"
            3) "v33"
            192.168.100.10:6379> KEYS v? 
                     <!--查看當前數(shù)據(jù)庫中以v開頭后面包含任意一位的數(shù)據(jù)-->
            1) "v1"
            2) "v2"
            192.168.100.10:6379> KEYS v??
                       <!--查看當前數(shù)據(jù)庫中以v開頭后面包含任意兩位的數(shù)據(jù)-->
            1) "v33"
            192.168.100.10:6379> KEYS k* 
                                 <!--查看當前數(shù)據(jù)庫中以k開頭的所有數(shù)據(jù)-->
            1) "k2"
            2) "k1"
            192.168.100.10:6379> KEYS k?
                    <!--查看當前數(shù)據(jù)庫中以k開頭后面包含任意一位的數(shù)據(jù)-->
            1) "k2"
            2) "k1"

            2)exists

            使用exists命令可以判斷鍵值是否存在。

            192.168.100.10:6379> EXISTS xingming
            (integer) 1
            192.168.100.10:6379> EXISTS xingbie
            (integer) 1
            192.168.100.10:6379> EXISTS danwei
            (integer) 0
            192.168.100.10:6379> EXISTS nianling
            (integer) 0

            3)del

            使用del命令可以刪除當前數(shù)據(jù)庫的指定key。

            192.168.100.10:6379> KEYS *  <!--查看當前數(shù)據(jù)庫中所有鍵-->
            1) "xingming"
            2) "xingbie"
            3) "k1"
            4) "k2"
            5) "v1"
            6) "v2"
            7) "v33"
            192.168.100.10:6379> del k2  <!--刪除當前數(shù)據(jù)庫中的k2鍵-->
            (integer) 1
            192.168.100.10:6379> del v2  <!--刪除當前數(shù)據(jù)庫中的v2鍵-->
            (integer) 1

            4)type

            使用type命令可以獲取key對應的value值類型。

            192.168.100.10:6379>> type v33
            string
            192.168.100.10:6379> type xingbie
            string

            5)rename

            使用rename命令對已有key進行重命名。(使用rename命令進行重命名時,無論目標key是否存在都進行重命名,且源key的值會覆蓋目標key的值;在實際工作當中,建議先使用exists命令查看目標key是否存在,再決定是否執(zhí)行rename命令,以避免覆蓋重要數(shù)據(jù))。

            192.168.100.10:6379> KEYS *  <!--查看當前數(shù)據(jù)庫中所有鍵-->
            1) "xingming"
            2) "xingbie"
            3) "k1"
            4) "k2"
            5) "v1"
            6) "v2"
            7) "v33"
            192.168.100.10:6379> rename v33 v3
            OK
            192.168.100.10:6379> keys v*
            1) "v1"
            2) "v3"
            192.168.100.10:6379> get v1
            "3"
            192.168.100.10:6379> get v3
            "6"
            192.168.100.10:6379> rename v1 v3
            OK
            192.168.100.10:6379> get v1
            (nil)
            192.168.100.10:6379> get v3
            "3"

            6)renamenx

            使用renamenx命令對已有key進行重命名,并檢測新名是否存在。(使用renamenx命令進行重命名時,如果目標key存在則不進行重命名)。

            192.168.100.10:6379> KEYS *  <!--查看當前數(shù)據(jù)庫中所有鍵-->
            1) "xingming"
            2) "xingbie"
            3) "k1"
            4) "v3"
            192.168.100.10:6379> get xingming
            "liyanxin"
            192.168.100.10:6379> get k1
            "1"
            127.0.0.1:6379> renamenx k1 xingming
            (integer) 0
            192.168.100.10:6379> KEYS *  <!--查看當前數(shù)據(jù)庫中所有鍵-->
            1) "xingming"
            2) "xingbie"
            3) "k1"
            4) "v3"
            192.168.100.10:6379> get k1
            "1"

            7)dbsize

            使用dbsize命令查看當前數(shù)據(jù)庫中key的數(shù)目。

            192.168.100.10:6379> dbsize
            (integer) 6

            2、多數(shù)據(jù)庫常用命令

            1)多數(shù)據(jù)庫間切換

            Redis支持多數(shù)據(jù)庫,Redis在沒有任何改動的情況下默認包含16個數(shù)據(jù)庫,數(shù)據(jù)庫名使用數(shù)字0--15來依次命名。使用select命令可以進行Redis的多數(shù)據(jù)庫之間的切換,命令格式為“select index”。其中,index表示數(shù)據(jù)庫的序號。而使用redis-cli連接redis數(shù)據(jù)庫后,默認使用的是序列號為0的數(shù)據(jù)庫

            如下所示,使用select命令切換數(shù)據(jù)庫后,會在前端的提示符中顯示當前所在的數(shù)據(jù)庫序號,如“192.168.100.10:6379 [5]>”表示當前使用的是序列號為5的數(shù)據(jù)庫;若當前使用的數(shù)據(jù)庫是序列號為0的數(shù)據(jù)庫,提示符中則不顯示序號,如“192.168.100.10:6379>”表示當前使用的是序號為0的數(shù)據(jù)庫。

            192.168.100.10:6379> select 5  <!--切換至序列號為5的數(shù)據(jù)庫-->
            OK
            192.168.100.10:6379 [5]> keys *   <!--查看所有鍵-->
            (empty list or set)
            192.168.100.10:6379 [5]> select 10 <!--切換至序列號為10的數(shù)據(jù)庫-->
            OK
            192.168.100.10:6379 [10]> keys *   
            (empty list or set)
            192.168.100.10:6379 [10]> select 0 <!--切換至序列號為0的數(shù)據(jù)庫-->
            OK
            192.168.100.10:6379> keys *   <!--查看所有鍵-->
            1) "k1"
            2) "xingming"
            3) "v3"
            4) "key:__rand_int__"
            5) "xingbie"
            6) "mylist"

            2)多數(shù)據(jù)庫間移動數(shù)據(jù)

            Redis的多數(shù)據(jù)庫在一定程度上是相當獨立的。例如,在數(shù)據(jù)庫0上面存放的w1的數(shù)據(jù),在其它的1~15的數(shù)據(jù)庫上是無法看到的。

            192.168.100.10:6379> set w1 100    <!--在數(shù)據(jù)庫0上面存放w1的數(shù)據(jù)-->
            OK
            192.168.100.10:6379> get w1    <!--在當前數(shù)據(jù)庫可以看到-->
            "100"
            192.168.100.10:6379> select 1   <!--切換到數(shù)據(jù)庫1-->
            OK
            192.168.100.10:6379 [1]> get w1  <!--查看w1數(shù)據(jù),顯然是看不到的-->
            (nil)
            192.168.100.10:6379 [1]> select 0  <!--切換回數(shù)據(jù)庫0-->
            OK
            192.168.100.10:6379> get w1   <!--查看w1數(shù)據(jù),可以看到 -->
            "100"
            192.168.100.10:6379> move w1 1    <!--將數(shù)據(jù)庫0中的w1移動到數(shù)據(jù)庫1中-->
            (integer) 1
            192.168.100.10:6379> select 1    <!--切換到數(shù)據(jù)庫1-->
            OK
            192.168.100.10:6379 [1]> get w1  <!--查看被移動的數(shù)據(jù) -->
            "100"
            192.168.100.10:637979[1]> select 0    <!--切換回數(shù)據(jù)庫0-->
            OK
            192.168.100.10:6379> get w1        <!--在數(shù)據(jù)庫0中無法查看到w1的值-->

            3)清除數(shù)據(jù)庫內(nèi)數(shù)據(jù)

            Redis數(shù)據(jù)庫的整庫數(shù)據(jù)刪除主要分為兩個部分:清空當前數(shù)據(jù)庫中的數(shù)據(jù),使用flushdb命令實現(xiàn);清空所有庫中的數(shù)據(jù),使用flushall命令。數(shù)據(jù)庫清空操作比較危險,生產(chǎn)環(huán)境下一般不建議使用!

            192.168.100.10:6379> FLUSHALL
            OK

            —————— 本文至此結(jié)束,感謝閱讀 ——————

            分享名稱:聊一聊Centos7中的Redis(非關(guān)系型數(shù)據(jù)庫)
            文章出自:http://www.jbt999.com/article34/gsedpe.html

            成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、網(wǎng)站維護網(wǎng)頁設計公司、網(wǎng)站內(nèi)鏈、網(wǎng)站改版移動網(wǎng)站建設

            廣告

            聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

            商城網(wǎng)站建設

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 中文字幕综合在线观看 | 国产白嫩精品久久久久久 | 12一15女人a毛片13 | 免费毛片视频 | 97人妻精品一区二区三区香蕉 |