Here’s how you can check its status and set up a database using the CLI:
—
1. Check if Redis is Installed and Running
Run the following command to check the status of the Redis service:
sudo systemctl status redis
To ensure Redis starts on boot:
sudo systemctl enable redis
—
2. Access Redis CLI
To interact with Redis, use the Redis CLI:
redis-cli
You will be inside the Redis CLI prompt (usually represented by 127.0.0.1:6379>
). Here you can directly execute Redis commands.
—
3. Set Up a Database in Redis
Redis does not use traditional databases like SQL-based systems. Instead, it provides logical database “slots” identified by numbers, with 16 databases (numbered 0–15) by default. You can switch between them or manage data in the default database (DB 0).
To switch to a specific database:
SELECT <db-number>
For example:
SELECT 1
This switches to database 1.
To add key-value pairs in the current database:
SET mykey "myvalue"
To retrieve a value:
GET mykey
To check the selected database:
INFO keyspace
This shows the data stored in all databases.
—
4. Configure a Redis Password (Optional for Security)
If you need to secure Redis with a password:
- Edit the Redis configuration file:
sudo nano /etc/redis/redis.conf
- Look for the line
# requirepass
and uncomment it, then set a password:
requirepass yourpassword
- Restart Redis for the changes to take effect:
sudo systemctl restart redis
- When connecting via
redis-cli
, authenticate using:
AUTH yourpassword
—
5. Verify Redis is Working
You can test Redis functionality:
redis-cli ping
The response PONG
confirms Redis is operational.
Let me know if you need help with a specific use case!