memcache常被用來加速應用程式的處理 關於memcache就不用多說了,就是a distributed memory object caching system,既然是一個用來存東西的系統,那麼一定要有個存放的地方吧,我們就叫它伺服器端吧,然後誰把東西存放在上面就叫它用戶端吧,那怎麼放呢,肯定是 用戶端-- 串連伺服器端-- 把東西發送給伺服器端-- 實現了東西的存放麼,要去取的時候也是一樣的,先串連,在取東西回來了。
安裝之前採用yum 方式安裝php 和httpd 並注意下面採用yum 方式安裝php 應注意的問題 yum install php httpd
之前沒用過 前一段時間公司需要用到memcache 在網上看了一下資料 自己就安裝了一下 遇到的問題 也有記錄 有不妥或者未詳盡之處 望 大家指點
1)memcached用到了libevent這個庫,用於Socket的處理
1. wget http://www.monkey.org/~provos/libevent-2.0.10-stable.tar.gz
2. tar xzvf libevent-2.0.10-stable.tar.gz
3. cd libevent-2.0.10-stable
4. ./configure --prefix=/usr
5. make && make install
驗證libevent是否正確安裝
1. ls -al /usr/lib |grep libevent
1. [root@test /]# ls -al /usr/lib |grep libevent
2. lrwxrwxrwx 1 root root 21 Feb 28 19:27 libevent-2.0.so.5 -> libevent-2.0.so.5.0.1
3. -rwxr-xr-x 1 root root 943520 Feb 28 19:27 libevent-2.0.so.5.0.1
4. -rw-r--r-- 1 root root 1532202 Feb 28 19:27 libevent.a
5. lrwxrwxrwx 1 root root 26 Feb 28 19:27 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.0.1
6. -rwxr-xr-x 1 root root 554374 Feb 28 19:27 libevent_core-2.0.so.5.0.1
7. -rw-r--r-- 1 root root 928010 Feb 28 19:27 libevent_core.a
8. -rwxr-xr-x 1 root root 971 Feb 28 19:27 libevent_core.la
9. lrwxrwxrwx 1 root root 26 Feb 28 19:27 libevent_core.so -> libevent_core-2.0.so.5.0.1
10. lrwxrwxrwx 1 root root 27 Feb 28 19:27 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.0.1
11. -rwxr-xr-x 1 root root 410763 Feb 28 19:27 libevent_extra-2.0.so.5.0.1
12. -rw-r--r-- 1 root root 604266 Feb 28 19:27 libevent_extra.a
13. -rwxr-xr-x 1 root root 978 Feb 28 19:27 libevent_extra.la
14. lrwxrwxrwx 1 root root 27 Feb 28 19:27 libevent_extra.so -> libevent_extra-2.0.so.5.0.1
15. -rwxr-xr-x 1 root root 936 Feb 28 19:27 libevent.la
16. lrwxrwxrwx 1 root root 29 Feb 28 19:27 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.0.1
17. -rwxr-xr-x 1 root root 85731 Feb 28 19:27 libevent_openssl-2.0.so.5.0.1
18. -rw-r--r-- 1 root root 120996 Feb 28 19:27 libevent_openssl.a
19. -rwxr-xr-x 1 root root 1007 Feb 28 19:27 libevent_openssl.la
20. lrwxrwxrwx 1 root root 29 Feb 28 19:27 libevent_openssl.so -> libevent_openssl-2.0.so.5.0.1
21. lrwxrwxrwx 1 root root 30 Feb 28 19:27 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.0.1
22. -rwxr-xr-x 1 root root 18094 Feb 28 19:27 libevent_pthreads-2.0.so.5.0.1
23. -rw-r--r-- 1 root root 18678 Feb 28 19:27 libevent_pthreads.a
24. -rwxr-xr-x 1 root root 999 Feb 28 19:27 libevent_pthreads.la
25. lrwxrwxrwx 1 root root 30 Feb 28 19:27 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.0.1
26. lrwxrwxrwx 1 root root 21 Feb 28 19:27 libevent.so -> libevent-2.0.so.5.0.1
2)安裝memcached
1. wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
2. tar xzvf memcached-1.4.5.tar.gz
3. cd memcached-1.4.5
4. ./configure -–with-libevent=/usr
5. make && make install
安裝完成後,程式會將memcached 自動放到/usr/local/bin/memcached
1. [root@test /]# which memcached
2. /usr/local/bin/memcached
3)安裝PHP的memcache擴充
memcache 的項目
1. http://pecl.php.net/package/memcache
安裝步驟
1. wget http://pecl.php.net/get/memcache-3.0.5.tgz
2. tar xf memcache-3.0.5.tgz
3. cd memcache-3.0.5
4. usr/local/php/bin/phpize (產生編譯環境./configure)
5. 這一步如果使用yum 安裝的php 路徑改為--with-php-config=/usr/bin/php-config
6. ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
7. make && make install
安裝完成後,提示
1. Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
2. Yum 安裝的php 提示如下
3. Installing shared extensions: "/usr/lib64/php/modules"
配置php.ini檔案
修改extension_dir:
1. extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
2. yum 安裝設定檔如下
3. extension_dir = "/usr/lib64/php/modules"
添加一行以便載入memcache擴充:extension=memcache.so
1. extension=memcache.so
驗證PHP中是否成功載入memcache擴充
1. [root@test /]# php -m
2. [PHP Modules]
3. Core
4. ctype
5. curl
6. date
7. dom
8. eAccelerator
9. ereg
10. fileinfo
11. filter
12. hash
13. iconv
14. json
15. libxml
16. mcrypt
17. memcache
18. mysql
19. mysqli
20. pcre
21. PDO
22. pdo_sqlite
23. Phar
24. posix
25. Reflection
26. session
27. SimpleXML
28. SPL
29. SQLite
30. sqlite3
31. standard
32. tokenizer
33. xml
34. xmlreader
35. xmlwriter
36. zlib
37.
38. [Zend Modules]
39. eAccelerator
17行處,有memcahe,正確載入
後續:memcached基本配置
啟動memcached 服務端程式:
1. memcached -d -m 10 -u root -l localhost -p 11211 -c 256 -P /tmp/memcached.pid
2. 如果運行到一步報錯提示Libaraies 沒有 或者不能共用
執行ldconfig (載入動態連結程式庫)
執行上面操作之前 最好執行這一步ldconfig –C | grep libevent 看看有沒有
各個參數的含義:
1. -d選項是啟動一個守護進程,
2. -m是分配給Memcached使用的記憶體數量,單位是MB,
3. -u是運行Memcache的使用者,我這裡是root,
4. -l是監聽的伺服器IP地址,
5. -p是設定Memcache監聽的連接埠,最好是1024以上的連接埠,
6. -c選項是最大啟動並執行並發串連數,預設是1024,按照你伺服器的負載量來設定,
7. -P是設定儲存Memcache的pid檔案
PS:memcached 常用方法:
1. kill `cat /tmp/memcached.pid`
2.
3. 擷取運行狀態
4.
5. echo stats | nc 192.168.0.122 11200
6. watch “echo stats | nc 192.168.0.122 11200 ”(即時狀態)
7.
8. 快速清空
9.
10. echo “flush_all” | nc 192.168.0.122 11200
11.
12. 其他工具