Memcached 配置

Memcached简介

memcached是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但被许多网站使用。

memcached 是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态 Web 应用的速度、提高可扩展性

memcached数据流

image-20230313191754941

Slab是由多个Page组成的,Page按照指定大小切割成多个chunk

image-20230313191842011

Mencached安装

yum install -y memcached libmemcached libevent
#memcached服务开启
systemctl start memcached 
#状态查看
方式一:memcached-tool 127.0.0.1:11211  stats 
方式二:yum -y  install nc
	   echo stats |nc 127.0.0.1 11211 
stats slabs  #slabs

Memcached命令行

yum install -y telnet
telnet 127.0.0.1 11211

Memcached语法格式

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

Memcached导入和导出

#memcached导出数据
memcached-tool 127.0.0.1:11211 dump > data.txt
#memcache 导入数据
nc 127.0.0.1 11211 < data.txt
如果导入失败,将memcached重新启动即可导入成功
systemctl  restart  memcached
nc 127.0.0.1 11211 < data.txt

php5.4版本安装

目前认证:只有php的5.4版本可以使用

php使用yum安装

yum install gcc gcc-c++ prce-devel expat-devel
yum -y install epel-release yum-utils
yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php80 
yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis

php使用编译安装

yum install -y perl*  --skip-broken  httpd-devel  openssl openssl-devel  bzip2 bzip2-devel  libpng libpng-devel   freetype freetype-devel  epel-release  libmcrypt-devel  libxml2-devel  libtool 
gcc make automake autoconf libtool bison flex

具体安装配置

wget https://www.php.net/distributions/php-5.4.45.tar.gz
 cd /usr/local/src
tar -zxvf php-5.4.45.tar.gz
#安装必要的安装包
yum install -y openssl openssl-devel
yum install -y bzip2 bzip2-devel
yum install -y libpng libpng-devel
yum install -y freetype freetype-devel
yum install -y epel-release
yum install -y libmcrypt-devel
yum install -y libxml2-devel
cd  php-5.6.30
#进行编译安装
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-libxml-dir--with-gd --with-jpeg-dir --with-png-dir--with-freetype-dir --with-iconv-dir--with-zlib-dir --with-bz2 --with-openssl--with-mcrypt --enable-soap--enable-gd-native-ttf  --enable-mbstring--enable-sockets --enable-exif
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+
#出现此界面就可以直接编译安装了
make  && make  install

PHP连接Mencached

安装php的memcache扩展

cd /usr/local/src/
wget  https://pecl.php.net/get/memcache-2.2.3.tgz 
tar zxf memcache-2.2.3.tgz
cd memcache-2.2.3
#运行phpize文件
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config
make && make install	

文件编辑,数据写入

#编辑文件
vim  /etc/php.ini
#插入数据
extension="memcache.so"
#对插入的数据进行查询,红色返回结果
/usr/bin/php  -m | grep  memcache
memcache  

#新建一个php文件
cat 1.php
test success
#使用命令进行运行
 /usr/bin/php  1.php  start
test success

Memcached中存储session

php.ini 里面的 session.save_handler配置参数来让 session_set_save_handler() 正常工作.

来存储和获取与会话关联的数据的处理器的名字,默认是files

#向php.ini中插入数据
vim  php.ini
session.save_handler = memcache 
session.save_path = "tcp://127.0.0.1:11211"  
#或者http.conf
php_value session.save_handler "memcache" 
php_value session.save_path "tcp://127.0.0.1:11211"  
#或者php-fpm.conf对应的pool中添加
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://127.0.0.1:11211"

#php.ini配置文件,为了防止下面telnet 验证出错进行注释
;session.save_handler = files
session.save_handler = "memcache"
session.save_path = "tcp://127.0.0.1:11211"

session.php

<?php
    session_start();
    $_SESSION['name']='test';    
    echo session_id()."<br/>";        
    echo $_SESSION['name'];
?>

测试配置

#获取session值
/usr/bin/php  session.php  start
4bit5200ot06m8d83se4fmu2a4<br/>test
#telnet测试
[root@slave htdocs]# telnet  127.0.0.1  11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get shqov9bq80vu3m3er5dg6v00a6
VALUE shqov9bq80vu3m3er5dg6v00a6 0 16
name|s:4:"test";
END

猜你喜欢

转载自blog.csdn.net/m0_64118193/article/details/129655800
今日推荐