Memcache連接出錯的問題

今天一早突然發現Memcache連接不到了,檢查了一下Memcache的服務發現很正常。

 

 

 寫道
# ps aux|grep memcached
root 7409 0.0 0.0 130296 1088 ? Ssl 10:14 0:00 memcached -d -m 128 -u root -p 11211 -c 1024 -P /tmp/memcached.pid
root 7426 0.0 0.0 61220 764 pts/3 S+ 10:14 0:00 grep memcached

 既然Memcache很正常那麼,爲什麼我的代碼突然就連不上去了呢?

後來一想可能是誰把防火牆開啓了,查看一下防火牆狀態,出來了一大堆

 

 

 寫道
# /etc/init.d/iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306

Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0

Chain OUTPUT (policy ACCEPT)
num target prot opt source destination

Chain RH-Firewall-1-INPUT (2 references)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
3 ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
5 ACCEPT udp -- 0.0.0.0/0 224.0.0.251 udp dpt:5353
6 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:631
7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:631
8 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
9 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
10 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
11 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:5900
12 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 state NEW udp dpt:5900
13 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

 先把防火牆關掉試試結果可以連到。找到原因了,於是修改一下防火牆的規則,不讓它攔截11211端口的連接就可以了。

 

 

 寫道
[root@cloud1 ~]# /sbin/iptables -I INPUT -p tcp --dport 11211 -j ACCEPT
[root@cloud1 ~]# /sbin/iptables -I INPUT -p udp --dport 11211 -j ACCEPT
[root@cloud1 ~]# /etc/rc.d/init.d/iptables save
將當前規則保存到 /etc/sysconfig/iptables:[確定]
[root@cloud1 ~]# /etc/init.d/iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:11211
2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:11211
3 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306

 

添加兩條規則,然後保存一下就可以。

# /sbin/iptables -I INPUT -p tcp --dport 11211 -j ACCEPT

# /sbin/iptables -I INPUT -p udp --dport 11211 -j ACCEPT

# /etc/rc.d/init.d/iptables save

 

如果還不管用就重啓一下防火牆

# /etc/init.d/iptables restart

 

memcahced的幫助

 

Memcached的幫助 寫道
memcached 1.4.13
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-s <file> UNIX socket path to listen on (disables network support)
-a <mask> access mask for UNIX socket, in octal (default: 0700)
-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
-r maximize core file limit
-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)
-k lock down all paged memory. Note that there is a
limit on how much memory you may lock. Trying to
allocate more than that would fail, so be sure you
set the limit correctly for the user you started
the daemon with (not for -u <username> user;
under sh this is done with 'ulimit -S -l NUM_KB').
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-vvv extremely verbose (also print internal state transitions)
-h print this help and exit
-i print memcached and libevent license
-P <file> save PID in <file>, only used with -d option
-f <factor> chunk size growth factor (default: 1.25)
-n <bytes> minimum space allocated for key+value+flags (default: 48)
-L Try to use large memory pages (if available). Increasing
the memory page size could reduce the number of TLB misses
and improve the performance. In order to get large pages
from the OS, memcached will allocate the total item-cache
in one large chunk.
-D <char> Use <char> as the delimiter between key prefixes and IDs.
This is used for per-prefix stats reporting. The default is
":" (colon). If this option is specified, stats collection
is turned on automatically; if not, then it may be turned on
by sending the "stats detail on" command to the server.
-t <num> number of threads to use (default: 4)
-R Maximum number of requests per event, limits the number of
requests process for a given connection to prevent
starvation (default: 20)
-C Disable use of CAS
-b Set the backlog queue limit (default: 1024)
-B Binding protocol - one of ascii, binary, or auto (default)
-I Override the size of each slab page. Adjusts max item size
(default: 1mb, min: 1k, max: 128m)
-o Comma separated list of extended or experimental options
- (EXPERIMENTAL) maxconns_fast: immediately close new
connections if over maxconns limit
- hashpower: An integer multiplier for how large the hash
table should be. Can be grown at runtime if not big enough.
Set this based on "STAT hash_power_level" before a
restart.

 

 

memcached -d -m 256 -u root -l 10.1.106.20 -p 11211 -c 1024 -P /tmp/memcached.pid

 

-d選項是啓動一個守護進程,

 

-m是分配給Memcache使用的內存數量,單位是MB,這裏是200MB
-u是運行Memcache的用戶,如果當前爲 root 的話,需要使用此參數指定用戶。
-l是監聽的服務器IP地址,如果有多個地址的話,我這裏指定了服務器的IP地址192.168.1.91
-p是設置Memcache監聽的端口,我這裏設置了12301,最好是1024以上的端口
-c選項是最大運行的併發連接數,默認是1024,這裏設置了256
-P是設置保存Memcache的pid文件,我這裏是保存在 /tmp/memcached.pid
停止Memcache進程:直接kill 進程號 或者 kill `cat /tmp/memcached.pid`

查看啓動的memcache服務:netstat -lp | grep memcached 或者 ps -ef | grep memcached 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章