软件准备
[hadoop@cdh soft]$ tar -zxvf redis-3.0.7.tar.gz
[hadoop@cdh soft]$ sudo yum install gcc tcl -y
[hadoop@cdh soft]$ cd redis-3.0.7
[hadoop@cdh redis-3.0.7]$ make
# 如果报错:
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 MALLOC=libc
[hadoop@cdh redis-3.0.7]$ make PREFIX=/home/hadoop/apps/redis-3.0.7 install
[hadoop@cdh redis-3.0.7]$ make test
# 如果安装正确,在命令执行执行完后会有如下提示信息:
\o/ All tests passed without errors!
# 注意,这个路径是第(4)步指定的那个路径
[hadoop@cdh redis-3.0.7]$ vim ~/.bash_profile
export REDIS_HOME=/home/hadoop/apps/redis-3.0.7
export PATH=$PATH:$REDIS_HOME/bin
[hadoop@cdh redis-3.0.7]$ source ~/.bash_profile
[hadoop@cdh redis-3.0.7]$ cd utils/
[hadoop@cdh utils]$ sudo ./install_server.sh
[sudo] password for hadoop:
Welcome to the redis service installer
This script will help you easily set up a running redis server
# 如果不用默认配置,需要手动输入端口号,我们这里就用默认端口,按Enter
Please select the redis port for this instance: [6379]
Selecting default: 6379
# 选择默认的配置文件的名称,按Enter,使用别的路径和名称需要手动指定
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
# 日志存放位置,处理同上
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
# 持久化数据目录,处理同上
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
# 指定redis的命令执行路径,需要手动指定到REDIS_HOME/bin/redis-server
Please select the redis executable path [] /home/hadoop/apps/redis-3.0.7/bin/redis-server
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /home/hadoop/apps/redis-3.0.7/bin/redis-server
Cli Executable : /home/hadoop/apps/redis-3.0.7/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
# Enter
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
# 直接使用 redis-cli 连接服务,默认连接端口号为6379的服务
[hadoop@cdh bin]$ redis-cli
127.0.0.1:6379>
redis 单节点安装成功!
redis伪分布式即在单节点上启动多个实例
安装的时候指定端口号和命令执行路径,其余保持默认即可,这里演示另一个服务的安装并省略部分重复内容
[hadoop@cdh utils]$ sudo ./install_server.sh
......
Please select the redis port for this instance: [6379] 6381
......
Please select the redis executable path [] /home/hadoop/apps/redis-3.0.7/bin/redis-server
......
Installation successful!
[hadoop@cdh ~]$ redis-cli -p 6379
127.0.0.1:6379>
[hadoop@cdh ~]$ redis-cli -p 6380
127.0.0.1:6380>
[hadoop@cdh ~]$ redis-cli -p 6381
127.0.0.1:6381>
redis 伪分布式安装成功!
已连接服务的情况下可以直接在交互界面停止服务
127.0.0.1:6379> shutdown
not connected> quit
[hadoop@cdh ~]$
127.0.0.1:6380> shutdown
not connected> quit
[hadoop@cdh ~]$
127.0.0.1:6381> shutdown
not connected> quit
[hadoop@cdh ~]$
未连接的情况下执行以下命令:
[hadoop@cdh bin]$ redis-cli -p 6379 shutdown
[hadoop@cdh bin]$ redis-cli -p 6380 shutdown
[hadoop@cdh bin]$ redis-cli -p 6381 shutdown
在安装redis服务的时候,默认指定了每个实例使用的配置文件
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
所以启动不同端口的实例,需要用不同的配置文件:
# 尽管配置了环境变量,但还是需要进入到REDIS_HOME/bin下来执行这些命令
# 否则报错:sudo: redis-server: command not found
[hadoop@cdh bin]$ sudo ./redis-server /etc/redis/6379.conf
[hadoop@cdh bin]$ sudo ./redis-server /etc/redis/6380.conf
[hadoop@cdh bin]$ sudo ./redis-server /etc/redis/6381.conf
看看配置文件中一些简单配置的作用:
# redis是否以守护进程模式运行。默认 no
daemonize yes
# redis的PID文件位置
pidfile /var/run/redis_6379.pid
# redis监听的端口号,默认6379
port 6379
# 存储日志的级别,可以为debug/verbose/notice/warning
loglevel notice
# 日志文件的位置
logfile /var/log/redis_6379.log
# 默认的数据库个数
databases 16
持久化数据的存放位置
dir /var/lib/redis/6379
如果不指定启动redis服务的时候使用哪个配置文件,将会使用redis编译目录,就是解压redis后的那个目录下的redis.conf配置文件,其中内容如下:
非守护进程方式启动
daemonize no
pidfile /var/run/redis.pid
# 使用6379端口
port 6379
loglevel notice
# 不存储日志
logfile ""
# 在哪个目录下启动,数据就存放在哪个目录下
dir ./
启动后的效果如下:
[hadoop@cdh ~]$ redis-server
8733:C 04 Jun 14:43:42.753 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
8733:M 04 Jun 14:43:42.754 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
8733:M 04 Jun 14:43:42.754 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
8733:M 04 Jun 14:43:42.754 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8733
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
8733:M 04 Jun 14:43:42.756 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
8733:M 04 Jun 14:43:42.756 # Server started, Redis version 3.0.7
8733:M 04 Jun 14:43:42.756 # 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.
8733:M 04 Jun 14:43:42.756 # 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.
8733:M 04 Jun 14:43:42.756 * The server is now ready to accept connections on port 6379
直接按下Ctrl+Z
关闭进程后,服务也就停止了:
^C8733:signal-handler (1528095027) Received SIGINT scheduling shutdown...
8733:M 04 Jun 14:50:27.615 # User requested shutdown...
8733:M 04 Jun 14:50:27.615 * Saving the final RDB snapshot before exiting.
8733:M 04 Jun 14:50:27.619 * DB saved on disk
8733:M 04 Jun 14:50:27.620 # Redis is now ready to exit, bye bye...
[hadoop@cdh ~]$
在redis的编译目录的utils目录下,有一个名为redis_init_script
的脚本文件,首先把这个文件复制为redis_6382,然后修改redis_6382这个文件:
[hadoop@cdh utils]$ cp redis_init_script redis_6382
[hadoop@cdh utils]$ vim redis_6382
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# 端口改为6382
REDISPORT=6382
# 修改执行命令的路径:REDIS_HOME/bin/
EXEC=/home/hadoop/apps/redis-3.0.7/bin/redis-server
CLIEXEC=/home/hadoop/apps/redis-3.0.7/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
case "$1" in
# ...... 这里的内容不用修改
esac
提前创建好/var/run
和/etc/redis
这两个目录,我们这里不用再创建了,因为使用install_server.sh
安装服务的时候就已经创建好了,但是需要创建/var/lib/redis/6382
这个目录
然后创建一个配置文件,名为6382.conf
,可以复制redis编译目录下的redis.conf
中的内容,然后进行修改:
daemonize yes
pidfile /var/run/redis_6382.pid
port 6382
logfile /var/log/redis_6382.log
dir /var/lib/redis/6382
# 其余默认
......
把这个配置文件移动到/etc/redis/目录下,然后就可以使用这个脚本启动redis服务了:
[hadoop@cdh utils]$ sudo ./redis_6382 start
[sudo] password for hadoop:
Starting Redis server...
[hadoop@cdh utils]$ redis-cli -p 6382
127.0.0.1:6382>
redis_init_script
,你需要设置端口和一些目录,然后还要手动创建这些目录,手动创建配置文件,还要让配置文件在正确的目录下,太麻烦,但是在生产环境中,一个节点安装一个服务,配置一次,一劳永逸,而且可以通过这个脚本配置redis服务开机自启动(感兴趣可以自己在网络上查找),所以推荐在生产环境使用这种方式install_server.sh
这个脚本,我们只要输入各种路径就行,脚本帮你生成配置文件,伪分布式使用这用方式会简单很多