赞
踩
1月份将 《Redis入门指南》过了一遍,现将 Redis
五大类型的常用命令做一总结,留着后续备用。
Redis
是 RemoteDictionary Server
(远程字典服务器)的缩写,它以字典结构存储数据,并允许其它应用通过 TCP
的协议读写字典中的内容。Redis
字典中的键值除了可以是字符串,还可以是其它数据类型。支持的数据类型如下:
字符串类型
散列类型
列表类型
集合类型
有序集合类型
Redis
数据库中的所有数据都存储在内存中,由于内存的读写速度远快于硬盘,因此 Redis
在性能上对比其他基于硬盘存储的数据有非常明显的优势。在普通笔记本电脑上,Redis
可以在一秒内读写超过 10 万以上的键值。
Redis
提供了对于持久化的支持,即可以将内存中的属于异步写入到硬盘中,同时不影响继续提供服务。
除此之外,Redis
的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易实现一个高性能的优先级队列。同时还支持“发布/订阅”的消息模式。
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar xzf redis-4.0.8.tar.gz
cd redis-4.0.8
make
cd src
src
目录下,通过下面的命令启动 redis
服务:./redis-server
# 或者后台运行服务器,daemonize表示在后台运行
./redis-server --daemonize yes
redis-cli
进行使用:./redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
# mac
brew install redis
# ubuntu
sudo apt-get install -y redis-server
# redhat
yum install redis
# 运行客户端
redis-cli
-y
表示确认安装
安装完成后,Redis
服务器会自动启动,我们检查 Redis
服务器程序
wohu@Z:/$ service redis status
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-12-20 21:51:23 CST; 2min 17s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 3559 (redis-server)
CGroup: /system.slice/redis-server.service
└─3559 /usr/bin/redis-server 127.0.0.1:6379
docker pull redis
docker run -p 6378:6379 \
--name redis01 \
-v /root/redis/redis01/conf/redis.conf:/etc/redis/redis.conf \
-v /root/redis/redis01/data:/data \
-d redis redis-server /etc/redis/redis.conf \
--appendonly yes
重要: 配置文件映射,docker镜像redis 默认无配置文件。
重要: docker 镜像reids 默认 无配置文件启动
$ redis-server
Redis
安装后默认就以这种方式启动。
$ redis-server --configKey1 configValue1 --configKey2 configValue2
例如
$ redis-server --port 6380
将配置写到指定文件里,例如我们将配置写到了 /opt/redis/redis.conf
中,那么只需要执行如下命令即可启动 Redis
:
$ redis-server /opt/redis/redis.conf
redis.conf
文件参数说明:
图片来源于 https://gitbook.cn/books/5a196cb24a97251edce2ae9a/index.html
Redis
默认是不支持远程连接的,要开启这个功能,需要作如下修改,执行以下命令
sudo cat /etc/redis/redis.conf
找到如下节点
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
bind 127.0.0.1
将 bind 127.0.0.1
前面加 #
号注释掉
将 /etc/redis/redis.conf
文件下 requirepass 123456
(密码设置为 123456 )
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
$ requirepass foobared
注意:配置 Redis
复制的时候如果主数据库设置了密码,需要在从数据库的配置文件中通过 masterauth
参数设置主数据库的密码,以使从数据库连接主数据库时自动使用 AUTH
命令认证。
输入 redis-cli
命令可启动 Redis
客户端,输入 ping
命令有如下返回值时,说明 Redis
服务正常
wohu@Z:/$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
wohu@Z:/$ redis-cli -h host_ip -p port
其中 host_ip
为 redis
远程服务器地址, port
为 redis
的端口地址,默认为 6379
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。