Docker安装官方Redis镜像并启用密码认证
docker run -d --name myredis -p 6379:6379 redis --requirepass "mypassword"
2.选择最新版latest
1 |
docker pull redis:latest |
1 2 3 4 5 6 7 8 9 10 |
[root@localhost~] # docker pull redis:latest latest: Pulling from library /redis 4d0d76e05f3c: Pull complete cfbf30a55ec9: Pull complete 82648e31640d: Pull complete fb7ace35d550: Pull complete 497bf119bebf: Pull complete 89340f6074da: Pull complete Digest: sha256:166788713c58c2db31c41de82bbe133560304c16c70e53a53ca3cfcf35467d8a Status: Downloaded newer image for redis:latest |
3.启动容器并带密码
1 |
docker run --name redis- test -p 6379:6379 -d --restart=always redis:latest redis-server --appendonly yes --requirepass "your passwd" |
-p 6379:6379 :将容器内端口映射到宿主机端口(右边映射到左边)
redis-server –appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置
requirepass “your passwd” :设置认证密码
–restart=always : 随docker启动而启动
4.查看容器
1 2 3 4 5 6 7 |
[root@localhost~] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a126ec987cfe redis:latest "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6379->6379 /tcp redis- test 3645da72ece6 portainer /portainer "/portainer" 7 days ago Up 7 days 0.0.0.0:9000->9000 /tcp sharp_lovelace 118ba79de20a hwdsl2 /ipsec-vpn-server "/opt/src/run.sh" 12 days ago Up 12 days 0.0.0.0:500->500 /udp , 0.0.0.0:4500->4500 /udp l2tp-vpn-server 848fdba6de60 kylemanna /openvpn "ovpn_run" 12 days ago Up 12 days 1194 /udp , 0.0.0.0:1194->1194 /tcp openvpn a273504f9646 mysql:5.6.38 "docker-entrypoint.s…" 8 weeks ago Up 5 days 0.0.0.0:3306->3306 /tcp mysql5.6.38 |
redis容器的id是 a126ec987cfe
5.查看进程
1 2 3 |
[root@localhost~] # ps -ef|grep redis polkitd 26547 26535 0 14:58 ? 00:00:00 redis-server *:6379 root 26610 26432 0 15:05 pts /0 00:00:00 grep --color=auto redis |
6.进入容器执行redis客户端
1 |
docker exec -it a126ec987cfe redis-cli -a 'your passwd' |
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@localhost~] # docker exec -it a126ec987cfe redis-cli -h 127.0.0.1 -p 6379 -a 'your passwd' 127.0.0.1:6379> ping PONG 127.0.0.1:6379> info # Server redis_version:4.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:d3ebfc7feabc1290 redis_mode:standalone os:Linux 3.10.0-693.21.1.el7.x86_64 x86_64 ... |
-h 127.0.0.1 :默认不加为-h 127.0.0.1
-p 6379 :默认不加为 -p 6379
或者连接的时候不带密码,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@localhost ~] # docker exec -it a126ec987cfe redis-cli 127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379> auth 'your passwd' OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379> info # Server redis_version:4.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:d3ebfc7feabc1290 redis_mode:standalone os:Linux 3.10.0-693.21.1.el7.x86_64 x86_64 arch_bits:64 |
进入redis,测试是否安装成功
redis-cli
查看所有的key列表
keys *
设置、获取、删除参数
set name ‘val’
get name
del name
设置用户名密码
编辑redis.conf 设置访问密码为redis
~ sudo vi /etc/redis/redis.conf
#取消注释requirepass
requirepass redis
登陆Redis服务器,输入密码
redis-cli -a redis
Redis服务器不允许远程访问,注释bind 重启服务设置允许远程访问
~ sudo vi /etc/redis/redis.conf
#bind 127.0.0.1
sudo /etc/init.d/redis-server restart 重启
检查redis服务占用
ps -ef | grep redis (注释前)
redis 2644 1 0 07:11 ? 00:00:47 /usr/bin/redis-server 127.0.0.1:6379
ps -ef | grep redis (注释后)
redis 13536 1 0 14:33 ? 00:00:00 /usr/bin/redis-server *:6379
远程测试登陆其他服务器
~ redis-cli -a redisredis -h 192.168.1.10
redis192.168.1.10:6379>
远程访问正常。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~