php怎么redis安装,Redis安装与(php-redis)扩展

一、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、测试

$redis = new Redis();

$redis->connect('127.0.0.1',6379);

$redis->set('test','hello world!');

echo $redis->get('test');

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值