【6】redis安装(离线、在线)

368 阅读2分钟

一:基于docker安装

运行命令

docker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 --name redis -v /usr/local/redis/conf/redis.conf:/etc/redis/redis.conf -v /usr/local/redis/data:/data -d redis redis-server /etc/redis/redis.conf  --appendonly yes  --requirepass 000415

说明:

--restart=always 总是开机启动

--log是日志方面的

-p 6379:6379 将6379端口挂载出去

--name 给这个容器取一个名字

-v 数据卷挂载

  • /usr/local/redis/redis.conf:/etc/redis/redis.conf 这里是将 liunx 路径下的myredis.conf 和redis下的redis.conf 挂载在一起。
  • /usr/local/redis/data:/data 这个同上 -d redis 表示后台启动redis redis-server /etc/redis/redis.conf 以配置文件启动redis,加载容器内的conf文件,最终找到的是挂载的目录 /etc/redis/redis.conf 也就是liunx下的/home/redis/myredis/myredis.conf

–appendonly yes 开启redis 持久化

–requirepass 000415 设置密码

二:离线安装redis

1.安装依赖(没有gcc环境就执行下面命令,有则忽略):

yum install -y gcc-c++

无法使用yum安装,请参考nginx离线安装中的gcc安装

2.下载redis包(下载到自定义目录,这里是下载到 /usr/local):

curl -O http://download.redis.io/releases/redis-5.0.4.tar.gz

服务器离线下载:redis-5.0.4.tar.gz

3.将压缩包上传到服务器并解压:

# 解压并制定解压目录
tar -zxvf /usr/local/redis-5.0.4.tar.gz

# 进入到解压目录下
cd /usr/local/redis-5.0.4

4.编译安装到指定目录:

# 编译
make

# 安装到指定目录
make PREFIX=/usr/local/redis install

5.复制配置文件到redis目录:

# 创建目录用于存储redis配置文件
mkdir -p /usr/local/redis/config

# 复制配置文件到/usr/local/redis/config
cp /usr/local/redis-5.0.4/redis.conf   /usr/local/redis/config/

6.修改配置文件:

# 修改配置文件
vi /usr/local/redis/config/redis.conf

# 修改内容如下:
# 允许后台运行
daemonize yes 

# redis 默认密码 xxx为自定义密码
requirepass xxxx

7.将redis添加到守护进程并设置开机自启:

# 创建redis.service文件
vim /etc/systemd/system/redis.service

# 添加如下内容:
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/config/redis.conf
ExecStop=/usr/local/redis/bin/redis-server -s stop
PrivateTmp=true
User=root
Group=root

[Install]
WantedBy=multi-user.target

常用命令如下:

  • 启动redis:systemctl start redis
  • 关闭redis:systemctl stop redis
  • 设置开机自启:systemctl enable redis
  • 关闭开机自启:systemctl disable redis
  • 查看运行状态:systemctl status redis

8.全局使用redis-cli配置:

#编辑配置文件
vim /etc/profile

#在配置文件最后加上这句话
export PATH=$PATH:/usr/local/redis/bin