search
HomeDatabaseMysql TutorialCentOS 5.4下的Memcache安装步骤(Linux+Nginx+PHP+Memcached)

CentOS 5.4下的Memcache安装步骤分享,想要配置Linux+Nginx+PHP+Memcached运行环境的朋友可以参考下

一、源码包准备

服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-v1.4.4 。
下载:http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,

libevent的最新版本是libevent-1.4.13-stable。(如果你的系统已经安装了libevent,可以不用安装)
官网:http://www.monkey.org/~provos/libevent/
下载:http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz

准备Memcached的PHP扩展的源码安装包:
官网:http://pecl.php.net/get/memcache-2.2.5.tgz

Linux指令下载:

代码如下:

wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz 
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz 
wget http://pecl.php.net/get/memcache-2.2.5.tgz


二、安装与配置
1、先安装libevent

代码如下:

tar zxvf libevent-1.4.13-stable.tar.gz 
cd libevent-1.4.13-stable 
./configure --prefix=/usr 
make 
make install


2、测试libevent是否安装成功

代码如下:

ls -al /usr/lib | grep libevent 
libevent-1.1a.so.1 
libevent-1.1a.so.1.0.2 
libevent-1.4.so.2 
libevent-1.4.so.2.1.3 
libevent.a 
libevent_core-1.4.so.2 
libevent_core-1.4.so.2.1.3 
libevent_core.a 
libevent_core.la 
libevent_core.so 
libevent_extra-1.4.so.2 
libevent_extra-1.4.so.2.1.3 
libevent_extra.a 
libevent_extra.la 
libevent_extra.so 
libevent.la 
libevent.so


版本不同,可能文件列表不同。

3、安装memcached,同时需要安装中指定libevent的安装位置

代码如下:

tar zxvf memcached-1.4.4.tar.gz 
cd memcached-1.4.4 
./configure –with-libevent=/usr 
make && make install


安装完成后会把memcached 自动放到 /usr/local/bin/memcached

4、测试是否成功安装memcached

 代码如下:

ls -al /usr/local/bin/mem* 
-rwxr-xr-x 1 root root 201869 12-14 21:44 /usr/local/bin/memcached


5、安装Memcache的PHP扩展

①安装PHP的memcache扩展

代码如下:

tar vxzf memcache-2.2.5.tgz 
cd memcache-2.2.5 
/usr/local/webserver/php/bin/phpize 
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir 
make 
make install


②上述安装完后会有类似这样的提示:

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

③把php.ini中的extension_dir = “./”修改为

代码如下:


extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/”


④添加一行来载入memcache扩展:extension=memcache.so

三、memcached的基本设置
1.启动Memcache的服务器端:

代码如下:


memcached -d -m 10 -u root -l 202.207.177.177 -p 11211 -c 256 -P /tmp/memcached.pid


参数说明:

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址202.207.177.177,
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

2.如果要结束Memcache进程,执行:

 代码如下:


kill `cat /tmp/memcached.pid`


也可以启动多个守护进程,不过端口不能重复。

3.检查Memcached是否启动

 代码如下:


netstat -ant
tcp 0 0 202.207.177.177:11211 0.0.0.0:* LIST


11211端口已经打开,说明Memcached已正常启动。

4.重启CentOS

代码如下:


reboot


四、Memcache环境测试
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。开始你的Memcache的征途吧!

 代码如下:

<?php 
$mem = new Memcache; 
$mem->connect("202.207.177.177", 11211); 
$mem->set(&#39;key&#39;, &#39;This is a test!&#39;, 0, 60); 
$val = $mem->get(&#39;key&#39;); 
echo $val; 
?>


著名的PHPCMS同样支持Memcached扩展:

代码如下:

<?php 
//MemCache服务器配置 
//define(&#39;MEMCACHE_HOST&#39;, &#39;localhost&#39;); //MemCache服务器主机 
//define(&#39;MEMCACHE_PORT&#39;, 11211); //MemCache服务器端口 
//define(&#39;MEMCACHE_TIMEOUT&#39;, 1); //S,MemCache服务器连接超时 
class cache 
{ 
var $memcache; 
function __construct() 
{ 
$this->memcache = &new Memcache; 
$this->memcache->pconnect(MEMCACHE_HOST, MEMCACHE_PORT, MEMCACHE_TIMEOUT); 
} 
function cache() 
{ 
$this->__construct(); 
} 
function get($name) 
{ 
return $this->memcache->get($name); 
} 
function set($name, $value, $ttl = 0) 
{ 
return $this->memcache->set($name, $value, 0, $ttl); 
} 
function rm($name) 
{ 
return $this->memcache->delete($name); 
} 
function clear() 
{ 
return $this->memcache->flush(); 
} 
} 
?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.