Redis 笔记(01)— 安装、启动配置、开启远程连接、设置密码、远程连接

1. Redis 简介

1月份将 《Redis入门指南》过了一遍,现将 Redis 五大类型的常用命令做一总结,留着后续备用。

RedisRemoteDictionary Server (远程字典服务器)的缩写,它以字典结构存储数据,并允许其它应用通过 TCP 的协议读写字典中的内容。Redis 字典中的键值除了可以是字符串,还可以是其它数据类型。支持的数据类型如下:

  • 字符串类型

  • 散列类型

  • 列表类型

  • 集合类型

  • 有序集合类型

Redis 数据库中的所有数据都存储在内存中,由于内存的读写速度远快于硬盘,因此 Redis 在性能上对比其他基于硬盘存储的数据有非常明显的优势。在普通笔记本电脑上,Redis 可以在一秒内读写超过 10 万以上的键值。

Redis 提供了对于持久化的支持,即可以将内存中的属于异步写入到硬盘中,同时不影响继续提供服务。

除此之外,Redis 的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易实现一个高性能的优先级队列。同时还支持“发布/订阅”的消息模式。

2. Redis 安装

2.1 源码安装

  1. 下载,解压,编译:
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
  1. 二进制文件是编译完成后在 src 目录下,通过下面的命令启动 redis 服务:
./redis-server
# 或者后台运行服务器,daemonize表示在后台运行
./redis-server --daemonize yes
  1. 你可以使用内置的客户端命令 redis-cli 进行使用:
./redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

2.2 工具管理包安装

# 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 

2.3 Docker 安装

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
  • -p 6378:6379 容器redis 端口6379 映射 宿主机未6378
  • –name redis01 容器 名字 为 redis01
  • -v /root/redis/redis01/conf/redis.conf:/etc/redis/redis.conf 容器 /etc/redis/redis.conf 配置文件 映射宿主机 /root/redis/redis01/conf/redis.conf。 会将宿主机的配置文件复制到docker中。

重要: 配置文件映射,docker镜像redis 默认无配置文件。

  • -v /root/redis/redis01/data:/data 容器 /data 映射到宿主机 /root/redis/redis01/data
  • -d redis 后台模式启动 redis
  • redis-server /etc/redis/redis.conf redis 将以 /etc/redis/redis.conf 为配置文件启动
  • –appendonly yes 开启redis 持久化

重要: docker 镜像reids 默认 无配置文件启动

3. Redis 配置

3.1 启动配置

  1. 默认启动
$ redis-server

Redis 安装后默认就以这种方式启动。

  1. 运行启动
$ redis-server --configKey1 configValue1 --configKey2 configValue2

例如

$ redis-server --port 6380
  1. 配置文件启动

将配置写到指定文件里,例如我们将配置写到了 /opt/redis/redis.conf 中,那么只需要执行如下命令即可启动 Redis :

$ redis-server /opt/redis/redis.conf

redis.conf 文件参数说明:

图片来源于 https://gitbook.cn/books/5a196cb24a97251edce2ae9a/index.html

redis.conf参数说明

3.2 开启远程连接

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 前面加 # 号注释掉

3.3 设置密码

/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 命令认证。

4. Redis 测试

4.1 本地连接

输入 redis-cli 命令可启动 Redis 客户端,输入 ping 命令有如下返回值时,说明 Redis 服务正常

wohu@Z:/$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

4.2 远程连接

wohu@Z:/$ redis-cli -h host_ip -p port

其中 host_ipredis 远程服务器地址, portredis 的端口地址,默认为 6379

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值