To connect to a Redis server using the <code>redis-cli</code> command-line interface, you can follow these simple steps:
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>
This command will attempt to connect to 127.0.0.1
at port 6379
.
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.
Redis supports authentication to secure your Redis instance. When connecting with <code>redis-cli</code>, you have a couple of options to authenticate:
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>
This method is convenient but less secure as the password is visible in the command history and process lists.
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>
After entering this command, if the authentication is successful, Redis will respond with OK
.
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>
Then, connect using:
<code>redis-cli</code>
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.
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>
For example, if your Redis server is running on port 6380, you would use:
<code>redis-cli -p 6380</code>
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>
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.
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>
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>
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>
If authentication is required, you can add the -a
option:
<code>redis-cli -h 192.168.1.100 -p 6380 -a yourpassword</code>
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!