Fork me on GitHub

redis 安装 与错误解决办法

redis 安装与安装中遇到的错误

redis 安装

wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar xzf redis-4.0.11.tar.gz
cd redis-4.0.11
make

启动服务端

src/redis-server

客户端连接与测试

src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

redis常用命令

redis-cli

-h  指定远程登陆ip
-p  指定远程redis访问端口
-n  指定库b编号
-a  指定密码

示例
./redis-cli  -h 127.0.0.1  -p 6379 -n 3  -a djx

远程执行命令
清空所有的数据
./redis-cli  -h 127.0.0.1  -p 6379 -n 3  -a djx  flushall

redis 常用配置

redis设置密码

临时生效

       在命令行用  config set  requirepass  password  来进行设置。重启redis后即失效。

[root@djx2 src]# ./redis-cli 
127.0.0.1:6379> config set  requirepass djx
OK
127.0.0.1:6379> config get  requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth djx
OK
127.0.0.1:6379> config get  requirepass
1) "requirepass"
2) "djx"

永久生效

  通过在redis的配置文件redis.conf 进行配置,在配置文件中有个参数: requirepass  这个就是配置redis访问密码的参数;

requirepass password

然后我们启动的时候需要指定我们的配置文件进行启动。 

redis-server  /etc/redis.conf

 redis 指定端口

默认是6379,我们可以更改成公司内部统一的端口。

port 6379

redis  指定监听

redis 默认绑定的是 127.0.0.1 ,也就是只能本地访问了,如果我们需要让外网也可以进行访问,那么我们需要更改默认的绑定。

bind  0.0.0.0

这样我们就可以让应用访问了。

redis 指定日志文件存放位置

默认日志文件的存放位置是为空的,也就是直接在控制台输出了。

我们可以在logfile中配置日志文件路径。

logfile "/var/log/redis.log"

redis 指定数据存放位置(要指定)

在redis.conf 的 

# The filename where to dump the DB
dbfilename dump.rdb  #指定数据存放的文件名称

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./   #指定数据存放的位置。
创建目录
mkdir /opt/redis/data/ 在配置文件中指定目录 dir /opt/redis/data/

redis 开启rdbchecksum

该参数,在3.2版本和4.0版本是默认开启的,但是在2.4版本中是没有开启的,该参数我们进行使用dump.rdb文件时是有作用的,因为在使用dump.rdb 的时候有该值是会效验该文件的完整性。rdbchecksum设置为no的话就不会效验该文件的完整性。

redis 后台运行

我们可以使用nohup和& 让redis在后台正常运行,并写入日志到/var/log/redis.log

nohup  ./src/redis-server   ./redis.conf  >>/var/log/redis.log 2>&1 &

安全配置

详细见

  • 如果只是内网使用的话,我们可以只监听本地,也就是 bind  127.0.0.1.
  • 设置访问密码,密码设置复杂点,requirepass 
  • 使用专门的用户来运行 Redis,不要使用 root。
    useradd -M -s /sbin/nologin [username]

     

  • 隐藏重要命令

  Redis 无权限分离,其管理员账号和普通账号无明显区分。攻击者登录后可执行任意操作,因此需要隐藏以下重要命令:FLUSHDB, FLUSHALL, KEYS,PEXPIRE, DEL, CONFIG, SHUTDOWN, BGREWRITEAOF, BGSAVE, SAVE, SPOP, SREM, RENAME,DEBUG, EVAL

  我们可以隐藏,也可以将这些命令设置为复杂的字符。

  隐藏命令和重命名命令需要在 配置文件中配置  redis.conf。

  隐藏命令

rename-command CONFIG ""
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command SHUTDOWN ""

  重命名命令

rename-command CONFIG FYConfigdjx
rename-command FLUSHALL FYFlushalldjx
rename-command FLSUHDB FYFlushdbdjx
rename-command SHUTDOWN  FYShutdowndjx

 

安装中遇到的错误

错误1   gcc  编译器没有安装

解决办法 : 安装gcc  编译器

yum  install  gcc  -y

错误2  jemalloc/jemalloc.h: No such file or directory。 (注意,这里需要特别注意)

针对这个错误,我们可以在README.md 文件中看到解释。

---------

Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.

To force compiling against libc malloc, use:

    % make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:

    % make MALLOC=jemalloc

Verbose build
-------------

网上大部分解决办法都是错误的,如下文:

centos(错误解决办法)

make MALLOC=libc

正确解决办法(针对2.2以上的版本)

make distclean  && make

导致出现这个错误的原因

  错误的本质是我们在开始执行make 时遇到了错误(大部分是由于gcc未安装),然后我们安装好了gcc 后,我们再执行make  ,这时就出现了jemalloc/jemalloc.h: No such file or directory。这是因为上次的

编译失败,有残留的文件,我们需要清理下,然后重新编译就可以了。

网上的解决办法是有什么错误吗?

  网上的解决办法虽然最后也是可以成功安装好 redis ,但是是有一些隐患的,首先我们要知道redis 需要使用内存分配器的, make  MALLOC=jemalloc  就是指定内存分配器为 jemalloc ,make MALLOC=libc 就是指定内存分配器为 libc ,这个是有安全隐患的,jemalloc 内存分配器在实践中处理内存碎片是要比libc 好的,而且在README.md 文档也说明到了,jemalloc内存分配器也是包含在源码包里面的,可以在deps  目录下看到 jemalloc 目录。

以上就是我在安装的时候遇到的问题,后续如果还有其他会继续补充。

posted @ 2018-10-08 10:50  自由早晚乱余生  阅读(4730)  评论(2编辑  收藏  举报