成都网站建设设计

将想法与焦点和您一起共享

Memcached安装配置

操作系统:CentOS release 6.8 (Final)

创新互联主营汝城网站建设的网络公司,主营网站建设方案,成都app软件开发公司,汝城h5小程序制作搭建,汝城网站营销推广欢迎汝城等地区企业咨询

虚拟机:VM

任务:服务与客户端在一台虚拟机主安装和配置。

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。

Memcached服务安装

[root@localhost ~]# yum install libevent libevent-devel nc –y

[root@localhost ~]# rpm -qa libevent libevent-devel nc

libevent-1.4.13-4.el6.x86_64

libevent-devel-1.4.13-4.el6.x86_64

nc-1.84-24.el6.x86_64

#可以通过yum安装或编译安装,yum安装的版本比较低,可以从官网上下载安装下载最新版本编译安装http://memcached.org/downloads或者http://pecl.php.net/package/memcached

[root@localhost ~]# yum install memcached –y

[root@localhost ~]# which memcached

/usr/bin/memcached

[root@localhost ~]# memcached -m 16m -p 11211 -d -u root -c 8192 #启动第一个实例

[root@localhost ~]# lsof -i:11211  #查看启动情况

COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

memcached 1857 root   26u  IPv4  14225      0t0  TCP *:memcache (LISTEN)

memcached 1857 root   27u  IPv6  14226      0t0  TCP *:memcache (LISTEN)

memcached 1857 root   28u  IPv4  14229      0t0  UDP *:memcache

memcached 1857 root   29u  IPv6  14230      0t0  UDP *:memcache

[root@localhost ~]# ps -ef|grep memcached|grep -v grep #查看进程

root      1857     1  0 19:14 ?        00:00:00 memcached -m 16m -p 11211 -d -u root -c 8192

[root@localhost ~]# memcached -m 16m -p 11212 -d -u root -c 8192 #启动第二个实例

[root@localhost ~]# ps -ef|grep memcached|grep -v grep  

root      1857     1  0 19:14 ?        00:00:00 memcached -m 16m -p 11211 -d -u root -c 8192

root      1868     1  0 19:15 ?        00:00:00 memcached -m 16m -p 11212 -d -u root -c 8192

[root@localhost ~]# lsof -i  :11211

COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

memcached 1737 root   26u  IPv4  12689      0t0  TCP *:memcache (LISTEN)

memcached 1737 root   27u  IPv6  12690      0t0  TCP *:memcache (LISTEN)

memcached 1737 root   28u  IPv4  12693      0t0  UDP *:memcache

memcached 1737 root   29u  IPv6  12694      0t0  UDP *:memcache

[root@localhost ~]# lsof -i  :11212

COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

memcached 1739 root   26u  IPv4  12698      0t0  TCP *:11212 (LISTEN)

memcached 1739 root   27u  IPv6  12699      0t0  TCP *:11212 (LISTEN)

memcached 1739 root   28u  IPv4  12702      0t0  UDP *:11212

memcached 1739 root   29u  IPv6  12703      0t0  UDP *:11212

 [root@localhost ~]# tail -2 /etc/rc.local

[root@localhost ~]# vi  /etc/rc.local #开机可以自动启动两个实例

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

memcached -m 16m -p 11211 -d -u root -c 8192

memcached -m 16m -p 11212 -d -u root -c 8192

------------------------------------------------------------------------

memcached参数说明:

-d选项是启动一个守护进程

-m是分配给Memcache使用的内存数量,单位是MB,我这里是16MB

-u是运行Memcache的用户,我这里是root

-l是监听的服务器IP地址

-p是设置Memcache监听的端口,最好是1024以上的端口

-c选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定

-P是设置保存Memcache的pid文件

其他参数省略说明。

[root@localhost ~]# chkconfig --level 2345 memcached on #开机启动服务

#向memcached中写入数据

[root@localhost ~]# printf "set key1 0 0 6\r\n skyboy\r\n"|nc 127.0.0.1 11211

CLIENT_ERROR bad data chunk

ERROR

[root@localhost ~]# printf "set key1 0 0 6\r\nskyboy\r\n"|nc 127.0.0.1 11211

