Redis安裝使用
通過homebrew安裝
brew install redis
安裝完之后會顯示如下信息
To have launchd start redis now and restart at login:
brew services start redis
Or, if you don't want/need a background service you can just run:
redis-server /usr/local/etc/redis.conf
啟動redis服務
上述兩種情況是如何啟動redis的,第一種后臺運行
brew services start redis
第二種情況即非后臺運行
redis-server /usr/local/etc/redis.conf
使用第二種方式,顯示如下結果說明redis服務器安裝成功
注:啟動redis服務器之后終端所在的窗口就不能輸入別的命令了,需要在終端打開新的窗口才能使用客戶端功能
5417:C 01 Jun 12:45:37.066 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5417:C 01 Jun 12:45:37.067 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=5417, just started
5417:C 01 Jun 12:45:37.067 # Configuration loaded
5417:M 01 Jun 12:45:37.068 * Increased maximum number of open files to 10032 (it was originally set to 4864).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5417
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
5417:M 01 Jun 12:45:37.070 # Server initialized
5417:M 01 Jun 12:45:37.070 * DB loaded from disk: 0.000 seconds
5417:M 01 Jun 12:45:37.070 * Ready to accept connections
使用如下命令可以使redis后臺運行
nohup redis-server /usr/local/etc/redis.conf &
查看redis服務是否啟動
ps aux | grep redis
通過redis-cli命令可以啟動redis客戶端
redis-cli
接下來就可以操作redis了
示例:
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> get 123
(nil)
127.0.0.1:6379> set 123 jim
OK
127.0.0.1:6379> get 123
"jim"
127.0.0.1:6379> keys *
1) "123"
127.0.0.1:6379> set a 123
OK
127.0.0.1:6379> EXPIRE a 3600
(integer) 1
127.0.0.1:6379> TTL a
(integer) 3594
127.0.0.1:6379> TTL a
(integer) 3591
退出redis服務
(1)客戶端退出
redis-cli shutdown
2)關閉pid
ps -u XXX(替換成你的用戶名) -o pid,rss,command | grep redis-server
查看結果
5888 3432 /usr/local/opt/redis/bin/redis-server 127.0.0.1:6379
5919 232 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn redis-server
關閉pid
kill 5888
ps:如果你的電腦里面安裝了oh my zsh,只需要在終端里輸入
kill redis
按下tab,會自動替換成對應的pid
安裝Redis擴展
查看php有哪些擴展
php -m
下載phpredis
git clone https://github.com/nicolasff/phpredis
進入phpredis目錄下
cd phpredis
運行phpize命令生成配置文件
sudo phpize
這時如果沒有安裝m4和autoconf就會出現(xiàn)下面錯誤
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script
安裝m4和autoconf
brew install m4
brew install autoconf
安裝完之后再回到phpredis目錄下執(zhí)行phpize,顯示如下結果則成功
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
運行./configure:
需要找一下php-config的位置:
find / -name php-config
我本地的位置是:/usr/bin/php-config,所以我執(zhí)行的命令是:
./configure --with-php-config=/usr/bin/php-config
編譯安裝
make && make install
這時程序會講編譯好的.so 文件拷貝到php默認的擴展文件夾下面,但是mac的 /usr 文件下的有些目錄 root 賬戶也沒有寫權限,所以會報錯。這時 需要 重啟電腦 按住 command + r 進入安全模式,打開終端 csrutil disable 將 csrutil 這個功能禁用掉,這樣root就有權限了,重新執(zhí)行命令,得到如下結果:
Libraries have been installed in:
/usr/local/redis-3.1.2/modules ........
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20131226/
配置php.ini
我的php.ini文件是在 /etc下,如果沒有該文件 則將php.ini.default文件copy一下
在php.ini文件中添加
extension = redis.so
重啟nginx
查看phpinfo()
輸入如下命令可以看到redis
php -m|grep redis
升級系統(tǒng)之后導致的問題
最近升級了一下系統(tǒng),出現(xiàn)了好多問題。
但是我的redis擴展沒有了,沒辦法我只能重裝。
重復上述步驟
phpize
但是報了如下錯誤
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
上網(wǎng)查了一下方法,找到了解決方法
就是我們本來有這些文件,但是都換目錄了
那么我們就找一下這些東西在哪個目錄
sudo find /-name php.h
找到了在這個目錄下
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
然后我們創(chuàng)建個軟鏈接
sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include /usr/include
然后再依次執(zhí)行上述步驟就可以了
./configure --with-php-config=/usr/bin/php-config
make
make test
sudo make install
Mysql安裝使用
安裝
brew install mysql
在命令行運行mysql,出現(xiàn)以下錯誤
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
原因,mysql沒有啟動,解決方法
mysql.server start
結果如下,成功啟動
Starting MySQL
. SUCCESS!
接下來便可以運行mysql了,
下面是因為安裝mysql默認的用戶名是root,端口是3306,導致的權限問題,用root登就可以了
mysql
ERROR 1045 (28000): Access denied for user '************' (using password: NO)
a088@088deMacBook-Pro:~|? sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.22 Homebrew
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
連接遠程服務器數(shù)據(jù)庫
mysql -h host -P PORT -u user -p password