mac安装redis与php-redis扩展

目录

一、安装redis服务(6.0.5版本)

二、安装php的redis扩展(5.2.2版本)

三、php代码中使用redis


一、安装redis服务(6.0.5版本)

1、先搜索:brew search redis, 再安装:brew install redis
安装过程中每次会进行Homebrew更新检测,影响安装速度,如果不需要可以在终端输入(此命令临时有效)
export HOMEBREW_NO_AUTO_UPDATE=true
brew install redis

2、brew info redis
默认配置文件路径是/usr/local/etc/redis.conf,可根据需要自行修改

3、开机自启动
# 加软连接,加入launchd进程,当用户登陆系统后会被执行
ln -f /usr/local/Cellar/redis/5.0.8/homebrew.mxcl.redis.plist ~/Library/LaunchAgents
# 加载任务
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

关于开机自启动,详见:Mac中的定时任务利器launchctl 

4、管理redis
(1)手动启动redis服务
---使用brew启动:brew services start redis
---使用配置文件启动:redis-server /usr/local/etc/redis.conf
(2)关闭redis服务
brew services stop redis
(3)重启redis服务
brew services restart redis
(4)卸载redis
brew uninstall redis
rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
(5)命令连接redis服务
# redis-cli -h <ip> -p <port> -a <password>
刚安装redis密码为空,-a可以不要

二、安装php的redis扩展(5.2.2版本)

如果没有pecl的话,先安装pecl,详见:mac安装ImageMagick与PHP扩展Imagick中的二(1)

先搜索:pecl search redis, 再安装:pecl install redis
安装过程中:
enable igbinary serializer support? [no] :直接回车了
enable lzf compression support? [no] :直接回车了
enable zstd compression support? [no] :直接回车了
不知道跳过会有什么影响?
再次报错如下,cd进去看下: cd /usr/local/Cellar/[email protected]/7.2.30_1

很明显,pecl是个软连接,这时就算手动新建目录(mkdir -p pecl)还是报错:
mkdir: pecl: Input/output error
解决:更改名字,mv pecl pecl2

第三次重新执行sudo pecl install redis, 这次成功安装了redis5.2.2版本:
Build process completed successfully
Installing '/usr/local/Cellar/[email protected]/7.2.30_1/pecl/20170718/redis.so'
install ok: channel://pecl.php.net/redis-5.2.2
Extension redis enabled in php.ini

收尾,php --ini找到php.ini文件中,打开redis扩展如下,没有就新增
extension="redis.so"

安装成功但是php -m和phpinfo()都没有找到redis那一栏,what??
问题:要么是装的不对,要么是引入的不对
解决:extension="/usr/local/Cellar/[email protected]/7.2.30_1/pecl/20170718/redis.so"
 

三、php代码中使用redis

<?php
$redis = new Redis();//连接本地Redis服务
if ( $redis->connect('127.0.0.1', 6379) ) {
	echo "Connection to server successfully,Server is running: " . $redis->ping();
	$redis->set("AAA", "hello-world");//存
	echo "<br>从redis获取数据是" . $redis->get("AAA");//取
} else {
	echo "Connection to server failed";
}

更多操作详见:php使用redis

猜你喜欢

转载自blog.csdn.net/wuhuagu_wuhuaguo/article/details/106716398