memcache安装
一,memcache簡單介紹:
memcached是高性能的分布式內存緩存服務器,為了提高性能,memcached中的數據都保存在內存中,重啟memcached及重啟操作系統都會導致緩存中的數據全部丟失,其緩存的數據達到指定的內存分配值之后,就會使用LRU算法刪除不使用的緩存。(LRU算法的基本概念:當分配的內存可用空間不足時,它盡可能地先保留最常用的數據,將最近沒有使用的數據移出內存,釋放出的空間來存儲其它的數據。)
其作用是緩存數據庫查詢結果,這樣就減少了對數據庫的訪問次數據,從而減輕數據庫的壓力,這樣就提高了用戶的訪問速度,典型應用如下圖所示:
?
?
?實驗環境:3臺虛擬機, Web——memcache——mysql
web服務器安裝:Apache+PHP+PHP(memcache擴展)+mysql+mysql-devel
memcache服務器:libevent libevent-devel? memcache
mysql服務器: 安裝mysql
一、web安裝
1、#yum? install? -y? mysql??? mysql-devel
#./configure??? --preifx=/usr/local/apache2?? --enable-so? --enable-rewrite???????????????? //安裝Apache服務
#make? ;? make install
#LDFLAGS=-L/usr/lib/mysql? ./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 --with-mysql --with-pdo-mysql --with-mysqli=/usr/lib/mysql/mysql_config??????????? //編譯安裝PHP
#make? ; make install
注:編譯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/lib/mysql/* /usr/lib/
2、#vi? /usr/local/apache2/conf/httpd.conf????????????????????????? //修改httpd.conf支持php
#AddType application/x-gzip .tgz
??? AddType application/x-httpd-php .php
??? AddType application/x-httpd-php-source .phps?????????????????? //加上下面2行
<IfModule dir_module>
??? DirectoryIndex index.php index.html index.htm
</IfModule>??????????????????????????????????????? //加上index.php,這時候啟動Apache能看見PHP測試頁
3、安裝客戶端(需要PHP環境及PHP的memcache擴展):
[root@youxia205 opt]# wget http://www.php.net/get/php-5.2.14.tar.bz2/from/cn.php.net/mirror
[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?4、#vi??? /usr/local/apache2/htdocs/memtest.php?????????????? //建立memtest.php測試頁,測試memcache使用
<?php
$memcachehost = '192.168.37.20';???????????????? //memcache的IP地址
$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("192.168.37.30","root","123456");??????? //這里注意IP是memcache服務IP,root是mysql用戶名,123456是密碼
??????????????? 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);
??????????????? $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>";
}
?>????????????????????????????????????? //OK web服務器就算安裝完畢
二、 memcache安裝
安裝libevent庫,它將Linux的epoll、freebsd操作系統的kqueue等事件處理功能封裝成統一的接口,memcached使用這個庫,可以發揮其高性能。
[root@youxia205 opt]# yum install libevent libevent-devel 1、下載memcache源碼包: [root@youxia205 opt]# wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz 2、解壓、編譯、安裝: [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 3、安裝完成之后可以看下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) 4、啟動memcached服務: [root@youxia205 local]# memcached -d -m 64 -u root -l 192.168.37.20 -p 11211 -c 64 -P /tmp/memcached.pid #-u是指運行memcache的用戶,-p是設置memcache監聽的端口,-m是分配給memcache使用的內存數據量 -d是指作為daemon在后臺啟動。 5、查看是否啟動成功: [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? 三、mysql數據安裝這里不多介紹,可以rpm或者編譯安裝,這里介紹web需要能登錄到mysql這樣才能夠訪問數據內容,我當時就因為這個問題折騰半天訪問web看不到測試內容,最后經同事提醒我才弄好~哈哈 #允許root用戶在任何地方進行遠程登錄,并具有所有庫任何操作權限,具體操作如下:1、在本機先使用root用戶登錄mysql:
mysql -u root -p"youpassword"
進行授權操作:
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
重載授權表:
FLUSH PRIVILEGES;
退出,然后測試一下在web上 mysql? -u root? -h IP -p?????是否能登錄上mysql 2、建立一個數據庫表用于測試使用 設置數據庫的相關信息: #建立一個名稱為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) 四、執行測試: http://IP/memtest.php
#執行的時候報錯
Fatal error: Class 'Memcache' not found in /usr/local/apache2/htdocs/memtest.php on line 5
?
1、解決方法如下:
[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
?
2、vi /usr/local/php/etc/php.ini
extension_dir = "./"
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20060613"
3、正確就會顯示如下內容:
memcache
number is 1
name is eric
tel is 13611031222
qq is 55555555
email is eric@nginxs.com
?
4、查看數據庫中正在執行的語句,沒有發現對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)總結
以上是生活随笔為你收集整理的memcache安装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 加盟个幼儿园大概需要投资多少钱
- 下一篇: nginx做方向代理不显示图片的问题