歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> redhat as 6.2下安裝memcached

redhat as 6.2下安裝memcached

日期:2017/3/3 16:28:00   编辑:關於Linux

Memcached是什麼?

emcached 是一個高性能的分布式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提供動態、數據庫驅動網站的速度。Memcached基於一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,並通過memcached協議與守護進程通信。但是它並不提供冗余(例如,復制其hashmap條目);當某個服務器S停止運行或崩潰了,所有存放在S上的鍵/值對都將丟失。

Memcached由Danga Interactive開發,其最新版本發布於2010年,作者為Anatoly Vorobey和Brad Fitzpatrick。用於提升LiveJournal . com訪問速度的。LJ每秒動態頁面訪問量幾千次,用戶700萬。Memcached將數據庫負載大幅度降低,更好的分配資源,更快速訪問。

如何使用memcached-Server端?

在服務端運行:

# ./memcached -d -m 2048 -l 10.0.0.40 -p 11211  這將會啟動一個占用2G內存的進程,並打開11211端口用於接收請求。由於32位系統只能處理4G內存的尋址,所以在大於4G內存使用PAE的32位服務器上可以運行2-3個進程,並在不同端口進行監聽。

如何使用memcached-Client端?

在應用端包含一個用於描述Client的Class後,就可以直接使用,非常簡單。

PHP Example:   $options["servers"] = array("192.168.1.41:11211", "192.168.1.42:11212");   $options["debug"] = false;   $memc = new MemCachedClient($options);   $myarr = array("one","two", 3);   $memc->set("key_one", $myarr);   $val = $memc->get("key_one");   print $val[0]."\n"; // prints 'one‘   print $val[1]."\n"; // prints 'two‘   print $val[2]."\n"; // prints 3

一個用PHP編寫的可視化的MemCached管理系統

MemAdmin是一款可視化的Memcached管理與監控工具,使用PHP開發,體積小,操作簡單。

主要功能:服務器參數監控:STATS、SETTINGS、ITEMS、SLABS、SIZES實時刷新   服務器性能監控:GET、DELETE、INCR、DECR、CAS等常用操作命中率實時監控   支持數據遍歷,方便對存儲內容進行監視

支持條件查詢,篩選出滿足條件的KEY或VALUE   數組、JSON等序列化字符反序列顯示   兼容memcache協議的其他服務,如Tokyo Tyrant (遍歷功能除外)   支持服務器連接池,多服務器管理切換方便簡潔

http://pecl.php.net/package/memcache

1.[root@localhost html]# cat /etc/yum.repos.d/ 本地源倉庫

backup/ redhat.repo test.repo

[root@localhost html]# cat /etc/yum.repos.d/test.repo

[test]

name=test

baseurl=file:///mnt

enable=1

gpgcheck=0

gpgfile=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

2.[root@localhost memcache-3.0.6]# mount /dev/cdrom //掛載iso9660光盤鏡像

mount: block device /dev/sr0 is write-protected, mounting read-only

3.[root@localhost memcache-3.0.6]# ls /mnt /查看/mnt目錄是否掛載

EULA RELEASE-NOTES-es-ES.html RELEASE-NOTES-ru-RU.html

GPL RELEASE-NOTES-fr-FR.html RELEASE-NOTES-si-LK.html

HighAvailability RELEASE-NOTES-gu-IN.html RELEASE-NOTES-ta-IN.html

images RELEASE-NOTES-hi-IN.html RELEASE-NOTES-te-IN.html

isolinux RELEASE-NOTES-it-IT.html RELEASE-NOTES-zh-CN.html

LoadBalancer RELEASE-NOTES-ja-JP.html RELEASE-NOTES-zh-TW.html

media.repo RELEASE-NOTES-kn-IN.html repodata

Packages RELEASE-NOTES-ko-KR.html ResilientStorage

README RELEASE-NOTES-ml-IN.html RPM-GPG-KEY-redhat-beta

RELEASE-NOTES-as-IN.html RELEASE-NOTES-mr-IN.html RPM-GPG-KEY-redhat-release

RELEASE-NOTES-bn-IN.html RELEASE-NOTES-or-IN.html Server

RELEASE-NOTES-de-DE.html RELEASE-NOTES-pa-IN.html TRANS.TBL

RELEASE-NOTES-en-US.html RELEASE-NOTES-pt-BR.html

4.[root@localhost memcache-3.0.6]# rpm -q libevent//libevent是一個事件觸發的網絡庫,適用於windows、linux、bsd等多種平台,內部使用select、epoll、kqueue等系統調用管理事件機制。著名分布式緩存軟件memcached也是libevent based,而且libevent在使用上可以做到跨平台,而且根據libevent官方網站上公布的數據統計,似乎也有著非凡的性能。

package libevent is not installed

5.[root@localhost memcache-3.0.6]# yum -y install libevent//安裝libevent包

Loaded plugins: product-id, subscription-manager

Updating certificate-based repositories.

test | 4.0 kB 00:00 ...

Setting up Install Process

Resolving Dependencies

--> Running transaction check

---> Package libevent.i686 0:1.4.13-1.el6 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

================================================================================

Package Arch Version Repository Size

================================================================================

Installing:

libevent i686 1.4.13-1.el6 test//test本地源 67 k

Transaction Summary

================================================================================

Install 1 Package(s)

Total download size: 67 k

Installed size: 226 k

Downloading Packages:

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Warning: RPMDB altered outside of yum.

** Found 1 pre-existing rpmdb problem(s), 'yum check' output follows:

perl-Proc-Daemon-0.14-1.el6.remi.noarch has missing requires of perl(Proc::ProcessTable)

Installing : libevent-1.4.13-1.el6.i686 1/1

Installed products updated.

Installed:

libevent.i686 0:1.4.13-1.el6

Complete!//安裝成功!

6.[root@localhost memcache-3.0.6]# ls -al /usr/lib|grep libevent//查看libevent依賴庫

lrwxrwxrwx 1 root root 21 Apr 20 23:24 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3

-rwxr-xr-x 1 root root 108948 Dec 16 2009 libevent-1.4.so.2.1.3

lrwxrwxrwx 1 root root 26 Apr 20 23:24 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3

-rwxr-xr-x 1 root root 37004 Dec 16 2009 libevent_core-1.4.so.2.1.3

lrwxrwxrwx 1 root root 27 Apr 20 23:24 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3

-rwxr-xr-x 1 root root 84228 Dec 16 2009 libevent_extra-1.4.so.2.1.3

7.[root@localhost html]# yum list|grep memcache//查看memcache軟件包

libmemcached.i686 0.31-1.1.el6 @test

memcached.i686 1.4.4-3.el6 @test

php-pecl-memcache.i686 3.0.5-3.el6 @test

python-memcached.noarch 1.43-5.3.el6 @test

8.[root@localhost html]# rpm -q libmemcache//查看是否安裝如果沒安裝直接yum -y libmemcached memcached php-pecl-memcache python-memcached

package libmemcache is not installed

[root@localhost html]# rpm -q libmemcached

libmemcached-0.31-1.1.el6.i686

[root@localhost html]# rpm -q memcached//已經安裝

memcached-1.4.4-3.el6.i686

[root@localhost html]# chkconfig --list memcached//查看啟動級別

memcached 0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@localhost html]# chkconfig memcached on//設置runlevel 3啟動

[root@localhost html]# service memcached start//啟動服務memcached服務

Starting memcached: [ OK ]

9.[root@localhost html]# netstat -ntlp//查看端口

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 1899/memcached

tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 1201/vsftpd

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1190/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1400/master

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1308/mysqld

tcp 0 0 :::11211 :::* LISTEN 1899/memcached //已經啟動memcached服務11211端口

tcp 0 0 :::139 :::* LISTEN 1426/smbd

tcp 0 0 :::80 :::* LISTEN 1410/httpd

tcp 0 0 :::22 :::* LISTEN 1190/sshd

tcp 0 0 ::1:25 :::* LISTEN 1400/master

tcp 0 0 :::445 :::* LISTEN 1426/smbd

10.測試

[root@localhost html]#memcached -d -m 15360 -u root -l 192.168.40.40 -p 11211 -c 40960//這裡設置的11211端口默認連接數是40960 可以直接echo "xxx">>/etc/rc.local中去

[root@localhost html]# telnet 192.168.40.40 11211 //登錄memcached

Trying 192.168.40.40...

Connected to 192.168.40.40.

Escape character is '^]'.

stats

STAT pid 1899

STAT uptime 1589

STAT time 1334940144

STAT version 1.4.4

STAT pointer_size 32

STAT rusage_user 0.000999

STAT rusage_system 0.000000

STAT curr_connections 10

STAT total_connections 12

STAT connection_structures 11

STAT cmd_get 0

STAT cmd_set 0

STAT cmd_flush 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 auth_cmds 0

STAT auth_errors 0

STAT bytes_read 70

STAT bytes_written 797

STAT limit_maxbytes 67108864

STAT accepting_conns 1

STAT listen_disabled_num 0

STAT threads 4

STAT conn_yields 0

STAT bytes 0

STAT curr_items 0

STAT total_items 0

STAT evictions 0

END

在網絡上查到資料, Memcache也有自己的查看工具:

如何查看memcache服務器端版本: ./memcached -h

memcache的運行狀態可以方便的用 stats 命令顯示。

首先用telnet 127.0.0.1 11211這樣的命令連接上memcache,然後直接輸入stats就可以得到當前memcache的狀態。

這些狀態的說明如下:

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數(分配給memcache的空間用滿後需要刪除舊的items來得到空間分配給新的items)

bytes_read 總讀取字節數(請求字節數)

bytes_written 總發送字節數(結果字節數)

limit_maxbytes 分配給memcache的內存大小(字節)

threads 當前線程數

11.管理工具memadmin

MemAdmin是一款可視化的Memcached管理與監控工具,基於 PHP5 & JQuery 開發,體積小,操作簡單。

主要功能:

•服務器參數監控:STATS、SETTINGS、ITEMS、SLABS、SIZES實時刷新

•服務器性能監控:GET、DELETE、INCR、DECR、CAS等常用操作命中率實時監控

•支持數據遍歷,方便對存儲內容進行監視

•支持條件查詢,篩選出滿足條件的KEY或VALUE

•數組、JSON等序列化字符反序列顯示

•兼容memcache協議的其他服務,如Tokyo Tyrant (遍歷功能除外)

•支持服務器連接池,多服務器管理切換方便簡潔

項地址:http://www.junopen.com/memadmin/

演示:http://demo.junopen.com/memadmin_demo/index.php

memcached是一套分布式的快取系統,當初是Danga Interactive為了LiveJournal所發展的,但目前被許多軟件(如MediaWiki)所使用。這是一套開放源代碼軟件,以BSD license授權釋出。

memcached缺乏認證以及安全管制,這代表應該將memcached服務器放置在防火牆後。

memcached的API使用三十二位元的循環冗余校驗(CRC-32)計算鍵值後,將資料分散在不同的機器上。當表格滿了以後,接下來新增的資料會以LRU機制替換掉。由於memcached通常只是當作快取系統使用,所以使用memcached的應用程式在寫回較慢的系統時(像是後端的數據庫)需要額外的程式碼更新memcached內的資料。

memcached具有多種語言的客戶端開發包,包括:Perl/PHP/JAVA/C/Python/Ruby/C#/MySQL/

把memadmin-1.0.9 用ssh上傳到網站根目錄然後查看config.php默認為admin,admin

本文出自 “有志者事竟成!” 博客,請務必保留此出處http://zh888.blog.51cto.com/1684752/839824

Copyright © Linux教程網 All Rights Reserved