设为首页 收藏本站
查看: 1177|回复: 0

[经验分享] Redis安装与(php-redis)扩展

[复制链接]

尚未签到

发表于 2018-11-7 07:14:29 | 显示全部楼层 |阅读模式
  一、redis简介
  Redis是一种高级key-value数据库。它跟memcached类似不过数据可以持久化而且支持的数据类型很丰富。有字符串链表集 合和有序集合。支持在服务器端计算集合的并交和补集(difference)等还支持多种排序功能。所以Redis也可以被看成是一个数据结构服务器。
  Redis的所有数据都是保存在内存中然后不定期的通过异步方式保存到磁盘上(这称为“半持久化模式”)也可以把每一次数据变化都写入到一个append only file(aof)里面(这称为“全持久化模式”)
  二、安装部署
mkdir /soft  
cd /soft
  安装插件
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz  
tar zxvf tcl8.6.1-src.tar.gz
  
cd tcl8.6.1
  
./configure
  
make && make install
  安装redis
wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz  
tar zxvf redis-2.6.14.tar.gz
  
cd redis-2.6.14
  
make test
  
make --prefix=/storage/redisdb
  
make intsall
  三、安装完成后在src目录下生成5个可执行文件redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump它们的作用如下redis-serverRedis服务器的daemon启动程序redis-cliRedis命令行操作工具。当然你也可以用telnet根据其纯文本协议来操作redis-benchmarkRedis性能测试工具测试Redis在你的系统及你的配置下的读写性能redis-check-aof更新日志检查
  redis-check-dump用于本地数据库检查
  redis启动
cd src  
./redis-server
  
[5056] 04 Apr 03:33:50.744 # 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.
  
[5056] 04 Apr 03:33:50.744 * The server is now ready to accept connections on port 6379
  四、设置内存分配策略
  可选值0、1、2。0 表示内核将检查是否有足够的可用内存供应用进程使用如果有足够的可用内存内存申请允许否则内存申请失败并把错误返回给应用进程。1 表示内核允许分配所有的物理内存而不管当前的内存状态如何。2 表示内核允许分配超过所有物理内存和交换空间总和的内存值得注意的一点是redis在dump数据的时候会fork出一个子进程理论上child进程所占用的内存和parent是一样的比如parent占用的内存为8G这个时候也要同样分配8G的内存给child,如果内存无法负担往往会造成redis服务器的down机或者IO负载过高效率下降。所以这里比较优化的内存分配策略应该设置为 1表示内核允许分配所有的物理内存而不管当前的内存状态如何
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf  
sysctl -p
  验证
src/redis-cli  
redis> set foo bar
  
OK
  
redis> get foo
  
bar
  
shutdown
  Redis远程连接
  telnet 127.0.0.1 6379
  安全设置
  关闭掉selinux
/usr/sbin/setenforce 0 立刻关闭 SELINUX  
#/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
  
#/etc/rc.d/init.d/iptables save
  五、配置自启动
  cp redis.conf /etc
  mkdir -p /storage/redis db文件放在这里要修改redis.conf
  mkdir -p /var/log/redislog log文件放在这里要修改redis.conf
  修改redis.conf db文件位置
cp redis.conf /etc/  
vim /etc/redis
  
# The working directory.
  
# The DB will be written inside this directory, with the filename specified
  
# above using the 'dbfilename' configuration directive.
  
# Also the Append Only File will be created inside this directory
  
#
  
daemonize yes  //改为后台启动
  
# Note that you must specify a directory here, not a file name.
  
dir /storage/redis  // db文件位置
  
logfile=/var/log/redislog   //修改redis.conf log文件位置
  配置自启动:vi /etc/init.d/redis
  startup脚本代码如下所示将其建立为/etc/init.d/redis文件
#!/bin/bash  
#
  
# Init file for redis
  
#
  
# chkconfig: - 80 12
  
# description: redis daemon
  
#
  
# processname: redis
  
# config: /etc/redis.conf
  
# pidfile: /var/run/redis.pid
  
source /etc/init.d/functions
  
#BIN="/usr/local/bin"
  
BIN="/usr/local/bin"
  
CONFIG="/etc/redis.conf"
  
PIDFILE="/var/run/redis.pid"
  
### Read configuration
  
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
  
RETVAL=0
  
prog="redis-server"
  
desc="Redis Server"
  
start() {
  
        if [ -e $PIDFILE ];then
  
             echo "$desc already running...."
  
             exit 1
  
        fi
  
        echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG  
        RETVAL=$?
  
        echo
  
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
  
        return $RETVAL
  
}
  
stop() {
  
        echo -n $"Stop $desc: "
  
        killproc $prog
  
        RETVAL=$?
  
        echo
  
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
  
        return $RETVAL
  
}
  
restart() {
  
        stop
  
        start
  
}
  
case "$1" in
  
  start)
  
        start
  
        ;;
  
  stop)
  
        stop
  
        ;;
  
  restart)
  
        restart
  
        ;;
  
  condrestart)
  
        [ -e /var/lock/subsys/$prog ] && restart
    RETVAL=$?  
        ;;
  
  status)
  
        status $prog
  
        RETVAL=$?
  
        ;;
  
   *)
  
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
  
        RETVAL=1
  
esac
  
exit $RETVAL
  
chmod +x /etc/init.d/redis
  重新启动
/etc/init.d/redis restart  
Stop Redis Server:                                         [  OK  ]
  
Starting Redis Server:                                     [  OK  ]
  六、配置php扩展支持redis
  1、下载php-redis zip安装包
https://github.com/nicolasff/phpredis  2、找到PHP安装路径
  命令whereis phpize和whereis php-config 找到phpize和php-config路径
  3、生成configure
# /usr/bin/phpize  4、编译安装
# ./configure --with-php-config=/usr/bin/php-config# make && make install  5、加入安装的redis.so模块
# vim /etc/php.ini  
extension=redis.so
  6、重启apache或nginx
  7、测试




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-631674-1-1.html 上篇帖子: 线上修改redis内存分配大小 下篇帖子: Postgres到redis的数据迁移
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表