0
0
Redisquery~5 mins

Connection configuration in Redis

Choose your learning style9 modes available
Introduction
We need to set up a connection to Redis so our program can talk to the Redis database and store or get data.
When your app needs to save user sessions in Redis.
When you want to cache data to make your app faster.
When you need to share data between different parts of your app using Redis.
When you want to connect to a Redis server running on another computer.
When you want to use Redis commands from your program.
Syntax
Redis
redis-cli -h <host> -p <port> -a <password>
Replace with the Redis server address, like 'localhost' or an IP.
Replace with the Redis port number, usually 6379.
Use -a only if your Redis server requires a password.
Examples
Connects to Redis on localhost at default port 6379 without a password.
Redis
redis-cli
Connects to Redis at IP 192.168.1.10 on port 6380 without a password.
Redis
redis-cli -h 192.168.1.10 -p 6380
Connects to Redis at redis.example.com on port 6379 using password 'mysecret'.
Redis
redis-cli -h redis.example.com -p 6379 -a mysecret
Sample Program
This connects to Redis on your local computer and sends the PING command to check if Redis is alive.
Redis
redis-cli -h localhost -p 6379
PING
OutputSuccess
Important Notes
If you don't specify host and port, redis-cli uses localhost and port 6379 by default.
Make sure your Redis server is running before connecting.
If your Redis server has no password, do not use the -a option.
Summary
Connection configuration tells your program where and how to reach Redis.
You need to know the host, port, and password (if any) to connect.
Using redis-cli with these options helps you test and interact with Redis.