前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安装Redis

安装Redis

作者头像
吟风者
发布2019-07-24 12:48:55
5490
发布2019-07-24 12:48:55
举报
文章被收录于专栏:吟风者吟风者

1 解压Redis

创建一个redis的文件夹,用户存放redis文件

代码语言:javascript
复制
mkdir /usr/local/redis

通过ftp工具把下载好的redis安装包上传到redis文件夹下。

解压:

代码语言:javascript
复制
cd /usr/local/redis
tar -vxzf redis-3.2.10.tar.gz

解压完成

2 编译

代码语言:javascript
复制
cd redis-3.2.10
make

如果编译失败,请检查是否安装了gcc环境,未安装环境,请先安装:

代码语言:javascript
复制
apt-get install gcc

清除上次编译失败的文件:

代码语言:javascript
复制
make clean

3 安装

代码语言:javascript
复制
make install

4 启动

代码语言:javascript
复制
cd src
./redis-server

查看是否启动

代码语言:javascript
复制
ps -aux|grep redis

测试

代码语言:javascript
复制
./redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

5 修改配置

进入redis根目录:

代码语言:javascript
复制
cd /etc/local/redis/ redis-3.2.10

编辑配置文件:

代码语言:javascript
复制
vim redis.conf

远程访问

注释掉bind 127.0.0.1可以使所有的ip访问redis

若是想指定多个ip访问,但并不是全部的ip访问,可以bind

在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,如下:

代码语言:javascript
复制
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address 
was specified, no authentication password is requested to clients. In this mode connections are only 
accepted from the loopback interface. If you want to connect from external computers to Redis you may 
adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET 
protected-mode no' from the loopback interface by connecting to Redis from the same host the server is 
running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG 
REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by 
editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the 
server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) 
Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in 
order for the server to start accepting connections from the outside.

修改办法:protected-mode no

设置为守护进程后台启动

修改:daemonize no

使用redis账号访问

默认情况下,访问Redis服务器是不需要密码的,为了增加安全性,设置Redis服务器的访问密码,取消requirepass前的注释#,并设置密码

requirepass 123456

1.6 设置开机启动

在redis目录下找到 utils/redis_init_script 复制到 /etc/init.d/redis 打开文件进行修改

代码语言:javascript
复制
cp redis_init_script /etc/init.d/redis   # 复制文件
cd /etc/init.d/                         # 进入文件目录
vim redis                              #  编辑配置文件redis

原配置文件

代码语言:javascript
复制
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

修改后:

代码语言:javascript
复制
#!/bin/sh
#
# chkconfig:   2345 90 10   ---这里修改
# description:  Redis is a persistent key-value database   ---这里修改

### BEGIN INIT INFO
# Provides:     redis_6379
# Required-Start:          ---这里修改
# Required-Stop:           ---这里修改            
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"  # ---这里修改

修改完配置文件后执行下面两条命令

代码语言:javascript
复制
chmod +x /etc/init.d/redis  # 取得权限
update-rc.d redis defaults  # 加载到系统自启动文件

其他命令:

代码语言:javascript
复制
service redis start     #启动服务 
service redis stop      #停止服务
service redis restart       #重启服务

重启电脑,查看redis是否启动

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 解压Redis
  • 2 编译
  • 3 安装
  • 4 启动
  • 5 修改配置
  • 在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,如下:
    • 1.6 设置开机启动
    相关产品与服务
    云数据库 Redis
    腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档