Memcached筆記——(一)安裝&常規錯誤&監控
08年的時候接觸過Memcached,當時還對它的客戶端產品嗤之以鼻,畢竟手工程式碼沒有各種ORM原生XML配置方便。儘管如此,Memcached現在已經成了伺服器架構裡不可或缺的一部分!
相關連結:
Memcached筆記——(一)安裝&常規錯誤&監控
Memcached筆記——(二)XMemcached&Spring整合
Memcached筆記——(三)Memcached使用總結
Memcached筆記——(四)應對高併發攻擊
一、下載
1.Libevent
簡單的說就是一個事件觸發的網路庫,Memcached離不開它。
2.Memcached
今天的主角
二、安裝
1.Libevent
解壓縮
編譯、安裝
這裡一定要注意指定--prefix,後面配置memcached的時候就有必要用到。
2.Memcached
解壓
編譯、安裝
這裡一定要指定libevent的路徑,否則啟動的時候就有找不到libevent的so檔案的錯誤!
啟動
引數
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-l <addr> interface to listen on (default: INADDR_ANY, all addresses)
<addr> may be specified as host:port. If you don't specify
a port number, the value you specified with -p or -U is
used. You may specify multiple addresses separated by comma
or by using -l multiple times
-d run as a daemon
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes (default: 64 MB)
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections (default: 1024)
-v verbose (print errors/warnings while in event loop)
-P <file> save PID in <file>, only used with -d option
要關掉memcached
是否正常?Telnet上去看看
然後輸入
接著就能看到:
STAT pid 3021
STAT uptime 3621
STAT time 1331261509
STAT version 1.4.13
STAT libevent 2.0.17-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.000999
STAT curr_connections 6
STAT total_connections 7
STAT connection_structures 7
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 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 touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 72
STAT bytes_written 1038
STAT limit_maxbytes 52428800
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END
上面狀況說明Memcached服務正常。
還可以試試get、set、delete、replace
輸入
三、系統服務
參照Nginx的系統服務,自己寫了一個Memcached的系統服務指令碼。
先構建/etc/init.d/memcahed這個檔案,然後賦予其可執行許可權:
memcached指令碼如下:
注意這幾行配置,請根據實際情況配置memcached執行檔案路徑,以及Memcached使用記憶體大小:
追加為系統服務:
然後就可以通過service memcached start|stop|status|restart控制memcached了!
四、常規錯誤
一開始沒有指定libevent路徑安裝memcached的時候,啟動memcached就報這個錯誤:
其實這個檔案就在/usr/lib下。錯就錯在Linux是64bit系統,如果沒有指定libevent路徑,memcached就會去/usr/lib64下去找。
找到這個檔案
提示找不到libevent-2.0.so.5
定位
注意這句:
做個軟連線
再試試:
這回找到了!
五、監控
可以在伺服器上配置一個PHP頁面來監測Memcached的情況,
下載MemcachePHP
配置也比較簡單,主要包括賬戶配置,以及Memcached Server地址配置。
常見錯誤:
在memcache.php頂端加上“date_default_timezone_set('Asia/Hong_Kong');”即可,具體地域設定參考:http://www.php.net/manual/zh/datetime.configuration.php#ini.date.timezone
如果不方便搭建PHP服務,可以使用Perl指令碼memcache-top
修改@default_instances或使用--instances引數:
先到這裡,後續做壓力測試,Java客戶端開發,Spring系統整合等。
相關連結:
Memcached筆記——(一)安裝&常規錯誤&監控
Memcached筆記——(二)XMemcached&Spring整合
Memcached筆記——(三)Memcached使用總結
Memcached筆記——(四)應對高併發攻擊
相關連結:
Memcached筆記——(一)安裝&常規錯誤&監控
Memcached筆記——(二)XMemcached&Spring整合
Memcached筆記——(三)Memcached使用總結
Memcached筆記——(四)應對高併發攻擊
一、下載
1.Libevent
簡單的說就是一個事件觸發的網路庫,Memcached離不開它。
- wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
2.Memcached
今天的主角
- wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
二、安裝
1.Libevent
解壓縮
- tar zxvf libevent-2.0.17-stable.tar.gz
tar zxvf libevent-2.0.17-stable.tar.gz
編譯、安裝
- ./configure --prefix=/usr && make && make install
./configure --prefix=/usr && make && make install
這裡一定要注意指定--prefix,後面配置memcached的時候就有必要用到。
2.Memcached
解壓
- tar zxvf memcached-1.4.13.tar.gz
tar zxvf memcached-1.4.13.tar.gz
編譯、安裝
- ./configure --with-libevent=/usr/lib && make && make install
./configure --with-libevent=/usr/lib && make && make install
這裡一定要指定libevent的路徑,否則啟動的時候就有找不到libevent的so檔案的錯誤!
啟動
- memcached -d -m 512 -p 11211 -u root -c 256 -P /var/run/memcached.pid
memcached -d -m 512 -p 11211 -u root -c 256 -P /var/run/memcached.pid
引數
引用
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-l <addr> interface to listen on (default: INADDR_ANY, all addresses)
<addr> may be specified as host:port. If you don't specify
a port number, the value you specified with -p or -U is
used. You may specify multiple addresses separated by comma
or by using -l multiple times
-d run as a daemon
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes (default: 64 MB)
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections (default: 1024)
-v verbose (print errors/warnings while in event loop)
-P <file> save PID in <file>, only used with -d option
要關掉memcached
- kill -9 `cat /var/run/memcached.pid`
kill -9 `cat /var/run/memcached.pid`
是否正常?Telnet上去看看
- telnet xxx.xxx.xxx.xxx 11211
telnet xxx.xxx.xxx.xxx 11211
然後輸入
- stats
stats
接著就能看到:
引用
STAT pid 3021
STAT uptime 3621
STAT time 1331261509
STAT version 1.4.13
STAT libevent 2.0.17-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.000999
STAT curr_connections 6
STAT total_connections 7
STAT connection_structures 7
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 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 touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 72
STAT bytes_written 1038
STAT limit_maxbytes 52428800
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END
上面狀況說明Memcached服務正常。
還可以試試get、set、delete、replace
引用
set foo 0 0 3 (儲存命令)
bar (資料)
STORED (結果)
get foo (取得命令)
VALUE foo 0 3 (資料)
bar (資料)
bar (資料)
STORED (結果)
get foo (取得命令)
VALUE foo 0 3 (資料)
bar (資料)
輸入
- quit
quit退出。
三、系統服務
參照Nginx的系統服務,自己寫了一個Memcached的系統服務指令碼。
先構建/etc/init.d/memcahed這個檔案,然後賦予其可執行許可權:
- touch /etc/init.d/memcached
- chmod +x /etc/init.d/memcached
touch /etc/init.d/memcached chmod +x /etc/init.d/memcached
memcached指令碼如下:
- #!/bin/bash
- # v.0.0.1
- # create by snowolf at 2012.5.25
- #
- # memcached - This shell script takes care of starting and stopping memcached.
- #
- # chkconfig: - 90 10
- # description: Memcache provides fast memory based storage.
- # processname: memcached
- memcached_path="/usr/local/bin/memcached"
- memcached_pid="/var/run/memcached.pid"
- memcached_memory="1024"
- # Source function library.
- . /etc/rc.d/init.d/functions
- [ -x $memcached_path ] || exit 0
- RETVAL=0
- prog="memcached"
- # Start daemons.
- start() {
- if [ -e $memcached_pid -a ! -z $memcached_pid ];then
- echo $prog" already running...."
- exit 1
- fi
- echo -n $"Starting $prog "
- # Single instance for all caches
- $memcached_path -m $memcached_memory -l 0.0.0.0 -p 11211 -u root -d -P $memcached_pid
- RETVAL=$?
- [ $RETVAL -eq 0 ] && {
- touch /var/lock/subsys/$prog
- success $"$prog"
- }
- echo
- return $RETVAL
- }
- # Stop daemons.
- stop() {
- echo -n $"Stopping $prog "
- killproc -d 10 $memcached_path
- echo
- [ $RETVAL = 0 ] && rm -f $memcached_pid /var/lock/subsys/$prog
- RETVAL=$?
- return $RETVAL
- }
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status $prog
- RETVAL=$?
- ;;
- restart)
- stop
- start
- ;;
- *)
- echo $"Usage: $0 {start|stop|status|restart}"
- exit 1
- esac
- exit $RETVAL
#!/bin/bash # v.0.0.1 # create by snowolf at 2012.5.25 # # memcached - This shell script takes care of starting and stopping memcached. # # chkconfig: - 90 10 # description: Memcache provides fast memory based storage. # processname: memcached memcached_path="/usr/local/bin/memcached" memcached_pid="/var/run/memcached.pid" memcached_memory="1024" # Source function library. . /etc/rc.d/init.d/functions [ -x $memcached_path ] || exit 0 RETVAL=0 prog="memcached" # Start daemons. start() { if [ -e $memcached_pid -a ! -z $memcached_pid ];then echo $prog" already running...." exit 1 fi echo -n $"Starting $prog " # Single instance for all caches $memcached_path -m $memcached_memory -l 0.0.0.0 -p 11211 -u root -d -P $memcached_pid RETVAL=$? [ $RETVAL -eq 0 ] && { touch /var/lock/subsys/$prog success $"$prog" } echo return $RETVAL } # Stop daemons. stop() { echo -n $"Stopping $prog " killproc -d 10 $memcached_path echo [ $RETVAL = 0 ] && rm -f $memcached_pid /var/lock/subsys/$prog RETVAL=$? return $RETVAL } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status $prog RETVAL=$? ;; restart) stop start ;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac exit $RETVAL
注意這幾行配置,請根據實際情況配置memcached執行檔案路徑,以及Memcached使用記憶體大小:
引用
memcached_path="/usr/local/bin/memcached"
memcached_memory="1024"
memcached_memory="1024"
追加為系統服務:
- chkconfig --add memcached
- chkconfig memcached on
chkconfig --add memcached chkconfig memcached on
然後就可以通過service memcached start|stop|status|restart控制memcached了!
四、常規錯誤
一開始沒有指定libevent路徑安裝memcached的時候,啟動memcached就報這個錯誤:
引用
memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
其實這個檔案就在/usr/lib下。錯就錯在Linux是64bit系統,如果沒有指定libevent路徑,memcached就會去/usr/lib64下去找。
找到這個檔案
- whereis libevent-2.0.so.5
whereis libevent-2.0.so.5
引用
libevent-2.0.so: /usr/lib/libevent-2.0.so.5
- ldd /usr/local/bin/memcached
ldd /usr/local/bin/memcached
提示找不到libevent-2.0.so.5
引用
linux-vdso.so.1 => (0x00007fff41dfd000)
libevent-2.0.so.5 => not found
librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)
libevent-2.0.so.5 => not found
librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)
定位
- LD_DEBUG=libs /usr/local/bin/memcached -v
LD_DEBUG=libs /usr/local/bin/memcached -v
引用
19905: find library=libevent-2.0.so.5 [0]; searching
19905: search path=/usr/lib/lib/tls/x86_64:/usr/lib/lib/tls:/usr/lib/lib/x86_64:/usr/lib/lib (RPATH from file /usr/local/bin/memcached)
19905: trying file=/usr/lib/lib/tls/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/tls/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/libevent-2.0.so.5
19905: search cache=/etc/ld.so.cache
19905: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64 (system search path)
19905: trying file=/lib64/tls/x86_64/libevent-2.0.so.5
19905: trying file=/lib64/tls/libevent-2.0.so.5
19905: trying file=/lib64/x86_64/libevent-2.0.so.5
19905: trying file=/lib64/libevent-2.0.so.5
19905: trying file=/usr/lib64/tls/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib64/tls/libevent-2.0.so.5
19905: trying file=/usr/lib64/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib64/libevent-2.0.so.5
19905:
/usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
19905: search path=/usr/lib/lib/tls/x86_64:/usr/lib/lib/tls:/usr/lib/lib/x86_64:/usr/lib/lib (RPATH from file /usr/local/bin/memcached)
19905: trying file=/usr/lib/lib/tls/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/tls/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib/lib/libevent-2.0.so.5
19905: search cache=/etc/ld.so.cache
19905: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64 (system search path)
19905: trying file=/lib64/tls/x86_64/libevent-2.0.so.5
19905: trying file=/lib64/tls/libevent-2.0.so.5
19905: trying file=/lib64/x86_64/libevent-2.0.so.5
19905: trying file=/lib64/libevent-2.0.so.5
19905: trying file=/usr/lib64/tls/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib64/tls/libevent-2.0.so.5
19905: trying file=/usr/lib64/x86_64/libevent-2.0.so.5
19905: trying file=/usr/lib64/libevent-2.0.so.5
19905:
/usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
注意這句:
引用
19905: trying file=/usr/lib64/libevent-2.0.so.5
做個軟連線
- ln -s /usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
ln -s /usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
再試試:
- ldd /usr/local/bin/memcached
ldd /usr/local/bin/memcached
這回找到了!
引用
linux-vdso.so.1 => (0x00007fffffef6000)
libevent-2.0.so.5 => /usr/lib64/libevent-2.0.so.5 (0x00002b5608a26000)
librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)
libevent-2.0.so.5 => /usr/lib64/libevent-2.0.so.5 (0x00002b5608a26000)
librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)
五、監控
可以在伺服器上配置一個PHP頁面來監測Memcached的情況,
下載MemcachePHP
配置也比較簡單,主要包括賬戶配置,以及Memcached Server地址配置。
- define('ADMIN_USERNAME','memcache'); // Admin Username
- define('ADMIN_PASSWORD','password'); // Admin Password
- define('DATE_FORMAT','Y/m/d H:i:s');
- define('GRAPH_SIZE',200);
- define('MAX_ITEM_DUMP',50);
- $MEMCACHE_SERVERS[] = 'mymemcache-server1:11211'; // add more as an array
- $MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
define('ADMIN_USERNAME','memcache'); // Admin Username
define('ADMIN_PASSWORD','password'); // Admin Password
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);
$MEMCACHE_SERVERS[] = 'mymemcache-server1:11211'; // add more as an array
$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
常見錯誤:
引用
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting
this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/usr/local/apache.../memcache.php on line 726
在memcache.php頂端加上“date_default_timezone_set('Asia/Hong_Kong');”即可,具體地域設定參考:http://www.php.net/manual/zh/datetime.configuration.php#ini.date.timezone
如果不方便搭建PHP服務,可以使用Perl指令碼memcache-top
修改@default_instances或使用--instances引數:
- perl memcache-top-v0.6 --instances 10.11.155.26 10.11.155.41
perl memcache-top-v0.6 --instances 10.11.155.26 10.11.155.41
先到這裡,後續做壓力測試,Java客戶端開發,Spring系統整合等。
相關連結:
Memcached筆記——(一)安裝&常規錯誤&監控
Memcached筆記——(二)XMemcached&Spring整合
Memcached筆記——(三)Memcached使用總結
Memcached筆記——(四)應對高併發攻擊
相關文章
- web前端之異常/錯誤監控Web前端
- MySQL 安裝常見錯誤MySql
- 聊聊前端監控——錯誤監控篇前端
- 安裝RAC常見小錯誤
- 前端錯誤監控與上報前端
- 做個前端錯誤監控前端
- Golang 學習筆記八 錯誤異常Golang筆記
- 【監控】Zabbix安裝
- 使用memcache.php監控memcachedPHP
- memcached程式埠監控指令碼指令碼
- redhat6.5中Rac11g安裝錯誤筆記Redhat筆記
- 安裝 Memcached
- memcached 安裝
- memcached安裝
- 搭建前端錯誤監控系統前端
- mysql 主從錯誤以及監控MySql
- PHP異常、錯誤處理機制筆記PHP筆記
- 分散式監控系統Zabbix--完整安裝記錄 -新增apache監控分散式Apache
- 分散式監控系統Zabbix-完整安裝記錄 -新增埠監控分散式
- Zabbix監控安裝部署
- memcached的執行狀態監控
- PHP編譯安裝時常見錯誤解決辦法,php編譯常見錯誤PHP編譯
- Webfunny知識分享:JS錯誤監控WebJS
- 微信小程式錯誤監控方法談微信小程式
- Memcached筆記——(三)Memcached使用總結筆記
- Ubuntu 安裝 MemcachedUbuntu
- ubuntu安裝memcachedUbuntu
- memcached 安裝配置
- STREAMS筆記(12) 效能監控筆記
- Oracle安裝錯誤Oracle
- pymssql安裝錯誤SQL
- jive安裝錯誤
- Cacti安裝磁碟IO監控
- ruby2.2安裝錯誤一則
- 分散式監控系統Zabbix--完整安裝記錄 -新增web頁面監控分散式Web
- 分散式監控系統Zabbix-3.0.3-完整安裝記錄(2)-新增mysql監控分散式MySql
- 曹政資訊保安課筆記-常見資訊保安的常識錯誤筆記
- Python 3 學習筆記之——錯誤和異常Python筆記