美文网首页
NoSQL一--Memcached

NoSQL一--Memcached

作者: XiaoMing丶 | 来源:发表于2019-02-16 21:57 被阅读0次

    目录

    一、什么是NoSQL
    二、常见NoSQL数据库
    三、Memcached

    一、什么是NoSQL

     非关系型数据库就是NoSQL,关系型数据库代表MySQL。
     对于关系型数据库来说,是需要把数据存储到库、表、行、字段里,查询的时候根据条件一行一行地去匹配,当量非常大的时候就很耗费时间和资源,尤其是数据是需要从磁盘里去检索。
     NoSQL数据库存储原理非常简单(典型的数据类型为k-v),不存在繁杂的关系链,比如mysql查询的时候,需要找到对应的库、表(通常是多个表)以及字段。
     NoSQL数据可以存储在内存里,查询速度非常快。
     NoSQL在性能表现上虽然能优于关系型数据库,但是它并不能完全替代关系型数据库。
     NoSQL因为没有复杂的数据结构,扩展非常容易,支持分布式。

    二、常见NoSQL数据库

     k-v形式的:memcached、redis 适合储存用户信息,比如会话、配置文件、参数、购物车等等。这些信息一般都和ID(键)挂钩,这种情景下键值数据库是个很好的选择。
     文档数据库:mongodb 将数据以文档的形式储存。每个文档都是一系列数据项的集合。每个数据项都有一个名称与对应的值,值既可以是简单的数据类型,如字符串、数字和日期等;也可以是复杂的类型,如有序列表和关联对象。数据存储的最小单位是文档,同一个表中存储的文档属性可以是不同的,数据可以使用XML、JSON或者JSONB等多种形式存储。
     列存储 Hbase
     图 Neo4J、Infinite Graph、OrientDB。

    三、Memcached

    • Memcached简介
       Memcached是国外社区网站LiveJournal团队开发,目的是为了通过缓存数据库查询结果,减少数据库访问次数,从而提高动态web站点性能。
       官方站点 http://www.memcached.org/
       数据结构简单(k-v),数据存放在内存里,不支持持久化,可以自行保存在磁盘里
       多线程
       基于c/s架构,协议简单
       基于libevent的事件处理
       自主内存存储处理(slab allowcation)
       数据过期方式:Lazy Expiration 和 LRU

    • Memcached原理图


    • Slab Allocation
       Slab Allocation的原理
       将分配的内存分割成各种尺寸的块(chunk), 并把尺寸相同的块分成组(chunk的集合),每个chunk集合被称为slab。
       Memcached的内存分配以Page为单位,Page默认值为1M,可以在启动时通过-I参数来指定。
       Slab是由多个Page组成的,Page按照指定大小切割成多个chunk。

    image.png
    • Growth factor
       Memcached在启动时通过-f选项可以指定 Growth Factor因子。该值控制chunk大小的差异。默认值为1.25。
       通过memcached-tool命令查看指定Memcached实例的不同slab状态,可以看到各Item所占大小(chunk大小)差距为1.25
       命令:# memcached-tool 127.0.0.1:11211 display

    • Memcached的数据过期方式
       Lazy Expiration
      Memcached 内部不会监视记录是否过期,而是在get时查看记录的时间戳,检查记录是否过期。这种技术被称为lazy(惰性)expiration。因此,Memcached不会在过期监视上耗费CPU时间。
       LRU
      Memcached会优先使用已超时的记录的空间,但即使如此,也会发生追加新记录时空间不足的情况,此时就要使用名为Least Recently Used(LRU)机制来分配空间。顾名思义,这是删除“最近最少使用”的记录的机制。因此,当内存空间不足时(无法从slab class获取到新的空间时),就从最近未被使用的记录中搜索,并将其空间分配给新的记录。从缓存的实用角度来看,该模型十分理想。

    • 安装memcached

    [root@minglinux-01 ~] yum list |grep memcache
    collectd-memcachec.x86_64               5.8.1-1.el7                    epel     
    dmlite-plugins-memcache.x86_64          1.10.4-2.el7                   epel     
    libmemcached.i686                       1.0.16-5.el7                   base     
    libmemcached.x86_64                     1.0.16-5.el7                   base     
    libmemcached-devel.i686                 1.0.16-5.el7                   base     
    libmemcached-devel.x86_64               1.0.16-5.el7                   base     
    memcached.x86_64                        1.4.15-10.el7_3.1              base     
    memcached-devel.i686                    1.4.15-10.el7_3.1              base     
    memcached-devel.x86_64                  1.4.15-10.el7_3.1              base     
    opensips-memcached.x86_64               1.10.5-4.el7                   epel     
    pcp-pmda-memcache.x86_64                4.1.0-5.el7_6                  updates  
    php-ZendFramework-Cache-Backend-Libmemcached.noarch
    php-pecl-memcache.x86_64                3.0.8-4.el7                    base     
    php-pecl-memcached.x86_64               2.2.0-1.el7                    epel     
    python-memcached.noarch                 1.48-4.el7                     base     
    rubygem-memcache-client.noarch          1.8.5-13.el7                   epel     
    uwsgi-router-memcached.x86_64           2.0.17.1-1.el7                 epel     
    [root@minglinux-01 ~] yum install -y memcached  #yum安装memcached
    [root@minglinux-01 ~] systemctl start memcached #启动memcached服务
    [root@minglinux-01 ~] ps aux |grep memcache
    memcach+  80200  0.0  0.0 344080  1664 ?        Ssl  22:46   0:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
    root      80275  0.0  0.0 112720   984 pts/0    S+   22:47   0:00 grep --color=auto memcache
    #-u指定运行memcached服务的用户,-m指定memcached分配内存,-c指定最大并发数,指定参数可以修改
    
    #修改方法1是以如下方式修改参数后启动服务
    [root@minglinux-01 ~] /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
    #修改方法2是编辑/etc/sysconfig/memcached文件修改
    [root@minglinux-01 ~] vim /etc/sysconfig/memcached
    
    PORT="11211"
    USER="memcached"
    MAXCONN="1024"
    CACHESIZE="64"
    OPTIONS=""  #监听的ip
    
    
    • 查看memcached状态
    [root@minglinux-01 ~] memcached-tool 127.0.0.1:11211  stats
    #127.0.0.1:11211   Field       Value
             accepting_conns           1
                   auth_cmds           0
                 auth_errors           0
                       bytes           0
                  bytes_read           7
               bytes_written           0
                  cas_badval           0
                    cas_hits           0
                  cas_misses           0
                   cmd_flush           0
                     cmd_get           0
                     cmd_set           0
                   cmd_touch           0
                 conn_yields           0
       connection_structures          11
            curr_connections          10
                  curr_items           0 ##需关注数字
                   decr_hits           0
                 decr_misses           0
                 delete_hits           0
               delete_misses           0
           evicted_unfetched           0
                   evictions           0
           expired_unfetched           0
                    get_hits           0 #需关注数字
                  get_misses           0
                  hash_bytes      524288
           hash_is_expanding           0
            hash_power_level          16
                   incr_hits           0
                 incr_misses           0
                    libevent 2.0.21-stable
              limit_maxbytes    67108864
         listen_disabled_num           0
                         pid       80200
                pointer_size          64
                   reclaimed           0
                reserved_fds          20
               rusage_system    0.036318
                 rusage_user    0.036318
                     threads           4
                        time  1548083101
           total_connections          11
                 total_items           0
                  touch_hits           0
                touch_misses           0
                      uptime        1111
                     version      1.4.15
    
    或者使用echo stats |nc 127.0.0.1 11211  需要安装nc工具  yum install -y nc
    [root@minglinux-01 ~] echo stats |nc 127.0.0.1 11211
    STAT pid 80200
    STAT uptime 1347
    STAT time 1548083337
    STAT version 1.4.15
    STAT libevent 2.0.21-stable
    STAT pointer_size 64
    STAT rusage_user 0.042126
    STAT rusage_system 0.042126
    STAT curr_connections 10
    STAT total_connections 12
    STAT connection_structures 11
    STAT reserved_fds 20
    STAT cmd_get 0
    STAT cmd_set 0
    STAT cmd_flush 0
    STAT cmd_touch 0
    STAT get_hits 0
    STAT get_misses 0
    STAT delete_misses 0
    STAT delete_hits 0
    STAT incr_misses 0
    STAT incr_hits 0
    STAT decr_misses 0
    STAT decr_hits 0
    STAT cas_misses 0
    STAT cas_hits 0
    STAT cas_badval 0
    STAT touch_hits 0
    STAT touch_misses 0
    STAT auth_cmds 0
    STAT auth_errors 0
    STAT bytes_read 13
    STAT bytes_written 1026
    STAT limit_maxbytes 67108864
    STAT accepting_conns 1
    STAT listen_disabled_num 0
    STAT threads 4
    STAT conn_yields 0
    STAT hash_power_level 16
    STAT hash_bytes 524288
    STAT hash_is_expanding 0
    STAT bytes 0
    STAT curr_items 0
    STAT total_items 0
    STAT expired_unfetched 0
    STAT evicted_unfetched 0
    STAT evictions 0
    STAT reclaimed 0
    END
    
    #若安装libmemcached后,可以使用命令 
    # memstat --servers=127.0.0.1:11211 查看memcached服务状态
    [root@minglinux-01 ~] rpm -qa libmemcached
    [root@minglinux-01 ~] yum install -y libmemcached
    [root@minglinux-01 ~] memstat --servers=127.0.0.1:11211 
    Server: 127.0.0.1 (11211)
         pid: 80200
         uptime: 1927
         time: 1548083917
         version: 1.4.15
         libevent: 2.0.21-stable
         pointer_size: 64
         rusage_user: 0.066200
         rusage_system: 0.052014
         curr_connections: 10
         total_connections: 13
         connection_structures: 11
         reserved_fds: 20
         cmd_get: 0
         cmd_set: 0
         cmd_flush: 0
         cmd_touch: 0
         get_hits: 0
         get_misses: 0
         delete_misses: 0
         delete_hits: 0
         incr_misses: 0
         incr_hits: 0
         decr_misses: 0
         decr_hits: 0
         cas_misses: 0
         cas_hits: 0
         cas_badval: 0
         touch_hits: 0
         touch_misses: 0
         auth_cmds: 0
         auth_errors: 0
         bytes_read: 30
         bytes_written: 2072
         limit_maxbytes: 67108864
         accepting_conns: 1
         listen_disabled_num: 0
         threads: 4
         conn_yields: 0
         hash_power_level: 16
         hash_bytes: 524288
         hash_is_expanding: 0
         bytes: 0
         curr_items: 0
         total_items: 0
         expired_unfetched: 0
         evicted_unfetched: 0
         evictions: 0
         reclaimed: 0
    

    部分需关注的参数说明如下:
    pid #memcache服务器的进程ID
    uptime #服务器已经运行的秒数
    time #服务器当前的unix时间戳
    version #memcache版本
    pointer_size #当前操作系统的指针大小(32位系统一般是32bit)
    rusage_user #进程的累计用户时间
    rusage_system #进程的累计系统时间
    curr_items #服务器当前存储的items数量
    total_items #从服务器启动以后存储的items总数量
    bytes #当前服务器存储items占用的字节数
    curr_connections #当前打开着的连接数
    total_connections #从服务器启动以后曾经打开过的连接数
    connection_structures #服务器分配的连接构造数
    cmd_get #get命令(获取)总请求次数
    cmd_set #set命令(保存)总请求次数
    get_hits #总命中次数
    get_misses #总未命中次数
    evictions #为获取空闲内存而删除的items数
    bytes_read #总读取字节数(请求字节数)
    bytes_written #总发送字节数(结果字节数)
    limit_maxbytes #分配给memcache的内存大小(字节)
    threads #当前线程数

    • memcached命令行操作
    #使用telnet进入memcached命令行
    [root@minglinux-01 ~] yum install -y telnet
    [root@minglinux-01 ~] telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    
    
    • Memcached语法规则

    <command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
    注:\r\n在windows下是Enter键
    <command name>可以是set, add, replace
    set表示按照相应的<key>存储该数据,没有的时候增加,有的时候覆盖
    add表示按照相应的<key>添加该数据,但是如果该<key>已经存在则会操作失败
    replace表示按照相应的<key>替换数据,但是如果该<key>不存在则操作失败。
    <key>客户端需要保存数据的key
    <flags>是一个16位的无符号的整数(以十进制的方式表示)。该标志将和需要存储的数据一起存储,并在客户端get数据时返回。客户端可以将此标志用做特殊用途,此标志对服务器来说是不透明的。
    <exptime>为过期的时间。若为0表示存储的数据永远不过期(但可被服务器算法:LRU 等替换)。如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。
    <bytes>需要存储的字节数,当用户希望存储空数据时<bytes>可以为0
    <data block>需要存储的内容,输入完成后,最后客户端需要加上\r\n(直接点击Enter)作为结束标志。

    • Memcached数据示例
    set key1 1 50 3 #储存key名为key1,flags为1,过期时间为50s,字节数为3的数据
    cba
    STORED
    get key1        #查看key1
    VALUE key1 1 3
    cba
    END
    add key2 2 0 4  #添加数据
    qwer
    STORED
    get key2        
    VALUE key2 2 4
    qwer
    END
    replace key2 2 0 5   #替换数据
    abcde
    STORED
    get key2       
    VALUE key2 2 5
    abcde
    END
    delete key2   #删除数据
    DELETED
    get key2  
    END
    
    • Memcached数据导出和导入
    [root@minglinux-01 ~] telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    set name 1 0 4
    ming
    STORED
    set age 1 0 2   
    20
    STORED
    ^]  #ctrl+]退出
    telnet> quit
    Connection closed.
    [root@minglinux-01 ~] memstat --servers=127.0.0.1:11211
    Server: 127.0.0.1 (11211)
         pid: 80200
         uptime: 46369
         time: 1548128359
         version: 1.4.15
         libevent: 2.0.21-stable
         pointer_size: 64
         rusage_user: 1.038780
         rusage_system: 1.175762
         curr_connections: 10
         total_connections: 17
         connection_structures: 11
         reserved_fds: 20
         cmd_get: 5
         cmd_set: 6
         cmd_flush: 0
         cmd_touch: 0
         get_hits: 3
         get_misses: 2
         delete_misses: 0
         delete_hits: 1
         incr_misses: 0
         incr_hits: 0
         decr_misses: 0
         decr_hits: 0
         cas_misses: 0
         cas_hits: 0
         cas_badval: 0
         touch_hits: 0
         touch_misses: 0
         auth_cmds: 0
         auth_errors: 0
         bytes_read: 278
         bytes_written: 4315
         limit_maxbytes: 67108864
         accepting_conns: 1
         listen_disabled_num: 0
         threads: 4
         conn_yields: 0
         hash_power_level: 16
         hash_bytes: 524288
         hash_is_expanding: 0
         bytes: 143
         curr_items: 2
         total_items: 5
         expired_unfetched: 0
         evicted_unfetched: 0
         evictions: 0
         reclaimed: 1
    
    [root@minglinux-01 ~] memcached-tool 127.0.0.1:11211 dump 
    Dumping memcache contents
      Number of buckets: 1
      Number of items  : 2
    Dumping bucket 1 - 2 total items
    add name 1 1548081990 4
    ming
    add age 1 1548081990 2
    20
    [root@minglinux-01 ~] memcached-tool 127.0.0.1:11211 dump > data.txt #数据导出
    Dumping memcache contents
      Number of buckets: 1
      Number of items  : 2
    Dumping bucket 1 - 2 total items
    [root@minglinux-01 ~] cat data.txt 
    add name 1 1548081990 4
    ming
    add age 1 1548081990 2
    20
    [root@minglinux-01 ~] systemctl restart memcached #memcached存储在内存,重启服务清除数据
    [root@minglinux-01 ~] nc 127.0.0.1 11211 < data.txt 
    STORED
    STORED
    #注意:导出的数据是带有一个时间戳的,这个时间戳就是该条数据过期的时间点,
    #如果当前时间已经超过该时间戳,那么是导入不进去的
    [root@minglinux-01 ~] telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    get name
    END
    get age  
    END
    ^]
    telnet> quit
    Connection closed.
    [root@minglinux-01 ~] date
    2019年 02月 15日 星期五 21:39:50 CST
    [root@minglinux-01 ~] date -d @1548081990
    2019年 01月 21日 星期一 22:46:30 CST  #该时间戳已经过期,数据导入不成功
    [root@minglinux-01 ~] date -d "+1 hour" +%s
    1550241775
    [root@minglinux-01 ~] date -d @1550241775
    2019年 02月 15日 星期五 22:42:55 CST
    [root@minglinux-01 ~] cat data.txt  #修改data.txt文件里的时间戳如下
    add name 1 1550241775 4
    ming
    add age 1 1000 2
    20
    [root@minglinux-01 ~] systemctl restart memcached.service 
    [root@minglinux-01 ~] nc 127.0.0.1 11211 < data.txt 
    STORED
    STORED
    [root@minglinux-01 ~] telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    get name
    VALUE name 1 4
    ming
    END
    get age  #age数据也导入成功
    VALUE age 1 2
    20
    END
    
    • PHP连接Memcached
    #安装php的memcache扩展
    [root@minglinux-01 /usr/local/sbin] wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz 
    [root@minglinux-01 /usr/local/sbin] tar -zxvf memcache-2.2.3.tgz 
    [root@minglinux-01 /usr/local/sbin] cd memcache-2.2.3/
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] ls
    config9.m4   memcache.c                  memcache_session.c
    config.m4    memcache_consistent_hash.c  memcache_standard_hash.c
    config.w32   memcache.dsp                php_memcache.h
    CREDITS      memcache_queue.c            README
    example.php  memcache_queue.h
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] /usr/local/php-fpm/bin/phpize #生成configure文件
    Configuring for:
    PHP Api Version:         20131106
    Zend Module Api No:      20131226
    Zend Extension Api No:   220131226
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] ls
    acinclude.m4    config.sub    Makefile.global             missing
    aclocal.m4      configure     memcache.c                  mkinstalldirs
    autom4te.cache  configure.in  memcache_consistent_hash.c  php_memcache.h
    build           config.w32    memcache.dsp                README
    config9.m4      CREDITS       memcache_queue.c            run-tests.php
    config.guess    example.php   memcache_queue.h
    config.h.in     install-sh    memcache_session.c
    config.m4       ltmain.sh     memcache_standard_hash.c
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] ./configure --with-php-config=/usr/local/php-fpm/bin/php-config #安装
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] echo $?
    0
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] make && make install
    #安装完成有如下提示,表示memcache扩展模块的存放路径
    #Installing shared extensions:  /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
    memcache.so  opcache.a  opcache.so
    
    #然后修改php.ini添加一行extension="memcache.so"
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] vim /usr/local/php-fpm/etc/php.ini 
    
    #重启php-fpm服务
    #查看memcache模块是否成功加载
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] /usr/local/php-fpm/sbin/php-fpm -m | grep memcache
    memcache
    
    #创建一个测试脚本
    #脚本内容参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/1.php
    
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] vim 1.php 
    #内容如下:
      1 <?php
      2 //连接Memcache Memcache
      3 $mem = new Memcache;
      4 $mem->connect("localhost", 11211);
      5 //保存数据
      6 $mem->set('key1', 'This is first value', 0, 60);
      7 $val = $mem->get('key1');
      8 echo "Get key1 value: " . $val ."<br>";
      9 //替换数据
     10 $mem->replace('key1', 'This is replace value', 0, 60);
     11 $val = $mem->get('key1');
     12 echo "Get key1 value: " . $val . "<br>";
     13 //保存数组数据
     14 $arr = array('aaa', 'bbb', 'ccc', 'ddd');
     15 $mem->set('key2', $arr, 0, 60);
     16 $val2 = $mem->get('key2');
     17 echo "Get key2 value: ";
     18 print_r($val2);
     19 echo "<br>";
     20 //删除数据
     21 $mem->delete('key1');
     22 $val = $mem->get('key1');
     23 echo "Get key1 value: " . $val . "<br>";
     24 //清除所有数据
     25 $mem->flush();
     26 $val2 = $mem->get('key2');
     27 echo "Get key2 value: ";
     28 print_r($val2);
     29 echo "<br>";
     30 //关闭连接
     31 $mem->close();
     32 ?>
    
    #执行脚本
    [root@minglinux-01 /usr/local/sbin/memcache-2.2.3] /usr/local/php-fpm/bin/php 1.php 
    Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array
    (
        [0] => aaa
        [1] => bbb
        [2] => ccc
        [3] => ddd
    )
    <br>Get key1 value: <br>Get key2 value: <br>
    #或者将1.php放到某个虚拟主机根目录下面,在浏览器访问,即可看到效果
    
    
    • Memcached中存储session

    #实例是在lamp/lnmp环境下实现

    修改配置

    1. 编辑php.ini添加两行
      session.save_handler = memcache
      session.save_path = "tcp://127.0.0.1:11211"
    2. apache在httpd.conf中对应的虚拟主机中添加
      php_value session.save_handler "memcache"
      php_value session.save_path "tcp://127.0.0.1:11211"
    3. nginx在php-fpm.conf对应的pool中添加(这里使用3)
      php_value[session.save_handler] = memcache
      php_value[session.save_path] = " tcp://127.0.0.1:11211 "

    测试

    #下载测试代码
    [root@minglinux-01 ~] wget http://study.lishiming.net/.mem_se.txt
    
    #session.php内容可以参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php
    [root@minglinux-01 /data/wwwroot/default] mv /root/.mem_se.txt 1.php #将测试网页放到站点根目录
    [root@minglinux-01 /data/wwwroot/default] curl localhost/1.php
    1550302840<br><br>1550302840<br><br>q6e9f7hck0jdnpv33k1cmlpak6 
    [root@minglinux-01 /data/wwwroot/default] ls -lt /tmp/
    总用量 4
    -rw------- 1 php-fpm php-fpm 37 2月  16 15:35 sess_lg5nghee3afa3jr68u1icctsd1
    -rw-r--r-- 1 root    root     0 2月  16 15:33 test.com.log
    drwx------ 2 root    root    40 2月  14 21:58 vmware-root
    srwxrwxrwx 1 mysql   mysql    0 12月 24 22:45 mysql.sock
    srw-rw-rw- 1 root    root     0 12月 24 22:45 ming.sock
    srw-rw-rw- 1 root    root     0 12月 24 22:45 php-fcgi.sock
    [root@minglinux-01 /data/wwwroot/default] rm -rf /tmp/sess_lg5nghee3afa3jr68u1icctsd1 
    [root@minglinux-01 /data/wwwroot/default] ls -lt /tmp/
    总用量 0
    -rw-r--r-- 1 root  root   0 2月  16 15:33 test.com.log
    drwx------ 2 root  root  40 2月  14 21:58 vmware-root
    srwxrwxrwx 1 mysql mysql  0 12月 24 22:45 mysql.sock
    srw-rw-rw- 1 root  root   0 12月 24 22:45 ming.sock
    srw-rw-rw- 1 root  root   0 12月 24 22:45 php-fcgi.sock
    
    #在php-fpm.conf对应的pool中添加两行
    [root@minglinux-01 /data/wwwroot/default] vim /usr/local/php-fpm/etc/php-fpm.d/ming.conf 
    
      1 [ming]
      4 user = php-fpm
      5 group = php-fpm
      6 pm = dynamic
      7 pm.max_children = 50
      8 pm.start_servers = 20
      9 pm.min_spare_servers = 5
     10 pm.max_spare_servers = 35
     11 pm.max_requests = 500
     12 rlimit_files = 1024
     13 php_value[session.save_handler] = memcache
     14 php_value[session.save_path] = " tcp://127.0.0.1:11211 "
    
    [root@minglinux-01 /data/wwwroot/default] /etc/init.d/php-fpm reload
    Reload service php-fpm  done
    [root@minglinux-01 /data/wwwroot/default] curl localhost/1.php
    1550325224<br><br>1550325224<br><br>5nt1uusu6871nid87o5b54qs26 
    [root@minglinux-01 /data/wwwroot/default] telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    get 5nt1uusu6871nid87o5b54qs26
    VALUE 5nt1uusu6871nid87o5b54qs26 0 37
    TEST|i:1550325224;TEST3|i:1550325224;
    END
    
    

    相关文章

      网友评论

          本文标题:NoSQL一--Memcached

          本文链接:https://www.haomeiwen.com/subject/cpwvdqtx.html