全栈系列-搭建redis服务

396 阅读3分钟

Max OS 安装

有两种方式

  1. 通过 brew 安装 (推荐
brew install redis
  1. 通过源码安装
# 确保文件夹存在
sudo mkdir -p /usr/local/lib/redis

cd /usr/local/lib/redis

# 下载redis安装包
curl -O http://download.redis.io/releases/redis-6.0.7.tar.gz
  
# 解压  
tar xzf redis-6.0.7.tar.gz

# 移动
mv redis-6.0.7 /usr/local/redis

# 进入redis目录
cd /usr/local/redis

# 编译
sudo make
sudo make test
sudo make install

# 移动文件
mv redis.conf /etc/redis.conf

启动redis服务

redis-server

看到以下界面, 说明启动成功了

42468:C 10 Sep 2020 17:04:10.799 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
42468:C 10 Sep 2020 17:04:10.799 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=42468, just started
42468:C 10 Sep 2020 17:04:10.799 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
42468:M 10 Sep 2020 17:04:10.801 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 42468
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

42468:M 10 Sep 2020 17:04:10.805 # Server initialized
42468:M 10 Sep 2020 17:04:10.806 * Ready to accept connections


window 安装

下载地址: github.com/tporadowski…

下载 Redis-x64-xxx.mis 安装包

下载完成后,双击安装包,按照提示,安装redis

安装完成后, 在shell命令行执行

redis-server.exe redis.windows.conf

出现以下界面,说明安装成功

Centos 安装

以下几种安装方式:

1. 使用EPEL安装(推荐

安装epel-release

sudo yum install epel-release

通过yum安装redis

sudo yum install redis

启动redis

sudo systemctl start redis

2. 通过源码安装

# 确保文件夹存在
sudo mkdir -p /usr/local/lib/redis

cd /usr/local/lib/redis

# 下载redis安装包
wget http://download.redis.io/releases/redis-6.0.7.tar.gz
  
# 解压  
tar xzf redis-6.0.7.tar.gz

# 移动
mv redis-6.0.7 /usr/local/redis

# 进入redis目录
cd /usr/local/redis

# 编译
sudo make
sudo make test
sudo make install

# 移动文件
mv redis.conf /etc/redis.conf

启动redis服务

redis-server

Centos部署redis服务

  1. 修改配置文件
vi /etc/redis.conf

修改内容如下

# 将no改为yes, 以守护进程启动redis
daemonize yes

# 注释以下代码, 允许所有IP连接
# bind 127.0.0.1

# 设置密码
requirepass your-password

# 设置redis日志级别
loglevel debug

# 设置日志路径
logfile /var/log/redis/redis.log

# 禁用危险命令
rename-command CONFIG ""
rename-command keys ""
rename-command flushall ""
rename-command flushdb ""
  1. 创建redis日志文件夹
mkdir -p /var/log/redis
  1. 设置日志切割
vim /etc/logrotate.d/redis

# 内容如下
/var/log/redis/*.log {
    weekly
    rotate 10
    copytruncate
    delaycompress
    compress
    notifempty
    missingok
}
  1. 启动redis服务
# 停止redis服务
kill -9 $(ps -ef | grep redis |awk '{print $2}')

# 重启服务
redis-server /etc/redis.conf
  1. 验证配置
[root@VM-0-7-centos /]# ./src/redis-cli 
127.0.0.1:6379> info
NOAUTH Authentication required.     #没验证密码前,对redis进行操作
127.0.0.1:6379> auth 123456         # 验证密码
OK
127.0.0.1:6379> info stats          # 可以正常操作redis
# Stats
total_connections_received:4
...
127.0.0.1:6379> keys *
(error) ERR unknown command 'keys'       #禁用keys命令
127.0.0.1:6379> flushall
(error) ERR unknown command 'flushall'   #禁用flushall
127.0.0.1:6379> flsuhdb
(error) ERR unknown command 'flsuhdb'    #禁用flushdb
  1. redis连接配置
host = 服务器ip
port = 6379
password = 上面设置的密码