Redis [noSQL資料庫,KV資料庫,緩存資料庫,關系資料庫]
redis開源的可基于記憶體亦可持久化的日志型、key-value資料庫,支援永久存資料
redis功能更多,支援的存儲value類型更多string、lists、sets、zsets、hashes
memcache的加強版,記憶體--->硬碟;(簡單的key-value)-->(更多詳細資訊,清單,記錄)
redis的存儲分為記憶體存儲、磁盤存儲和log檔案三部分,配置檔案中有三個參數對其進行配置。
redis會周期性的把更新的資料寫入磁盤或者把修改的操作寫入追加的記錄檔案,并且在此基礎上實作了主從同步master-slave。
(新浪的微網誌架構基于redis,文字不能超過100個字,記憶體)
源碼安裝Redis緩存服務
1.安裝Redis
#tar -xf /root/lnmp_soft/redis-3.0.6.tar.gz
#cd /opt/lnmp_soft/redis-3.0.6
# yum -y install gcc
#make && make install
#./utils/install_server.sh
【初始化---起服務,設定端口号,配置檔案,日志檔案,資料目錄,存儲路徑,啟動腳本,連結等】
# netstat -antpu | grep 6379
伺服器程式redis-server、用戶端程式redis-cli、主配置檔案
/etc/redis/redis_portnumber
#########################################
2.簡單測試(192.168.2.100)
#redis-cli /連服務,支援上下鍵Tab鍵,不區分大小寫
>set key "hello the redis" EX 5 /過期時間5秒
>get key
>incr hit
>decr hit
Redis支援的資料類型
1.字元型常用指令: <參考TTS案例>
nx 隻能建立不能覆寫;xx 隻能覆寫不能建立;ex過期時間秒或毫秒,不寫預設不過期;setrange 複寫改寫getrange;strlen統計字串長度;append追加或建立;setbit設定或清除特定量上的位;bitcount統計特定位上的數量;incr 遞增1;decr遞減1(通路量,關注的增減);incrby ab 4遞增4;bitcount qq(統計q齡);mset 一次設定多個值,空格分開;mget擷取多個設定的值;
2.hash表常用指令:<參考TTS案例>
redis hash是一個string類型的field和value的映射表;一個key可以對應多個field,一個field對應一個value;将一個對象存儲位hash類型,較于每個子段都存儲成string類型更能節省記憶體。hset、hget、hmset、hmget、hkeys
tom {age=11
addreess=beijing
phone=1333544545
sex=female}
hset lily phone 13333333
hset lily sex female /設定使用者的單個資訊
hget lily sex /檢視使用者的單個資訊
hgetall lily /檢視hash表中所有使用者的資訊
hmset jerry sex male phone 1388888 addr chao /設定使用者的大體資訊
hdel jerry sex /删除使用者的單個資訊
hdel jerry phone
hkeys jerry /檢視使用者的資訊欄
hvals jerry /檢視使用者的詳細資訊
3.清單常用指令: <參考TTS案例>
redis的list是一個字元隊列,一個key可以有多個值。lpop移除并傳回清單頭元素資料,llen傳回清單的長度。lpush插入或建立,lrange讀取,lrem删除表中特定值,lset修改;rpush插入指定值,rpop删除指定值
清單,先進後出的桶
購買記錄,聊天記錄, 圖文直播
lpush list1 a
lpush list1 b
lpush list1 c
lrange list1 0 -1 /檢視0-1位的值
lindex list1 2 精确查找第2位
lpop list1 删除頭部
rpush list1 xx
4.其他操作指令
del、exists 測試一個值是否存在、ttl key 檢視生存周期、expire key 50 設定生存周期、persist key 設定永不過期、keys * 列出所有資料的,flushall清空所有資料、select選擇資料庫、move庫之間移動、rename改名覆寫、renamex值裡為空時是才改名、sort排序、type傳回類型
########################################
配置檔案解析/etc/redis/6379.conf
maxmemory-policy volatile-lru 記憶體滿時使用LRU算法清理舊資料
daemonize yes 守護程序
pidfile /var/run/redis_6379.pid 程序pid
port 6379 端口号
timeout 0 連結逾時時間
loglevel notice 日志級别
logfile /var/log/redis_6379.log 日志檔案
注意機關1k = 1000 bytes 1kb =1024 bytes
databases 16 資料庫個數
dbfilename dump.rdb 鏡像備份檔案名
/var/lib/redis/6379 備份檔案路徑
maxmemory<bytes>最大記憶體使用量
maxclients 10000 最大用戶端并發連接配接數量
select 1 進入庫1,從0開始算,預設16個databases。
save 900 1 /900秒内有1個變動則鏡像備份
save 300 10 /300秒内有十個變動則鏡像備份
save 60 10000 /60秒内有1千個變動則鏡像備份
剛寫完若強制關機會自動儲存
記憶體---》硬碟的 /var/lib/redis/6379路徑(拷貝備份)
####################################
192.168.2.100主 192.168.2.200從
主從同步:都安裝redis
1.主伺服器操作192.168.2.100:
#vim /etc/redis/6379.conf
397行: requirepass 123456 //給主伺服器配置密碼
#/etc/init.d/redis_6379 restart 重新開機服務
密碼進資料庫
# redis-cli -h //檢視幫助
#redis-cli -a 123456 //輸入密碼才能使用資料庫
43行給啟動腳本添加一個密碼-a 123456
#vim /etc/init.d/redis_6379
$CLIEXEC -a 123456 -p $REDISPORT shutdown
#/etc/init.d/redis_6379 restart 重新開機服務
2.從伺服器操作192.168.2.200:
#vim /etc/redis/6379.conf
211行: slaveof 192.168.2.100 6379 /主伺服器,端口
218行: masterauth 123456 /密碼
#/etc/init.d/redis_6379 restart /重新開機服務
3.驗證:主伺服器寫資料,從伺服器自動同步,可查詢該資料;主伺服器set,從伺服器get,沒有順序
# redis-cli –h 192.168.4.100 –a 123456
# redis-cli –h 192.168.4.200
案例1:源碼安裝Redis緩存服務
案例2:常用Redis資料庫操作指令
案例3:配置Redis主從伺服器
1 案例1:源碼安裝Redis緩存服務
1.1 問題
本案例要求先快速搭建好一台Redis伺服器,并測試該緩存伺服器:
設定變量test,值為123
檢視變量test的值
設定計數器mycounter
對計數器mycounter進行增量加1操作
1.2 方案
使用源碼包安裝Redis伺服器,使用redis-cli用戶端工具連接配接Redis伺服器并測試緩存資料庫。
使用redis-cli指令測試Redis服務時可以使用的指令清單如表-1所示。
表-1 Redis指令清單
1.3 步驟
實作此案例需要按照如下步驟進行。
步驟一:安裝Redis伺服器
1)源碼安裝Redis軟體
[[email protected] ~]# tar -xzf redis-3.0.6.tar.gz
[[email protected] ~]# cd redis-3.0.6
[[email protected] ~]# make
[[email protected] ~]# make install
[[email protected] ~]# ./utils/install_server.sh //初始化
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]
//Redis伺服器軟體存儲路徑
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
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!
[[email protected] ~]# ls /etc/init.d/redis_6379 //檢視啟動腳本
2)啟用Redis服務并檢視監聽端口狀态
[[email protected] ~]# /etc/init.d/redis_6379 restart
[[email protected] ~]# netstat -nutlp |grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5749/redis-server *
tcp 0 0 :::6379 :::* LISTEN 5749/redis-server *
步驟二:測試緩存資料庫
1)使用redis-cli測試資料庫
[[email protected] ~]# redis-cli
127.0.0.1:6379> ping //測試伺服器
PONG
127.0.0.1:6379> set test 123 //設定變量
OK
127.0.0.1:6379> get test //檢視test值
"123"
127.0.0.1:6379> INCR mycounter //設定計數器mycounter
(integer) 1
127.0.0.1:6379> INCR mycounter //對計數器mycounter進行自增運算
(integer) 2
2 案例2:常用Redis資料庫操作指令
2.1 問題
沿用練習一,通過redis-cli工具,對Redis資料庫各資料類型進行增删改查等操作,要求如下:
分别對Strings、Hash表、List清單三種資料類型進行增删改查等常見操作
設定資料緩存時間
清空所有資料
對資料庫操作
2.2 方案
使用redis-cli用戶端工具連接配接Redis伺服器并測試緩存資料庫。
使用redis-cli指令測試Redis服務是可以使用的指令清單如表-2所示。
表-2 Redis指令清單
2.3 步驟
實作此案例需要按照如下步驟進行。
步驟一:常用Redis資料操作指令
1)使用redis-cli測試資料庫(字元串常見操作指南)
[[email protected] ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set string1 "hello the word" //設定字元串變量
OK
127.0.0.1:6379> get string1 //檢視字元串變量
"hello the word"
127.0.0.1:6379> set string2 "hello" ex 5 //設定字元串變量,并設定過期時間為5秒
OK
127.0.0.1:6379> get string2 //檢視字元串變量
"hello"
127.0.0.1:6379> get string2 //字元串過期後,檢視該值為空
(nil)
127.0.0.1:6379> get string1
"hello the word"
127.0.0.1:6379> set string1 hello nx //僅當string1不存在時,才執行set指令
(nil)
127.0.0.1:6379> set string1 hello xx //僅當string1存在時,才執行set指令
OK
127.0.0.1:6379> get string1 //檢視修改後string1的值
"hello"
127.0.0.1:6379> set string1 "hello the world" //修改string1的值
OK
127.0.0.1:6379> get string1
"hello the world"
127.0.0.1:6379> setrange string1 6 "Redis" //從第6個字元開始替換string1的值
(integer) 15
127.0.0.1:6379> get string1
"hello Redisorld"
127.0.0.1:6379> strlen string1 //計算string1的長度
(integer) 15
127.0.0.1:6379> append string1 xxx //對string1進行追加操作
(integer) 18
127.0.0.1:6379> get string1
"hello Redisorldxxx"
127.0.0.1:6379> append string1 " xxx"
(integer) 22
127.0.0.1:6379> get string1
"hello Redisorldxxx xxx"
127.0.0.1:6379> setbit string2 0 1 //按位設定string2的值,0位為1
(integer) 0
127.0.0.1:6379> setbit string2 1 1 //按位設定string2的值,1位為1
(integer) 0
127.0.0.1:6379> setbit string2 2 1
(integer) 0
127.0.0.1:6379> setbit string2 3 0
(integer) 0
127.0.0.1:6379> get string2 //不可以檢視所有的值
"\xe0"
127.0.0.1:6379> bitcount string2 //統計string2中1的個數
(integer) 3
127.0.0.1:6379> getbit string2 0 //檢視string2第0位的值
(integer) 1
127.0.0.1:6379> getbit string2 1 //檢視string2第1位的值
(integer) 1
127.0.0.1:6379> decr string3 //遞減運算,初始值為0
(integer) -1
127.0.0.1:6379> decr string3
(integer) -2
127.0.0.1:6379> decr string3
(integer) -3
127.0.0.1:6379> decr string3
(integer) -4
127.0.0.1:6379> decr string3
(integer) -5
127.0.0.1:6379> set string4 10 //自定義變量初始值為10
OK
127.0.0.1:6379> decr string4 //對自定義變量進行遞減運算
(integer) 9
127.0.0.1:6379> decr string4
(integer) 8
127.0.0.1:6379> decr string4
(integer) 7
127.0.0.1:6379> decrby string4 2 //對變量進行遞減2運算
(integer) 5
127.0.0.1:6379> decrby string4 2
(integer) 3
127.0.0.1:6379> get string4
"3"
127.0.0.1:6379> set string5 "hello the world" //設定字元串變量
OK
127.0.0.1:6379> getrange string5 0 4 //檢視字串的第0至第4位
"hello"
127.0.0.1:6379> getrange string5 -3 -1 //檢視字串的倒數第3位至倒數第1位
"rld"
127.0.0.1:6379> getset db mongodb //擷取變量的值,并賦新值
(nil) //沒有改變量所有擷取的值為空
127.0.0.1:6379> get db //賦新值後,該變量db的值為mongodb
"mongodb"
127.0.0.1:6379> getset db redis //擷取變量的值,并賦新值
"mongodb"
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> incr page //對變量進行遞增運算,初始值為0
(integer) 1
127.0.0.1:6379> incr page
(integer) 2
127.0.0.1:6379> incr page
(integer) 3
127.0.0.1:6379> incr page
(integer) 4
127.0.0.1:6379> set string6 10 //設定字元串變量為10
OK
127.0.0.1:6379> incr string6 //對變量進行遞增運算
(integer) 11
127.0.0.1:6379> incr string6
(integer) 12
127.0.0.1:6379> incrby string6 2 //對變量進行遞增2運算
(integer) 14
127.0.0.1:6379> incrby string6 2
(integer) 16
127.0.0.1:6379> incrby string6 2
(integer) 18
127.0.0.1:6379> incrby string6 2
(integer) 20
127.0.0.1:6379> set num 16.1 //設定浮點數變量
OK
127.0.0.1:6379> incrbyfloat num 1.1 //對浮點數進行遞增1.1運算
"17.2"
127.0.0.1:6379> incrbyfloat num 1.1
"18.3"
127.0.0.1:6379> incrbyfloat num 1.1
"19.4"
127.0.0.1:6379> incrbyfloat num 1.1
"20.5"
2)Hash表常見操作指南
127.0.0.1:6379> hset hkey google “www.g.cn”
//設定hash表hkey,google列的值為www.g.cn
(integer) 1
127.0.0.1:6379> hset hkey baidu “www.baidu.com”
//設定hash表hkey,baidu列的值為www.baidu.com
(integer) 1
127.0.0.1:6379> hget hkey google //檢視hash表hkey中google列的值
"www.g.cn"
127.0.0.1:6379> hget hkey baidu //檢視hash表hkey中baidu列的值
"www.baidu.com"
127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com"
OK
//一次性設定hash表site的多個列與值
127.0.0.1:6379> hmget site google baidu
1) "www.g.cn"
2) "www.baidu.com"
//一次性檢視hash表site的多個列值
127.0.0.1:6379> hgetall site //檢視site表中所有的列與值
1) "google"
2) "www.g.cn"
3) "baidu"
4) "www.baidu.com"
127.0.0.1:6379> hdel site google //删除site表中google列
(integer) 1
127.0.0.1:6379> hgetall site //驗證删除效果
1) "baidu"
2) "www.baidu.com"
127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com" sina "www.sina.com"
OK
127.0.0.1:6379> hkeys site //檢視site表的所有列
1) "baidu"
2) "google"
3) "sina"
127.0.0.1:6379> hvals site //檢視site表中所有列的值
1) "www.baidu.com"
2) "www.g.cn"
3) "www.sina.com"
127.0.0.1:6379> hmget site google baidu //一次性檢視site表中的多個列值
1) "www.g.cn"
2) "www.baidu.com"
3)List清單常見操作指南
127.0.0.1:6379> lpush list1 a b c //建立清單并指派
(integer) 3
127.0.0.1:6379> lpush list2 a //建立清單并指派
(integer) 1
127.0.0.1:6379> lpush list2 b //給清單追加新值
(integer) 2
127.0.0.1:6379> lpush list2 c //給清單追加新值
(integer) 3
127.0.0.1:6379> lrange list1 0 -1
//檢視清單list1中的所有值,從0位到最後1位
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> lrange list1 1 2 //檢視清單中第1和2位的值
1) "b"
2) "a"
127.0.0.1:6379> lrange list1 1 -1 //檢視清單中第1至最後1位的值
1) "b"
127.0.0.1:6379> lrange list2 0 -1 //檢視所有的值
1) "a"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> lrange list2 0 -1
1) "t"
2) "a"
3) "c"
4) "b"
5) "a"
127.0.0.1:6379> lpop list2
//傳回list2清單頭元素資料,并将該值從清單中删除,key不存在則傳回nil
"t"
127.0.0.1:6379> lrange list2 0 -1 //驗證結果
1) "a"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> lpop list2 //接續删除頭部元素
"a"
127.0.0.1:6379> lrange list2 0 -1 //驗證結果
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> lpop list2
"c"
127.0.0.1:6379> lrange list2 0 -1
1) "b"
2) "a"
127.0.0.1:6379> lpush list4 a b c a b c a b c d e f
(integer) 12
127.0.0.1:6379> lrange list4 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
7) "c"
8) "b"
9) "a"
10) "c"
11) "b"
12) "a"
127.0.0.1:6379> lrem list4 2 c //從前往後删除list4中的2個c值
(integer) 2
127.0.0.1:6379> lrange list4 0 -1 //驗證結果
1) "f"
2) "e"
3) "d"
4) "b"
5) "a"
6) "b"
7) "a"
8) "c"
9) "b"
10) "a"
127.0.0.1:6379> lrem list4 -2 a //從後往前删除list4中的2個a值
(integer) 2
127.0.0.1:6379> lrange list4 0 -1 //驗證結果
1) "f"
2) "e"
3) "d"
4) "b"
5) "a"
6) "b"
7) "c"
8) "b"
127.0.0.1:6379> lrem list4 0 b //删除全部的b值
(integer) 3
127.0.0.1:6379> lrange list4 0 -1 //驗證結果
1) "f"
2) "e"
3) "d"
4) "a"
5) "c"
127.0.0.1:6379> lindex list4 0 //傳回list4中第0個值
"f"
127.0.0.1:6379> lindex list4 1 //傳回list4中第1個值
"e"
127.0.0.1:6379> lindex list4 -1 //傳回list4中最後1個值
"c"
127.0.0.1:6379> lindex list4 -2 //傳回list4中倒數第2個值
"a"
127.0.0.1:6379> lrange list4 0 -1
1) "f"
2) "e"
3) "d"
4) "a"
5) "c"
127.0.0.1:6379> lset list4 0 test //給list4的第0為插入值,值為test
OK
127.0.0.1:6379> lrange list4 0 -1 //驗證結果
1) "test"
2) "d"
3) "a"
127.0.0.1:6379> rpush list1 test //往list1清單的尾部插入test
(integer) 4
127.0.0.1:6379> lrange list1 0 -1
1) "c"
2) "b"
3) "a"
4) "test" //被插入的值test
4)其他操作指南
127.0.0.1:6379> set mykey "hello" //定義字元串變量
OK
127.0.0.1:6379> get mykey //檢視變量
"hello"
127.0.0.1:6379> del mykey //删除變量
(integer) 1
127.0.0.1:6379> get mykey //驗證結果
(nil)
127.0.0.1:6379> exists mykey //測試變量是否存在
(integer) 0
127.0.0.1:6379> set mykey "hello" //定義變量即值
OK
127.0.0.1:6379> exists mykey //測試變量是否存在
(integer) 1
127.0.0.1:6379> expire mykey 5 //設定變量過期時間為5秒
(integer) 1
127.0.0.1:6379> get mykey //檢視有值
"hello"
127.0.0.1:6379> get mykey
"hello"
127.0.0.1:6379> get mykey //5秒後檢視,無值
(nil)
127.0.0.1:6379> set mykey "hello" //設定變量
OK
127.0.0.1:6379> expire mykey 10 //定義過期時間
(integer) 1
127.0.0.1:6379> persist mykey //重新定義過期時間為,永久有效
(integer) 1
127.0.0.1:6379> get mykey
"hello"
127.0.0.1:6379> get mykey
"hello"
127.0.0.1:6379> ttl mykey
(integer) -1 //永不過期
127.0.0.1:6379> expire mykey 10 //定義過期時間
(integer) 1
127.0.0.1:6379> ttl mykey //檢視過期時間
(integer) 9
127.0.0.1:6379> ttl mykey
(integer) 8
127.0.0.1:6379> ttl mykey
(integer) 7
127.0.0.1:6379> ttl mykey
(integer) 6
127.0.0.1:6379> ttl mykey
(integer) 5
127.0.0.1:6379> ttl mykey
(integer) 4
127.0.0.1:6379> ttl mykey
(integer) 3
127.0.0.1:6379> ttl mykey
(integer) 3
127.0.0.1:6379> ttl mykey
(integer) 2
127.0.0.1:6379> ttl mykey
(integer) 1
127.0.0.1:6379> ttl mykey
(integer) -2 //已經過期
127.0.0.1:6379> get mykey //檢視mykey的值已經為空
(nil)
127.0.0.1:6379> set mykey "hello"
OK
127.0.0.1:6379> keys * //檢視資料庫下所有資料
1) "string6"
2) "list7"
3) "mykey"
4) "string4"
5) "db"
6) "num"
7) "result"
8) "hkey"
9) "string5"
10) "string1"
11) "bit1"
12) "page"
13) "bit2"
14) "site"
15) "string2"
16) "list1"
17) "string3"
18) "list6"
127.0.0.1:6379> keys li*
1) "list7"
2) "list1"
3) "list6"
127.0.0.1:6379> keys s*
1) "string6"
2) "string4"
3) "string5"
4) "string1"
5) "site"
6) "string2"
7) "string3"
127.0.0.1:6379> keys string[15] //檢視string1或string5
1) "string5"
2) "string1"
127.0.0.1:6379> keys string[0-9] //檢視string0值9的資料
1) "string6"
2) "string4"
3) "string5"
4) "string1"
5) "string2"
6) "string3"
127.0.0.1:6379> keys ?it* //使用通配符所有資料
1) "bit1"
2) "bit2"
127.0.0.1:6379> select 1 //進入1資料庫,預設資料庫為0
OK
127.0.0.1:6379[1]> keys * //在新資料庫中檢視資料為空
(empty list or set)
127.0.0.1:6379[1]> set test "test" //在資料庫1中建立變量
OK
127.0.0.1:6379[1]> get test //檢視變量的值
"test"
127.0.0.1:6379[1]> select 2 //進入2資料庫
OK
127.0.0.1:6379[2]> keys * //檢視所有資料
(empty list or set)
127.0.0.1:6379[2]> select 1
OK
127.0.0.1:6379[1]> keys *
1) "test"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys *
1) "string6"
2) "list7"
3) "mykey"
4) "string4"
5) "db"
6) "num"
7) "result"
8) "hkey"
9) "string5"
10) "string1"
11) "bit1"
12) "page"
13) "bit2"
14) "site"
15) "string2"
16) "list1"
17) "string3"
18) "list6"
127.0.0.1:6379> flushall //清空所有資料
OK
127.0.0.1:6379> keys * //驗證結果
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> set mykey "hello"
OK
127.0.0.1:6379> keys *
1) "mykey"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys *
1) "mykey"
127.0.0.1:6379> move mykey 1 //将資料庫0中的mykey變量移動至資料庫1
(integer) 1
127.0.0.1:6379> keys * //在資料庫0中檢視為空
(empty list or set)
127.0.0.1:6379> select 1 //進入資料庫1
OK
127.0.0.1:6379[1]> keys * //檢視所有資料庫
1) "mykey"
127.0.0.1:6379[1]> get mykey //檢視mykey的值
"hello"
127.0.0.1:6379[1]> rename mykey testkey //對變量進行重命名
OK
127.0.0.1:6379[1]> keys *
1) "testkey"
127.0.0.1:6379[1]> lpush cost 1 8 7 2 5 //建立清單cost
(integer) 5
127.0.0.1:6379[1]> sort cost //對清單值進行排序
1) "1"
2) "2"
3) "5"
4) "7"
5) "8"
127.0.0.1:6379[1]> lpush test "about" "site"
(integer) 2
127.0.0.1:6379[1]> sort test alpha //對清單進行排序,按字母順序排列
1) "about"
2) "site"
127.0.0.1:6379[1]> lpush test a f c e g i
(integer) 8
127.0.0.1:6379[1]> sort test alpha
1) "a"
2) "about"
3) "c"
4) "e"
5) "f"
6) "g"
7) "i"
8) "site"
127.0.0.1:6379[1]> lpush test a f c e g i
(integer) 14
127.0.0.1:6379[1]> sort test alpha
1) "a"
2) "a"
3) "about"
4) "c"
5) "c"
6) "e"
7) "e"
8) "f"
9) "f"
10) "g"
11) "g"
12) "i"
13) "i"
14) "site"
127.0.0.1:6379[1]> sort test alpha limit 0 3 //排序後,僅顯示0至3位的值
1) "a"
2) "a"
3) "about"
127.0.0.1:6379[1]> sort test alpha limit 0 3
1) "a"
2) "a"
3) "about"
127.0.0.1:6379[1]> sort test alpha limit 0 3 desc //倒序排列
1) "site"
2) "i"
3) "i"
127.0.0.1:6379[1]> sort test alpha limit 0 3 desc store cost2
//将排序後的結果存入cost2
(integer) 3
127.0.0.1:6379[1]> keys *
1) "test"
2) "c"
3) "cost"
4) "a"
5) "name1"
6) "name2"
7) "testkey"
8) "b"
9) "cost2"
127.0.0.1:6379[1]> lrange cost2 0 -1 //檢視cost2中的值
1) "site"
2) "i"
3) "i"
127.0.0.1:6379[1]> type cost2 //檢視cost2的資料類型
list
127.0.0.1:6379[1]> set name tom
OK
127.0.0.1:6379[1]> type name //檢視name的資料類型
string
3 案例3:配置Redis主從伺服器
3.1 問題
本案例要求先快速搭建好兩台Redis伺服器,實作兩台伺服器之間自動資料同步,具體要求如下:
主伺服器IP為192.168.2.100
從伺服器IP為192.168.2.200
主伺服器認證密碼為redis123
測試主從資料是否正常通過
3.2 方案
通過修改Redis配置檔案,實作兩台伺服器之間的自動主從同步功能,方案拓撲如圖-1所示。
圖-1
3.3 步驟
實作此案例需要按照如下步驟進行。
步驟一:配置主從伺服器設定
1)主伺服器安裝Redis
[[email protected] ~]# tar -xzf redis-3.0.6.tar.gz
[[email protected] ~]# cd redis-3.0.6
[[email protected] ~]# make
[[email protected] ~]# make install
[[email protected] ~]# cd utils/
[[email protected] ~]#./install_server.sh
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
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
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!
2)從伺服器安裝Redis
[[email protected] ~]# tar -xzf redis-3.0.6.tar.gz
[[email protected] ~]# cd redis-3.0.6
[[email protected] ~]# make
[[email protected] ~]# make install
[[email protected] ~]# cd utils/
[[email protected] ~]#./install_server.sh
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
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
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!
步驟二:配置主從伺服器設定
1)修改主伺服器/etc/redis/6379.conf配置檔案
[[email protected] ~]# vim /etc/redis/6379.conf
requirepass redis123 //設定伺服器密碼
[[email protected] ~]# /etc/init.d/redis_6379 restart //重新開機服務
2)修改主伺服器的啟動腳本,添加伺服器密碼
[[email protected] ~]# vim /etc/init.d/redis_6379
$CLIEXEC –a redis123 -p $REDISPORT shutdown
3)修改從伺服器/etc/redis/6379.conf配置檔案
[[email protected] ~]# vim /etc/redis/6379.conf
slaveof 192.168.4.100 6379
masterauth redis123
[[email protected] ~]# /etc/init.d/redis_6379 restart
步驟三:驗證效果
1) 主伺服器操作
[[email protected] ~]# redis-cli –h 192.168.4.100 –a redis123 //登入主伺服器設定資料
192.168.4.10:6379> set test 123456
OK
2) 從伺服器操作
[[email protected] ~]# redis-cli –h 192.168.4.200 //登入主伺服器檢視資料同步效果
192.168.4.20:6379> set test
“123456”
轉載于:https://www.cnblogs.com/fuzhongfaya/p/8952699.html