redis系列--2.redis轻食入门

看了上一篇的介绍,是不是急不可耐地想试试怎么玩转redis?这就来轻食入门篇。这篇文章主要使用CRUD四个命令。

try redis

官方提供了一个线上客户端用于测试和练习。网址是https://try.redis.io/

tryredisio.png

> set wanzhouyi strong
OK
> get wanzhouyi
"strong"
> set wanzhouyi nice
OK
> get wanzhouyi
"nice"
> del wanzhouyi
(integer) 1
> get wanzhouyi
(nil)

命令解释:

  1. set wanzhouyi strong (增)设置wanzhouyi为键,strong为值
  2. get wanzhouyi (查)获取键为wanzhouyi的值
  3. set wanzhouyi nice (改)修改wanzhouyi的值为nice
  4. del wanzhouyi (删)删除wanzhouyi这个键

ubuntu本地安装

第一步:安装redis

安装命令:sudo apt install redis-server

ubuntu_install1.png

第二步:启动redis

启动命令:redis-server

start_redis_server.png

从上图中也可以看到一个关键信息,默认端口是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的第一扇大门将由此开启。

你可能感兴趣的:(redis系列--2.redis轻食入门)