现在位置: 首页 > 博客文章 > 电脑相关 > IT开发 > 大数据 > Redis > 正文
大数据学习初级入门教程(二) —— Redis 3.x 单节点的安装、启动和测试
2019年01月12日 16:15:14 Redis ⁄ 共 4271字 暂无评论 ⁄ 被围观 3,112次

在日常开发中,为了满足系统的及时响应性,一般都会在开发中使用 Redis 做缓存,这篇主要介绍下 Redis 3.x 版本的安装、启动及测试。

一、下载 Redis 安装包

这里就不再赘述,本篇测试用的版本为:redis-3.2.9.tar.gz。

二、上传安装包到服务器

也不再赘述。

三、解压安装包

命令:tar -zxvf redis-3.2.9.tar.gz

四、进入安装包

命令:cd redis-3.2.9

五、指定安装目录并安装

命令:make PREFIX=/usr/redis install

安装成功后,可以看到 /usr 下自动生成了 redis 目录,目录 /usr/redis/bin 结构如下:

.
├── redis-benchmark
├── redis-check-aof
├── redis-check-rdb
├── redis-cli
├── redis-sentinel -> redis-server
└── redis-server

0 directories, 6 files

六、拷贝配置文件

从安装包复制配置文件 redis.conf 到 /usr/redis/bin 下

命令:cp redis.conf /usr/redis/bin/

七、修改配置文件

修改配置文件:

命令:cd /usr/redis/bin/

           vi redis.conf 

比如修改端口(默认为6379):

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

比如修改密码(默认为 foobared):

# requirepass foobared

再比如修改运行方式(默认为前台进程运行):

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no

八、启动 redis 服务

命令:./redis-server redis.conf 

如果 daemonize 为 no,启动后效果如下:

36201:M 12 Jan 10:35:42.160 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 36201
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

36201:M 12 Jan 10:35:42.177 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
36201:M 12 Jan 10:35:42.177 # Server started, Redis version 3.2.9
36201:M 12 Jan 10:35:42.179 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
36201:M 12 Jan 10:35:42.180 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
36201:M 12 Jan 10:35:42.180 * The server is now ready to accept connections on port 6379

如果 daemonize 修改为了 yes,则会在后台启动服务,看不到上面的日志,可以查看其进程是否启动。

命令:ps -ef |grep redis

root      37040      1  0 10:41 ?        00:00:00 ./redis-server 127.0.0.1:6379
root      37086   1492  0 10:41 pts/0    00:00:00 grep redis

到此,单点的 Redis 已安装成功,如果有错误,请参考最后的常见错误部分。 

九、简单测试

(1)启动客户端命令

命令:./redis-cli

可以看到有输出:127.0.0.1:6379> 

(2)检测 redis 服务是否启动命令

命令:127.0.0.1:6379> ping

可以看到有输出:PONG

(3)存值命令

命令:127.0.0.1:6379> set testStr redisok

可以看到有输出:OK

(4)取值

命令:127.0.0.1:6379> get testStr

可以看到有输出设置过的值:"redisok"

(5)其它命令自行学习、测试。。。

十、安装过程出现的常见错误

错误一:如果出现错误 gcc: Command not found 或者 cc: command not found,日志如下:

make[3]: gcc: Command not found
make[3]: *** [net.o] Error 127
make[3]: Leaving directory `/root/redis-3.2.9/deps/hiredis'
make[2]: *** [hiredis] Error 2
make[2]: Leaving directory `/root/redis-3.2.9/deps'
make[1]: [persist-settings] Error 2 (ignored)
    CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/root/redis-3.2.9/src'
make: *** [install] Error 2

则说明当前的服务器没有编译环境,需要安装,操作如下:

命令:yum install gcc-c++

看到如下日志,说明安装成功。

Installed:
  gcc-c++.x86_64 0:4.4.7-23.el6                                                                                                                                            

Dependency Installed:
  cloog-ppl.x86_64 0:0.15.7-1.2.el6          cpp.x86_64 0:4.4.7-23.el6                       gcc.x86_64 0:4.4.7-23.el6          glibc-devel.x86_64 0:2.12-1.212.el6       
  glibc-headers.x86_64 0:2.12-1.212.el6      kernel-headers.x86_64 0:2.6.32-754.6.3.el6      libgomp.x86_64 0:4.4.7-23.el6      libstdc++-devel.x86_64 0:4.4.7-23.el6     
  mpfr.x86_64 0:2.4.1-6.el6                  ppl.x86_64 0:0.10.2-11.el6                     

Dependency Updated:
  glibc.x86_64 0:2.12-1.212.el6           glibc-common.x86_64 0:2.12-1.212.el6           libgcc.x86_64 0:4.4.7-23.el6           libstdc++.x86_64 0:4.4.7-23.el6          

Complete!

错误二:如果出现错误 Newer version of jemalloc required,日志如下:

cd src && make install
make[1]: Entering directory `/root/redis-3.2.9/src'
    CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/root/redis-3.2.9/src'
make: *** [install] Error 2

则说明当前服务器可能没有安装 jemalloc,先安装一下:

命令:yum install jemalloc

如果已经安装了,可以执行下面命令:

命令:make MALLOC=libc

错误三:客户端输入命令出现错误 (error) NOAUTH Authentication required. 怎么办?

在启动客户端后,如果输入任何命令,提示这个错误,应该是认证问题,是 redis 设置了认证密码,输入密码后就不报错了。

命令:auth "密码"

回车后会提示 OK 信息。

给我留言

留言无头像?