If you are a developer on a Windows machine and you want to access a Redis Server while you build your application, you have several options; one of them is to use the Redis port on Windows project by MSOpenTech, download the latest binaries and have your Redis Server running on Windows. Another option is to have a Linux VM running on your machine and connect to it whenever you need it.
Now you have one more option, with the Windows 10 Anniversary update and Bash on Windows. You can have it installed on Bash on Ubuntu on Windows and access it as if it were installed locally on your machine.
[box type=”note” width=”100%” ]If you haven’t setup Bash on Windows 10 you can read about it here.[/box]Install Redis on Bash on Windows
Open the Start Menu and search for “Bash”:
Then install the Redis Server using apt-get:
sudo apt-get install redis-server
After installing it, hit `redis-server` and Redis Server will start:
By default, the Redis Server accepts connections in port `6379`, so, in order to connect from your Windows machine, you can gain access using`localhost:6379` url.
Connecting using Redis Desktop Manager
NodeJS app connecting to Redis Server
If you want to connect to Redis with NodeJS you need to start Redis Server as a service, otherwise you will get an error like the following:
Error: Redis connection to 127.0.0.1:6379 failed – connect ECONNREFUSED 127.0.0.1:6379 at Object.exports._errnoException (util.js:1012:11) at exports._exceptionWithHostPort (util.js:1035:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
In order to do so, you need to execute the following command on Bash:
$ sudo service redis-server start
Then you need to create a very simple node app so that you can properly connect to Redis Server.
Step 1. Initialize node app
Start a new project by creating a folder redis-test
and in command prompt
execute:
npm init
to initialize node project, and then:
npm install redis --save
to install the Redis library, and update package.json
and the app’s dependencies.
Step 2. Code snippet to test connection
Create a file app.js
with the following code to test the Redis connection:
// app.js var redis = require('redis'); var client = redis.createClient(); client.on('connect', function() { console.log('connected'); }); client.set('framework', 'AngularJS');
Run your app with the following command:
node app.js
The response will be connected
and there will be a “framework” key with value “AngularJS” in your Redis Server.
Hi, Nice post.
I’m getting this working just like you describe, but now I’m trying to connect on Redis with Nodejs and I’m getting connection refused error.
I have node running on windows CMD and Redis running on Bash, is it possible to get both working together this way or I have to run Nodejs on Bash too?
Tks.
Currently you are not able to connect with redis-client to Bash from Windows using `localhost` as `localhost` is translated to `127.0.01`.
I created a very simple app to test it out and got the same error:
Error: Redis connection to 127.0.0.1:6379 failed – connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
This is happening if you start redis server with
But!
If you start redis-server as a service you are able to connect without any issues.
So start redis-server with the following command:
Hi 🙂
I’ve started Redis with “sudo service redis-server start” and yet even in the same bash window I can’t connect to Redis.
I’m running Windows 10 Home (1709, 16299.309), “uname -r => 4.4.0-43-Microsoft”, redis 3.2.11.
I’ve tried to add TCP rule inbound/outbound to port 6379 on firewall but with no success though.
Is ther anything else that I could do to get it working?
Appreciate any help. Thanks a lot!
Have you tried connecting using `127.0.0.1`? What error do you get?