首页 > 代码库 > centos7下memcached的安装配置

centos7下memcached的安装配置

1.下载

 
 
 
 
官网
http://memcached.org/
 
memcached 依赖 libevent-devel 
 
需要先安装libevent-devel
 
1 yum -y install libevent-devel

 

 
安装完毕 
 
继续安装memcached
 
1 wget http://memcached.org/files/memcached-1.4.35.tar.gz
2  
3 tar -zxvf memcached-1.4.35.tar.gz
4  
5 cd memcached-1.4.35.tar.gz
6  
7 ./configure && make && sudo make install

 

 
安装成功 ,可以在
1 /usr/local/bin
2 找到memcached

 

 

2.启动与关闭

1.启动服务端

1 /usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1  -p 12000 -c 256 -P /tmp/memcached.pid

 说明:

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

 

检查是否启动成功:
1 [root@localhost bin]# ps aux |grep memcached
2 apache     9949  0.0  0.4 324676  4104 ?        Ssl  10:21   0:01 /usr/local/bin/memcached -d -m 10 -u apache -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
3 root      10652  0.0  0.0 112648   960 pts/0    R+   11:08   0:00 grep --color=auto memcached

2.结束memcached

1 # kill `cat /tmp/memcached.pid`
也可以启动多个守护进程,不过端口不能重复。

3.php中使用memcached

1.安装php-pecl-memcached

1 yum -y install php-pecl-memcached
2  
3 添加模块到php
4 vim /etc/php.ini
5 添加
6  
7 extension=memcached.so
技术分享
 看到这个,说明模块安装成功。
 技术分享

 

2.编写测试php

 
1 <?php
2 $m = new Memcached();
3 $m->addServer(localhost, 12000);
4 print_r($m->getVersion());

 

 
如果正常显示版本号
 Array ( [localhost:12000] => 1.4.35 )
 
则php连接memcached成功
 
如果显示
 Array ( [localhost:12000] => 255.255.255)
则连接失败
 

解决方法:

第一,注意开启memcached服务的时候 指定-u apache
 
第二,开启了SeLinux 
 
运行
1 /usr/sbin/setsebool httpd_can_network_connect=1

 

