Memcached Could not connect to Remote Server | memcached.js

99封情书 提交于 2019-12-23 03:20:12

问题


My local environment for API

node -v: v8.9.4
npm  -v:  5.6.0

Package 
memcached.js: "memcached": "^2.2.2"

We have a Node API in which we are using package memcached.js to connect to Memcache server with below configurations.

MEMCACHED_CONFIG:
{
    MAX_VALUE: 1024,
    SERVER: "X.X.X.X",
    PORT: 11211,
    COMPLETE_PATH: "X.X.X.X:11211",
    CACHE_TIMEOUT: 3600,
    POOL_SIZE: 50,
    maxKeySize: 1024,
    timeout: 5000
}

So X.X.X.X is remote server IP where our Memcache server is running. and I am able to connect this X.X.X.X server from my system by using telnet command like c:/> telnet X.X.X.X 11211 and it works.

cacheUtility.js

var MEMCACHED_CONFIG= require('./MEMCACHED_CONFIG');
var Memcached = require('memcached');
Memcached.config.maxValue = MEMCACHED_CONFIG.MAX_VALUE;
Memcached.config.poolSize = MEMCACHED_CONFIG.POOL_SIZE;
Memcached.config.maxKeySize= MEMCACHED_CONFIG.maxKeySize;
Memcached.config.timeout=  MEMCACHED_CONFIG.timeout;

var memcached = new Memcached();
memcached.connect(MEMCACHED_CONFIG.COMPLETE_PATH, function( err, 
conn ){
   if( err ) {
     CONFIG.CONSOLE_MESSAGE("Cache Connect Error "+conn.server);
  }
 });

We are using above code to connect to Memcached Server and as you can see remote server IP is coming from MEMCACHED_CONFIG.

My issue is that it is always trying to connect to 127.0.0.1 server instead of passing Remote Memcached Server. So in order to run it, I have to make changes in the memcached.js file of the core package.

C:\BitBucketProjects\Licensor Server\node_modules\memcached\lib\memcached.js

function Client (args, options) {
var servers = []
, weights = {}
, regular = 'localhost:11211'
 //, regular = 'X.X.X.X:11211'
, key;

I don't want to make any change in core package. Why is it not connecting to the given server?


回答1:


When you have memcached server setup on a different machine than the server using it then always mention the server IP and options otherwise it defaults to localhost. You can see that if you view the "server" property of the client (using NodeJs memcached client version 2.2.2):

var Memcached = require('memcached');
var memcached = new Memcached();
console.log(memcached.server);

Seems to be some issue with the "memcache.connect" method as it does not override the localhost server. To make it work, you have to mention the IP of memcached server in the constructor as mentioned in the documentation:

var Memcached = require('memcached');
var memcached = new Memcached('192.168.10.10:11211');

Now you should be able to connect to the server without an issue if you have the 11211 port opened on the host. If not allowed, you can execute the following command on Memcached host to open the port:

$ sudo ufw allow 11211

To ensure you are able to connect to memcached server use following command:

telnet 192.168.10.10:11211

If even that does not work, your server might have stopped working so you need to start it either as a service or as a process:

Start as a process:

$ memcached -u memcached -d -m 30 -l 192.168.10.10 -p 11211

Start as a service:

$ sudo systemctl start memcached

OR

$ sudo service memcached start

Just for reference for those who might not know, to expose memcached server on the network, you can either specify the IP and port like in the command above or in the memcached configuration file. To provide default configuration, look for "-l 127.0.0.1" in the following file and replace the loopback address with your host server's network IP:

$ sudo nano /etc/default/ufw

Of course, above commands will work only if you have memcached installed on the server, if not installed then run the following to install it first:

$ sudo apt-get install memcached

I hope it helps.



来源:https://stackoverflow.com/questions/50135302/memcached-could-not-connect-to-remote-server-memcached-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!