1.redis下载安装

cd /usr/local/src
wget http://download.redis.io/releases/redis-3.2.3.tar.gz
tar -zxvf redis-3.2.3.tar.gz
cd redis-3.2.3
make
make PREFIX=/usr/local/redis-3.2.3 install
ln -s /usr/local/redis-3.2.3 /usr/local/redis

# 加入到环境变量
echo 'PATH="/usr/local/redis/bin:$PATH"'>>/etc/profile
source /etc/profile

2.redis启动脚本

cp /usr/local/src/redis-3.2.3/utils/redis_init_script /etc/init.d/redis
chmod +x /etc/init.d/redis
vim /etc/init.d/redis
  6 REDISPORT=6379
  7 EXEC=/usr/local/redis/bin/redis-server   # 启动命令更改
  8 CLIEXEC=/usr/local/redis/bin/redis-cli

3.redi配置文件

mkdir /etc/redis
cp /usr/local/src/redis-3.2.3/redis.conf /etc/redis/6379.conf

# 修改redis的配置文件
[root@localhost ~]# vim /etc/redis/6379.conf
  61 bind 192.168.2.10              # 绑定IP地址

  88 # In high requests-per-second environments you need an high backlog in order
  89 # to avoid slow clients connections issues. Note that the Linux kernel
  90 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
  91 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
  92 # in order to get the desired effect.
  93 tcp-backlog 511	            # 修改/proc/sys/net/core/somaxconn值

 128 daemonize yes                  # no改成yes

 163 logfile "/etc/redis/redis_6379.log"  # 日志文件

 247 dir /etc/redis/redis_6379      # 数据目录

 480 requirepass xiaoyi		    # 开启密码认证


[root@localhost ~]# cat /proc/sys/net/core/somaxconn
128
[root@localhost ~]# echo 512 >/proc/sys/net/core/somaxconn
[root@localhost ~]# cat /proc/sys/net/core/somaxconn      
512

[root@localhost ~]# mkdir /etc/redis/redis_6379

4.redis开启密码保护,需要更改/etc/init.d/redis启动文件

[root@localhost ~]# vim /etc/init.d/redis
 28                 PID=$(cat $PIDFILE)
 29                 echo "Stopping ..."
 30                 $CLIEXEC -h 192.168.2.10 -p $REDISPORT -a xiaoyi shutdown  # -h主机,-a密码
 31                 while [ -x /proc/${PID} ]

5.redis启动,查看日志文件,出现二个警告

[root@localhost local]# /etc/init.d/redis start
[root@localhost local]# less /etc/redis/redis
redis_6379/     redis_6379.log  redis.conf      
[root@localhost local]# less /etc/redis/redis_6379.log 
6139:M 31 Aug 09:32:29.424 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6139
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6139:M 31 Aug 09:32:29.438 # Server started, Redis version 3.2.3
6139:M 31 Aug 09:32:29.438 # 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.
6139:M 31 Aug 09:32:29.439 # 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.
6139:M 31 Aug 09:32:29.439 * The server is now ready to accept connections on port 6379
/etc/redis/redis_6379.log (END)

 6.日志警告解决

[root@localhost local]# echo 1 > /proc/sys/vm/overcommit_memory                                                                                                                                                                                
内核参数说明如下:                                                                                 
overcommit_memory文件指定了内核针对内存分配的策略,其值可以是0、1、2。                               
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。 
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

[root@localhost local]# sysctl vm.overcommit_memory=1
# 永久生效
[root@localhost local]# echo 'vm.overcommit_memory = 1' >>/etc/sysctl.conf
[root@localhost local]# sysctl -p

[root@localhost local]# echo 512 >  /proc/sys/net/core/somaxconn

[root@localhost local]# echo never > /sys/kernel/mm/transparent_hugepage/enabled

 

7.查看日志文件,没有警告错误

[root@localhost local]# /etc/init.d/redis start
[root@localhost local]# tail -f /etc/redis/redis_6379.log 
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6202:M 31 Aug 10:01:11.834 # Server started, Redis version 3.2.3
6202:M 31 Aug 10:01:11.835 * DB loaded from disk: 0.000 seconds
6202:M 31 Aug 10:01:11.835 * The server is now ready to accept connections on port 6379