CentOS 5.2下Memcache的安装与配置

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

 一,memcache简单介绍:

memcached是高性能的分布式内存缓存服务器,为了提高性能,memcached中的数据都保存在内存中,重启memcached及重启操作系统都会导致缓存中的数据全部丢失,其缓存的数据达到指定的内存分配值之后,就会使用LRU算法删除不使用的缓存。(LRU算法的基本概念:当分配的内存可用空间不足时,它尽可能地先保留最常用的数据,将最近没有使用的数据移出内存,释放出的空间来存储其它的数据。)

其作用是缓存数据库查询结果,这样就减少了对数据库的访问次数据,从而减轻数据库的压力,这样就提高了用户的访问速度,典型应用如下图所示:

 

 

 

二,安装服务器端:

1,安装libevent库,它将Linuxepollfreebsd操作系统的kqueue等事件处理功能封装成统一的接口,memcached使用这个库,可以发挥其高性能。

[root@youxia205 opt]# yum install libevent libevent-devel

 

 

2,下载memcache源码包:

[root@youxia205 opt]# wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz

 

 

3,解压、编译、安装:

[root@youxia205 opt]# tar -zxvf memcached-1.4.5.tar.gz

[root@youxia205 opt]# cd memcached-1.4.5

[root@youxia205 memcached-1.4.5]# ./configure

[root@youxia205 memcached-1.4.5]# make && make install

 

 

4,安装完成之后可以看下memcache的参数:

[root@youxia205 local]# memcached -help

memcached 1.4.5

-p <num>      TCP port number to listen on (default: 11211)

-U <num>      UDP port number to listen on (default: 11211, 0 is off)

-s <file>     UNIX socket path to listen on (disables network support)

-a <mask>     access mask for UNIX socket, in octal (default: 0700)

-l <ip_addr>  interface to listen on (default: INADDR_ANY, all addresses)

-d            run as a daemon

-r            maximize core file limit

-u <username> assume identity of <username> (only when run as root)

-m <num>      max memory to use for items in megabytes (default: 64 MB)

-M            return error on memory exhausted (rather than removing items)

-c <num>      max simultaneous connections (default: 1024)

-k            lock down all paged memory.  Note that there is a

              limit on how much memory you may lock.  Trying to

              allocate more than that would fail, so be sure you

              set the limit correctly for the user you started

              the daemon with (not for -u <username> user;

              under sh this is done with 'ulimit -S -l NUM_KB').

-v            verbose (print errors/warnings while in event loop)

-vv           very verbose (also print client commands/reponses)

-vvv          extremely verbose (also print internal state transitions)

-h            print this help and exit

-i            print memcached and libevent license

-P <file>     save PID in <file>, only used with -d option

-f <factor>   chunk size growth factor (default: 1.25)

-n <bytes>    minimum space allocated for key+value+flags (default: 48)

-L            Try to use large memory pages (if available). Increasing

              the memory page size could reduce the number of TLB misses

              and improve the performance. In order to get large pages

              from the OS, memcached will allocate the total item-cache

              in one large chunk.

-D <char>     Use <char> as the delimiter between key prefixes and IDs.

              This is used for per-prefix stats reporting. The default is

              ":" (colon). If this option is specified, stats collection

              is turned on automatically; if not, then it may be turned on

              by sending the "stats detail on" command to the server.

-t <num>      number of threads to use (default: 4)

-R            Maximum number of requests per event, limits the number of

              requests process for a given connection to prevent

              starvation (default: 20)

-C            Disable use of CAS

-b            Set the backlog queue limit (default: 1024)

-B            Binding protocol - one of ascii, binary, or auto (default)

-I            Override the size of each slab page. Adjusts max item size

              (default: 1mb, min: 1k, max: 128m)

 

5,启动memcached服务:

[root@youxia205 local]# /usr/local/bin/memcached -u root -p 11211 -m 2048m -d

#-u是指运行memcache的用户,-p是设置memcache监听的端口,-m是分配给memcache使用的内存数据量

 -d是指作为daemon在后台启动。

 

 

6,查看是否启动成功:

[root@youxia205 local]# netstat -tunlp | grep memcache

tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      14494/memcached    

udp        0      0 0.0.0.0:11211               0.0.0.0:*                               14494/memcached 

 

 

 

7,设置数据库的相关信息:

#建立一个名称为mydb的库:

mysql> create database mydb;

Query OK, 1 row affected (0.00 sec)

 

#使用库,并创建personal_info表:

mysql> use mydb;

Database changed

