发布于 2014-11-30 23:54:30 | 176 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Redis 官方指南,程序狗速度看过来!

Redis Key-Value数据库

Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。


本文为大家介绍的是在PHP中使用redis示例讲解,感兴趣的同学参考下.

这里是在Mac OS上安装redis,并在PHP中开启redis。

在Mac OS上安装redis
首先是安装,它会默认安装到/usr/local/bin下


cd /tmp
wget http://redis.googlecode.com/files/redis-2.6.9.tar.gz
tar -zxf redis-2.6.9.tar.gz
cd redis-2.6.9
make
sudo make install


然后下载一些配置文件(主要就是把deamon打开之类的,没对比与默认配置的区别)


wget https://github.com/ijonas/dotfiles/raw/master/etc/redis.conf
sudo mv redis.conf /etc/redis.conf
sudo /usr/local/bin/redis-server redis.conf
说到这里备注下,如果没有目录权限,是无法建立
/var/log/redis/redis.log
/var/lib/redis/


导致redis启动失败
ok,现在已经大功告成,你的redis已经成功运行起来了。
试试看吧!


/opt/redis/redis-cli
#会看到提示 redis 127.0.0.1:6379>说明已经连接服务。
set anythink helloworld
get anythink
exit


good 看到了helloworld,说明一切正常。
如果我需要停止redis或者需要重新启动呢?


cat /opt/redis/redis.pid
#cat后会得到一个pid,我的是44277
sudo kill 44277
# 启动方法和之前一样。

设置开机自启动、后台运行
然后以root身份做以下事情:
在/Library/LaunchDaemons下新建com.redis.plist,内容如下:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.redis</string>
        <key>RunAtLoad</key>
        <true/>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/bin/redis-server</string>
                <string>/etc/redis.conf</string>
        </array>
</dict>
</plist>


之后运行


sudo launchtcl load /Library/LaunchDaemons/com.redis.plist
sudo launchtcl start com.redis


检查一下情况:


$ cat /var/run/redis.pid


如果出来pid的数字,说明就运行了~
安装php-redis扩展
如果你需要在PHP中使用redis,那么请继续往下看


curl -O https://nodeload.github.com/nicolasff/phpredis/zip/master
tar -zxf master
cd phpredis-master/
phpize
./configure
make
sudo make install

# 这时候会提示一个路径
# /usr/lib/php/extensions/no-debug-non-zts-20090626/
# 表示已经将扩展放置在该位置
vim /etc/php.ini

#增加如下内容
extension=redis.so

#重启apache
sudo httpd -k restart

#查看扩展安装情况
php -m |grep redis
#出现 redis 表示安装成功。


如果执行phpize提示如下错误
Cannot find autoconf. Please check your autoconf installation
and the $PHP_AUTOCONF environment variable.
Then, rerun this script.

请分别下载M4,autoconf编译安装


curl -O http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz


注意,以上使用的apache、php均为MacOS自带的,如果是自己安装的phpize请指定绝对路径。
图形管理工具
另:redis还有一个基于WEB的图形界面管理工具,叫phpRedisAdmin,如果刚开启服务会出现一些Undefined index,过一会就好了。如果想试试可以使用如下命令安装(git推荐使用SourceTree安装)该管理工具支持String、Hash、List、Set、Zset


git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git
cd phpRedisAdmin/
git clone https://github.com/nrk/predis.git


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务