一、memcached 安装
1. memcached 安装和启动、查看进程
安装扩展源 yum install -y epel-release
安装软件、组件、工具包 yum install -y libevent memcached libmemcached
启动 /etc/init.d/memcached start
进程 ps aux |grep memcached
解释:
-d 启动一个守护进程
-p 监听的端口,默认 11211
-u 运行 memcached 用户
-m 内存大小,单位 MB
-c 并发连接数,默认 1024
-P pid 文件
-l 监听的服务器 IP 地址
2. 编辑配置文件 / etc/sysconfig/memcached
说明:配置文件中添加监听地址 -l 127.0.0.1
3. 查看 memcached 运行状态
1) 使用 memcached 自带命令查看,一般优良的 memcache,cmd_hits/cmd_get 命中率大于 80%
命令 memcached-tool 127.0.0.1:11211 stats
2) 使用 nc 工具命令查看
说明:需 yum install -y nc
命令 echo status |nc 127.0.0.1 11211
3) 使用 libmemcached 工具的命令查看
命令 memstat --servers=127.0.0.1:11211
二、memcached 操作
Memcached 需在网站代码里指定,一般由开发人员操作,也可在测试和查看数据时,执行这些操作,一般用于 php 访问和存储
1. memcached 登录
telenet 方式:Telnet 127.0.0.1 11211
2. memcached 语法规则
格式:
<command name><key><flags><exptime><bytes> \r\n <data block> \r\n
说明:
1) \r\n 在 Windows 下是 Enter 键
2) <command name> 可以是 set(已存在则覆盖)、add(已存在则失败)、replace(不存在则失败)
3) <key>
4) <flags> 16 位无符号十进制整数
5) <exptime> 0 表示永不过期,但可以被服务器算法 LRU 替换
6) <bytes> 存储的字节数,空数据设为 0
7) <data block> 存储的数据
3. 操作数据
1) 存入和查看一条数据
说明:键名为 1;1 表示键标记;80 为过期时间;2 为字节大小;回车输入 2 个字节的数据
2)替换一条数据
3) 删除一条数据
4. 查看 chunk 值
命令:memcached-tool 127.0.0.1:11211 display
三、memcached 连接实现共享
Memcache 作为 php 的一个扩展模块, 数据存到了 memcached 里面后,php 通过 memcache 模块去和 memcached 服务交互
1. 下载和安装扩展工具 memcache 模块
说明:源码编译时,需用到 / usr/local/php/bin/phpize 工具生成 configure 文件,如没有需 yum install php-devel
命令 wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
tar -zxf memcache-2.2.3.tgz
cd memcache-2.2.3
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make;make install
2. 编辑 php 配置文件 / usr/local/php/etc/php.ini,指定扩展模块的目录 extension_dir(默认)和模块文件,并重启查看是否加载 / usr/local/php/bin/php -m
3. 编辑测试脚本 1.php,并用 / usr/local/php/bin/php 测试与 php 的解析
- <?php
- //连接Memcache Memcache
- $mem = new Memcache;
- $mem->connect("localhost", 11211);
- //保存数据
- $mem->set('key1', 'This is first value', 0, 60);
- $val = $mem->get('key1');
- echo "Get key1 value: " . $val ."<br>";
- //替换数据
- $mem->replace('key1', 'This is replace value', 0, 60);
- $val = $mem->get('key1');
- echo "Get key1 value: " . $val . "<br>";
- //保存数组数据
- $arr = array('aaa', 'bbb', 'ccc', 'ddd');
- $mem->set('key2', $arr, 0, 60);
- $val2 = $mem->get('key2');
- echo "Get key2 value: ";
- print_r($val2);
- echo "<br>";
- //删除数据
- $mem->delete('key1');
- $val = $mem->get('key1');
- echo "Get key1 value: " . $val . "<br>";
- //清除所有数据
- $mem->flush();
- $val2 = $mem->get('key2');
- echo "Get key2 value: ";
- print_r($val2);
- echo "<br>";
- //关闭连接
- $mem->close();
- ?>
4. memcached 实现 session 共享(lamp/lnmp 环境下实现方式有多种)
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"
php_value session.save_hander "memcache"
php_value session.save_path "tcp://127.0.0.1:11211"
php_value[session.save_handler]=memcache
php_value[session.save_path]="tcp:127.0.0.1:11211"
a. 编辑 php 配置文件 vim /usr/local/php/etc/php.ini,填写 session 保存方式路径
说明:如 memcached 服务器安装的远程机器上,在监听远程机器的 IP
内容:
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"
、
b. 编辑 php 测试脚本 session.php,移至网站目录下 / data/www/
内容:
- <?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>";
- print session_id();
- ?>
c. 用 curl 或者浏览器访问 session.php,获取返回的 session 值
命令 curl -x127.0.0.1:80 172.16.111.195/session.php
d. telnet 登录 memcached 服务器,查看本地保存的 session 信息与 curl 获取的一致
5. 通过 phpinfo 查看 session 相关配置
来源: http://www.bubuko.com/infodetail-1966611.html