Redis 安装(ubuntu/mac/windows)_初步使用

本文详细介绍了在Ubuntu、Mac和Windows平台上安装Redis的方法。包括在Ubuntu上使用apt-get安装、检查Redis服务器状态、设置访问密码和远程访问,Mac下使用brew安装及配置,以及Windows下从官网下载并配置Redis。

Redis 安装(ubuntu/mac/windows)

前言

Redis是常用基于内存的Key-Value数据库,比Memcache更先进,支持多种数据结构,高效,快速。用Redis可以很轻松解决高并发的数据访问问题;作为实时监控信号处理也非常不错。

ubuntu16.04下安装redis

  • 安装Redis服务器端
 sudo apt-get install redis-server

安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序

  • 检查Redis服务器系统进程
root@iZj6c51e3fbtaydnscnwf8Z:~# ps -aux|grep redis
root      4225  0.0  0.0  14224   916 pts/3    S+   01:14   0:00 grep --color=auto redis            
redis    31479  0.0  0.2  40136  3028 ?        Ssl  Jan02   0:19 /usr/bin/redis-server *:6379       
root@iZj6c51e3fbtaydnscnwf8Z:~# 
  • 通过启动命令检查Redis服务器状态
root@iZj6c51e3fbtaydnscnwf8Z:~# netstat -nlt|grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN                          
tcp6       0      0 :::6379                 :::*                    LISTEN                          
root@iZj6c51e3fbtaydnscnwf8Z:~# 

通过启动命令,查看当前redis状态

root@iZj6c51e3fbtaydnscnwf8Z:~# sudo /etc/init.d/redis-server status
● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-01-02 19:03:53 CST; 6h ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 31463 ExecStopPost=/bin/run-parts --verbose /etc/redis/redis-server.post-down.d (code=exi
ted, status=0/SUCCESS)
  Process: 31457 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 31451 ExecStop=/bin/run-parts --verbose /etc/redis/redis-server.pre-down.d (code=exited, 
status=0/SUCCESS)
  Process: 31480 ExecStartPost=/bin/run-parts --verbose /etc/redis/redis-server.post-up.d (code=exit
ed, status=0/SUCCESS)
  Process: 31477 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCES
S)
  Process: 31471 ExecStartPre=/bin/run-parts --verbose /etc/redis/redis-server.pre-up.d (code=exited
, status=0/SUCCESS)
 Main PID: 31479 (redis-server)
   CGroup: /system.slice/redis-server.service
           └─31479 /usr/bin/redis-server *:6379

Jan 02 19:03:53 iZj6c51e3fbtaydnscnwf8Z systemd[1]: Stopped Advanced key-value store.
Jan 02 19:03:53 iZj6c51e3fbtaydnscnwf8Z systemd[1]: Starting Advanced key-value store...
Jan 02 19:03:53 iZj6c51e3fbtaydnscnwf8Z systemd[1]: Started Advanced key-value store.
root@iZj6c51e3fbtaydnscnwf8Z:~# 
  • 访问Redis 访问redis有本地访问和远程访问
    安装Redis服务器,会自动地一起安装Redis命令行客户端程序。
    在本机输入redis-cli命令就可以启动,客户端程序访问Redis服务器。
    本机:
root@iZj6c51e3fbtaydnscnwf8Z:~# redis-cli
127.0.0.1:6379> status                                                                              
(error) ERR unknown command 'status'
127.0.0.1:6379> help                                                                                
redis-cli 3.0.6
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit
127.0.0.1:6379> 

远程访问,需要在远程端本地,配置redis环境,安装redis即可。如果是windos,访问如下:
在这里插入图片描述

  • 修改Redis的配置 使用Redis的访问账号
    默认情况下,访问Redis服务器是不需要密码的,为了增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为redisredis。
    用vi打开Redis服务器的配置文件redis.conf
sudo vi /etc/redis/redis.conf
#取消注释requirepass
requirepass redisredis
  • 让Redis服务器被远程访问

默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。
用vi打开Redis服务器的配置文件redis.conf

sudo vi /etc/redis/redis.conf

#注释bind
#bind 127.0.0.1
  • 修改后,重启Redis服务器
    重启redis:systemctl restart redies.service

