redis安装

centos下安装redis


# 解压(当前版本:3.0.5)
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
make install
  • 设为服务项自启动

cp utils/redis_init_script /etc/rc.d/init.d/redis
# 修改使后台执行
vim /etc/init.d/redis
# 在开头注释下添加下行注释,使支持chkconfig
# chkconfig:   2345 90 10
# 还需要注意pid文件名与conf里的一致,否则无法用于关闭redis-server


# 配置文件
mkdir /etc/redis     
cp /pathto/redis/redis.conf /etc/redis/6379.conf

# 创建redis工作目录
mkdir /var/redis/6379
#创建redis-pid目录
mkdir /var/run/redis
chmod redis /var/run/redis #指定你redis所在组权限,不然无法创建

# 编辑配置使后台运行
vim /etc/redis/6379.conf
daemonize yes   #修改no为yes,后台运行redis
pidfile /var/run/redis/redis_6379.pid
logfile /var/log/redis_6379.log
dir /var/redis/6379

# 加入服务项
chmod 755 /etc/init.d/redis
chkconfig --add redis
chkconfig --level 2345 redis on
chkconfig --list redis
  • 启动

service redis start
  • web
    • phpredis安装

# 下载

https://github.com/phpredis/phpredis
# 安装

phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
# 在php.ini中添加
extension="redis.so"
  • issue

service redis does not support chkconfig的解决办法

问题解决办法如下:

必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中:

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

上面的注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

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.

或者:
echo 1 > /proc/sys/vm/overcommit_memory
不需要启机器就生效

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.

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

echo 511 > /proc/sys/net/core/somaxconn

附录:
linux 运行级别

运行级别就是操作系统当前正在运行的功能级别。这个级别从0到6 ,具有不同的功能。这些级别在/etc/inittab文件里指定。这个文件是init程序寻找的主要文件,最先运行的服务是那些放在/etc/rc.d 目录下的文件。

不同的运行级定义如下:(可以参考Linux里面的/etc/inittab)

缺省的运行级,RHS用到的级别如下:

0:关机

1:单用户模式

2:无网络支持的多用户模式

3:有网络支持的多用户模式

4:保留,未使用

5:有网络支持有X-Window支持的多用户模式

6:重新引导系统,即重启

对各个运行级的详细解释:

0 为停机,机器关闭。

1 为单用户模式,就像Win9x下的安全模式类似。

2 为多用户模式,但是没有NFS支持。

3 为完整的多用户模式,是标准的运行级。

4 一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本 电脑的电池用尽时,可以切换到这个模式来做一些设置。

5 就是X11,进到X Window系统了。

6 为重启,运行init 6机器就会重启。

chkconfig用法

chkconfig命令可以用来检查、设置系统的各种服务

使用语法:

chkconfig [–add][–del][–list][系统服务] 或 chkconfig [–level <等级代号>][系统服务][on/off/reset]

参数用法:

–add  增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。

–del  删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据。

–level<等级代号>  指定读系统服务要在哪一个执行等级中开启或关毕。

使用范例:

chkconfig –list 列出所有的系统服务

chkconfig –add redis 增加redis服务

chkconfig –del redis 删除redis 服务

chkconfig –level redis 2345 on 把redis在运行级别为2、3、4、5的情况下都是on(开启)的状态。

参考自

如有疑问,请文末留言交流或邮件:newbvirgil@gmail.com 本文链接 : https://newbmiao.github.io/2015/06/26/redis-installation-guide.html