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

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

  1. 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.

  2. 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

  1. Stop the Redis service.
  2. 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:
      1
      /dae
      Press Enter.
    • Change daemonize no to daemonize yes to enable background operation. Save and exit.
  3. 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

Requiring a Password for Client Connections

To require a password for connecting to Redis (using Linux as an example):

  1. In the /usr/local/redis-5.0.9/redis.conf configuration file:
    • Locate the requirepass line, uncomment it, and set the password to 123456.
    • Find the line masterauth <master-password>, replace <master-password> with 123456.
  2. Find the Redis process:
    1
    ps -ef | grep redis
  3. Kill the Redis process:
    1
    kill -9 <pid>
  4. Restart Redis with the configuration file:
    1
    src/redis-server ./redis.conf
  5. Reconnect to Redis:
    • Method 1: Connect and then authenticate:
      1
      2
      src/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

Allowing Remote Connections

If your local machine cannot connect to the Redis service running on a virtual machine, follow these steps:

  1. 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.
  2. Open the Redis port on the firewall and reload the configuration:
    1
    2
    firewall-cmd --zone=public --add-port=6379/tcp --permanent
    firewall-cmd --reload

Testing Remote Connections:

  1. On the virtual machine, find the Redis process:
    1
    ps -ef | grep redis
  2. Kill the Redis process:
    1
    kill -9 <pid>
  3. Restart Redis with the configuration file:
    1
    src/redis-server ./redis.conf
  4. 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