日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在centos7中安装redis,并通过node.js操作redis

發布時間:2025/3/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在centos7中安装redis,并通过node.js操作redis 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、cent OS7 下使用redis

  • 關閉防火墻
systemctl stop firewalld.service #停止firewallsystemctl 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個可執行文件,分別是

  • redis-server、
  • redis-cli、
  • redis-benchmark、
  • redis-check-aof、
  • redis-check-rdb、
  • 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.1redis服務器地址
    port6379端口號
    connect_timeout3600000連接超時時間 以ms為單位
    passwordnull密碼

    公眾號

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

    總結

    以上是生活随笔為你收集整理的在centos7中安装redis,并通过node.js操作redis的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。