安装Redis

本文介绍如何通过源码包安装Redis,并进行基本配置,包括设置访问权限及将其配置为系统服务。

https://redis.io/

1.源码包安装
# tar -zxf redis-4.0.6.tar.gz -C /usr/local/
# cd /usr/local/ && mv redis-4.0.6 redis
# cd redis
# vim redis.conf, 修改如下:
bind 0.0.0.0 # 可以从任何IP访问
daemonize yes # 允许作为后台服务运行
# redis-server ./redis.conf
# ps -ef | grep redis
# redis-cli
127.0.0.1:6379> set key1 123456
OK
127.0.0.1:6379> get key1
"123456"
127.0.0.1:6379> exit


2.增加访问密码

# vim redis.conf 
requirepass bigdata
# redis-cli
127.0.0.1:6379> shutdown save # 关闭redis
not connected> exit
# ps -ef |grep redis
root       6410   2440  0 17:53 pts/0    00:00:00 grep --color=auto redis
# redis-server ./redis.conf # 启动redis
# redis-cli
127.0.0.1:6379> get key1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth bigdata
OK
127.0.0.1:6379> get key1

"123456"


3.将redis设置成系统服务
# pwd
/usr/local/redis
# cd utils/
# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis/redis.log
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/data
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /usr/local/redis/redis.conf
Log file       : /usr/local/redis/redis.log
Data dir       : /usr/local/redis/data
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
# chkconfig --list|grep redis
redis_6379     0:off 1:off 2:on 3:on 4:on 5:on 6:off
# systemctl status redis_6379
● redis_6379.service - LSB: start and stop redis_6379
   Loaded: loaded (/etc/rc.d/init.d/redis_6379)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)
# systemctl stop redis_6379
# systemctl start redis_6379
# ps -ef|grep redis
root       6412      1  0 17:53 ?        00:00:00 redis-server 0.0.0.0:6379
root       6530   2440  0 18:03 pts/0    00:00:00 grep --color=auto redis
# vim /etc/init.d/redis_6379
# redis-cli # 测试使用
127.0.0.1:6379> auth bigdata
OK
127.0.0.1:6379> set key3 abc
OK
127.0.0.1:6379> get key3
"abc"
在 Windows 系统上安装 Redis 有多种方式,主要包括使用 WSL(Windows Subsystem for Linux)运行原生的 Linux 版本 Redis、通过已编译的 Windows 兼容版本安装,或者以服务形式进行部署。以下是详细的安装方法: ### 使用 WSL 安装 Redis 1. **启用 WSL 并安装 Linux 发行版** - 打开 PowerShell,以管理员身份运行以下命令: ```powershell wsl --install ``` - 完成后重启系统,并设置默认的 Linux 发行版(如 Ubuntu)。 2. **更新系统并安装 Redis** - 启动 WSL 终端,输入以下命令更新包管理器并安装 Redis: ```bash sudo apt update sudo apt install redis-server ``` 3. **配置与启动 Redis** - 默认配置文件位于 `/etc/redis/redis.conf`,可根据需求修改。 - 启动 Redis 服务: ```bash sudo service redis start ``` - 验证是否正常运行: ```bash redis-cli ping ``` 若返回 `PONG`,则表示安装成功[^1]。 ### 使用 Windows 编译版本安装 Redis 1. **下载适用于 Windows 的 Redis** - 可从 GitHub 获取适用于 Windows 的 Redis 发行版本,例如 Redis-x64-3.2.100 或更高版本。 2. **解压并配置 Redis** - 将下载的压缩包解压到指定目录(如 `D:\Redis`)。 - 修改 `redis.windows.conf` 文件中的配置项(如绑定 IP 地址、端口等)。 3. **启动 Redis 服务器** - 打开命令提示符(CMD),进入 Redis 解压目录并运行: ```cmd cd D:\Redis\Redis-x64-3.2.100 redis-server redis.windows.conf ``` 4. **将 Redis 安装为 Windows 服务** - 在 CMD 中执行以下命令将其注册为服务: ```cmd redis-server --service-install redis.windows.conf ``` - 成功安装后,可通过“服务”管理界面启动或停止 Redis 服务[^2]。 ### 自定义安装路径与多实例配置 1. **复制 Redis 文件至新路径** - 假设目标路径为 `E:\Program Files\Redis2`,可将原有 Redis 文件夹内容复制至此。 2. **修改配置文件** - 编辑 `redis.conf` 文件,更改监听 IP 地址(`bind`)和端口号(`port`)以避免冲突。 3. **安装多个 Redis 实例** - 每个实例应使用不同的配置文件,分别指定各自的端口和服务名称。 - 使用类似命令安装服务: ```cmd redis-server --service-install redis-instance2.conf --service-name redisService2 ``` - 此方法允许在同一台机器上运行多个 Redis 服务[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值