Configuring and Starting Redis Service on Linux
Starting the Service
Method 1: Start Redis Server Directly
Navigate to the /usr/local/redis-5.0.9/src/
directory (the src
folder under your Redis installation path) and execute:
1 | ./redis-server |
Method 2: Start Redis with Configuration File (Recommended for running Redis in the background)
Navigate to the /usr/local/redis-5.0.9/
directory and execute:
1 | src/redis-server ./redis.conf |
Stopping the Service
To stop the service, press Ctrl + C
in the service terminal.
Connecting to Redis
Connecting Without a Password:
Open a new terminal window, navigate to/usr/local/redis-5.0.9/src/
, and execute:1
./redis-cli
If you see
127.0.0.1:6379>
, the connection to the local Redis server (port 6379) is successful.Connecting with a Password:
Navigate to/usr/local/redis-5.0.9/
and execute:1
src/redis-cli -h localhost -p 6379 -a your_password
Exiting the Connection
To exit the Redis connection, type:
1 | exit |
Configuring Redis to Run in the Background
- Stop the Redis service.
- Modify the
/usr/local/redis-5.0.9/redis.conf
configuration file as follows:- Navigate to the directory:
1
cd /usr/local/redis-5.0.9/
- Open the configuration file:
1
vim redis.conf
- Search for the keyword
daemonize
:Press Enter.1
/dae
- Change
daemonize no
todaemonize yes
to enable background operation. Save and exit.
- Navigate to the directory:
- Restart Redis and explicitly load the configuration file:
- Navigate to the Redis directory:
1
cd /usr/local/redis-5.0.9
- Execute:
1
src/redis-server ./redis.conf
- Navigate to the Redis directory:
Requiring a Password for Client Connections
To require a password for connecting to Redis (using Linux as an example):
- In the
/usr/local/redis-5.0.9/redis.conf
configuration file:- Locate the
requirepass
line, uncomment it, and set the password to123456
. - Find the line
masterauth <master-password>
, replace<master-password>
with123456
.
- Locate the
- Find the Redis process:
1
ps -ef | grep redis
- Kill the Redis process:
1
kill -9 <pid>
- Restart Redis with the configuration file:
1
src/redis-server ./redis.conf
- Reconnect to Redis:
- Method 1: Connect and then authenticate:
1
2src/redis-cli -h localhost -p 6379
auth 123456 - Method 2: Provide the password during login:
1
src/redis-cli -h localhost -p 6379 -a 123456
- Method 1: Connect and then authenticate:
Allowing Remote Connections
If your local machine cannot connect to the Redis service running on a virtual machine, follow these steps:
- Modify the
/usr/local/redis-5.0.9/redis.conf
configuration file on the virtual machine:- Locate the line
bind 127.0.0.1
and comment it out.
- Locate the line
- Open the Redis port on the firewall and reload the configuration:
1
2firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
Testing Remote Connections:
- On the virtual machine, find the Redis process:
1
ps -ef | grep redis
- Kill the Redis process:
1
kill -9 <pid>
- Restart Redis with the configuration file:
1
src/redis-server ./redis.conf
- On the local machine, connect to the Redis service on the virtual machine:
1
redis-cli -h 192.168.50.158 -p 6379 -a 123456