从 unix socket 获取 PHP memcached 统计数据和版本

Get PHP memcached stats and version from unix socket

当侦听 TCP 端口时,我能够使用 PHP 5.6 成功获取 memcached 版本

<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
print_r($m->getStats());
print_r($m->getVersion());
?>

然后我得到我期望的输出

root@debian8x64:~# php memcached
Array
(
    [localhost:11211] => Array
        (
            [pid] => 22165
            [uptime] => 8
            [threads] => 4
            [time] => 1460472519
            [pointer_size] => 64
            [rusage_user_seconds] => 0
            [rusage_user_microseconds] => 0
            [rusage_system_seconds] => 0
            [rusage_system_microseconds] => 0
            [curr_items] => 0
            [total_items] => 0
            [limit_maxbytes] => 67108864
            [curr_connections] => 5
            [total_connections] => 6
            [connection_structures] => 6
            [bytes] => 0
            [cmd_get] => 0
            [cmd_set] => 0
            [get_hits] => 0
            [get_misses] => 0
            [evictions] => 0
            [bytes_read] => 8
            [bytes_written] => 0
            [version] => 1.4.25
        )

)
Array
(
    [localhost:11211] => 1.4.25
)

当我更改为 unix 套接字并指定端口 0 时

$m->addServer('/var/run/memcached.sock', 0);

我得到这个输出

Array
(
    [/var/run/memached.sock:11211] => Array
        (
            [pid] => -1
            [uptime] => 0
            [threads] => 0
            [time] => 0
            [pointer_size] => 0
            [rusage_user_seconds] => 0
            [rusage_user_microseconds] => 0
            [rusage_system_seconds] => 0
            [rusage_system_microseconds] => 0
            [curr_items] => 0
            [total_items] => 0
            [limit_maxbytes] => 0
            [curr_connections] => 0
            [total_connections] => 0
            [connection_structures] => 0
            [bytes] => 0
            [cmd_get] => 0
            [cmd_set] => 0
            [get_hits] => 0
            [get_misses] => 0
            [evictions] => 0
            [bytes_read] => 0
            [bytes_written] => 0
            [version] =>
        )

)
Array
(
    [/var/run/memached.sock:11211] => 255.255.255
)

我运行 memcached 这些参数只是为了测试

memcached -u root -s /var/run/memcached.sock -a 755 -vvvv

我的 php 内存缓存信息

memcached support => enabled
Version => 2.2.0
libmemcached version => 1.0.18
SASL support => yes
Session support => yes
igbinary support => no
json support => no
msgpack support => no
memcached
memcached support => enabled
libmemcached version => 1.0.18
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.serializer => php => php
memcached.sess_binary => 0 => 0
memcached.sess_connect_timeout => 1000 => 1000
memcached.sess_consistent_hash => 0 => 0
memcached.sess_lock_expire => 0 => 0
memcached.sess_lock_max_wait => 0 => 0
memcached.sess_lock_wait => 150000 => 150000
memcached.sess_locking => 1 => 1
memcached.sess_number_of_replicas => 0 => 0
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
memcached.sess_randomize_replica_read => 0 => 0
memcached.sess_remove_failed => 0 => 0
memcached.store_retry_count => 2 => 2

问题最终与权限有关,尽管我读过 755 是不够的。如果你想保证 memcached unix socket 工作,使用 unix socket 的 /tmp 文件夹中的 777 将使其工作。

更安全的解决方案可以是 found here 与 php5-fpm 或 php7.0-fpm 与 nginx 或 Apache 一起使用。