- Docker Redis Connection Refused
- Redis Cli Connection Refused
- Redis Connection Refused Mac Os
- Redis Show Connections
Secure Shell (SSH) is a key WordPress development tool. It grants advanced users access to key platforms and software that make coding and other tasks easier, faster, and more organized.
Accept connections on the specified port, default is 6379. If port 0 is specified Redis will not listen on a TCP socket. Port 6379 If you want you can bind a single interface, if the bind option is not. Randomly get 'connection refused' when trying to use redis-cli command, help? I've noticed my application crashing and it appears to be pointing towards my redis-server. I'm attempting to debug, but can't find anything in the logs to detail why I keep getting my connections refused at a seemingly random times. The thing is that redis-server is binded to listed by default on localhost. So, find redis.conf and find then bind setting (bind 127.0.0.1 - its something like this). Replace the ip address with the private ip address of the VM where redis is running. Then, restart the redis server. This issue can be marked as resolved.
So if you attempt to use SSH only to see a “Connection refused” error, you may start to feel concerned. However, this is a common issue, and it’s entirely possible to fix it on your own with just a bit of troubleshooting. You’ll be back to running commands in no time flat.
In this post, we’ll discuss what SSH is and when to use it. Then we’ll explain some common reasons your connection may be refused, including in PuTTY. Finally, we’ll provide some troubleshooting tips.
Let’s dive in!
What Is SSH and When Should I Use It?
Secure Shell (SSH), also sometimes called Secure Socket Shell, is a protocol for securely accessing your site’s server over an unsecured network. In other words, it’s a way to safely log in to your server remotely using your preferred command-line interface:
Using SSH to remotely access a WordPress site hosted on Kinsta
Unlike File Transfer Protocol (FTP), which only enables you to upload, delete, and edit files on your server, SSH can accomplish a wide range of tasks. For instance, if an error locks you out of your WordPress site, you can use SSH to access it remotely.
This protocol also enables you to use several key developer tools, including:
- WP-CLI. The WordPress command line. You can use it for a variety of tasks, including new installations, bulk plugin updates, and media file imports.
- Composer. A PHP package manager. It enables you to implement several frameworks for use in your site’s code by pulling the necessary libraries and dependencies.
- Git. A version control system used to track changes in code. This is especially useful for teams of developers working together on a single project.
- npm. A JavaScript package manager. It includes a command-line and JavaScript software registry. Note: Kinsta customers will need an Enterprise plan in order to access this feature.
It’s important to note that using SSH is an advanced skill. Generally speaking, lay users of WordPress should contact their developers or hosting providers for help, rather than trying to resolve issues with SSH themselves.
A simple mistyped command could ruin your website, so it’s vital that you know what you’re doing.
Why Is My SSH Connection Refused? (5 Reasons for Connectivity Errors)
Unfortunately, there are many scenarios that could occur while you’re trying to connect to your server via SSH, which might result in an error reading “Connection refused”.
Below are some of the most common issues that might be causing problems for you.
1. Your SSH Service Is Down
In order to connect to your server with SSH, it must be running an SSH daemon – a program that runs in the background to listen for and accept connections.
If this service is down, you will not be able to successfully connect to your server and may receive a Connection refused error:
Connection Refused error in Terminal
Your server’s SSH daemon may be down for a wide variety of reasons, including unexpected traffic spikes, resource outages, or even a Distributed Denial of Service (DDoS) attack. In addition to the troubleshooting steps we’ll mention below, you may want to contact your hosting provider to determine the root cause of the issue.
If you suspect that your SSH service might be down, you can run this command to find out:
Docker Redis Connection Refused
If the command line returns a status of down, then you’ve likely found the reason behind your connectivity error.
Kinsta is developer-friendly and offers SSH access on all of its hosting plans.
2. You Have the Wrong Credentials
Although it may seem too simple to be true, it’s possible that you’re just entering the wrong credentials when trying to connect to your server. There are four pieces of information needed to run SSH:
- Host name. The IP address of the server you’re trying to connect to or your domain name.
- Username. Your (S)FTP username.
- Password. Your (S)FTP password.
- Port. The default port is 22. However, some hosting providers (including Kinsta) change their SSH port number for security reasons. If this is the case, you should be able to find it by logging in to your MyKinsta dashboard.
You can also check to see which port is being used for SSH by running this command:
The command line should return the correct port.
Check to make sure you’re entering the right credentials and taking into account the possibility of typos or entering the wrong IP address or port.
3. The Port You’re Trying to Use Is Closed
A “port” is simply the endpoint to which you’re directed when connecting to your server. In addition to making sure you have the correct one, you’ll also want to check to see if the port you’re trying to use is open.
Any open port is a security vulnerability, as hackers can try to exploit it and gain access to the server. For this reason, unused ports are often closed to prevent attacks.
In the event that port 22, or the custom SSH port for your server, has been closed, you will likely see a Connection refused error. You can see all the ports listening on your server by running this command:
This command should return a list of ports with the LISTEN state. Ideally, you want to see port 22 or your server’s custom SSH port listed here. If it’s not, you’ll need to reopen the port in order to connect to your server.
4. SSH Isn’t Installed on Your Server
As we briefly mentioned earlier, servers use SSH daemons to listen for and accept connections. Therefore, if the server you’re trying to connect to doesn’t have one installed, you won’t be able to access it using SSH.
Generally speaking, almost all hosting providers will have SSH daemons installed on their servers by default. This particular issue is more common on localhost or dedicated servers.
5. Firewall Settings Are Preventing an SSH Connection
Since open ports present a security risk, firewalls installed to protect servers from hackers sometimes block connections to them. Unfortunately, this means that even harmless users who are trying to SSH into their servers may receive a Connection refused error as a result of firewall settings.
If your setup appears to be in order and you still can’t connect, take a look at your firewall’s rules. You can display them in your command-line interface with the following commands:
Your results will vary, but you’ll want to look for these elements to determine if your firewall is blocking SSH connections:
Need a blazing-fast, secure, and developer-friendly hosting? Kinsta is built with WordPress developers in mind and provides plenty of tools and a powerful dashboard. Check out our plans
- dport 22: This refers to the destination port, which for SSH is usually port 22 (reminder: Kinsta doesn’t use this port number).
- REJECT: This would indicate that connections are being refused from the specified destination.
- DROP: Like REJECT, this means that connections to the relevant port are being blocked.
If you search the results of the commands above for dport 22, you should be able to determine if your firewall is preventing an SSH connection. If so, you’ll have to change the rules to accept requests.
Why Does PuTTY Say Connection Refused?
PuTTY is an SSH client. If you’re familiar with FTP, this platform is the FileZilla equivalent to SSH on Windows machines. In other words, PuTTY enables users to input their credentials and launch an SSH connection:
If you’re a PuTTY user and see the Connection refused error, the cause is likely one of those listed above.
This is an SSH connectivity error like any other, and the troubleshooting tips below should work whether you’re using PuTTY, Terminal, or any other program for connecting to your server with SSH.
We’ve taken our knowledge of effective website management at scale, and turned it into an ebook and video course. Click here to download the The 2020 Guide to Managing 40+ WordPress Sites!
How Do I Troubleshoot SSH Connectivity Errors?
When you’re experiencing an SSH connectivity error, there are a few steps you can take to troubleshoot it depending on the cause. Here are some tips for troubleshooting the reasons for a Connection refused error that we covered above:
- If your SSH service is down. Contact your hosting provider to see why your SSH service isn’t running. For localhost or dedicated servers, you can use the command
sudo service ssh restart
to try to get it running again. - If you entered the wrong credentials. Once you’ve double-checked the SSH port using the
grep Port /etc/ssh/sshd_config
command, try connecting again with the correct details. - If your SSH port is closed. This is usually a side effect of one of the two reasons listed below. Either install an SSH daemon on the server you want to connect to or change your firewall rules to accept connections to your SSH port.
- If SSH isn’t installed on your server. Install an SSH tool such as OpenSSH on the server you want to connect to using the
sudo apt install openssh-server
command. - If your firewall is blocking your SSH connection. Disable the firewall rules blocking your SSH connection by changing the destination port’s settings to ACCEPT.
If you’re attempting to connect to your hosting provider’s server, it may be wiser to contact support than to try troubleshooting the problem yourself. Users on localhost or dedicated servers may be able to find further support on more advanced forums if none of the above solutions works.
Summary
Being able to connect to your server with SSH is convenient in a wide range of situations. It can enable you to access your site when you’re locked out of your WordPress dashboard, run commands via WP-CLI, track changes in your site’s code with Git, and more.
Although there are several causes that could be behind your SSH connectivity error, these are a few of the most common:
- Your SSH service is down.
- You have the wrong credentials.
- The port you’re trying to use is closed.
- SSH isn’t installed on your server.
- Firewall settings are preventing an SSH connection.
If you enjoyed this tutorial, then you’ll love our support. All Kinsta’s hosting plans include 24/7 support from our veteran WordPress developers and engineers. Chat with the same team that backs our Fortune 500 clients. Check out our plans
You’ve heard that Redis has an embedded scripting language, but haven’t given ita try yet? Here’s a tour of what you need to understand to use thepower of Lua with your Redis server.
Hello, Lua!
Our first Redis Lua script just returns a value without actually interacting withRedis in any meaningful way:
This is as simple as it gets. The first line sets up a local variable with ourmessage, and the second line returns that value from the Redis server to theclient. Save this file locally as hello.lua
and run it like so:
Connectivity Problems?
This redis-cli
example assumes that you're running a Redisserver locally. If you're working with a remote server like RedisGreen, you'llneed to specify host and port information. Find the Connect button onyour RedisGreen dashboard to quickly copy the login info for your server.
See also: Could not connectto Redis at 127.0.0.1:6379: Connection refused.
Running this will print “Hello, world!”. The first argument ofEVAL
is the complete lua script — here we’re using the cat
command to read the script from a file. The second argument is the number of Rediskeys that the script will access. Our simple “Hello World” script doesn’taccess any keys, so we use 0
.
Accessing Keys and Arguments
Suppose we’re building a URL-shortener. Each time a URL comes in we want to storeit and return a unique number that can be usedto access the URL later.
We’ll use a Lua script to get a unique ID from Redis using INCR
andimmediately store the URL in a hash that is keyed by the unique ID:
We’re accessing Redis for the first time here, using the call()
function.call()
’s arguments are the commands to send to Redis: first we INCR <key>
,then we HSET <key> <field> <value>
. These two commands will run sequentially— Redis won’t do anything else while this script executes, and it will runextremely quickly.
We’re accessing two Lua tables, KEYS
and ARGV
. Tables are associativearrays, and Lua’s only mechanism for structuring data. For ourpurposes you can think of them as the equivalent of an array in whateverlanguage you’re most comfortable with, but note these two Lua-isms that trip upfolks new to the language:
Tables are one-based, that is, indexing starts at 1. So the first element in
mytable
ismytable[1]
, the second ismytable[2]
, etc.Tables cannot hold nil values. If an operation would yield a table of
[ 1,nil, 3, 4 ]
, the result will instead be[ 1 ]
— the table istruncated at the first nil value.
When we invoke this script, we need to also pass along the values for the KEYS
and ARGV
tables. In the raw Redis protocol, the command looks like this:
When calling EVAL
, after the script we provide 2
as the number ofKEYS
that will be accessed, then we list our KEYS
, and finally we providevalues for ARGV
.
Normally when we build apps with Redis Lua scripts, the Redisclient library will take care of specifying the number of keys. The above codeblock is shown for completeness, but here’s the easier way to do this on at thecommand line:
When using --eval
as above, the comma separates KEYS[]
from ARGV[]
items.
Just to make things clear, here’s our original script again, this time withKEYS
and ARGV
expanded:
When writing Lua scripts for Redis, every key that isaccessed should be accessed only by the KEYS
table. The ARGV
table is used for parameter-passing — here it’s the value ofthe URL we want to store.
Conditional Logic: increx and hincrex
Our example above saves the link for our URL-shortener, but we also need totrack the number of times a URL has been accessed. To do that we’ll keep acounter in a hash in Redis. When a user comes along with a link identifier,we’ll check to see if it exists, and increment our counter for it if it does:
Each time someone clicks on a shortlink, we run this script to track that thelink was shared again. We invoke the script using EVAL
and pass inlinks:visits
for our single key and the link identifier returned from ourprevious script as the single argument.
Redis Cli Connection Refused
The script would look almost the same without hashes. Here’s ascript which increments a standard Redis key only if it exists:
SCRIPT LOAD and EVALSHA
Remember that when Redis is running a Lua script, it will not run anything else.The best scripts simply extend the existing Redis vocabulary of small atomic dataoperations with the smallest bit of logic necessary. Bugs in Lua scripts can lockup a Redis server altogether — best to keep things short and easy to debug.
Even though they’re usually quite short, we need not specify the full Lua scripteach time we want to run one. In a real application you’ll instead register eachof your Lua scripts with Redis when your application boots (or when you deploy),then call the scripts later by their unique SHA-1 identifier.
An explicit call to SCRIPT LOAD
is usually unnecessary in a live applicationsince EVAL
implicitly loads the script that is passed to it. An applicationcan attempt to EVALSHA
optimistically and fall back to EVAL
only if the scriptis not found.
If you’re a Ruby programmer, take a look at Shopify’s Wolverine,which simplifies the loading and storing of Lua scripts for Ruby apps. For PHPprogrammers, Predis supports adding Luascripts to be called just as though they were normal Redis commands. If youuse these or other tools to standardize your interaction with Lua, let me know— I’d be interested to find out what else is out there.
When to use Lua?
Redis support for Lua overlaps somewhat with WATCH
/MULTI
/EXEC
blocks, which group operations so they are executed together. So how do youchoose to use one over the other? Each operation in a MULTI
block needs to beindependent, but with Lua, later operations candepend on the results of earlier operations. Using Lua scripts can also avoidrace conditions that can starve slow clients when WATCH
is used.
From what we’ve seen at RedisGreen, most apps that use Lua will also useMULTI/EXEC, but not vice versa. Most successful Lua scripts are tiny, and justimplement a single feature that your app needs but isn’t a part of the Redisvocabulary.
Visiting the Library
The Redis Lua interpreter loads seven libraries: base, table,string,math,debug,cjson,and cmsgpack. The firstseveral are standard libraries that allow you to do the basic operations you’dexpect from any language. The last two let Redis understand JSON and MessagePack— this is an extremely useful feature, and I keep wondering why I don’tsee it used more often.
Redis Connection Refused Mac Os
Web apps with public APIs tend to have JSON lying around all over. So maybe youhave a bunch of JSON blobs stored in normal Redis keys and you want to accesssome particular values inside of them, as though you had stored them as a hash.With Redis JSON support, that’s easy:
Here we check to see if the key exists and quickly return nil if not. Then weget the JSON value out of Redis, parse it with cjson.decode()
, and return therequested value.
Loading this script into your Redis server lets you treat JSON values stored inRedis as though they were hashes. If your objects are reasonably small, this isactually quite fast, even though we have to parse the value on each access.
If you’re working on an internal API for a system that demands performance,you’re likely to choose MessagePack over JSON, as it’s smaller and faster.Luckily with Redis (as in most places), MessagePack is pretty much a drop-inreplacement for JSON:
Crunching Numbers
Lua and Redis have different type systems, so it’s important to understandhow values may change when crossing the Redis-Lua border. When a number comes fromLua back to a Redis client, it becomes aninteger — any digits past the decimal point are dropped:
Redis Show Connections
When you run this script, Redis will return an integer of 3 — you lose theinteresting pieces of pi. Seems simple enough, but things get a bit more trickywhen you start interacting with Redis in the middle of the script. An example:
The resulting value here is astring: '3.2'
Why? Redis doesn’t have a dedicated numeric type. When we first SET
the value, Redis saves it as a string, losing all record of the fact that Lua initially thought of the value as a float. When we pull the value out later, it’s still a string.
Values in Redis that are accessed with GET
/SET
should be thought of as strings exceptwhen numeric operations like INCR
and DECR
are run against them. Thesespecial numeric operations will actually return integer replies (andmanipulate the stored value according to mathematical rules), but the“type” of the value stored in Redis is still a string value.
Gotchas: A Summary
These are the most common errors that we see when working with Lua in Redis:
Tables are one-based in Lua, unlike most popular languages. Thefirst element in the KEYS table is
KEYS[1]
, the second isKEYS[2]
,etc.A nil value terminates a table in Lua. So
[ 1, 2, nil, 3 ]
willautomatically become[1, 2]
. Don’t use nil values in tables.redis.call
will raise exception-style Lua errors, whileredis.pcall
will automatically trap any errors and return them astables that can be inspected.Lua numbers are converted to integers when being sent to Redis — everythingpast the decimal point is lost. Convert any floating point numbers to stringsbefore returning them.
Be sure to specify all the keys you use in your Lua scripts in the
KEYS
table, otherwise your scripts will probably break in future versions ofRedis.Lua scripts are just like any other operation in Redis: nothing else runswhile they’re being executed. Think ofscripts as a way to expand the vocabulary of the Redis server — keep themshort and to-the-point.
Further Reading
There are lots of great resources for Lua and Redis online — here are a few Iuse: