1. 程式人生 > >LAMP架構中Memcached緩存應用

LAMP架構中Memcached緩存應用

包括 dev 負載 man username 整型 upd 51cto cal

什麽是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之間具有分布特征

網站架構圖

技術分享圖片

系統環境:

主機名 操作系統 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

技術分享圖片

5.客戶端訪問測試

技術分享圖片

6.安裝mysql數據庫

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

7.查看軟件包情況

rpm -qa | grep mariadb

技術分享圖片

8.設置開機自啟動

systemctl enable mariadb

9.啟動mysql服務

systemctl start mariadb

10.查看端口監聽情況

netstat -tunlp | grep mysql

技術分享圖片

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

技術分享圖片

13.安裝php

yum -y install php php-devel

14.查看已安裝php相關軟件包

rpm -ql php

技術分享圖片

15.將php和mysql作關聯

yum install php-mysql

16.查看

rpm -ql php-mysql

技術分享圖片

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

技術分享圖片

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

技術分享圖片

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緩存應用