背景:因為項目用到了redis,加上之前一直聽說了redis,但一直沒有用過,正好項目現在要用到了,抽時間簡單學習了下,做個記錄總結下。
一 、Redis簡介
介紹Redis之前,先了解下NoSQL (Not noly SQL)不僅僅是SQL
屬於非關系型數據庫;Redis就屬於非關系型數據庫
傳統的Mysql ,oracle ,sql server 等 都是關系型數據庫
為什么需要NoSQL,主要應對以下問題,傳統關系型數據庫力不從心
High performance -高並發讀寫
Huge Storage-海量數據的高效率存儲和訪問
High Scalablility && High Availability 高可擴展性和高可用性
NoSQL 產品 MongoDB Redis等等 Redis目前主流
NoSQL的特點
易擴展
靈活的數據模型
大數據量,高性能
高可用
高性能鍵值對數據庫,支持的鍵值數據類型
字符串類型
列表類型
有序集合類型
散列類型
集合類型
Redis的應用場景
緩存
任務隊列
網站訪問統計
數據過期處理
應用排行榜
分布式集群架構中的session分離
二、Redis安裝
Redis從一開始就只支持Linux,后面雖然有團隊搞出Window版本,但是我還是建議大伙安裝到Linux中。
准備工作 VMware 以及Xshell
redis官方安裝教程,但是比較簡單,不適合生產環境的安裝部署;
我們這邊詳細點,
第一步:安裝gcc
gcc編譯c的,因為redis是c編寫的,所以我們先安裝下gcc
yum install gcc-c++
第二步:wget方式 下載redis壓縮包,並解壓,以及編譯
下載
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
解壓
tar -zxvf redis-3.2.9.tar.gz
[root@localhost ~]# ll
總用量 1548
-rw-------. 1 root root 1261 6月 27 19:54 anaconda-ks.cfg
-rw-r--r--. 1 root root 25680 4月 27 18:45 mysql57-community-release-el7-11.noarch.rpm
drwxrwxr-x. 6 root root 4096 5月 17 23:39 redis-3.2.9
-rw-r--r--. 1 root root 1547695 5月 17 23:40 redis-3.2.9.tar.gz
[root@localhost ~]#
這樣/root/目錄下就有redis了
編譯:
cd redis-3.2.9
進入目錄,
make
編譯
第三步:安裝redis
make PREFIX=/usr/local/redis install
安裝到/usr/local/redis/
已經有redis
[root@localhost local]# cd redis/
[root@localhost redis]# ll
總用量 0
drwxr-xr-x. 2 root root 134 7月 2 16:44 bin
[root@localhost redis]#
redis里有個bin
[root@localhost redis]# cd bin/
[root@localhost bin]# ll
總用量 15060
-rwxr-xr-x. 1 root root 2431832 7月 2 16:44 redis-benchmark
-rwxr-xr-x. 1 root root 25168 7月 2 16:44 redis-check-aof
-rwxr-xr-x. 1 root root 5181840 7月 2 16:44 redis-check-rdb
-rwxr-xr-x. 1 root root 2584848 7月 2 16:44 redis-cli
lrwxrwxrwx. 1 root root 12 7月 2 16:44 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5181840 7月 2 16:44 redis-server
[root@localhost bin]#
bin里是一些工具
cd回到root,我們需要把一個配置文件 復制到 redis下 后台啟動用到
[root@localhost redis-3.2.9]# cp redis.conf /usr/local/redis/
[root@localhost redis-3.2.9]# ll /usr/local/redis/
總用量 48
drwxr-xr-x. 2 root root 134 7月 2 16:44 bin
-rw-r--r--. 1 root root 46695 7月 2 16:49 redis.conf
[root@localhost redis-3.2.9]#
第四步:啟動和關閉redis服務
啟動redis就是執行redis里的bin里的redis-server命令
進入redis目錄 執行
[root@localhost redis-3.2.9]# cd /usr/local/redis/
[root@localhost redis]# bin/redis-server
出現這種圖標,說明啟動成功;
但是 ,這種啟動是前端或者前台啟動,假如退出 程序就終止或者退出了。
所以這種服務程序,必須后端運行;
我們通過修改配置文件redis.conf
操作,
我們ctrl+c 退出當前程序;
vi打開redis.conf vi /usr/local/redis/redis.conf
找到
把no改成yes
esc退出 !wq保存;
然后進入redis目錄,然后加載配置文件運行;
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ./bin/redis-server ./redis.conf
我們通過ps -ef | grep -i redis命令來搜索redis服務
[root@localhost redis]# ps -ef | grep -i redis
root 8692 1 0 16:52 ? 00:00:19 bin/redis-server *:6379
root 8954 8930 0 20:30 pts/0 00:00:00 grep --color=auto -i redi
[root@localhost redis]#
我們通過shutdown命令來停止redis服務的運行
[root@localhost redis]# ./bin/redis-cli shutdown
[root@localhost redis]# ps -ef | grep -i redis
root 8959 8930 0 20:35 pts/0 00:00:00 grep --color=auto -i redi
[root@localhost redis]#
第五步:redis基本使用
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379>
進入客戶端
我們存儲 name:java1234
key:value格式
127.0.0.1:6379> set name java1234
OK
127.0.0.1:6379> get name
"java1234"
通過set設置,通過get獲取
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
通過del刪除key,keys * 顯示所有keys
三、Jedis簡介
實際開發中,我們需要用Redis的連接工具連接Redis然后操作Redis,
對於主流語言,Redis都提供了對應的客戶端;
提供了很多客戶端 官方推薦的是Jedis 托管地址:https://github.com/xetorthio/jedis
四、Jedis連接 HelloWorld實現
建一個Maven項目,
pom里加下jedis依賴,
1
2
3
4
5
|
< dependency >
< groupId >redis.clients</ groupId >
< artifactId >jedis</ artifactId >
< version >2.9.0</ version >
</ dependency >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.java1234.redis;
import redis.clients.jedis.Jedis;
/**
* 測試類
* @author user
*
*/
public class JedisTest {
public static void main(String[] args) {
Jedis jedis= new Jedis( "192.168.1.107" , 6379 ); // 創建客戶端 設置IP和端口
jedis.set( "name" , "java知識分享網" ); // 設置值
String value=jedis.get( "name" ); // 獲取值
System.out.println(value);
jedis.close(); // 釋放連接資源
}
}
|
測試代碼,
運行 報錯了
連接超時,
我們配置下防火牆 開一個6379端口權限
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
繼續運行 還是報錯 連接超時 錯誤;
我們配置下 redis配置文件
[root@localhost redis]# vi /usr/local/redis/redis.conf
這里綁定了本機,我們把這個備注掉;
# bind 127.0.0.1
配置完后
[root@localhost redis]# ./bin/redis-cli shutdown
[root@localhost redis]# ./bin/redis-server ./redis.conf
要重啟下redis服務;
繼續運行 又報錯了
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
at redis.clients.jedis.Protocol.processError(Protocol.java:127)
at redis.clients.jedis.Protocol.process(Protocol.java:161)
at redis.clients.jedis.Protocol.read(Protocol.java:215)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
at redis.clients.jedis.Jedis.set(Jedis.java:121)
at com.java1234.redis.JedisTest.main(JedisTest.java:14)
這個是因為遠程連接redis redis自我保護 拒絕訪問;
有兩種方法 解決
第一種 直接去掉自我保護功能(不推薦)
[root@localhost redis]# vi /usr/local/redis/redis.conf
進入配置
找到 protected-mode yes
改成 no即可
編輯后 重啟redis服務,然后運行 ,結果出來了
第二種 設置redis連接密碼
進入客戶端
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> config set requirepass 123456
設置密碼 123456
127.0.0.1:6379> quit
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> auth 123456
OK
說明設置成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.java1234.redis;
import redis.clients.jedis.Jedis;
/**
* 測試類
* @author user
*
*/
public class JedisTest {
public static void main(String[] args) {
Jedis jedis= new Jedis( "192.168.1.107" , 6379 ); // 創建客戶端 設置IP和端口
jedis.auth( "123456" ); // 設置密碼
jedis.set( "name" , "java知識分享網" ); // 設置值
String value=jedis.get( "name" ); // 獲取值
System.out.println(value);
jedis.close(); // 釋放連接資源
}
}
|
這樣就OK了
連接池:
package com.java1234.redis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * 測試類 * @author user * */ public class JedisTest { public static void main(String[] args) { JedisPoolConfig config=new JedisPoolConfig(); // 連接池的配置對象 config.setMaxTotal(100); // 設置最大連接數 config.setMaxIdle(10); // 設置最大空閑連接數 JedisPool jedisPool=new JedisPool(config,"192.168.1.107",6379); Jedis jedis=null; try{ jedis=jedisPool.getResource(); // 獲取連接 jedis.auth("123456"); // 設置密碼 jedis.set("name", "java知識分享網"); // 設置值 String value=jedis.get("name"); // 獲取值 System.out.println(value); }catch(Exception e){ e.printStackTrace(); }finally{ if(jedis!=null){ jedis.close(); } if(jedisPool!=null){ jedisPool.close(); } } } }
五、Redis的數據結構之字符串
五種數據類型:
字符串(String)
字符串列表(list)
有序字符串集合(sorted set)
哈希(hash)
字符串集合(set)
Key定義的注意點:
不要過長,
不要過短,
統一的命名規范
存儲String
二進制安全的,存入和獲取的數據相同
Value最多可以容納的數據長度是512M
存儲String常用命令
賦值
取值
刪除
數值增減
擴展命令
[root@localhost redis]# ./bin/redis-cli
賦值
127.0.0.1:6379> set name java1234
OK
取值
127.0.0.1:6379> get name
"java1234"
獲取並設置值
127.0.0.1:6379> getset name open1111
"java1234"
127.0.0.1:6379> get name
"open1111"
刪除
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> get name
(nil)
incr 自增
假如沒有定義 則默認0
假如非數值類型 則報錯
127.0.0.1:6379> incr n
(integer) 1
127.0.0.1:6379> get n
"1"
127.0.0.1:6379> set nn 2
OK
127.0.0.1:6379> incr nn
(integer) 3
127.0.0.1:6379> get nn
"3"
127.0.0.1:6379>
127.0.0.1:6379> set n2 java1234
OK
127.0.0.1:6379> incr n2
(error) ERR value is not an integer or out of range
127.0.0.1:6379>
自減 decr 同上
127.0.0.1:6379> decr nn
(integer) 2
127.0.0.1:6379> decr n3
(integer) -1
擴展
incrby 指定增量值
127.0.0.1:6379> incrby n3 8
(integer) 7
decrby 指定減量值
127.0.0.1:6379> decrby n3 5
(integer) 2
append 追加字符串
假如沒定義 直接賦值
127.0.0.1:6379> append s1 jj
(integer) 2
127.0.0.1:6379> get s1
"jj"
127.0.0.1:6379> append s1 bb
(integer) 4
127.0.0.1:6379> get s1
"jjbb"
六、Redis的數據結構之哈希
存儲Hash
String key和String Value的Map容器
每一個Hash可以存儲4294967295個鍵值對
存儲Hash常用命令:
賦值
取值
刪除
增加數字
判斷字段是否存在
獲取hash屬性個數
獲取hash所有屬性名稱
定義h1 以及username和password字段 值分別是java1234 123456
hget單個字段設置
127.0.0.1:6379> hset h1 username java1234
(integer) 1
127.0.0.1:6379> hset h1 password 123456
(integer) 1
127.0.0.1:6379> hget h1 username
"java1234"
127.0.0.1:6379> hget h1 password
"123456"
hmset 多個字段一起設置
127.0.0.1:6379> hmset h2 username open1111 password 23456
OK
127.0.0.1:6379> hmget h2 username
1) "open1111"
127.0.0.1:6379> hmget h2 password
1) "23456"
127.0.0.1:6379> hmget h2 username password
1) "open1111"
2) "23456"
127.0.0.1:6379> hgetall h2
1) "username"
2) "open1111"
3) "password"
4) "23456"
hdel刪除屬性
可以一次刪除一個或者多個
127.0.0.1:6379> hdel h2 username password
(integer) 2
127.0.0.1:6379> hgetall h2
(empty list or set)
hincrby增加數字
127.0.0.1:6379> hset h1 age 20
(integer) 1
127.0.0.1:6379> hincrby h1 age 5
(integer) 25
hexists判斷字段是否存在 1表示存在 0表示不存在
127.0.0.1:6379> hexists h1 age
(integer) 1
127.0.0.1:6379> hexists h1 age2
(integer) 0
hlen獲取hash屬性個數
127.0.0.1:6379> hlen h1
(integer) 3
127.0.0.1:6379> hlen h2
(integer) 0
hkeys獲取所有屬性名稱
127.0.0.1:6379> hkeys h1
1) "username"
2) "password"
3) "age"
127.0.0.1:6379> hkeys h2
(empty list or set)
hvals獲取所有屬性值
127.0.0.1:6379> hvals h1
1) "java1234"
2) "123456"
3) "25"
127.0.0.1:6379> hvals h2
(empty list or set)
127.0.0.1:6379>
七、Redis的數據結構之List
存儲list:
ArrayList使用數組方式
LinkedList使用雙向鏈接方式
雙向鏈接表中增加數據
雙向鏈接表中刪除數據
存儲list常用命令
兩端添加
兩端彈出
擴展命令
lpush 方式添加
從左邊開始添加
127.0.0.1:6379> lpush l1 a b c d
(integer) 4
127.0.0.1:6379> lpush l1 1 2 3 4
(integer) 8
lrange 獲取指定方位的集合元素
從第1個開始 到倒數第一個 也就是最后一個 也就是 所有數據
127.0.0.1:6379> lrange l1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
8) "a"
獲取從第1個到第6個集合元素
127.0.0.1:6379> lrange l1 0 6
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
rpush 從右端開始添加(一般人比較習慣這種方式)
127.0.0.1:6379> rpush l2 a b c d
(integer) 4
127.0.0.1:6379> rpush l2 1 2 3 4
(integer) 8
127.0.0.1:6379> lrange l2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "1"
6) "2"
7) "3"
8) "4"
127.0.0.1:6379> lrange l2 0 6
1) "a"
2) "b"
3) "c"
4) "d"
5) "1"
6) "2"
7) "3"
lpop 左側彈出集合元素
rpop 右側彈出集合元素
127.0.0.1:6379> lrange l2 0 -1
1) "b"
2) "c"
3) "d"
4) "1"
5) "2"
6) "3"
7) "4"
127.0.0.1:6379> rpop l2
"4"
127.0.0.1:6379> lrange l2 0 -1
1) "b"
2) "c"
3) "d"
4) "1"
5) "2"
6) "3"
llen查看元素個數
127.0.0.1:6379> llen l2
(integer) 6
lpushx 集合頭部插入元素
127.0.0.1:6379> lpushx l2 xx
(integer) 7
127.0.0.1:6379> lrange l2 0 -1
1) "xx"
2) "b"
3) "c"
4) "d"
5) "1"
6) "2"
7) "3"
rpushx 集合尾部插入元素
127.0.0.1:6379> rpushx l2 yy
(integer) 8
127.0.0.1:6379> lrange l2 0 -1
1) "xx"
2) "b"
3) "c"
4) "d"
5) "1"
6) "2"
7) "3"
8) "yy"
lpush集合頭部插入多個元素
127.0.0.1:6379> lpush l2 a1 a2
(integer) 10
127.0.0.1:6379> lrange l2 0 -1
1) "a2"
2) "a1"
3) "xx"
4) "b"
5) "c"
6) "d"
7) "1"
8) "2"
9) "3"
10) "yy"
127.0.0.1:6379> rpush l2 a3 a4
(integer) 12
127.0.0.1:6379> lrange l2 0 -1
1) "a2"
2) "a1"
3) "xx"
4) "b"
5) "c"
6) "d"
7) "1"
8) "2"
9) "3"
10) "yy"
11) "a3"
12) "a4"
lrem 從指定方向刪除指定個數的指定元素
先加點數據搞個新集合l3
127.0.0.1:6379> lpush l3 1 3 2 3 2 1 2 1 3
(integer) 9
127.0.0.1:6379> lrange l3 0 -1
1) "3"
2) "1"
3) "2"
4) "1"
5) "2"
6) "3"
7) "2"
8) "3"
9) "1"
從左邊開始刪除2個1
127.0.0.1:6379> lrem l3 2 1
(integer) 2
127.0.0.1:6379> lrange l3 0 -1
1) "3"
2) "2"
3) "2"
4) "3"
5) "2"
6) "3"
7) "1"
從右邊開始刪除2個3
127.0.0.1:6379> lrem l3 -2 3
(integer) 2
127.0.0.1:6379> lrange l3 0 -1
1) "3"
2) "2"
3) "2"
4) "2"
5) "1"
刪除所有2
127.0.0.1:6379> lrem l3 0 2
(integer) 3
127.0.0.1:6379> lrange l3 0 -1
1) "3"
2) "1"
lset 設置集合指定索引的值
127.0.0.1:6379> lrange l1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
8) "a"
索引從0開始
127.0.0.1:6379> lset l1 3 xxxx
OK
127.0.0.1:6379> lrange l1 0 -1
1) "4"
2) "3"
3) "2"
4) "xxxx"
5) "d"
6) "c"
7) "b"
8) "a"
linsert 在集合里插入指定元素
在xxxx元素之前插入aa
127.0.0.1:6379> linsert l1 before xxxx aa
(integer) 9
127.0.0.1:6379> lrange l1 0 -1
1) "4"
2) "3"
3) "2"
4) "aa"
5) "xxxx"
6) "d"
7) "c"
8) "b"
9) "a"
在xxxx元素之后插入bb
127.0.0.1:6379> linsert l1 after xxxx bb
(integer) 10
127.0.0.1:6379> lrange l1 0 -1
1) "4"
2) "3"
3) "2"
4) "aa"
5) "xxxx"
6) "bb"
7) "d"
8) "c"
9) "b"
10) "a"
rpoplpush 把A集合尾部元素彈出並插入到B集合頭部
127.0.0.1:6379> rpush l4 a b c
(integer) 3
127.0.0.1:6379> rpush l5 1 2 3
(integer) 3
127.0.0.1:6379> lrange l4 0 -1
1) "a"
2) "b"
3) "c"
127.0.0.1:6379> lrange l5 0 -1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> rpoplpush l4 l5
"c"
127.0.0.1:6379> lrange l4 0 -1
1) "a"
2) "b"
127.0.0.1:6379> lrange l5 0 -1
1) "c"
2) "1"
3) "2"
4) "3"
八、Redis的數據結構之Set
存儲Set
和List類型不同的是,Set集合中不允許出現重復的元素
Set可包含的最大元素數量是4294967295
存儲set常用命令:
添加/刪除元素
獲取集合中的元素
集合中的差集運算
集合中的交集運算
集合中的並集元算
擴展命令
sadd key member [member ...]
添加set元素
127.0.0.1:6379> sadd set1 a b c
(integer) 3
添加三個元素
smembers key
查看指定key集合元素
127.0.0.1:6379> smembers set1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> sadd set1 a d e
(integer) 2
127.0.0.1:6379> smembers set1
1) "c"
2) "d"
3) "b"
4) "a"
5) "e"
繼續添加元素 發現 重復元素不再添加
srem key member [member ...]
刪除元素
127.0.0.1:6379> srem set1 a d
(integer) 2
127.0.0.1:6379> smembers set1
1) "b"
2) "c"
3) "e"
刪除兩個元素
sismember key member
判斷某個元素是否存在 返回1 表示存在 返回0表示不存在
127.0.0.1:6379> sismember set1 a
(integer) 0
127.0.0.1:6379> sismember set1 b
(integer) 1
sdiff計算差集
127.0.0.1:6379> sadd set2 a b c
(integer) 3
127.0.0.1:6379> sadd set3 b c d e
(integer) 4
127.0.0.1:6379> sdiff set2 set3
1) "a"
127.0.0.1:6379> sdiff set3 set2
1) "d"
2) "e"
我們發現 集合的順序不同 結果不一樣 根據前者參考
sinter計算交集
127.0.0.1:6379> sinter set2 set3
1) "c"
2) "b"
sunion計算並集
127.0.0.1:6379> sunion set2 set3
1) "e"
2) "a"
3) "b"
4) "c"
5) "d"
scard計算元素總數
127.0.0.1:6379> smembers set1
1) "b"
2) "c"
3) "e"
127.0.0.1:6379> scard set1
(integer) 3
srandmember 隨機取一個元素
127.0.0.1:6379> srandmember set1
"c"
127.0.0.1:6379> srandmember set1
"e"
sdiffstore 把差集結果存儲到新集合中
127.0.0.1:6379> smembers set2
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> smembers set3
1) "c"
2) "e"
3) "d"
4) "b"
127.0.0.1:6379> sdiffstore r1 set2 set3
(integer) 1
127.0.0.1:6379> smembers r1
1) "a"
sinterstore 把交集結果存儲到新集合中
127.0.0.1:6379> sinterstore r2 set2 set3
(integer) 2
127.0.0.1:6379> smembers r2
1) "c"
2) "b"
sunionstore把並集結果存儲到新集合中
127.0.0.1:6379> sunionstore r3 set2 set3
(integer) 5
127.0.0.1:6379> smembers r3
1) "e"
2) "a"
3) "b"
4) "c"
5) "d"
存儲Set使用場景
跟蹤一些唯一性數據
用於維護數據對象之間的關聯關系
九、Redis持久化
所有的數據都存在內存中,從內存當中同步到硬盤上,這個過程叫做持久化過程
持久化操作,兩種方式:rdb方式、aof方式,可以單獨使用或者結合使用。
使用方法:
rdb持久化方法:在指定的時間間隔寫入硬盤
aof方式:將以日志,記錄每一個操作,服務器啟動后就構建數據庫。
配置可以禁用 持久化功能。
也可以同時使用兩種方式。
Redis的持久化之RDB方式:
RDB方式 Redis是默認支持的
優勢:只有一個文件,時間間隔的數據,可以歸檔為一個文件,方便壓縮轉移(就一個文件)
劣勢:如果宕機,數據損失比較大,因為它是沒一個時間段進行持久化操作的。也就是積攢的數據比較多,一旦懵逼,就徹底懵逼了
配置:
[root@localhost redis]# vi redis.conf
編輯redis.conf
往下拉:
這里save 900 1 表示 每900秒內至少有1個kery發生變化,就持久化
save 300 10表示 每300秒內至少有10個key發生變化,就持久化
save 60 10000表示 每60秒內至少有10000個key發生變化,就持久化
再往下拉:
這里有個dbfilename配置 是 保存的持久化的文件名 默認是dump.rdb
再往下:
dir ./ 表示文件存儲路徑是當前路徑;
我們退出
當前路徑里確實是有這個文件的。
RDB北風和恢復數據
假如遇到斷電或者宕機或者自然災害, 我們需要恢復數據 我們模擬下。
我們先重置下數據
shutdown關閉下redis
127.0.0.1:6379> shutdown
not connected> exit
[root@localhost redis]# ll
總用量 52
drwxr-xr-x. 2 root root 134 7月 6 09:18 bin
-rw-r--r--. 1 root root 99 7月 18 10:41 dump.rdb
-rw-r--r--. 1 root root 46697 7月 18 10:41 redis.conf
然后刪除掉rdb文件,再啟動redis
[root@localhost redis]# rm -rf dump.rdb
[root@localhost redis]# ll
總用量 48
drwxr-xr-x. 2 root root 134 7月 6 09:18 bin
-rw-r--r--. 1 root root 46697 7月 18 10:41 redis.conf
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> keys *
(empty list or set)
這時候是沒有數據的
這時候啟動 是沒數據的
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> keys *
(empty list or set)
我們搞幾個key,然后shutdown save 保存退出
127.0.0.1:6379> set n1 1
OK
127.0.0.1:6379> set n2 2
OK
127.0.0.1:6379> set n3 3
OK
127.0.0.1:6379> shutdown save
not connected> exit
假如這時候 我們再重啟redis 這時候啟動過程會進程rdb check驗證 然后加載redis目錄下rdb文件;加載數據;
我們驗證下:
我們再次啟動
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> keys *
1) "n1"
2) "n3"
3) "n2"
說明是數據加載進來了;
這里我們把redis下的rdb文件剪切到其他地方去 然后再啟動試下
[root@localhost redis]# ll
總用量 52
drwxr-xr-x. 2 root root 134 7月 18 11:05 bin
-rw-r--r--. 1 root root 99 7月 18 11:35 dump.rdb
-rw-r--r--. 1 root root 46697 7月 18 10:41 redis.conf
[root@localhost redis]# mv dump.rdb /root/
[root@localhost redis]# ll
總用量 48
drwxr-xr-x. 2 root root 134 7月 18 11:05 bin
-rw-r--r--. 1 root root 46697 7月 18 10:41 redis.conf
[root@localhost redis]# ll /root/
總用量 8
-rw-------. 1 root root 1261 7月 6 05:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 99 7月 18 11:35 dump.rdb
-rw-r--r--. 1 root root 0 7月 13 22:00 java牛逼
drwxr-xr-x. 2 root root 6 7月 13 22:07 java書籍
剪切到了root下
這時候再啟動下:
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> keys *
(empty list or set)
數據沒了
恢復數據的話 我們只需要把備份文件搞到redis下即可
再復制過來即可:
[root@localhost redis]# cp /root/dump.rdb /usr/local/redis/
cp:是否覆蓋"/usr/local/redis/dump.rdb"? y
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> keys *
1) "n2"
2) "n1"
3) "n3"
這時候 數據就有了 這就是恢復過程;
繞了一大圈 總結下
平時我們可以定期把rdb文件備份到指定地方 需要恢復的時候 直接把rdb搞到redis下即可;
Redis的持久化之AOF方式
AOF方式:將以日志,記錄每一個操作
優勢:安全性相對RDB方式高很多;
劣勢:效率相對RDB方式低很多;
配置:
[root@localhost redis]# vi redis.conf
編輯redis.conf
往下拉 找到:
appendonly no默認關閉aof方式 我們修改成yes 就開啟
下面那個是默認的aof文件名
再往下拉:
這里是三種同步策略:
always 是 只要發生修改,立即同步 (推薦實用 安全性最高)
everysec 是 每秒同步一次
no是不同步
我們修改成always
然后保存 退出;
我們重新啟動redis,然后隨便加幾個key
這里就有一個appendonly.aof文件;
aof方式恢復數據
我們先重置數據
[root@localhost redis]# rm -rf dump.rdb
[root@localhost redis]# ll
總用量 48
drwxr-xr-x. 2 root root 134 7月 18 11:05 bin
-rw-r--r--. 1 root root 46698 7月 18 12:14 redis.conf
啟動redis
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> keys *
(empty list or set)
目前數據庫是空
添加數據
127.0.0.1:6379> set n1 1
OK
127.0.0.1:6379> set n2 2
OK
127.0.0.1:6379> set n3 3
OK
127.0.0.1:6379> shutdown nosave
not connected> exit
[root@localhost redis]# ll
總用量 52
-rw-r--r--. 1 root root 107 7月 18 12:17 appendonly.aof
drwxr-xr-x. 2 root root 134 7月 18 11:05 bin
-rw-r--r--. 1 root root 46698 7月 18 12:14 redis.conf
[root@localhost redis]#
我們把aof文件剪切到其他地方去 然后啟動試下
[root@localhost redis]# mv appendonly.aof /root/
[root@localhost redis]# ll
總用量 48
drwxr-xr-x. 2 root root 134 7月 18 11:05 bin
-rw-r--r--. 1 root root 46698 7月 18 12:14 redis.conf
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> keys *
(empty list or set)
沒數據;
我們再把aof文件復制回來;
[root@localhost redis]# cp /root/appendonly.aof /usr/local/redis/
cp:是否覆蓋"/usr/local/redis/appendonly.aof"? y
[root@localhost redis]# ll
總用量 52
-rw-r--r--. 1 root root 107 7月 18 12:22 appendonly.aof
drwxr-xr-x. 2 root root 134 7月 18 11:05 bin
-rw-r--r--. 1 root root 46698 7月 18 12:14 redis.conf
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> keys *
1) "n1"
2) "n3"
3) "n2"
我們發現 以及有數據了
小結: 我們平時可以把aof文件定期備份 然后需要的時候 拷貝到redis下 重啟即可;
學習資料參考:《一頭扎進Redis》 和 http://blog.java1234.com 感謝:小鋒