Php连接有密码的memcached服务
跳转到导航
跳转到搜索
我的环境
ubuntu 18.04 xampp 7.3.12 有密码的memcached服务 相关信息 '192.168.10.214:11211'), 'memcached', 'RV3kEjX5Ug5ufTKO'
ins sasl 库
apt-get install -y \
libevent-dev \
libsasl2-2 \
sasl2-bin \
libsasl2-2 \
libsasl2-dev \
wget \
pwgen \
gcc \
make \
libsasl2-modules
ins libmemcached
wget -c https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar xvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18/
./configure --prefix=/usr/local/libmemcached --enable-sasl
Ubuntu 18.04 LTS使用GCC 7.3版本,编译libmemcached失败。
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)
^~~~~
编译前需要打个补丁,参见: https://bugs.launchpad.net/libmemcached/+bug/1663985 https://src.fedoraproject.org/cgit/rpms/libmemcached.git/plain/libmemcached-build.patch
wget https://src.fedoraproject.org/rpms/libmemcached/raw/HEAD/f/libmemcached-build.patch libmemcached-1.0.18# patch -p1 < libmemcached-build.patch make -j3 && make install
ins php memcached
memcached PHP extension for interfacing with memcached via libmemcached library wget -c https://pecl.php.net/get/memcached-3.1.5.tgz apt install m4 autoconf tar xvf memcached-3.1.5.tgz cd /opt/memcached-3.1.5/ /opt/lampp/bin/phpize ./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl --with-php-config=/opt/lampp/bin/php-config make -j3 make install Installing shared extensions: /opt/lampp/lib/php/extensions/no-debug-non-zts-20180731/ vim /opt/lampp/etc/php.ini ; add extension=memcached.so memcached.use_sasl = 1 检验 /opt/lampp/bin/php -m | grep mem memcached 或者直接 phpinfo
PHP 代码示例
示例1:基本的连接云数据库 Memcache 及 set/get 操作
<?php
$connect = new Memcached; //声明一个新的memcached链接
$connect->setOption(Memcached::OPT_COMPRESSION, false); //关闭压缩功能
$connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //使用binary二进制协议
$connect->setOption(Memcached::OPT_TCP_NODELAY, true); //重要,php memcached有个bug,当get的值不存在,有固定40ms延迟,开启这个参数,可以避免这个bug
$connect->addServer('192.168.10.214', 11211); //添加memcached地址及端口号
$connect->setSaslAuthData('memcached', 'RV3kEjX5Ug5ufTKO'); //S帐号密码进行鉴权,如已开启免密码功能,则无需此步骤
$connect->set("hello", " hey evan world");
echo 'hello: ',$connect->get("hello");
$connect->quit();
?>
打开浏览器
hello: hey evan world
troubleshooting
装cyrus-sasl-devel软件包然后重新编译libmemcached。
重新libme 再安装一次吧
要先打包时一样安装 sasl之类的
configure: error: no, libmemcached built with sasl disabled. Run configure with --disable-memcached-sasl or update libmemcached with sasl support
php编译扩展库时报错:Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script.
yum -y install autoconf m4
configure: error: Cannot find php-config. Please use --with-php-config=PATH 错误的解决方案
一般出现这个错误说明你执行 ./configure 时 --with-php-config 这个参数配置路径错误导致的。
修改为:
./configure --with-php-config=/usr/local/php/bin/php-config
就可以解决问题
上面的 /usr/local/php/ 是你的 php 安装路径
see also
https://www.php.net/manual/en/book.memcached.php