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 MALLOC=libc
複製代碼
注意:javascript
make命令執行完成編譯後,會在src目錄下生成6個可執行文件,分別是java
make install
複製代碼
./utils/install_server.sh
複製代碼
顯示結果信息以下:node
Welcome to the redis service installer
This script will help you easily set up a running redis server
複製代碼
config set protected-mode no
複製代碼
// 獲取密碼
config get requirepass
// 設置密碼
config set requirepass yourpassword
複製代碼
安裝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);
});
複製代碼
經過redis回顯redis
client.set('key','value')
// 設置過時時間 10s後過時
client.set('key','value','EX',10)
複製代碼
client.get('key') // value
複製代碼
client.hset('hash key','key','value', redis.print)
複製代碼
client.hget('hash key','key', redis.print)
複製代碼
// 遍歷哈希表"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();
});
複製代碼
client.hmset('hash 1', 'key', 'value111', 'key2', 'value222', 'key3', 'value3', redis.print)
複製代碼
client.hmget('hash 1', 'key', 'key2', 'key3', redis.print)
複製代碼
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();
}
});
複製代碼
redis客戶端鏈接準備好後觸發,在此前全部發送給redis服務器的命令會以隊列的形式進行排隊,會在ready事件觸發後發送給redis服務器數據庫
client.on('ready',function(){
console.log('ready');
})
複製代碼
client.on('connect',function(){
console.log('connect');
})
複製代碼
client.on('reconnecting ', function (resc) {
console.log('reconnecting',resc);
})
複製代碼
client.on("error", function (err) { console.log(err); });npm
client.on('end',function(){ console.log('end')
})bash
redis.createClient([options])
redis.createClient(unix_socket[, options])
redis.createClient(redis_url[, options])
redis.createClient(port[, host][, options])
複製代碼
屬性 | 默認值 | 描述 |
---|---|---|
host | 127.0.0.1 | redis服務器地址 |
port | 6379 | 端口號 |
connect_timeout | 3600000 | 鏈接超時時間 以ms爲單位 |
password | null | 密碼 |
歡迎關注個人公衆號「碼上開發」,天天分享最新技術資訊。關注獲取最新資源服務器