STORED

[root@localhost ~]# printf "set key2 0 0 5\r\nskyboy\r\n"|nc 127.0.0.1 11211

CLIENT_ERROR bad data chunk

ERROR

[root@localhost ~]# printf "get key1\r\n"|nc 127.0.0.1 11211

VALUE key1 0 6

skyboy

END

[root@localhost ~]# printf "delete key1\r\n"|nc 127.0.0.1 11211

DELETED

[root@localhost ~]# printf "get key1\r\n"|nc 127.0.0.1 11211

END

 

通过telnet命令写入数据

Connection closed by foreign host.

[root@localhost ~]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

set user01 0 0 7

oldgirl

STORED

get user01

VALUE user01 0 7

oldgirl

END

delete user01

DELETED

get user01

END

quit

Connection closed by foreign host.

 

安装Memcached客户端

http://pecl.php.net/package/memcache#下载所有版本地址

[root@localhost tools]#wgethttp://pecl.php.net/get/memcache-2.2.4.tgz

[root@localhost tools]# tar zxf memcache-2.2.4.tgz

[root@localhost tools]# cd memcache-2.2.4

[root@localhost memcache-2.2.4]#

#phpize是用来扩展PHP扩展模块的,通过phpize可以建立PHP的外挂模块。

[root@localhost memcache-2.2.4]# /application/php/bin/phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

[root@localhost memcache-2.2.4]# ./configure --enable-memcache \

--with-php-config=/application/php/bin/php-config

[root@localhost memcache-2.2.4]# make

[root@localhost memcache-2.2.4]# make install

Installing shared extensions:     /application/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/

[root@localhost memcache-2.2.4]# ls /application/php/lib/php/extensions/no-debug-non-zts-20090626/

memcache.so   #生成了.so模块

[root@localhost memcache-2.2.4]#cd /application/php/lib/

[root@localhost lib]# vi php.ini  #在末尾添加以下两行

extension_dir="/application/php/lib/php/extensions/no-debug-non-zts-20090626/"

extension=memcache.so

[root@localhost lib]# /application/php/sbin/php-fpm  -t  #检查语法

[19-Jan-2017 19:50:52] NOTICE: configuration file /application/php5.3.27/etc/php-fpm.conf test is successful

[root@localhost lib]# /application/php/sbin/php-fpm   #启动服务

[root@localhost lib]# ps -ef|grep php-fpm|grep -v grep

root      4624     1  0 19:51 ?        00:00:00 php-fpm: master process (/application/php5.3.27/etc/php-fpm.conf)

nginx     4625  4624  0 19:51 ?        00:00:00 php-fpm: pool www           

nginx     4626  4624  0 19:51 ?        00:00:00 php-fpm: pool www     

 重启fpm

[root@localhost ~]# pkill php-fpm

[root@localhost ~]# ps -ef|grep php-fpm|grep -v grep

[root@localhost ~]# /application/php/sbin/php-fpm

[root@localhost ~]# ps -ef|grep php-fpm|grep -v grep

root      2186     1  0 23:39 ?        00:00:00 php-fpm: master process (/application/php5.3.27/etc/php-fpm.conf)

nginx     2187  2186  0 23:39 ?        00:00:00 php-fpm: pool www           

nginx     2188  2186  0 23:39 ?        00:00:00 php-fpm: pool www   

 测试Memcached客户端安装是否成功?http://192.168.222.130/phpinfo.php

Phpinfo.php文件内容如下:

Phpinfo();

?>

 Memcached安装配置

测试LNMP环境是否连接Memcached服务成功?

[root@localhost www]# vi  op_mem.php

$memcache=new memcache;

$memcache->connect('192.168.222.130',11211) or die ("Could not connect Mc Server");

$memcache->set('key','学习Memcached');

$get=$memcache->get('key');

echo $get;

?>
[root@localhost www]# /application/php/bin/php  op_mem.php 

学习Memcached

或者浏览器访问:http://192.168.222.130/op_mem.php 

 


新闻名称:Memcached安装配置
转载来于:http://chengdu.cdxwcx.cn/article/ppoiii.html