Linux system memcached boot normal, but the program can not connect to solve the problem

Source: Internet
Author: User

After installing the virtual machine Linux memcached, try to connect the memcached server with the Java program, but the following error occurred.

Com.schooner.MemCached.SchoonerSockIOPool Fri Jan 09:45:59 CST-++++ failed to get Sockio obj for:192.168.168.2: 1121109:45:59 CST-++++ failed to create connection to:192.168.168.2:11211-only 0 created.

Tip Unable to establish memcached connection and Sockio, view memcached process with Ps-ef|grep memcached command

Nobody    1871     1  0 08:40?        00:00:00/usr/local/memcached-1.4.17/bin/memcached-d-P 11211-u nobody-m 64-c 1024-p/USR/LOCAL/MEMCACHED-1.4.17/MEMC Ached.pid

Description memcached service started normally, can't connect virtual machine, this is unlikely Ah, open cmd command, ping 192.168.168.2

The virtual machine connection is OK, that could be a problem with the port, Telnet 192.168.168.2. 11211

Port 11211 failed to connect, and there was a problem with the port.

So in the online check, the original is due to Linux iptables firewall reasons, need to open in the Iptables 11211 port access rights,

Connect the virtual machine with the SSH client, locate the directory/etc/sysconfig under the iptables file, download it, and open it with Notepad

# Generated by Iptables-save v1.4.7 on Thu Jan 8 13:46:05 2015*filter:input ACCEPT[0:0]: FORWARD ACCEPT[0:0]: OUTPUT ACCEPT[59:6824]-A input-m state--state related,established-j accept-a input-p icmp-j accept-a input-i lo-j accept-a input-p tcp-m State--state new-m TCP --dport22-j accept-a input-j REJECT--reject-with icmp-host-prohibited-a forward-j REJECT--reject-with icmp-host-prohibite D commit# completed on Thu Jan8 13:46:05 2015

Found that there is only a 22 port, no wonder you can directly use SSH client to connect the virtual machine, the original iptables default is open 22 port, divert, Copy22 port of the line code, the port is changed to 11211,

# Generated by Iptables-save v1.4.7 on Thu Jan 8 13:46:05 2015*filter:input ACCEPT[0:0]: FORWARD ACCEPT[0:0]: OUTPUT ACCEPT[59:6824]-A input-m state--state related,established-j accept-a input-p icmp-j accept-a input-i lo-j accept-a input-p tcp-m State--state new-m TCP --dport22-j accept-a input-p tcp-m State--state new-m TCP--dport11211-j accept-a input-j REJECT--reject-with icmp-host-prohibited-a forward-j REJECT--reject-with icmp-host-prohibit Ed commit# completed on Thu Jan8 13:46:05 2015

Ctrl+s, save, Upload, execute command service iptables restart

Back to cmd window, Telnet 192.168.168.2 11211

OK, the port is connected successfully.

Executing Java programs in MyEclipse

 Public classMycache {/**     * @paramargs*/@SuppressWarnings ("Deprecation")     Public Static voidMain (string[] args) {memcachedclient client=Newmemcachedclient (); String[] Addrs= {"192.168.168.2:11211"}; Integer [] Weights= {3}; //Sockiopool is a socket connection pool provided by the memcached client, which, in layman's words, is the object that exchanges data with the memcached server side. //Sockiopool is initialized once when the application is started.Sockiopool pool =sockiopool.getinstance ();        Pool.setservers (Addrs);        Pool.setweights (weights); Pool.setinitconn (5); Pool.setminconn (5); Pool.setmaxconn (200); Pool.setmaxidle (1000*30*30); Pool.setmaintsleep (30); Pool.setnagle (false); Pool.setsocketto (30); Pool.setsocketconnectto (0);         Pool.initialize (); System.out.println (Pool.getservers () [0]);//        //string[] s =pool.getservers (); Client.setcompressenable (true); Client.setcompressthreshold (1000*1024); //Putting data into the cacheClient.set ("Test2", "Test2"); //put data into the cache and set the expiration timeDate date=NewDate (2); Client.set ("Test1", "Test1", date); Testbean Bean=NewTestbean (); Bean.setname ("Bean1"); Client.add ("Bean1", Bean); Testbean Bean1= (Testbean) client.get ("Bean1");          System.out.println (Bean1.getname ()); //Delete cached Data//client.delete ("test1"); //Get Cached DataString str = (string) client.get ("Test1");  System.out.println (str); //System.out.println (s.length);            }}

Output Result:

memcached connection is successful!

PS: On the problem of unable to connect memcached, there is another possibility is that Linux security Control module SELinux Security control issues, specifically can be searched on the internet.

Worked for more than three years. Mixed for more than three years, do not understand anything, Linux also rarely contact, forgive me even iptables are not familiar with it.

Linux system memcached boot normal, but the program can not connect to solve the problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.