EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis URL
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

Redis URL

Introduction to Redis URL

Redis URL supports you to connect to the redis database. It will connect redis standalone, sentinel, and the redis cluster with TLS, SSL, plain, and the Unix domain connections. The parameter of the database in the query contains higher precedence than the database path. Redis url contains the authentication details which contain the hostname, and username password. This detail is used to connect the database server using authentication.

Redis URL

Key Takeaways

  • Redis URL supports redis clusters as well as other databases for TLS and Unix socket domains. The redesigned URL includes authentication information for the database server.
  • We need to define the hostname, username, database name, and password at the time of using the redis url.

What is Redis URL?

Redis database connections are authenticated by using the information provided through the redis credentials. Credentials are obtained by using connection time from the provider of redis credentials. At the time of configuring username and password into the URL statistically, the statistical credential provider holds the information which was configured. At the time of using redis sentinel, the password from the URL will apply to the data nodes. We need to configure the sentinel authentication for every node.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

In redis, the URL class supports the URL object which belongs to the redis server. Same url we use to encode the connections from the database server. For connecting to the redis server locally we need to define the username and password for authentication. If we have not defined any authentication methods then we can directly connect the local server using the redis-cli command.

How to Create a Connection URL in redis?

To create the connection URL in redis, first, we need to install the redis server in our system. In the below example, we install the redis server by using the apt command as follows.

Command:

# apt install redis-server

Output:

Install redis server

While installing the redis server, now we are starting the server by using the redis-server command as follows.

Command:

# redis-server

Output:

redis-server command

The redis url module exposes the function that we are using to extract the redis url path. It works in the same way as the node url parse. However, it will include two new properties: database name and password. This feature of the Redis database comes in handy when we are using redis-backed tools that are not accepting URLs that are not fully qualified. Below example shows how we can create the connection URL as follows.

Code:

require("redis-url").parse('redis://:[email protected]:6379/15?test=test1')
{
 	 protocol : 'redis:',
  	slashes : true,
  	auth : '[email protected]',
  	host : 'exp.com:6379',
  	port : '6379',
  	hostname : 'exp.com',
  	hash : null,
  	……
  	pathname : '/15',
  	path : '15',
  	href : 'redis://:[email protected]:6379/15?test=test1',
  	password : '[email protected]',
  	database : '15'
}

Output:

Create connection URL

How to Find Localhost Url?

To find the localhost url we need to check our server is running on the local server. Also, we need to check the port of the local server from which it is running. We are checking the status of the redis localhost server by using the below command as follows.

Command:

# redis-cli ping
# redis-cli
> PING

Output:

Status of redis localhost

While checking the status of the server we also need to check the port number from which our redis server is running. We are checking the port number while login into the redis server by using the redis-cli command. In the below example, we can see that we have connected to localhost and it will show the port number as 6379.

port number 6379

We are finding the redis localhost url by executing the below code. The below code defines the environment and the details of the DB server are as follows.

Code:

if Rails.env.prod?
 		  puts "prod: #{ENV['REDIS_URL']}"
  		  url = URL.parse (ENV ["REDIS_URL"])
else
  		   puts "non prod"
  		   url = URL.parse("redis://localhost:6379")
end

Output:

Details of DB server

Redis class supports the URL object so we can connect locally by defining the localhost as the hostname in the configuration file. In the above example, we can see that we have defined the hostname as localhost and the port number as 6379.

Redis Localhost Url

The redis localhost url is supported below redis localhost URLs are as follows. Below is the syntax of the redis localhost url are as follows.

Syntax:

redis://name_of_name_of_host [:port_number_of_redis_server] [?db = DB_Name [&user_pwd = user_pwd]]
redis://name_of_host [:port_number_of_redis_server] [?user_pwd = user_pwd [&db = DB_Name]]
redis://[:[email protected]]name_of_host [:port_number_of_redis_server] [/DB_Name]
redis://[:[email protected]]name_of_host [:port_number_of_redis_server][?db = DB_Name]
redis://name_of_host [:port_number_of_redis_server]/DB_Name [?user_pwd = user_pwd]

At the time of using the localhost url in the above syntax, we need to change the only hostname of the database server to localhost. To connect the redis server locally we need to use the below command.

Command:

# redis-cli

Output:

Connect to redis server

The redis-cli command is taking us into the interactive mode which was present in the REPL, where we are running multiple commands. In interactive mode, our command line prompt indicates the connection to the instance of redis which was hosted locally and it will access by using port number 6379. We can also connect the localhost server to execute the command by using the following way.

Command:

# redis-cli ping

Output:

redis-cli command

In the above example, we can see that we are executing the command in the command line by using the redis-cli command.

Examples of Redis URL

Below are the examples of redis URL.

Example #1

We are connecting to the local database server as follows.

Code:

if Rails.env.dev?
 		  puts "prod: #{ENV['REDIS_URL']}"
 		  url = URL.parse (ENV["REDIS_URL"])
else
 		  puts "prod"
 		  url = URL.parse("redis://localhost:6379")
end

Output:

Redis URL Example 1

Example #2

In the below example, we are connecting to another localhost URL as follows.

Code:

require ("redis-url").parse ('redis://:[email protected]:6379/25?redis1 = redis2')
{
  		protocol : 'redis:',
  		slashes : true,
 		auth : '[email protected]',
  		host : 'test.com:6379',
  		port : '6379',
  		hostname : 'test.com',
  		hash : null,
  		search : '?redis1=redis2',
  		query : { redis1: 'redis2' },
  		pathname : '/25',
  		path : '25',
  		href : 'redis://:[email protected]:6379/25?redis1 = redis2',
  		password : '[email protected]',
  		database: '25'
}

Output:

Connect to another localhost Example 2

FAQ

Given below are the FAQs mentioned:

Q1. What is the use of the redis url?

Answer: We connect to the redis database server using the redis url. All authentication details must be specified in the redis URL.

Q2. What is the default port we use in the redis URL?

Answer: We are using redis port 6379 at the time of using the redis URL. This is the default port of the redis server.

Q3. How can we add the database to the redis URL?

Answer: If suppose we need to add the database to the redis url we need to define the database name into the redis URL.

Conclusion

Redis database connections are authenticated by using the information provided through the redis credentials. Credentials are obtained by using connection time from the provider of redis credentials. Redis url supports connecting to the redis database, it will support you to connect redis standalone, sentinel, and the redis cluster with TLS, SSL, plain, and the unix domain connections.

Recommended Articles

This is a guide to Redis URL. Here we discuss the introduction, and steps to create connection URL and find Localhost URL along with different examples. You may also have a look at the following articles to learn more –

  1. Redis API
  2. Redis Rails
  3. Redis Auth
  4. Scrapy Redis
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more