mysql> CREATE TABLE `personal_info` (

    -> `pi_id` bigint(20) NOT NULL auto_increment,

    -> `pi_name` varchar(50) NOT NULL,

    -> `pi_tel` varchar(15) default NULL,

    -> `pi_qq` varchar(15) default NULL,

    -> `pi_email` varchar(50) default NULL,

    -> PRIMARY KEY (`pi_id`)

    -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

Query OK, 0 rows affected (0.00 sec)

 

#插入一条数据

mysql> INSERT INTO `mydb`.`personal_info` (

    -> `pi_id` ,

    -> `pi_name` ,

    -> `pi_tel` ,

    -> `pi_qq` ,

    -> `pi_email`

    -> )

    -> VALUES (

    -> '1', 'eric', '13611031222', '55555555', 'eric@nginxs.com'

    -> );

Query OK, 1 row affected (0.00 sec)

 

#查看表中的数据:

mysql> select * from  personal_info ;

+-------+---------+-------------+----------+-----------------+

| pi_id | pi_name | pi_tel      | pi_qq    | pi_email        |

+-------+---------+-------------+----------+-----------------+

|     1 | eric    | 13611031222 | 55555555 | eric@nginxs.com |

+-------+---------+-------------+----------+-----------------+

1 row in set (0.00 sec)

 

 

 

 

三,安装客户端(需要PHP环境及PHPmemcache扩展):

1,由于服务器已经安装了ApacheMySQL,直接发安装PHP,下载源码包:

[root@youxia205 opt]# wget http://www.php.net/get/php-5.2.14.tar.bz2/from/cn.php.net/mirror

 

2,解压、编译、安装、配置:

[root@youxia205 opt]# tar -jxvf php-5.2.14.tar.bz2

[root@youxia205 opt]# cd php-5.2.14

 

[root@youxia205 php-5.2.14]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/var/lib/mysql --with-jpeg-dir \

> --with-png-dir --with-bz2 --with-freetype-dir --with-iconv-dir  --enable-thread-safe-client

 

 

注:编译PHP的时候遇到的错误:

configure: error: Cannot find libmysqlclient_r under /usr.

Note that the MySQL client library is not bundled anymore!

 

解决办法如下:

[root@youxia205 php-5.2.14]# cp -r /usr/lib64/mysql/* /usr/lib/

 

[root@youxia205 php-5.2.14]# make && make install

 

[root@youxia205 php-5.2.14]# cp php.ini-dist /usr/local/php/etc/php.ini

 

[root@youxia205 ~]# vi /usr/local/apache2/conf/httpd.conf

 

在AddType application/x-gzip .gz .tgz下面添加

AddType application/x-httpd-php .php

 

在index.html后面添加index.php,如下所示:

<IfModule dir_module>

    DirectoryIndex index.html index.php

</IfModule>

 

 

3,启动Apache服务:

[root@youxia205 htdocs]# /usr/local/apache2/bin/apachectl start

 

 

4,安装memcachePHP扩展(下载源码包、编译、安装、配置):

[root@youxia205 opt]# wget http://pecl.php.net/get/memcache-2.2.5.tgz

 

[root@youxia205 opt]# tar -zxvf memcache-2.2.5.tgz

[root@youxia205 opt]# cd memcache-2.2.5

[root@youxia205 memcache-2.2.5]# /usr/local/php/bin/phpize

Configuring for:

PHP Api Version:         20041225

Zend Module Api No:      20060613

Zend Extension Api No:   220060519

 

[root@youxia205 memcache-2.2.5]# ./configure --enable-memcache -with-php-config=/usr/local/php/bin/php-config --with-zlib-dir

[root@youxia205 memcache-2.2.5]# make

[root@youxia205 memcache-2.2.5]# make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20060613/

 

[root@youxia205 memcache-2.2.5]# vi /usr/local/php/etc/php.ini

extension=memcache.so

 

 

四,测试memcache

1,在网站目录下建立一个测试文件,名称为memtest.php,具体如下:

[root@youxia205 ~]# cd  /usr/local/apache2/htdocs

[root@youxia205 htdocs]# vi memtest.php

<?php

$memcachehost = '192.168.0.205';

$memcacheport = 11211;

$memcachelife = 60;

$memcache = new Memcache;

$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");

$query="select * from personal_info limit 10";

$key=md5($query);

if(!$memcache->get($key))

{

                $conn=mysql_connect("localhost","root","user");

                mysql_select_db(mydb);

                $result=mysql_query($query);

                while ($row=mysql_fetch_assoc($result))

                {

                        $arr[]=$row;

                }

                $f = 'mysql';

                $memcache->add($key,serialize($arr),0,30);        //mysql 查询后,插入 memcached

                $data = $arr ;

}

else{

        $f = 'memcache';

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

        $data = unserialize($data_mem);

}

echo $f;

echo "<br>";

//print_r($data);

foreach($data as $a)

{

                echo "number is <b><font color=#FF0000>$a[pi_id]</font></b>";

                echo "<br>";

                echo "name is <b><font color=#FF0000>$a[pi_name]</font></b>";

                echo "<br>";

                echo "tel is <b><font color=#FF0000>$a[pi_tel]</font></b>";

                echo "<br>";

                echo "qq is <b><font color=#FF0000>$a[pi_qq]</font></b>";

                echo "<br>";

                echo "email is <b><font color=#FF0000>$a[pi_email]</font></b>";

                echo "<br>";

 

}

?>

 

2,执行测试:

#执行的时候报错

Fatal error: Class 'Memcache' not found in /usr/local/apache2/htdocs/memtest.php on line 5

 

#解决方法如下:

[root@youxia205 htdocs]# find / -name memcache.so

/usr/local/php/lib/php/extensions/no-debug-zts-20060613/memcache.so

/opt/memcache-2.2.5/.libs/memcache.so

/opt/memcache-2.2.5/modules/memcache.so

 

vi /usr/local/php/etc/php.ini

extension_dir = "./"

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20060613"

 

 

再次执行:

 

 

 

查看数据库中正在执行的语句,没有发现对mydb库执行查询的线程,说明PHP直接从memcache中提取的数据:

mysql> show processlist;

+-----+----------+-----------------+-------+---------+------+-------+------------------+

| Id  | User     | Host            | db    | Command | Time | State | Info             |

+-----+----------+-----------------+-------+---------+------+-------+------------------+

| 697 | prog     | localhost:44175      | word | Sleep   |   23 |       | NULL             |

| 698 | prog     | localhost:44176       | word | Sleep   |   23 |       | NULL             |

| 744 | user   | localhost              | mydb  | Sleep   | 3443 |       | NULL             |

| 747 | user   | localhost              | NULL  | Query   |    0 | NULL  | show processlist |

+-----+----------+-----------------+-------+---------+------+-------+------------------+

4 rows in set (0.00 sec)

 










本文转自 trt2008 51CTO博客,原文链接:http://blog.51cto.com/chlotte/383327,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
13天前
|
前端开发 jenkins 持续交付
新的centos7.9安装docker版本的jenkins2.436.1最新版本-前端项目发布(五)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-前端项目发布(五)
35 1
|
13天前
|
jenkins 网络安全 持续交付
新的centos7.9安装docker版本的jenkins2.436.1最新版本-后端项目发布(四)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-后端项目发布(四)
44 3
|
2天前
|
缓存 Linux 编译器
Linux(CentOS7.5) 安装部署 Python3.6(超详细!包含 Yum 源配置!)
该指南介绍了在Linux系统中配置Yum源和安装Python3的步骤。首先,通过`yum install`和`wget`命令更新和备份Yum源,并从阿里云获取CentOS和EPEL的repo文件。接着,清理和更新Yum缓存。然后,下载Python3源代码包,推荐使用阿里云镜像加速。解压后,安装必要的依赖,如gcc。在配置和编译Python3时,可能需要解决缺少C编译器的问题。完成安装后,创建Python3和pip3的软链接,并更新环境变量。最后,验证Python3安装成功,并可选地升级pip和配置pip源以提高包下载速度。
|
4天前
|
存储 Linux 虚拟化
Hyper-V 安装 CentOS 8.5
本文档介绍了在 Windows 10 上使用 Hyper-V 安装 CentOS 8.5.2111 的详细步骤
25 3
|
4天前
|
Linux 开发工具
蓝易云 - 在CentOS7/RHEL7上安装OpenSSL1.1.x教程
以上就是在CentOS7/RHEL7上安装OpenSSL 1.1.x的步骤。希望对你有所帮助。
29 0
|
7天前
|
网络协议 Java Linux
CentOS上安装运行XWiKi
CentOS上安装运行XWiKi
10 0
|
7天前
|
Java Linux
CentOS上安装openjdk
CentOS上安装openjdk
11 0
|
8天前
|
Java 应用服务中间件 Linux
Centos7 安装JDK和Tomcat
Centos7 安装JDK和Tomcat
34 0
|
8天前
|
运维 网络协议 Linux
2024年最全CentOS8 Consul微服务架构安装(1)_agent(1),Linux运维开发面试
2024年最全CentOS8 Consul微服务架构安装(1)_agent(1),Linux运维开发面试
|
10天前
|
安全 关系型数据库 MySQL
解决centos7.0安装mysql后出现access defind for user@'localhost'的错误
在使用yum 安装完mariadb, mariadb-server, mariadb-devel后
15 0

热门文章

最新文章