一、下载最新的redis
docker pull redis:latest
二、下载redis配置文件和创建目录
mkdir redis
cd redis
mkdir conf
wget https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf -O conf/redis.conf
三、启动redis
# 创建并运行一个名为 myredis 的容器
docker run
-p 6379:6379
-v $PWD/data:/data
-v $PWD/conf/redis.conf:/etc/redis/redis.conf
--privileged=true
--network=host
--name myredis
-d redis redis-server /etc/redis/redis.conf
# 命令分解
docker run
-p 6379:6379 # 端口映射 宿主机:容器
-v $PWD/data:/data:rw # 映射数据目录 rw 为读写
-v $PWD/conf/redis.conf:/etc/redis/redis.conf:ro # 挂载配置文件 ro 为readonly
--privileged=true # 给与一些权限
--network= host #网络模式
--name myredis # 给容器起个名字
-d redis redis-server /etc/redis/redis.conf # deamon 运行 服务使用指定的配置文件
四、设置redis密码和网络
修改redis.conf配置文件,requirepass 密码,
bind 127.0.0.1 内网
![v2-d500881afb7af0a8ae42b6e71ced1f53_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=21be1ced-7d2f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-d500881afb7af0a8ae42b6e71ced1f53_b.jpg)
五、连接、查看redis容器
docker exec -it “容器ID” redis-cli -h “内网” –a “redis密码”
![v2-e62aea5a729e6d5dbe133a099cf5285d_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=21be1ced-7d2f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-e62aea5a729e6d5dbe133a099cf5285d_b.jpg)
六、php使用redis
1、Redis PHP String(字符串) 实例
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//设置 redis 字符串数据
$redis->set("tutorial-name", "Redis tutorial");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("tutorial-name");
?>
执行脚本,输出结果为:
Connection to server sucessfully
Stored string in redis:: Redis tutorial
2、Redis PHP List(列表) 实例
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//存储数据到列表中
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
// 获取存储的数据并输出
$arList = $redis->lrange("tutorial-list", 0 ,5);
echo "Stored string in redis";
print_r($arList);
?>
执行脚本,输出结果为:
Connection to server sucessfully
Stored string in redis
Redis
Mongodb
Mysql
3、Yii应用举例
![v2-ec5b114dcafc2df871a4795476c1f3d9_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=21be1ced-7d2f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-ec5b114dcafc2df871a4795476c1f3d9_b.jpg)
![v2-5112d7e2d086bfbe0b0327f5259d1441_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=21be1ced-7d2f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-5112d7e2d086bfbe0b0327f5259d1441_b.jpg)
![v2-7db9f24a329203d277de5c295c63f1d0_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=21be1ced-7d2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-7db9f24a329203d277de5c295c63f1d0_b.jpg)
![v2-5fe3a80b62055f244db730b78b3ee771_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=21be1ced-7d2f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-5fe3a80b62055f244db730b78b3ee771_b.jpg)
直接设置和查看redis key的值
docker exec -it myredis redis-cli
auth 密码
set “key” 值
get “key”
七、memcached和redis的比较
![v2-b427d38c598c1c0b279e19c62231b0cc_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=21be1ced-7d2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-b427d38c598c1c0b279e19c62231b0cc_b.jpg)