看了上一篇的介紹友扰,是不是急不可耐地想試試怎么玩轉redis批糟?這就來輕食入門篇凶异。這篇文章主要使用CRUD四個命令蜀撑。
try redis
官方提供了一個線上客戶端用于測試和練習。網(wǎng)址是https://try.redis.io/
> set wanzhouyi strong
OK
> get wanzhouyi
"strong"
> set wanzhouyi nice
OK
> get wanzhouyi
"nice"
> del wanzhouyi
(integer) 1
> get wanzhouyi
(nil)
命令解釋:
- set wanzhouyi strong (增)設置wanzhouyi為鍵剩彬,strong為值
- get wanzhouyi (查)獲取鍵為wanzhouyi的值
- set wanzhouyi nice (改)修改wanzhouyi的值為nice
- del wanzhouyi (刪)刪除wanzhouyi這個鍵
ubuntu本地安裝
第一步:安裝redis
安裝命令:sudo apt install redis-server
第二步:啟動redis
啟動命令:redis-server
從上圖中也可以看到一個關鍵信息酷麦,默認端口是6379。
第三步:用客戶端測試連接
打開客戶端:redis-cli
mango@wanzhouyi:~$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
收到pong返回喉恋,說明客戶端和服務端成功建立連接沃饶。
第四步:接下來就可以開始愉快地CRUD了。
mango@wanzhouyi:~$ redis-cli
127.0.0.1:6379> set wanzhouyi strong
OK
127.0.0.1:6379> get wanzhouyi
"strong"
127.0.0.1:6379> set wanzhouyi nice
OK
127.0.0.1:6379> get wanzhouyi
"nice"
127.0.0.1:6379> del wanzhouyi
(integer) 1
127.0.0.1:6379> get wanzhouyi
(nil)
127.0.0.1:6379>
docker 下 redis的使用
第一步:查看可用docker鏡像
mango@wanzhouyi:~$ sudo docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 9471 [OK]
bitnami/redis Bitnami Redis Docker Image 181 [OK]
sameersbn/redis 83 [OK]
grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2 78
rediscommander/redis-commander Alpine image for redis-commander - Redis man… 58 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 34
redislabs/redisinsight RedisInsight - The GUI for Redis 30
redislabs/redis Clustered in-memory database engine compatib… 30
oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Supp… 25
arm32v7/redis Redis is an open source key-value store that… 23
redislabs/rejson RedisJSON - Enhanced JSON data type processi… 23
bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 22 [OK]
redislabs/redisgraph A graph database module for Redis 15 [OK]
redislabs/redismod An automated build of redismod - latest Redi… 12 [OK]
arm64v8/redis Redis is an open source key-value store that… 12
webhippie/redis Docker images for Redis 11 [OK]
insready/redis-stat Docker image for the real-time Redis monitor… 10 [OK]
s7anley/redis-sentinel-docker Redis Sentinel 10 [OK]
goodsmileduck/redis-cli redis-cli on alpine 9 [OK]
circleci/redis CircleCI images for Redis 7 [OK]
centos/redis-32-centos7 Redis in-memory data structure store, used a… 5
clearlinux/redis Redis key-value data structure server with t… 3
tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove… 1 [OK]
wodby/redis Redis container image with orchestration 1 [OK]
xetamus/redis-resource forked redis-resource 0 [OK]
mango@wanzhouyi:~$
第二步:拉取鏡像
由于本文沒有特殊要求轻黑,直接拉取最新鏡像糊肤。
mango@wanzhouyi:~$ sudo docker pull redis:latest
latest: Pulling from library/redis
69692152171a: Pull complete
a4a46f2fd7e0: Pull complete
bcdf6fddc3bd: Pull complete
b7e9b50900cc: Pull complete
5f3030c50d85: Pull complete
63dae8e0776c: Pull complete
Digest: sha256:365eddf64356169aa0cbfbeaf928eb80762de3cc364402e7653532bcec912973
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
mango@wanzhouyi:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest bc8d70f9ef6c 10 days ago 105MB
mango@wanzhouyi:~$
第三步:運行容器
mango@wanzhouyi:~$ sudo docker run -itd --name redis-test -p 6379:6379 redis
f9dc3718228ae45b20d82499b5311cce2634e13bdf77157313937a9a910bc077
mango@wanzhouyi:~$ sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9dc3718228a redis "docker-entrypoint.s…" 45 seconds ago Up 44 seconds 0.0.0.0:6379->6379/tcp redis-test
mango@wanzhouyi:~$
上面的命令通過-p 6379:6379將容器服務的 6379 端口映射到宿主機的 6379 端口。外部可以直接通過宿主機ip:6379 訪問到 Redis 的服務氓鄙。
第四步:進入容器愉快地CRUD
mango@wanzhouyi:~$ sudo docker exec -it redis-test /bin/bash
root@f9dc3718228a:/data# redis-cli
127.0.0.1:6379> set wanzhouyi strong
OK
127.0.0.1:6379> get wanzhouyi
"strong"
127.0.0.1:6379> set wanzhouyi nice
OK
127.0.0.1:6379> get wanzhouyi
"nice"
127.0.0.1:6379> del wanzhouyi
(integer) 1
127.0.0.1:6379>
第五步:在宿主機上愉快地CRUD
mango@wanzhouyi:~$ redis-cli
127.0.0.1:6379> set wanzhouyi strong
OK
127.0.0.1:6379> get wanzhouyi
"strong"
127.0.0.1:6379> set wanzhouyi nice
OK
127.0.0.1:6379> get wanzhouyi
"nice"
127.0.0.1:6379> del wanzhouyi
(integer) 1
127.0.0.1:6379>
mango@wanzhouyi:~$
最后
本文通過三種方式輕輕地玩耍了一下redis馆揉,分別是在線方式、ubuntu主機方式抖拦、redis容器方式升酣。作為使用redis的第一扇大門將由此開啟。