千家信息网

NoSQL -- php应用redis、mongodb

发表于:2025-01-19 作者:千家信息网编辑
千家信息网最后更新 2025年01月19日,php - redis安装扩展模块wget https://codeload.github.com/phpredis/phpredis/zip/developmv develop phpredis.z
千家信息网最后更新 2025年01月19日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


"; print $_SESSION['TEST3']; print "

"; print session_id(); ?>

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

输出1443710814

1443710814

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 -m
重启apache重新加载php

mongodb 测试扩展

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

mydb;echo "Database mydb selected";?>

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

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

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

redis参数:http://beibing.blog.51cto.com/10693373/1875638

0