ok,可以正常使用了。
 
 
 
 
 
 
 
 
<style>html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", "微软雅黑", "Microsoft YaHei UI", SimSun, SimHei, arial, sans-serif; line-height: 1.6; margin: 0; padding: 1.33rem 1rem } h1,h2,h3,h4,h5,h6 { margin: 1.33rem 0 0.667rem; padding: 0; font-weight: bold } h1 { font-size: 1.4rem } h2 { font-size: 1.33rem } h3 { font-size: 1.2rem } h4 { font-size: 1.13rem } h5 { font-size: 1rem } h6 { font-size: 1rem; color: #777777; margin: 1rem 0 } div,p,ul,ol,dl,li { margin: 0 } blockquote,table,pre,code { margin: 8px 0 } ul,ol { padding-left: 2.13rem } blockquote { padding: 0 0.8rem } blockquote>:first-child { margin-top: 0 } blockquote>:last-child { margin-bottom: 0 } img { border: 0; max-width: 100%; height: auto !important; margin: 2px 0 } table { border-collapse: collapse; border: 1px solid #bbbbbb } td,th { padding: 4px 8px; border-collapse: collapse; border: 1px solid #bbbbbb }</style> <style>html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, "Microsoft YaHei UI", SimSun, SimHei, arial, sans-serif; line-height: 1.6; color: ; background-color: ; margin: 0; padding: 1.33rem 1rem } img { max-width: 100% }</style> <style>html { height: 100% } body { min-height: 100% } .wiz-img-resize-handle { position: absolute; z-index: 1000; border: 1px solid black; background-color: white } .wiz-img-resize-handle { width: 5px; height: 5px } .wiz-img-resize-handle.lt { cursor: nw-resize } .wiz-img-resize-handle.tm { cursor: n-resize } .wiz-img-resize-handle.rt { cursor: ne-resize } .wiz-img-resize-handle.lm { cursor: w-resize } .wiz-img-resize-handle.rm { cursor: e-resize } .wiz-img-resize-handle.lb { cursor: sw-resize } .wiz-img-resize-handle.bm { cursor: s-resize } .wiz-img-resize-handle.rb { cursor: se-resize } .wiz-table-body.wiz-table-moving *,.wiz-table-body.wiz-table-moving *::before,.wiz-table-body.wiz-table-moving *::after { cursor: default !important } td,th { position: relative } #wiz-table-range-border { display: none; width: 0; height: 0; position: absolute; top: 0; left: 0; z-index: 105 } #wiz-table-col-line,#wiz-table-row-line { display: none; background-color: #448aff; position: absolute; z-index: 120 } #wiz-table-col-line { width: 1px; cursor: col-resize } #wiz-table-row-line { height: 1px; cursor: row-resize } #wiz-table-range-border_start,#wiz-table-range-border_range { display: none; width: 0; height: 0; position: absolute } #wiz-table-range-border_start_top,#wiz-table-range-border_range_top { height: 2px; background-color: #448aff; position: absolute; top: 0; left: 0 } #wiz-table-range-border_range_top { height: 1px } #wiz-table-range-border_start_right,#wiz-table-range-border_range_right { width: 2px; background-color: #448aff; position: absolute; top: 0 } #wiz-table-range-border_range_right { width: 1px } #wiz-table-range-border_start_bottom,#wiz-table-range-border_range_bottom { height: 2px; background-color: #448aff; position: absolute; top: 0 } #wiz-table-range-border_range_bottom { height: 1px } #wiz-table-range-border_start_left,#wiz-table-range-border_range_left { width: 2px; background-color: #448aff; position: absolute; top: 0; left: 0 } #wiz-table-range-border_range_left { width: 1px } #wiz-table-range-border_start_dot,#wiz-table-range-border_range_dot { width: 5px; height: 5px; border: 2px solid rgb(255, 255, 255); background-color: #448aff; cursor: crosshair; position: absolute; z-index: 110 } .wiz-table-tools { display: block; background-color: #fff; position: absolute; left: 0px; border: 1px solid #ddd; z-index: 130 } .wiz-table-tools ul { list-style: none; padding: 0 } .wiz-table-tools .wiz-table-menu-item { position: relative; float: left; margin: 5px 2px 5px 8px } .wiz-table-tools .wiz-table-menu-item .wiz-table-menu-button { font-size: 15px; width: 20px; height: 20px; line-height: 20px; cursor: pointer; position: relative } .wiz-table-tools i.editor-icon { font-size: 15px; color: #455a64 } .wiz-table-tools .wiz-table-menu-item .wiz-table-menu-button i#wiz-menu-bg-demo { position: absolute; top: 1px; left: 0 } .wiz-table-tools .wiz-table-menu-sub { position: absolute; display: none; width: 125px; padding: 5px 0; background: #fff; border: 1px solid #E0E0E0; top: 28px; left: -9px } .wiz-table-tools .wiz-table-menu-sub>div { font-size: 15px } .wiz-table-tools .wiz-table-menu-item.active .wiz-table-menu-sub { display: block } .wiz-table-tools .wiz-table-menu-sub::before,.wiz-table-tools .wiz-table-menu-sub::after { position: absolute; content: " "; border-style: solid; border-color: transparent; border-bottom-color: #cccccc; left: 22px; margin-left: -14px; top: -8px; border-width: 0 8px 8px 8px; z-index: 10 } .wiz-table-tools .wiz-table-menu-sub::after { border-bottom-color: #ffffff; top: -7px } .wiz-table-tools .wiz-table-menu-sub-item { padding: 4px 12px; font-size: 14px } .wiz-table-tools .wiz-table-menu-sub-item.split { border-top: 1px solid #E0E0E0 } .wiz-table-tools .wiz-table-menu-sub-item:hover { background-color: #ececec } .wiz-table-tools .wiz-table-menu-sub-item.disabled { color: #bbbbbb; cursor: default } .wiz-table-tools .wiz-table-menu-sub-item.disabled:hover { background-color: transparent } .wiz-table-tools .wiz-table-menu-item.wiz-table-cell-bg:hover .wiz-table-color-pad { display: block } .wiz-table-tools .wiz-table-color-pad { display: none; padding: 10px; width: 85px; height: 88px; background-color: #fff; cursor: default } .wiz-table-tools .wiz-table-color-pad>div { font-size: 15px } .wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item { display: inline-block; width: 15px; height: 15px; margin-right: 9px; position: relative } .wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item i.pad-demo { position: absolute; top: 3px; left: 0 } .wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item .icon-oblique_line { color: #cc0000 } .wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child { margin-right: 0 } .wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item.active i.editor-icon.icon-box { color: #448aff } .wiz-table-tools .wiz-table-cell-align { display: none; padding: 10px; width: 85px; height: 65px; background-color: #fff; cursor: default } .wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item { display: inline-block; width: 15px; height: 15px; margin-right: 9px; position: relative } .wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child { margin-right: 0 } .wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item i.valign { position: absolute; top: 3px; left: 0; color: #d2d2d2 } .wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.valign { color: #a1c4ff } .wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.icon-box,.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.align { color: #448aff } .wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child,.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child { margin-right: 0 } th.wiz-selected-cell-multi,td.wiz-selected-cell-multi { background: rgba(0,102,255,.05) } th::before,td::before,#wiz-table-col-line::before,#wiz-table-range-border_start_right::before,#wiz-table-range-border_range_right::before { content: " "; position: absolute; top: 0; bottom: 0; right: -5px; width: 9px; cursor: col-resize; background: transparent; z-index: 100 } th::after,td::after,#wiz-table-row-line::before,#wiz-table-range-border_start_bottom::before,#wiz-table-range-border_range_bottom::before { content: " "; position: absolute; left: 0; right: 0; bottom: -5px; height: 9px; cursor: row-resize; background: transparent; z-index: 100 } .wiz-table-container { } .wiz-table-body { position: relative; padding: 0 0 10px } .wiz-table-body table { margin: 0; outline: none } td,th { height: 28px; outline: none } body pre.prettyprint { padding: 0 } body pre.prettyprint code { white-space: pre } body pre.prettyprint.linenums { overflow: auto } body pre.prettyprint.linenums ol.linenums { padding: 10px 10px 10px 40px !important }</style>

centos7下memcached的安装配置