Memcache连接出错的问题

今天一早突然发现Memcache连接不到了,检查了一下Memcache的服务发现很正常。

 写道
# ps aux|grep memcached

root74090.00.01302961088?Ssl10:140:00memcached-d-m128-uroot-p11211-c1024-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

ChainINPUT(policyACCEPT)

numtargetprotoptsourcedestination

1RH-Firewall-1-INPUTall--0.0.0.0/00.0.0.0/0

2ACCEPTtcp--0.0.0.0/00.0.0.0/0tcpdpt:3306

ChainFORWARD(policyACCEPT)

numtargetprotoptsourcedestination

1RH-Firewall-1-INPUTall--0.0.0.0/00.0.0.0/0

ChainOUTPUT(policyACCEPT)

numtargetprotoptsourcedestination

ChainRH-Firewall-1-INPUT(2references)

numtargetprotoptsourcedestination

1ACCEPTall--0.0.0.0/00.0.0.0/0

2ACCEPTicmp--0.0.0.0/00.0.0.0/0icmptype255

3ACCEPTesp--0.0.0.0/00.0.0.0/0

4ACCEPTah--0.0.0.0/00.0.0.0/0

5ACCEPTudp--0.0.0.0/0224.0.0.251udpdpt:5353

6ACCEPTudp--0.0.0.0/00.0.0.0/0udpdpt:631

7ACCEPTtcp--0.0.0.0/00.0.0.0/0tcpdpt:631

8ACCEPTall--0.0.0.0/00.0.0.0/0stateRELATED,ESTABLISHED

9ACCEPTtcp--0.0.0.0/00.0.0.0/0tcpdpt:3306

10ACCEPTtcp--0.0.0.0/00.0.0.0/0stateNEWtcpdpt:22

11ACCEPTtcp--0.0.0.0/00.0.0.0/0stateNEWtcpdpt:5900

12ACCEPTudp--0.0.0.0/00.0.0.0/0stateNEWudpdpt: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-IINPUT-pudp--dport11211-jACCEPT

[root@cloud1~]#/etc/rc.d/init.d/iptablessave

将当前规则保存到/etc/sysconfig/iptables:[确定]

[root@cloud1~]#/etc/init.d/iptablesstatus

表格:filter

ChainINPUT(policyACCEPT)

numtargetprotoptsourcedestination

1ACCEPTudp--0.0.0.0/00.0.0.0/0udpdpt:11211

2ACCEPTtcp--0.0.0.0/00.0.0.0/0tcpdpt:11211

3RH-Firewall-1-INPUTall--0.0.0.0/00.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>TCPportnumbertolistenon(default:11211)

-U<num>UDPportnumbertolistenon(default:11211,0isoff)

-s<file>UNIXsocketpathtolistenon(disablesnetworksupport)

-a<mask>accessmaskforUNIXsocket,inoctal(default:0700)

-l<addr>interfacetolistenon(default:INADDR_ANY,alladdresses)

<addr>maybespecifiedashost:port.Ifyoudon'tspecify

aportnumber,thevalueyouspecifiedwith-por-Uis

used.Youmayspecifymultipleaddressesseparatedbycomma

orbyusing-lmultipletimes

-drunasadaemon

-rmaximizecorefilelimit

-u<username>assumeidentityof<username>(onlywhenrunasroot)

-m<num>maxmemorytouseforitemsinmegabytes(default:64MB)

-Mreturnerroronmemoryexhausted(ratherthanremovingitems)

-c<num>maxsimultaneousconnections(default:1024)

-klockdownallpagedmemory.Notethatthereisa

limitonhowmuchmemoryyoumaylock.Tryingto

allocatemorethanthatwouldfail,sobesureyou

setthelimitcorrectlyfortheuseryoustarted

thedaemonwith(notfor-u<username>user;

undershthisisdonewith'ulimit-S-lNUM_KB').

-vverbose(printerrors/warningswhileineventloop)

-vvveryverbose(alsoprintclientcommands/reponses)

-vvvextremelyverbose(alsoprintinternalstatetransitions)

-hprintthishelpandexit

-iprintmemcachedandlibeventlicense

-P<file>savePIDin<file>,onlyusedwith-doption

-f<factor>chunksizegrowthfactor(default:1.25)

-n<bytes>minimumspaceallocatedforkey+value+flags(default:48)

-LTrytouselargememorypages(ifavailable).Increasing

thememorypagesizecouldreducethenumberofTLBmisses

andimprovetheperformance.Inordertogetlargepages

fromtheOS,memcachedwillallocatethetotalitem-cache

inonelargechunk.

-D<char>Use<char>asthedelimiterbetweenkeyprefixesandIDs.

Thisisusedforper-prefixstatsreporting.Thedefaultis

":"(colon).Ifthisoptionisspecified,statscollection

isturnedonautomatically;ifnot,thenitmaybeturnedon

bysendingthe"statsdetailon"commandtotheserver.

-t<num>numberofthreadstouse(default:4)

-RMaximumnumberofrequestsperevent,limitsthenumberof

requestsprocessforagivenconnectiontoprevent

starvation(default:20)

-CDisableuseofCAS

-bSetthebacklogqueuelimit(default:1024)

-BBindingprotocol-oneofascii,binary,orauto(default)

-IOverridethesizeofeachslabpage.Adjustsmaxitemsize

(default:1mb,min:1k,max:128m)

-oCommaseparatedlistofextendedorexperimentaloptions

-(EXPERIMENTAL)maxconns_fast:immediatelyclosenew

connectionsifovermaxconnslimit

-hashpower:Anintegermultiplierforhowlargethehash

tableshouldbe.Canbegrownatruntimeifnotbigenough.

Setthisbasedon"STAThash_power_level"beforea

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 

相关推荐