在centos7中安裝redis,並通過node.js操作redis

不懂程式碼的攻城師發表於2018-08-08

1、cent OS7 下使用redis

  • 關閉防火牆
	systemctl stop firewalld.service #停止firewall
	systemctl disable firewalld.service #禁止firewall開機啟動
    firewall-cmd --state #檢視預設防火牆狀態(關閉後顯示notrunning,開啟後顯示running)
複製程式碼
  • 配置編譯環境:
    sudo yum install gcc-c++
複製程式碼
  • 下載原始碼:
    wget http://download.redis.io/releases/redis-4.0.11.tar.gz
複製程式碼
  • 解壓原始碼:
tar -zxvf redis-4.0.11.tar.gz
複製程式碼
  • 進入到解壓目錄:
cd redis-4.0.11
複製程式碼
  • 進入到解壓目錄: 執行make編譯Redis:
make MALLOC=libc
複製程式碼

注意:

make命令執行完成編譯後,會在src目錄下生成6個可執行檔案,分別是

  1. redis-server、
  2. redis-cli、
  3. redis-benchmark、
  4. redis-check-aof、
  5. redis-check-rdb、
  6. redis-sentinel
  • 安裝Redis:
make install 
複製程式碼
  • 配置Redis能隨系統啟動:
./utils/install_server.sh
複製程式碼

顯示結果資訊如下:

Welcome to the redis service installer
This script will help you easily set up a running redis server
複製程式碼

redis 配置

  • 關閉保護模式
    config set protected-mode no
複製程式碼
  • 設定密碼
    // 獲取密碼
    config get requirepass
    
    // 設定密碼 
    config set requirepass yourpassword
複製程式碼

2、nodejs中操作redis

安裝redis

npm install redis --save
複製程式碼
//引入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);
});
複製程式碼

2.1常用API

  • redis.print

通過redis回顯

  • 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

client.hmset('hash 1', 'key', 'value111', 'key2', 'value222', 'key3', 'value3', redis.print)
複製程式碼
  • hmget

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])
複製程式碼
options object properties
屬性 預設值 描述
host  127.0.0.1 redis伺服器地址
port 6379 埠號
connect_timeout 3600000 連線超時時間 以ms為單位
password null 密碼

公眾號

歡迎關注我的公眾號“碼上開發”,每天分享最新技術資訊。關注獲取最新資源

在centos7中安裝redis,並通過node.js操作redis

相關文章