在centos7中安裝redis,並經過node.js操做redis

一、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
複製代碼

注意:javascript

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

  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
複製代碼

顯示結果信息以下:node

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
複製代碼

二、nodejs中操做redis

安裝redisc++

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回顯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); });npm

  • end 鏈接斷開時觸發

client.on('end',function(){ console.log('end')
})bash

  • 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 密碼

公衆號

歡迎關注個人公衆號「碼上開發」,天天分享最新技術資訊。關注獲取最新資源服務器

相關文章
相關標籤/搜索