{"id":231,"date":"2011-07-05T07:27:43","date_gmt":"2011-07-04T23:27:43","guid":{"rendered":"http:\/\/www.cnmiss.cn\/?p=231"},"modified":"2011-07-05T07:37:49","modified_gmt":"2011-07-04T23:37:49","slug":"linux%e4%b8%8b%e7%9a%84memcache%e5%ae%89%e8%a3%85","status":"publish","type":"post","link":"https:\/\/www.cnmiss.cn\/?p=231","title":{"rendered":"Linux\u4e0b\u7684Memcache\u5b89\u88c5(\u8f6c)"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u7814\u7a76\u600e\u4e48\u8ba9Discuz!\u53bb\u5e94\u7528Memcache\u53bb\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u8bb0\u5f55\u4e0bMemcache\u5b89\u88c5\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><strong>Linux\u4e0bMemcache\u670d\u52a1\u5668\u7aef\u7684\u5b89\u88c5<\/strong><br \/>\n\u670d\u52a1\u5668\u7aef\u4e3b\u8981\u662f\u5b89\u88c5memcache\u670d\u52a1\u5668\u7aef\uff0c\u76ee\u524d\u7684\u6700\u65b0\u7248\u672c\u662f memcached-1.3.0 \u3002<br \/>\n\u4e0b\u8f7d\uff1ahttp:\/\/www.danga.com\/memcached\/dist\/memcached-1.2.2.tar.gz<br \/>\n\u53e6\u5916\uff0cMemcache\u7528\u5230\u4e86libevent\u8fd9\u4e2a\u5e93\u7528\u4e8eSocket\u7684\u5904\u7406\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u5b89\u88c5libevent\uff0clibevent\u7684\u6700\u65b0\u7248\u672c\u662flibevent-1.3\u3002\uff08\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u5df2\u7ecf\u5b89\u88c5\u4e86libevent\uff0c\u53ef\u4ee5\u4e0d\u7528\u5b89\u88c5\uff09<br \/>\n\u5b98\u7f51\uff1ahttp:\/\/www.monkey.org\/~provos\/libevent\/<br \/>\n\u4e0b\u8f7d\uff1ahttp:\/\/www.monkey.org\/~provos\/libevent-1.3.tar.gz<\/p>\n<p>\u7528wget\u6307\u4ee4\u76f4\u63a5\u4e0b\u8f7d\u8fd9\u4e24\u4e2a\u4e1c\u897f.\u4e0b\u8f7d\u56de\u6e90\u6587\u4ef6\u540e\u3002<br \/>\n1.\u5148\u5b89\u88c5libevent\u3002\u8fd9\u4e2a\u4e1c\u897f\u5728\u914d\u7f6e\u65f6\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u5b89\u88c5\u8def\u5f84\uff0c\u5373.\/configure \u2013prefix=\/usr\uff1b\u7136\u540emake\uff1b\u7136\u540emake install\uff1b<br \/>\n2.\u518d\u5b89\u88c5memcached\uff0c\u53ea\u662f\u9700\u8981\u5728\u914d\u7f6e\u65f6\u9700\u8981\u6307\u5b9alibevent\u7684\u5b89\u88c5\u8def\u5f84\u5373.\/configure \u2013with-libevent=\/usr\uff1b\u7136\u540emake\uff1b\u7136\u540emake install\uff1b<br \/>\n\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86Linux\u4e0bMemcache\u670d\u52a1\u5668\u7aef\u7684\u5b89\u88c5\u3002\u8be6\u7ec6\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>1.\u5206\u522b\u628amemcached\u548clibevent\u4e0b\u8f7d\u56de\u6765\uff0c\u653e\u5230 \/tmp \u76ee\u5f55\u4e0b\uff1a<br \/>\n# cd \/tmp<br \/>\n# wget http:\/\/www.danga.com\/memcached\/dist\/memcached-1.2.0.tar.gz<br \/>\n# wget http:\/\/www.monkey.org\/~provos\/libevent-1.2.tar.gz<\/p>\n<p>2.\u5148\u5b89\u88c5libevent\uff1a<br \/>\n# tar zxvf libevent-1.2.tar.gz<br \/>\n# cd libevent-1.2<br \/>\n# .\/configure \u2013prefix=\/usr<br \/>\n# make<br \/>\n# make install<\/p>\n<p>3.\u6d4b\u8bd5libevent\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<br \/>\n# ls -al \/usr\/lib | grep libevent<br \/>\nlrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -&gt; libevent-1.2.so.1.0.3<br \/>\n-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3<br \/>\n-rw-r\u2013r\u2013 1 root root 454156 11?? 12 17:38 libevent.a<br \/>\n-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la<br \/>\nlrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -&gt; libevent-1.2.so.1.0.3<br \/>\n\u8fd8\u4e0d\u9519\uff0c\u90fd\u5b89\u88c5\u4e0a\u4e86\u3002<\/p>\n<p>4.\u5b89\u88c5memcached\uff0c\u540c\u65f6\u9700\u8981\u5b89\u88c5\u4e2d\u6307\u5b9alibevent\u7684\u5b89\u88c5\u4f4d\u7f6e\uff1a<br \/>\n# cd \/tmp<br \/>\n# tar zxvf memcached-1.2.0.tar.gz<br \/>\n# cd memcached-1.2.0<br \/>\n# .\/configure \u2013with-libevent=\/usr<br \/>\n# make<br \/>\n# make install<br \/>\n\u5982\u679c\u4e2d\u95f4\u51fa\u73b0\u62a5\u9519\uff0c\u8bf7\u4ed4\u7ec6\u68c0\u67e5\u9519\u8bef\u4fe1\u606f\uff0c\u6309\u7167\u9519\u8bef\u4fe1\u606f\u6765\u914d\u7f6e\u6216\u8005\u589e\u52a0\u76f8\u5e94\u7684\u5e93\u6216\u8005\u8def\u5f84\u3002<br \/>\n\u5b89\u88c5\u5b8c\u6210\u540e\u4f1a\u628amemcached\u653e\u5230 \/usr\/local\/bin\/memcached \uff0c<\/p>\n<p>5.\u6d4b\u8bd5\u662f\u5426\u6210\u529f\u5b89\u88c5memcached\uff1a<br \/>\n# ls -al \/usr\/local\/bin\/mem*<br \/>\n-rwxr-xr-x 1 root root 137986 11?? 12 17:39 \/usr\/local\/bin\/memcached<br \/>\n-rwxr-xr-x 1 root root 140179 11?? 12 17:39 \/usr\/local\/bin\/memcached-debug<\/p><\/blockquote>\n<p><strong>\u5b89\u88c5Memcache\u7684PHP\u6269\u5c55<\/strong><br \/>\n1.\u5728http:\/\/pecl.php.net\/package\/memcache \u9009\u62e9\u76f8\u5e94\u60f3\u8981\u4e0b\u8f7d\u7684memcache\u7248\u672c\u3002<br \/>\n2.\u5b89\u88c5PHP\u7684memcache\u6269\u5c55<\/p>\n<blockquote><p>tar vxzf memcache-2.2.1.tgz<br \/>\ncd memcache-2.2.1<br \/>\n\/usr\/local\/php\/bin\/phpize<br \/>\n.\/configure \u2013enable-memcache \u2013with-php-config=\/usr\/local\/php\/bin\/php-config \u2013with-zlib-dir<br \/>\nmake<br \/>\nmake install<\/p><\/blockquote>\n<p>3.\u4e0a\u8ff0\u5b89\u88c5\u5b8c\u540e\u4f1a\u6709\u7c7b\u4f3c\u8fd9\u6837\u7684\u63d0\u793a\uff1a<\/p>\n<blockquote><p>Installing shared extensions: \/usr\/local\/php\/lib\/php\/extensions\/no-debug-non-zts-2007xxxx\/<\/p><\/blockquote>\n<p>4.\u628aphp.ini\u4e2d\u7684extension_dir = \u201c.\/\u201d\u4fee\u6539\u4e3a<\/p>\n<blockquote><p>extension_dir = \u201c\/usr\/local\/php\/lib\/php\/extensions\/no-debug-non-zts-2007xxxx\/\u201d<\/p><\/blockquote>\n<p>5.\u6dfb\u52a0\u4e00\u884c\u6765\u8f7d\u5165memcache\u6269\u5c55\uff1aextension=memcache.so<\/p>\n<p><strong>memcached\u7684\u57fa\u672c\u8bbe\u7f6e<\/strong>\uff1a<br \/>\n1.\u542f\u52a8Memcache\u7684\u670d\u52a1\u5668\u7aef\uff1a<br \/>\n# \/usr\/local\/bin\/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P \/tmp\/memcached.pid<\/p>\n<blockquote><p>-d\u9009\u9879\u662f\u542f\u52a8\u4e00\u4e2a\u5b88\u62a4\u8fdb\u7a0b\uff0c<br \/>\n-m\u662f\u5206\u914d\u7ed9Memcache\u4f7f\u7528\u7684\u5185\u5b58\u6570\u91cf\uff0c\u5355\u4f4d\u662fMB\uff0c\u6211\u8fd9\u91cc\u662f10MB\uff0c<br \/>\n-u\u662f\u8fd0\u884cMemcache\u7684\u7528\u6237\uff0c\u6211\u8fd9\u91cc\u662froot\uff0c<br \/>\n-l\u662f\u76d1\u542c\u7684\u670d\u52a1\u5668IP\u5730\u5740\uff0c\u5982\u679c\u6709\u591a\u4e2a\u5730\u5740\u7684\u8bdd\uff0c\u6211\u8fd9\u91cc\u6307\u5b9a\u4e86\u670d\u52a1\u5668\u7684IP\u5730\u5740192.168.0.200\uff0c<br \/>\n-p\u662f\u8bbe\u7f6eMemcache\u76d1\u542c\u7684\u7aef\u53e3\uff0c\u6211\u8fd9\u91cc\u8bbe\u7f6e\u4e8612000\uff0c\u6700\u597d\u662f1024\u4ee5\u4e0a\u7684\u7aef\u53e3\uff0c<br \/>\n-c\u9009\u9879\u662f\u6700\u5927\u8fd0\u884c\u7684\u5e76\u53d1\u8fde\u63a5\u6570\uff0c\u9ed8\u8ba4\u662f1024\uff0c\u6211\u8fd9\u91cc\u8bbe\u7f6e\u4e86256\uff0c\u6309\u7167\u4f60\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u91cf\u6765\u8bbe\u5b9a\uff0c<br \/>\n-P\u662f\u8bbe\u7f6e\u4fdd\u5b58Memcache\u7684pid\u6587\u4ef6\uff0c\u6211\u8fd9\u91cc\u662f\u4fdd\u5b58\u5728 \/tmp\/memcached.pid\uff0c<\/p><\/blockquote>\n<p>2.\u5982\u679c\u8981\u7ed3\u675fMemcache\u8fdb\u7a0b\uff0c\u6267\u884c\uff1a<\/p>\n<blockquote><p># kill `cat \/tmp\/memcached.pid`<\/p><\/blockquote>\n<p>\u4e5f\u53ef\u4ee5\u542f\u52a8\u591a\u4e2a\u5b88\u62a4\u8fdb\u7a0b\uff0c\u4e0d\u8fc7\u7aef\u53e3\u4e0d\u80fd\u91cd\u590d\u3002<\/p>\n<p>3.\u91cd\u542fapache\uff0cservice httpd restart<\/p>\n<p><strong>Memcache\u73af\u5883\u6d4b\u8bd5<\/strong>\uff1a<br \/>\n\u8fd0\u884c\u4e0b\u9762\u7684php\u6587\u4ef6\uff0c\u5982\u679c\u6709\u8f93\u51faThis is a test!\uff0c\u5c31\u8868\u793a\u73af\u5883\u642d\u5efa\u6210\u529f\u3002\u5f00\u59cb\u9886\u7565Memcache\u7684\u9b45\u529b\u628a\uff01<br \/>\n&lt; ?php<br \/>\n$mem = new Memcache;<br \/>\n$mem-&gt;connect(\u201c127.0.0.1\u2033, 11211);<br \/>\n$mem-&gt;set(\u2018key\u2019, \u2018This is a test!\u2019, 0, 60);<br \/>\n$val = $mem-&gt;get(\u2018key\u2019);<br \/>\necho $val;<br \/>\n?&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u7814\u7a76\u600e\u4e48\u8ba9Discuz!\u53bb\u5e94\u7528Memcache\u53bb\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u8bb0\u5f55\u4e0bMemc &hellip; <a href=\"https:\/\/www.cnmiss.cn\/?p=231\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[41],"class_list":["post-231","post","type-post","status-publish","format-standard","hentry","category-xxbj","tag-memcache"],"_links":{"self":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=231"}],"version-history":[{"count":0,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts\/231\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}