NoSQL -- php应用redis、mongodb

前端之家收集整理的这篇文章主要介绍了NoSQL -- php应用redis、mongodb前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP - redis

安装扩展模块

wget https://codeload.github.com/PHPredis/PHPredis/zip/develop
mv develop PHPredis.zip
unzip PHPredis.zip
cd PHPredis-develop
/usr/local/PHP/bin/PHPize
./configure --with-PHP-config=/usr/local/PHP/bin/PHP-config
make && make install
vim /usr/local/PHP/PHP.ini // 增加一行extension = redis.so
/usr/local/PHP/bin/PHP -m |grep redis 检查是否成功

redis的session共享

PHP.ini中加入 //不建议
session.save_handler = "redis"
session.save_path = "tcp://127.0.0.1:6379"
或者apache虚拟主机加入 //用于LAMP架构中
PHP_value session.save_handler " redis"

PHP_value session.save_path " tcp://127.0.0.1:6379"
或者PHP-fpm.conf对应的pool中加入 //用于LNMP架构
PHP_value[session.save_handler] = redis
PHP_value[session.save_path] = " tcp://127.0.0.1:6379 " //ip可换成远端redis服务器ip
redis的session测试

vim /usr/local/apache/htdocs/session.PHP

<?PHP
session_start();
if(!isset($_SESSION['TEST'])){
$_SESSION['TEST']=time();
}
$_SESSION['TEST3']=time();
print$_SESSION['TEST'];
print"<br><br>";
print$_SESSION['TEST3'];
print"<br><br>";
printsession_id();
?>

测试curl localhost/session.PHP 或 web访问

输出1443710814<br><br>1443710814<br><br>9jm6nom9ogojedj3pth0iscv22

连接redis
/usr/local/redis/bin/redis-cli
127.0.0.1:6379> keys * //可以把所有的key都列出来
"PHPREDIS_SESSION:9jm6nom9ogojedj3pth0iscv22"
127.0.0.1:6379> get PHPREDIS_SESSION:9jm6nom9ogojedj3pth0iscv22
"TEST|i:1443710814;TEST3|i:1443710814;"

error:如果 curl 不出东西加 -I 后报500而且日志中看不出东西

解决# display_errors = Off 改为 display_errors = On //此时 curl 会输出错误之后记得改回去

PHP - mongodb

安装mongodb扩展

wget 'https://codeload.github.com/mongodb/mongo-PHP-driver/zip/master' -O /usr/local/src/mong_PHP.zip
cd /usr/local/src/
unzip mong_PHP.zip
cd mongo-PHP-driver-master/
/usr/local/PHP/bin/PHPize
./configure --with-PHP-config=/usr/local/PHP/bin/PHP-config
make && make install
vi /usr/local/PHP/etc/PHP.ini //增加 extension = mongo.so
/usr/local/PHP/bin/PHP �Cm
重启apache重新加载PHP

mongodb 测试扩展

vi /usr/local/apache2/htdocs/1.PHP //增加

<?PHP
//连接到mongodb
$m=newMongoClient();
echo"Connectiontodatabasesuccessfully";
//选择一个数据库
$db=$m->mydb;
echo"Databasemydbselected";
?>

测试curl localhost/1.PHP
----------------------------------------------------------------------------------------------

mongodb详解:http://www.runoob.com/mongodb/mongodb-php.html

redis详解:http://www.runoob.com/redis/redis-tutorial.html

redis参数:http://www.jb51.cc/article/p-drbbpkcs-bbm.html

原文链接:https://www.f2er.com/nosql/203569.html

猜你在找的NoSQL相关文章