php安装memcached扩展
背景:在CenOS7.7下
1 编译、安装、测试
# 安装依赖包
yum -y install cyrus-sasl-devel
# 编译安装libmemcached
tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure && make && make install && cd ..
# 为PHP的Memcached扩展生成configure文件
tar -zxvf memcached-2.2.0.tgz
(我实际使用了 memcached-3.1.5版本)
cd memcached-2.2.0
/usr/local/php/bin/phpize
# 编译安装PHP的Memcached扩展
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install && cd ..
# 在PHP的配置文件php.ini中加载Memcached扩展
vi /usr/local/php/lib/php.ini
# 添加如下配置
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcached.so
# PHP-FPM重新加载配置
service php-fpm reload
# 测试Memcached
vi /data/www/test.php
<?php
# 连接Memcached服务器
$mem = new Memcached();
$mem->addServer('192.168.78.19', 11211);
# 保存数据(Key/Value形式,Key=UserName,Value=James)
$mem->set('UserName', 'James');
# 获取数据(根据Key=UserName,获得Value)输出结果:James
echo $mem->get('UserName');
?>
2 出现的问题
2.1 编译libmemcached(./configure)时报错
报错内容如下:
clients/memflush.cc:42:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (opt_servers == false)
^~~~~
clients/memflush.cc:51:24: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (opt_servers == false)
^~~~~
原因:gcc版本比较高,不兼容。
解决方法:对源码略作修改。
# 将clients/memflush.cc的42行和51行作如下修改:
int main(int argc, char *argv[])
{
options_parse(argc, argv);
- if (opt_servers == false)
+ if (!opt_servers)
{
char *temp;
- if (opt_servers == false)
+ if (!opt_servers)
{
std::cerr << "No Servers provided" << std::endl;
exit(EXIT_FAILURE);
然后再次make,编译通过。
2.2 编译memcached-3.1.5报错
报错内容如下:
configure: error: no, libmemcached built with sasl disabled. Run configure with --disable-memcached-sasl or update libmemcached with sasl support
添加--disable-memcached-sasl,再次编译通过:
./configure --with-php-config=/usr/local/php/bin/php-config --disable-memcached-sasl