今天一早突然发现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