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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章