EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Redis Tutorial Redis URL
 

Redis URL

Lalita Gupta
Article byLalita Gupta
EDUCBA
Reviewed byRavi Rathore

Updated February 17, 2023

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

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

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 : 'Test@123',
  	host : 'exp.com:6379',
  	port : '6379',
  	hostname : 'exp.com',
  	hash : null,
  	……
  	pathname : '/15',
  	path : '15',
  	href : 'redis://:[email protected]:6379/15?test=test1',
  	password : 'Test@123',
  	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://[:user_pwd@]name_of_host [:port_number_of_redis_server] [/DB_Name]
redis://[:user_pwd@]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 : 'Redis@123',
  		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 : 'Redis@123',
  		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
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - 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
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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW