docker方式安装Redis及初始化配置


前提已安装docker和docker-compose

安装redis

# 此处我将redis安装在/home/redistest下
cd /home/
mkdir redistest
cd redistest

# 创建docker-compose.yml文件
vi docker-compose.yml
# 进入编辑状态
i
# 粘贴以下内容,此处容器名为redis-test,端口15001,密码设置的123456
version: '3'
services:
  redis-test:
    image: 'redis'
    restart: always
    container_name: 'redis-test',
    ports:
      - 15001:6379
    volumes:
      - /home/redistest:/data
    command: ['redis-server','--requirepass','123456']

# 退出编辑状态
esc 
# 保存
:wq

报错Creating network "redistest_default" with the default driver ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-e4e5df5b0799 -j RETURN: iptables: No chain/target/match by that name. (exit status 1))
原因:docker网关是关闭的,docker network无法对新的container进行网络配置,重启一下docker就行
systemctl restart docker

命令行Redis CLi

Redis 命令参考:
http://doc.redisfans.com/
http://redisdoc.com/

# 运行,若报错,参见报错
docker-compose up -d

# 连接进入redis
docker exec -it redis-test /bin/bash
redis-cli

# 登录
auth 123456

ping

# 断开当前redis服务
quit

# 从镜像中退出来
exit
  • 设置/取值(String,Hash,List,Set)
# 切换到指定数据库 16个数据库
select 0 // 切换到0数据库
set name testname0
get name

keys *
keys test*
exists test
del test

hset testname0 name testname0
hset testname0 email testname0@toimc.com
hget testname0 email
hgetall testname0
hmset testname1 name testname1 email testname1@toimc.com age 30
hmget testname1 name age
  • 发布者/订阅者(Pub/Sub)
# 订阅
subscribe testname testname1

# 发布
publish testname "hello world!!"
publish testname1 "hello from imooc1"

GUI工具Another Redis DeskTop Manager

下载Another Redis DeskTop Manager
在这里插入图片描述

Nodejs中集成Redis

https://www.npmjs.com/package/redis

  • 安装redis
# 安装redis
npm install -S redis@2.8.0
  • redis配置
// RedisConfig.js
import redis from 'redis'
const options = {
  host: '121.199.33.98',
  port: 15001,
  password: '123456',
  detect_buffers: true,
  retry_strategy: function (options) {
    if (options.error && options.error.code === 'ECONNREFUSED') {
      // End reconnecting on a specific error and flush all commands with
      // a individual error
      return new Error('The server refused the connection');
    }
    if (options.total_retry_time > 1000 * 60 * 60) {
      // End reconnecting after a specific timeout and flush all commands
      // with a individual error
      return new Error('Retry time exhausted');
    }
    if (options.attempt > 10) {
      // End reconnecting with built in error
      return undefined;
    }
    // reconnect after
    return Math.min(options.attempt * 100, 3000);
  }
}

const client = redis.createClient(options)
const {promisify} = require('util');
const getAsync = promisify(client.get).bind(client);
const setValue = (key,value) => {
  if (typeof value == 'undefined' || value == null || value === '') {
    return
  }
  if (typeof value === 'string') {
    client.set(key,value)
  } else if (typeof value === 'object') {
    Object.keys(value).forEach((item) => {
      client.hset(key,item, value[item], redis.print)
    })
  }
}

const getValue = (key) => {
  return getAsync(key)
}

const getHValue = (key) => {
  return promisify(client.hgetall).bind(client)(key)
}

export {
  client,
  setValue,
  getValue,
  getHValue
}
  • test.js中使用
// test.js
import {getHValue, getValue, setValue} from './RedisConfig'

setValue('imooc', 'imooc message from redis client')
getValue('imooc').then((res) => {
  console.log('getValue',res)
})

setValue('imoocobj', {name: 'brain', age: 30, email: 'brain@toimooc.com'})
getHValue('imoocobj').then(res => {
  console.log('getHValue', JSON.stringify(res, null, 1))
})
  • 运行测试下
# 执行
npx babel-node src/config/test.js
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值