pnxtech / hydra

A light-weight library for building distributed applications such as microservices
https://www.hydramicroservice.com
MIT License
645 stars 54 forks source link

How to connect to redis via unix socket? #153

Closed chkaligx closed 6 years ago

chkaligx commented 6 years ago

Unfortunately I am hosting my projects on a virtual host and the only way I can access my redis instance is using unix sockets.

Is there any way to pass the path to the socket file in hydra?

cjus commented 6 years ago

@chris9r this isn't something we've tried. However, by quick search I've found this:

https://serverfault.com/questions/591193/single-redis-instance-with-local-redis-connections-via-unix-socket-and-remote-vi

which shows how to configure Redis to use a unix socket. And Hydra uses the node_redis package which documents passing a unixsocket path: https://github.com/NodeRedis/node_redis#rediscreateclient

So it appears that in your config file you should be able to do something like this:

{
  "redis": "redis://tmp/redis.sock"
}

Have you tried this approach?

chkaligx commented 6 years ago

@cjus I tried this approach and it doesn't work.

https://serverfault.com/questions/591193/single-redis-instance-with-local-redis-connections-via-unix-socket-and-remote-vi

I could configure my redis socket in this file.

https://github.com/NodeRedis/node_redis#rediscreateclient If you have redis-server running on the same machine as node, then the defaults for port and host are probably fine and you don't need to supply any arguments.

Then I could use the redis-server package to read in the redis.conf. But, as I read in the documentation from my hoster, the reason why the redis connection on my virtual host is limited to using unix sockets is because otherwise EVERY other user would be able to access my database.

https://www.npmjs.com/package/redis-server#basic-example const server = new RedisServer(6379);

So if I do something like this and run an instance of redis-server on my host, that would be nothing else than routing my redis connection from the unix socket to whatever i configured (in the example above port 6379). If I am getting this right I don't want to do that.

this isn't something we've tried.

And why did it not work out? Where is the problem in passing the socket to redis.createClient() when it is supported by the package?

emadum commented 6 years ago

Hi @chris9r, I took a look at the Redis createClient docs - it looks like it expects the unix socket as the 'path' option.

E.g.

{
   "redis": {
      "path": "/tmp/redis.sock"
   }
}
chkaligx commented 6 years ago

@emadum

{ "redis": { "path": "/tmp/redis.sock" } }

That worked! Thank you all so far!

chkaligx commented 6 years ago

@cjus @emadum

Can someone add this information to the docs?

cjus commented 6 years ago

@chkaligx added here: https://www.hydramicroservice.com/docs/hydra-core/redis-config.html @emadum thanks for investigation and clarification.