LAMP架构中Memcached缓存应用

栏目: 数据库 · 发布时间: 5年前

内容简介:Memcached是一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,进而提升系统性能。Memcache多数情况是作为数据库的前端Cache来使用,因为它比数据库少了很多sql解析、磁盘操作等开销,而且使用内存来管理数据,所以它可以提供比直接读取数据库更好的性能。缓存一般用来保存一些经常存取的对象或数据(例如,浏览器会把经常访问的网页缓存起来),通过缓存来存取对象或数据要比磁盘快很多。Memcache是一种内存缓存,把经常存取的对象或数据存在内存中,内存中缓存的数据通过API的方式被存取,数据就

什么是Memcached ?

Memcached是一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,进而提升系统性能。Memcache多数情况是作为数据库的前端Cache来使用,因为它比数据库少了很多 sql 解析、磁盘操作等开销,而且使用内存来管理数据,所以它可以提供比直接读取数据库更好的性能。

缓存一般用来保存一些经常存取的对象或数据(例如,浏览器会把经常访问的网页缓存起来),通过缓存来存取对象或数据要比磁盘快很多。Memcache是一种内存缓存,把经常存取的对象或数据存在内存中,内存中缓存的数据通过API的方式被存取,数据就像一张大的HASH表,以key-value对的方式存在。

Memcache和数据库协作的流程逻辑如下:

  1. 检查客户端请求的数据是否在Memcache中存在,如果在,直接把请求的数据返回,不再对数据进行任何操作;
  2. 如果请求的数据不在Memcache中,就在查询数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到Memcache中;
  3. 每次更新数据库(如更新、删除数据库的数据)的同时更新Memcache中的数据,保证Memcache中的数据和数据库中的数据一致。
  4. 当分配给Memcache内存空间用完之后,会使用LRU(最近最少使用)策略加到期失效策略,失效的数据首先被替换掉,然后再替换掉最近未使用的数据。

Memcached的特征:

  • 协议简单(基于文本行协议,可直接通过telnet在服务器上存取数据)
  • 基于libevent的事件管理
  • 内置的内存管理方式
  • 互不通信的 Memcached 之间具有分布特征

网站架构图

LAMP架构中Memcached缓存应用

系统环境:

主机名 操作系统 IP地址 服务名
memcached centos7.4 192.168.96.22 memcached-1.5.9.tar.gz、libevent-2.0.21-stable.tar.gz
web1 centos7.4 192.168.96.23 LAMP、memcache-2.2.7.tgz
客户端 windows 10 192.168.96.2 网页浏览器

关闭防火墙及Selinux

systemctl stop firewalld
setenforce 0

相关软件包 百度云盘 密码:j2v3

开始部署

一、Memcache服务器

1.安装libevent软件

#解压
tar -zxvf libevent-2.0.21-stable.tar.gz -C /opt

#进入目录
cd /opt/libevent-2.0.21-stable/

#配置
./configure

#编译及安装
make && make install

2.安装memcached软件

#解压
tar zxvf memcached-1.5.9.tar.gz -C /opt

#进入目录
cd /opt/memcached-1.5.9/

#配置
./configure

#编译及安装
make && make install

3. 启动memcached服务

memcached -m 32m -p 11211 -d -u root -P /var/run/memcached.pid -c256

以上选项说明如下:

-p:使用的tcp端口,默认为11211

-m:最大内存大小,默认为64M

-vv:以very vrebose模式启动,将调试信息和错误输出到控制台

-d:作为守护进程的后台运行

-c:最大运行的并发连接数,默认是1024,一般按照服务器的负载量来设置

-P:设置保存Memcached的pid文件

-l:监听的服务器IP地址,若有多个地址

-u:运行Memcached的用户,默认不能用root启动,若使用需要-u来指定root用户

4.查看memcached服务信息

netstat -tunlp | grep memcached

5.连接登录

telnet 192.168.96.22 11211

五种基本操作命令

1. set
    2. add
    3. replace
    4. get
    5. delete

键值对语法如下:

command <key> <flags> <expiration time> <bytes> <value>

memcached修改命令参数

参数 用法
key key用于查找缓存值
flags 可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息
expiration time 在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)
bytes 在缓存中存储的字节点
value 存储的值(始终位于第二行)

memcached基本操作命令

