1

How can i connect to a Redis db that has username and password? below is the code example i'm trying to use, ( i'm using nodejs with node-redis and Redis version "redis": "^3.0.0",)

const client = redis.createClient({
  host: "localhost",
  port: 6379,
  password: "1234",
  username: "username"
});
2
  • Which error do you get? Commented Jul 12, 2022 at 8:14
  • Im not getting any error, but is this the correct way to implement this ? Pretty hard to find any documentation on this Commented Jul 12, 2022 at 13:46

2 Answers 2

3

Depends on the version of Node Redis that you are using. Since it looks like you are using Node Redis 3.x it would look like this:

const client = redis.createClient({
  host: "localhost",
  port: 6379,
  password: "1234",
  user: "username"
});

You could also connect using a connection string:

const client = redis.createClient("redis://username:1234@localhost:6379");

Full documentation for 3.x is available on the tagged branch in the GitHub repo for Node Redis.

That said, I would recommend using Node Redis 4.x as it supports Promises, newer Redis commands, and many common Redis modules like RedisJSON and RediSearch.

To connect using 4.x:

const client = redis.createClient({
  socket: {
    host: "localhost",
    port: 6379
  }
  password: "1234",
  username: "username"
});

or:

const client = redis.createClient({ url: "redis://username:1234@localhost:6379" });

Details on connecting using Node Redis 4.x can be found in the README on the main branch of Node Redis and in the Client Configuration Guide.

Sign up to request clarification or add additional context in comments.

3 Comments

if im using redis 3.x.x do i need to client.quit() ? is this necessary ? if i don't quit will it cause any errors?
I generally use client.quit() when I want to close a connection as it lets Redis know to dispose of the connection and closes it locally. I've run into problems when I just call client.close() where Redis doesn't know the connection is dead, and then I run out of connections. That said, for a Node.js app, I usually just keep one connection open, use it for everything, and call client.quit() when the process ends.
I do this will all Redis clients, regardless of language or version.
1

To connect Nodejs Project with Redis Version ^4.x:

const redisClient = redis.createClient({
  socket: {
    host: "localhost",
    port: 6379
  }
  password: "1234",
  username: "username"
});
redisClient.on("error", (error) => console.error(`Error : ${error}`));
redisClient.connect();

If you don't Provide socket, password or username then by default it will connection on 127.0.0.1:6379

for by Default Connection use Beloved mention code

const redisClient = redis.createClient();
redisClient.on("error", (error) => console.error(`Error : ${error}`));
redisClient.connect();

Redis Installation in system https://redis.io/docs/getting-started/installation/install-redis-on-windows/

For More information About redis as node package https://www.npmjs.com/package/redis

and For mode Redis Command https://redis.io/commands/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.