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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在centos7中安装redis,并通过node.js操作redis 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、cent OS7 下使用redis

  • 關(guān)閉防火墻
systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止firewall開(kāi)機(jī)啟動(dòng)firewall-cmd --state #查看默認(rèn)防火墻狀態(tài)(關(guān)閉后顯示notrunning,開(kāi)啟后顯示running) 復(fù)制代碼
  • 配置編譯環(huán)境:
sudo yum install gcc-c++ 復(fù)制代碼
  • 下載源碼:
wget http://download.redis.io/releases/redis-4.0.11.tar.gz 復(fù)制代碼
  • 解壓源碼:
tar -zxvf redis-4.0.11.tar.gz 復(fù)制代碼
  • 進(jìn)入到解壓目錄:
cd redis-4.0.11 復(fù)制代碼
  • 進(jìn)入到解壓目錄: 執(zhí)行make編譯Redis:
make MALLOC=libc 復(fù)制代碼

注意:

make命令執(zhí)行完成編譯后,會(huì)在src目錄下生成6個(gè)可執(zhí)行文件,分別是

  • redis-server、
  • redis-cli、
  • redis-benchmark、
  • redis-check-aof、
  • redis-check-rdb、
  • redis-sentinel
    • 安裝Redis:
    make install 復(fù)制代碼
    • 配置Redis能隨系統(tǒng)啟動(dòng):
    ./utils/install_server.sh 復(fù)制代碼

    顯示結(jié)果信息如下:

    Welcome to the redis service installer This script will help you easily set up a running redis server 復(fù)制代碼

    redis 配置

    • 關(guān)閉保護(hù)模式
    config set protected-mode no 復(fù)制代碼
    • 設(shè)置密碼
    // 獲取密碼config get requirepass// 設(shè)置密碼 config set requirepass yourpassword 復(fù)制代碼

    2、nodejs中操作redis

    安裝redis

    npm install redis --save 復(fù)制代碼//引入redis var redis = require('redis') // 連接redis服務(wù)器 // 連接redis數(shù)據(jù)庫(kù),createClient(port,host,options); // 如果REDIS在本機(jī),端口又是默認(rèn),直接寫(xiě)createClient()即可 client = redis.createClient(6379, '192.168.73.128', {password: 'lentoo' });//錯(cuò)誤監(jiān)聽(tīng)? client.on("error", function (err) {console.log(err); }); 復(fù)制代碼

    2.1常用API

    • redis.print

    通過(guò)redis回顯

    • set 像redis中存入一個(gè)鍵值對(duì)

    client.set('key','value') // 設(shè)置過(guò)期時(shí)間 10s后過(guò)期 client.set('key','value','EX',10) 復(fù)制代碼
    • get 獲取在redis中存入的值

    client.get('key') // value 復(fù)制代碼
    • hset 通過(guò)hash key 存值

    client.hset('hash key','key','value', redis.print) 復(fù)制代碼
    • hget 通過(guò)hash key 獲取值

    client.hget('hash key','key', redis.print) 復(fù)制代碼
    • 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(); }); 復(fù)制代碼
    • hmset

    client.hmset('hash 1', 'key', 'value111', 'key2', 'value222', 'key3', 'value3', redis.print) 復(fù)制代碼
    • hmget

    client.hmget('hash 1', 'key', 'key2', 'key3', redis.print) 復(fù)制代碼
    • publish/subscribe 發(fā)布/訂閱

    const sub = redis.createClient() // 訂閱者 const pub = redis.createClient() // 發(fā)布者 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();} });復(fù)制代碼
    • ready

    redis客戶端連接準(zhǔn)備好后觸發(fā),在此前所有發(fā)送給redis服務(wù)器的命令會(huì)以隊(duì)列的形式進(jìn)行排隊(duì),會(huì)在ready事件觸發(fā)后發(fā)送給redis服務(wù)器

    client.on('ready',function(){console.log('ready'); }) 復(fù)制代碼
    • connct 客戶端在連接到服務(wù)器后觸發(fā)

    client.on('connect',function(){console.log('connect'); }) 復(fù)制代碼
    • reconnecting 客戶端在連接斷開(kāi)后重新連接服務(wù)器時(shí)觸發(fā)

    client.on('reconnecting ', function (resc) {console.log('reconnecting',resc); }) 復(fù)制代碼
    • error 錯(cuò)誤監(jiān)聽(tīng)

    client.on("error", function (err) { console.log(err); });

    • end 連接斷開(kāi)時(shí)觸發(fā)

    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]) 復(fù)制代碼
    options object properties
    屬性默認(rèn)值描述
    host?127.0.0.1redis服務(wù)器地址
    port6379端口號(hào)
    connect_timeout3600000連接超時(shí)時(shí)間 以ms為單位
    passwordnull密碼

    公眾號(hào)

    歡迎關(guān)注我的公眾號(hào)“碼上開(kāi)發(fā)”,每天分享最新技術(shù)資訊。關(guān)注獲取最新資源

    總結(jié)

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

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。