Home > Database > Redis > How do I connect to a Redis server using the redis-cli command-line interface?

How do I connect to a Redis server using the redis-cli command-line interface?

Emily Anne Brown
Release: 2025-03-14 18:02:07
Original
342 people have browsed it

How do I connect to a Redis server using the redis-cli command-line interface?

To connect to a Redis server using the <code>redis-cli</code> command-line interface, you can follow these simple steps:

  1. Open a Terminal: Start by opening your terminal or command prompt.
  2. Basic Connection: If your Redis server is running on the default settings (localhost and port 6379), you can connect by simply typing:

    <code>redis-cli</code>
    Copy after login
    Copy after login

    This command will attempt to connect to 127.0.0.1 at port 6379.

  3. Verify Connection: Once connected, you should see the Redis prompt 127.0.0.1:6379>. You can verify the connection by typing a simple command like PING, and if everything is set up correctly, Redis will respond with PONG.

This is the most basic way to use <code>redis-cli</code>. For more advanced usage, such as connecting to different hosts or ports, or using authentication, refer to the following sections.

What authentication options are available when connecting to Redis with redis-cli?

Redis supports authentication to secure your Redis instance. When connecting with <code>redis-cli</code>, you have a couple of options to authenticate:

  1. Using the -a Option: You can pass the password directly with the -a flag. Here's how you can do it:

    <code>redis-cli -a yourpassword</code>
    Copy after login

    This method is convenient but less secure as the password is visible in the command history and process lists.

  2. Interactive Authentication: If you prefer not to expose your password on the command line, you can use the AUTH command interactively after connecting to Redis:

    <code>redis-cli
    127.0.0.1:6379> AUTH yourpassword</code>
    Copy after login

    After entering this command, if the authentication is successful, Redis will respond with OK.

  3. Using a .rediscli Configuration File: You can also store your authentication details in a configuration file named .rediscli in your home directory. Add the following lines to the file:

    <code>host 127.0.0.1
    port 6379
    auth yourpassword</code>
    Copy after login

    Then, connect using:

    <code>redis-cli</code>
    Copy after login
    Copy after login

    The <code>redis-cli</code> will read the configuration from the .rediscli file and use the specified authentication details.

Each method has its use cases, so choose the one that best fits your security requirements.

How can I specify a different port when using redis-cli to connect to Redis?

If your Redis server is running on a port other than the default 6379, you can specify the port using the -p option with <code>redis-cli</code>. Here's how you do it:

<code>redis-cli -p yourport</code>
Copy after login

For example, if your Redis server is running on port 6380, you would use:

<code>redis-cli -p 6380</code>
Copy after login

This command will attempt to connect to 127.0.0.1 at the specified port. If you're using other options like authentication, you can combine them like this:

<code>redis-cli -p 6380 -a yourpassword</code>
Copy after login

Remember that you can combine multiple options as needed. For instance, if you also need to specify a different host, you can add the -h option, which will be discussed in the next section.

Can I use redis-cli to connect to a Redis server on a remote host?

Yes, you can use <code>redis-cli</code> to connect to a Redis server on a remote host. To do so, you need to specify the host using the -h option. Here's the syntax:

<code>redis-cli -h hostname</code>
Copy after login

For example, if your Redis server is running on a remote host with the IP address 192.168.1.100, you would use:

<code>redis-cli -h 192.168.1.100</code>
Copy after login

If the remote Redis server is running on a non-standard port, you can combine the -h option with the -p option:

<code>redis-cli -h 192.168.1.100 -p 6380</code>
Copy after login

If authentication is required, you can add the -a option:

<code>redis-cli -h 192.168.1.100 -p 6380 -a yourpassword</code>
Copy after login

Keep in mind that for remote connections, you may need to configure your network settings and Redis server to allow remote access. This might involve modifying your Redis configuration file (redis.conf) to bind to the remote host's IP address and possibly adjusting firewall settings to allow incoming connections on the Redis port.

The above is the detailed content of How do I connect to a Redis server using the redis-cli command-line interface?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template