mac下安装redis

  • 安装 使用brew 安装即可
ogon:~ wfc$ brew install redis
Error: Another active Homebrew update process is already in progress.
Please wait for it to finish or terminate it to continue.
Warning: redis 4.0.11 is already installed and up-to-date
To reinstall 4.0.11, run `brew reinstall redis`
bogon:~ wfc$ brew uninstall redis
Uninstalling /usr/local/Cellar/redis/4.0.11... (13 files, 2.8MB)
redis 4.0.2 1 is still installed.
Remove all versions with `brew uninstall --force redis`.
bogon:~ wfc$ brew uninstall --force redis
Uninstalling redis... (26 files, 5.6MB)
bogon:~ wfc$ brew install redis
Updating Homebrew...
Warning: redis 4.0.11 is already installed and up-to-date
To reinstall 4.0.11, run `brew reinstall redis`
bogon:~ wfc$ cd /usr/local
bogon:local wfc$ ls -l
  • 配置文件路径: /usr/local/etc/redis.conf 更改配置文件,参看ubuntu 安装环境,修改配置文件

Mac使用Redis参考
启动服务: redis-server
正式执行命令行:redis-cli

mac环境下,Redis客户端:redis-desktop-manager
Redis桌面管理工具Mac版是一款基于Qt5的跨平台Redis桌面管理软件,支持C++编写,响应迅速,性能好。但不支持数据库备份与恢复。
官方现在是收费的,其他的渠道基本上也都要花钱购买,购买完分享给大家。
csdn 资源下载推荐:
https://download.youkuaiyun.com/download/itjavawfc/10894504

windows下安装redis

官网:https://redis.io/
最终会指向 github 地址:https://github.com/MicrosoftArchive/redis
在这里插入图片描述

在这里插入图片描述

下载得到的文件目录,输入: redis-server.exe redis.windows.conf 当你看见标志性图腾,就代表安装成功了。

Windows 下初步使用

  • 配置好命令环境,下载目录直接配置到系统变量path下
  • 在下载目录下,直接单击redis-server.exe 命令执行.出现图腾窗口,代表服务启动了
  • 另启一个窗口,输入命令redis-cli.exe -h 127.0.0.1 -p 6379 结果如下,就可以进行基本命令操作了。
    在这里插入图片描述