1.set
set命令用于向缓存添加新的键值对,如果已经存在,则之前的值将被替换,响应STORED

2.add
当缓存中不存在键时,add命令才会向缓存中添加一个键值对,如果缓存中已经存在该键,则之前的值将仍将保持不变,并返回响应NOT_STORED

3.append username 0 0 4    //键值后追加4个字节

4.prepend username 0 0 2   //键值前追加2个字节

5.replace
仅当键已经存在时,replace命令才会替换缓存中的键,如果缓存中不存在该键,则返回响应NOT_STORED

6.get
用于检索与之前添加的键值对相关的值

7.delete
用于删除memcached中的任何现有值,将使用一个键调用delete,如果该键存在于缓存中,则删除该值。如果不存在,则返回一条NOT_FOUND消息。

8.stats
转储所连接的 memcached 实例的当前统计数据。

9.flush_all
仅用于清理缓存中的所有名称/值对。如果需要将缓存重置到干净的状态,则 flush_all 能提供很大的用处。

10.quit //退出

二、应用程序Memcache API(PHP服务器)

这里选择通过yum来快速搭建LAMP服务器

1.安装apache

yum install httpd httpd-devel -y

2.设置httpd服务开机启动

systemctl enable httpd

3.启动httpd服务

systemctl start httpd

4.查看端口监听情况

netstat -tunlp | grep httpd

LAMP架构中Memcached缓存应用

5.客户端访问测试

LAMP架构中Memcached缓存应用

6.安装 mysql 数据库

yum install mariadb mariadb-server mariadb-libs mariadb-devel -y

7.查看软件包情况

rpm -qa | grep mariadb

LAMP架构中Memcached缓存应用

8.设置开机自启动

systemctl enable mariadb

9.启动mysql服务

systemctl start mariadb

10.查看端口监听情况

netstat -tunlp | grep mysql

LAMP架构中Memcached缓存应用

11.数据库安全设置

mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):           #直接“回车”

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] y        #输入“y”
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y        #输入“y”
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y        #输入“y”
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y        #输入“y”
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y        #输入“y”
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

12.登录mysql数据库测试

mysql -u root -p

LAMP架构中Memcached缓存应用

13.安装php

yum -y install php php-devel

14.查看已安装 php 相关软件包

rpm -ql php

LAMP架构中Memcached缓存应用

15.将php和mysql作关联

yum install php-mysql

16.查看

rpm -ql php-mysql

LAMP架构中Memcached缓存应用

17.安装常用的php模块

yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath

18.创建php测试页面

cd /var/www/html vim info.php
<?php
    phpinfo();
?>

19.重启httpd服务

systemctl restart httpd

20.客户端测试php

客户端访问http://192.168.96.22/info.php

LAMP架构中Memcached缓存应用

21.客户端安装Memcache的PHP扩展功能

#安装autoconf软件包
yum install autoconf -y

#解压
tar xf memcache-2.2.7.tgz -C /opt/

#进入目录
cd /opt/memcache-2.2.7

#增加为PHP的模块后再对memcache进行配置编译
/usr/bin/phpize

![](http://i2.51cto.com/images/blog/201807/25/7f0ba593f9d05d86f8e0f75065739d88.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

#配置
./configure \
--enable-memcache \
--with-php-config=/usr/bin/php-config

#编译及安装
make && make install

LAMP架构中Memcached缓存应用

22.编辑php.ini

vim /etc/php.ini
#732行,新增以下命令
extension_dir = "/usr/lib64/php/modules/"

#864行,新增以下命令
extension = memcache.so

23.编写测试页面,测试memcached工作是否正常

vim /var/www/html/index.php 
<?php
$memcache = new Memcache();
$memcache->connect('192.168.96.22',11211);
$memcache->set('key','Memcache test Successfull!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>

24.重启httpd服务

service httpd restart

25.客户端进行访问测试是否成功 , http://192.168.96.22/index.php

LAMP架构中Memcached缓存应用


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volumes 1-3 Boxed Set

The Art of Computer Programming, Volumes 1-3 Boxed Set

Donald E. Knuth / Addison-Wesley Professional / 1998-10-15 / USD 199.99

This multivolume work is widely recognized as the definitive description of classical computer science. The first three volumes have for decades been an invaluable resource in programming theory and p......一起来看看 《The Art of Computer Programming, Volumes 1-3 Boxed Set》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具