redis 是個 Key – Value 資料庫
Phpiredis 是個 PHP 模組,基於 hiredis
phpRedisAdmin 是個 Web 的操作介面,類似 phpMyAdmin
安裝 redis
wget http://download.redis.io/releases/redis-4.0.9.tar.gz tar zxvf redis-*.tar.gz cd redis-* make distclean make PREFIX=/usr/local/redis install
啟動 redis
/usr/local/redis/bin/redis-server
簡單測試 redis
redis-cli > ping > set foo bar > get foo
設定 redis 啟動程式 (複製 tarball 裡面的範例稍作修改)
cp -rp utils/redis_init_script /etc/init.d/redis sed -i 's/\/usr\/local\/bin\//\/usr\/local\/redis\/bin\//g' /etc/init.d/redis sed -i 's/^CONF=".*/CONF="\/etc\/redis\/redis.conf"/g' /etc/init.d/redis
設定 redis config (複製 tarball 裡面的範例稍作修改)
mkdir /etc/redis/ cp -rp redis.conf /etc/redis/
vi /etc/redis/redis.conf
bind 127.0.0.1 123.123.123.123 # 綁上除了本機的 IP,除非不要遠端連線 daemonize yes # 背景執行 requirepass foobared # 設置密碼,或者複雜點 echo "foobared" | sha256sum # 可是這個會讓當 /etc/init.d/redis stop 時出現 (error) NOAUTH Authentication required. Waiting for Redis to shutdown ...
設過密碼後可以這樣子登入,要認證才可以進一步動作
redis-cli > auth foobared
redis 有一個要求就是
vm.overcommit_memory = 1 # /etc/sysctl.conf
使用 phpRedisAdmin
安裝
composer create-project -s dev erik-dubbelboer/php-redis-admin /var/www/html/phpRedisAdmin # 又或者 git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git cd phpRedisAdmin git clone https://github.com/nrk/predis.git vendor
設定 phpRedisAdmin config
cd /var/www/html/phpRedisAdmin/ cp -rp includes/config.sample.inc.php includes/config.inc.php
vi includes/config.inc.php
// 設置 IP 或名稱,在 servers 陣列裡也可以設多組 host 'servers' => array( array( 'name' => 'local server', // Optional name. 'host' => '123.123.123.123', 'port' => 6379, 'filter' => '*', 'scheme' => 'tcp', // Optional. Connection scheme. 'tcp' - for TCP connection, 'unix' - for connection by unix domain socket 'path' => '', // Optional. Path to unix domain socket. Uses only if 'scheme' => 'unix'. Example: '/var/run/redis/redis.sock' // Optional Redis authentication. //'auth' => 'redispasswordhere' // Warning: The password is sent in plain-text to the Redis server. ), ), // 還有登入的帳密,讓連介面時可以有帳密 // Uncomment to enable HTTP authentication 'login' => array( // Username => Password // Multiple combinations can be used 'admin' => array( 'password' => 'adminpassword', // <<< 改這個 ), 'guest' => array( 'password' => '', 'servers' => array(1) // Optional list of servers this user can access. ) ),
使用 https://github.com/nrk/phpiredis (phpRedisAdmin 是用這個模組)
yum install hiredis hiredis-devel git clone https://github.com/nrk/phpiredis.git cd phpiredis phpize && ./configure --enable-phpiredis make && make install echo 'extension=phpiredis.so' > /etc/php.d/phpiredis.ini
寫個 PHP
<?php $redis = phpiredis_connect('172.16.10.142', 6379); $response = phpiredis_command_bs($redis, array('GET', 'foo')); echo $response . '<br>'; $response = phpiredis_multi_command_bs($redis, array( array('SET', 'test', '1'), array('GET', 'test'), )); echo '<pre>', print_r($response), '</pre>';
有另一個 PHP 模組 phpredis 可以參考 https://ssorc.tw/5977
留言