<think> 我们注意到错误信息是"sh: redis: not found",这表明系统在/tmp/redis目录下尝试执行redis命令时,没有找到redis可执行文件。 可能的原因和解决方法如下: 1. **Redis安装**:系统可能没有安装Redis客户端。需要安装Redis。 在基于Debian的系统(如Ubuntu)上,可以使用: ```bash sudo apt update sudo apt install redis-tools ``` 2. **环境变量问题**:如果Redis已经安装,但不在PATH环境变量中,则需要指定完整路径。 通常Redis安装在/usr/bin/redis-cli,因此可以尝试: ```bash /usr/bin/redis-cli -s /tmp/redis/redis_cfg.sock fGetALL REBOOT_SCHEDULE_KEY ``` 3. **指定socket连接**:注意,上面的命令中使用了`-s`参数来指定socket文件。 4. **检查Redis服务状态**:确保Redis服务正在运行,并且socket文件存在。 检查socket文件: ```bash ls -l /tmp/redis/redis_cfg.sock ``` 5. **权限问题**:当前用户是否有权限访问该socket文件? 可以使用`ls -l`查看权限,并使用`chmod`或`chown`调整。 ### 正确执行命令的步骤 #### 步骤1:检查Redis客户端是否安装 ```bash which redis-cli ``` 如果返回路径(如/usr/bin/redis-cli),则已安装;否则需要安装。 #### 步骤2:使用完整命令 ```bash # 如果已安装使用redis-cli的完整路径(或直接使用redis-cli,如果它在PATH中) redis-cli -s /tmp/redis/redis_cfg.sock fGetALL REBOOT_SCHEDULE_KEY ``` #### 步骤3:如果仍然找不到命令,考虑使用绝对路径 ```bash /usr/bin/redis-cli -s /tmp/redis/redis_cfg.sock fGetALL REBOOT_SCHEDULE_KEY ``` #### 步骤4:如果提示没有fGetALL命令 注意:Redis标准命令是`HGETALL`(获取哈希表所有字段)或`GET`(获取字符串值)。`fGetALL`可能是自定义命令或拼写错误。请确认命令的正确性。 ### 常见错误排查表 | 错误现象 | 可能原因 | 解决方案 | |----------|----------|----------| | `redis: not found` | 未安装redis-cli | 安装redis-tools包 | | `Can't connect to...` | socket文件不存在或服务未运行 | 检查Redis服务状态 | | `NOAUTH Authentication required` | 需要密码 | 使用`-a`参数提供密码 | | `ERR unknown command` | 命令错误 | 检查命令拼写,如使用`HGETALL`代替`fGetALL` | ### 示例命令(假设使用HGETALL) ```bash redis-cli -s /tmp/redis/redis_cfg.sock HGETALL REBOOT_SCHEDULE_KEY ``` §§相关问题§§ 1. 如何在Linux系统中检查Redis服务是否正在运行? 2. 使用redis-cli连接Redis服务器时,除了socket方式还有哪些连接方式? 3. 当遇到`redis-cli: command not found`错误时,如何正确安装Redis客户端工具? 4. Redis的`HGETALL`命令和`GET`命令分别适用于什么数据类型? 5. 如何解决因权限不足导致的Redis socket连接失败问题? </think>### 解决 Redis 命令未找到问题的方法 #### 错误分析 ```bash /tmp/redis # redis /tmp/redis/redis_cfg.sock> fGetALL REBOOT_SCHEDULE_KEY sh: redis: not found ``` 此错误表明: 1. `redis` 可执行文件未在系统 PATH 中 2. 当前目录 `/tmp/redis` 下无 redis 客户端 3. 命令语法格式错误(正确命令是 `redis-cli` 而非 `redis`) ### 解决方案 #### 1. 使用完整路径执行 redis-cli ```bash # 查找 redis-cli 位置 find / -name redis-cli 2>/dev/null # 使用找到的路径执行 /usr/bin/redis-cli -s /tmp/redis/redis_cfg.sock GETALL REBOOT_SCHEDULE_KEY ``` #### 2. 检查连接参数是否正确 | 参数选项 | 说明 | 示例 | |---------|------|------| | `-s` | 指定 socket 文件 | `-s /tmp/redis/redis_cfg.sock` | | `-h` | Redis 服务器 IP | `-h 127.0.0.1` | | `-p` | Redis 端口 | `-p 6379` | | `-a` | 认证密码 | `-a yourpassword` | #### 3. 正确的命令语法 ```bash # 获取键值 redis-cli -s /tmp/redis/redis_cfg.sock GET REBOOT_SCHEDULE_KEY # 获取哈希表所有字段 redis-cli -s /tmp/redis/redis_cfg.sock HGETALL REBOOT_SCHEDULE_KEY # 安全扫描键 redis-cli -s /tmp/redis/redis_cfg.sock SCAN 0 MATCH REBOOT_* ``` #### 4. 安装 Redis 客户端(如未安装) ```bash # Alpine Linux (Docker常用) apk add redis # Ubuntu/Debian apt-get update && apt-get install redis-tools # CentOS/RHEL yum install redis ``` #### 5. 环境变量修复 ```bash # 临时添加到PATH export PATH=$PATH:/usr/local/bin:/usr/bin # 永久生效(写入~/.bashrc或/etc/profile) echo 'export PATH=$PATH:/usr/bin' >> ~/.bashrc source ~/.bashrc ``` ### 关键排查步骤 1. **验证 Redis 服务状态**: ```bash # 检查socket文件是否存在 ls -l /tmp/redis/redis_cfg.sock # 检查服务是否运行 ps aux | grep redis-server ``` 2. **测试基本连接**: ```bash redis-cli -s /tmp/redis/redis_cfg.sock PING # 正常应返回 PONG ``` 3. **使用交互模式**: ```bash redis-cli -s /tmp/redis/redis_cfg.sock 127.0.0.1:6379> KEYS REBOOT_* ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

野火少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值