引言 最近在學習node.js 連接redis的模塊,所以嘗試了一下在虛擬機中安裝cent OS7,並安裝redis,並使用node.js 操作redis。所以順便做個筆記。 如有不對的地方,歡迎大家指正! 1、cent OS7 下使用redis 1.1、配置編譯環境: 1.2、下載源碼: 1.3、 ...
引言
最近在學習node.js 連接redis的模塊,所以嘗試了一下在虛擬機中安裝cent OS7,並安裝redis,並使用node.js 操作redis。所以順便做個筆記。
如有不對的地方,歡迎大家指正!
1、cent OS7 下使用redis
1.1、配置編譯環境:
sudo yum install gcc-c++
1.2、下載源碼:
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
1.3、解壓源碼:
tar -zxvf redis-4.0.11.tar.gz
1.4、進入到解壓目錄:
cd redis-4.0.11
1.5、進入到解壓目錄: 執行make編譯Redis:
make MALLOC=libc
make命令執行完成編譯後,會在src目錄下生成6個可執行文件,分別是
- redis-server、
- redis-cli、
- redis-benchmark、
- redis-check-aof、
- redis-check-rdb、
- redis-sentinel
1.6、安裝Redis:
make install
1.7、配置Redis能隨系統啟動:
./utils/install_server.sh
1.8、關閉防火牆
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
firewall-cmd --state #查看預設防火牆狀態(關閉後顯示notrunning,開啟後顯示running)
2、redis配置
如果redis客戶端和伺服器在同一台機子中,可以不配置,如果redis伺服器在虛擬機中,客戶端在本地系統中,則需要進行配置,否則可能會連接失敗
- 關閉保護模式
如果不關閉,通過node.js連接時,會連接失敗
config set protected-mode no
- 設置密碼
// 獲取密碼 config get requirepass // 設置密碼 config set requirepass yourpassword
2.1 、redis.conf 的配置信息
redis.conf 是redis的的配置文件,修改配置文件後,需要重啟redis才會生效
1、daemonize 如果需要在後臺運行,把該項改為yes 2、pidfile 配置多個pid的地址 預設在/var/run/redis.pid 3、bind 綁定ip,設置後只接受來自該ip的請求 4、port 監聽埠,預設是6379 5、loglevel 分為4個等級:debug verbose notice warning 6、logfile 用於配置log文件地址 7、databases 設置資料庫個數,預設使用的資料庫為0 8、save 設置redis進行資料庫鏡像的頻率。 9、rdbcompression 在進行鏡像備份時,是否進行壓縮 10、dbfilename 鏡像備份文件的文件名 11、Dir 資料庫鏡像備份的文件放置路徑 12、Slaveof 設置資料庫為其他資料庫的從資料庫 13、Masterauth 主資料庫連接需要的密碼驗證 14、Requriepass 設置 登陸時需要使用密碼 15、Maxclients 限制同時使用的客戶數量 16、Maxmemory 設置redis能夠使用的最大記憶體 17、Appendonly 開啟append only模式 18、Appendfsync 設置對appendonly.aof文件同步的頻率(對數據進行備份的第二種方式) 19、vm-enabled 是否開啟虛擬記憶體支持 (vm開頭的參數都是配置虛擬記憶體的) 20、vm-swap-file 設置虛擬記憶體的交換文件路徑 21、vm-max-memory 設置redis使用的最大物理記憶體大小 22、vm-page-size 設置虛擬記憶體的頁大小 23、vm-pages 設置交換文件的總的page數量 24、vm-max-threads 設置VM IO同時使用的線程數量 25、Glueoutputbuf 把小的輸出緩存存放在一起 26、hash-max-zipmap-entries 設置hash的臨界值 27、Activerehashing 重新hash
3、nodejs中操作redis
3.1、 安裝redis
// github https://github.com/NodeRedis/node_redis
npm install redis --save
3.2、 簡單使用
//引入redis var redis = require('redis') // 連接redis伺服器 // 連接redis資料庫,createClient(port,host,options); // 如果REDIS在本機,埠又是預設,直接寫createClient()即可 client = redis.createClient(6379, '192.168.73.128', { password: 'lentoo' }); //錯誤監聽? client.on("error", function (err) { console.log(err); });
client.set('key','value')
client.get('key')
3.3、常用API
-
redis.print
通過redis的列印api回顯
-
set
像redis中存入一個鍵值對
client.set('key','value') // 設置過期時間 10s後過期 client.set('key','value','EX',10)
-
get
獲取在redis中存入的值
client.get('key') // value
-
hset
通過hash key 存值
client.hset('hash key','key','value', redis.print)
-
hget
通過hash key 獲取值
client.hget('hash key','key', redis.print)
-
hkeys
所有的"hash key"
// 遍歷哈希表"hash key" client.hkeys("hash key", function (err, replies) { console.log(replies.length + " replies:"); replies.forEach(function (reply, i) { console.log(" " + i + ": " + reply); }); client.quit(); });
-
hmset
可同時設置多個key,value
client.hmset('hash 1', 'key', 'value111', 'key2', 'value222', 'key3', 'value3', redis.print)
-
hmget
可同時獲取多個key
client.hmget('hash 1', 'key', 'key2', 'key3', redis.print)
-
publish/subscribe
發佈/訂閱
const sub = redis.createClient() // 訂閱者 const pub = redis.createClient() // 發佈者 var msg_count = 0; sub.on("subscribe", function (channel, count) { client.publish("a nice channel", "I am sending a message."); client.publish("a nice channel", "I am sending a second message."); client.publish("a nice channel", "I am sending my last message."); }); sub.on("message", function (channel, message) { console.log("sub channel " + channel + ": " + message); msg_count += 1; if (msg_count === 3) { sub.unsubscribe(); sub.quit(); client.quit(); } });
-
ready
redis客戶端連接準備好後觸發,在此前所有發送給redis伺服器的命令會以隊列的形式進行排隊,會在ready事件觸發後發送給redis伺服器
client.on('ready',function(){ console.log('ready'); })
-
connct
客戶端在連接到伺服器後觸發
client.on('connect',function(){ console.log('connect'); })
- reconnecting
客戶端在連接斷開後重新連接伺服器時觸發
client.on('reconnecting ', function (resc) { console.log('reconnecting',resc); })
-
error
錯誤監聽
client.on("error", function (err) { console.log(err); });
-
end
連接斷開時觸發
client.on('end',function(){ console.log('end') })
-
createClient
創建一個鏈接
redis.createClient([options])
redis.createClient(unix_socket[, options])
redis.createClient(redis_url[, options])
redis.createClient(port[, host][, options])
3.4 options 對象屬性
屬性 | 預設值 | 描述 |
host | 127.0.0.1 | redis伺服器地址 |
port | 6379 | 埠號 |
connect_timeout | 3600000 | 連接超時時間 以ms為單位 |
password | null | 密碼 |