Redis is an open source key-value store. It is highly scalable and can be used by multiple processes or applications simultaneously.
How to change the Redis password?
You can modify the Redis password by editing the requirepass directive in the /opt/bitnami/redis/etc/redis.conf file. Replace the NEWPASSWORD placeholder with the value of your new password:
Then restart the Redis server:
$ sudo /opt/bitnami/ctlscript.sh restart redis
(Optional) If you don't want to restart the Redis service, appart from changing the configuration file, you can execute the following command to change the password at runtime. Replace the CURRENTPASSWORD and NEWPASSWORD placeholders with the values of the current password and your new password:
$ redis-cli -a CURRENTPASSWORD CONFIG set requirepass NEWPASSWORD
How to change Redis persistence mode?
You can change the modes to adapt Redis persistence to your needs. There are multiple possibilities to do so. Find below how to enable AOF as an example:
Edit the configuration file /opt/bitnami/redis/etc/redis.conf. Change the appendonly configuration directive from no to yes:
Edit the Redis configuration file /opt/bitnami/redis/etc/redis.conf. Change the appendonly configuration directive from no to yes as shown below:
This change will apply when you restart the server.
(Optional) To apply the change inmediately, execute the following command. Replace the YOURPASSWORD placeholder with the value of your password:
$ redis-cli -a YOURPASSWORD CONFIG set appendonly yes
For more information on this, please check the Redis official documentation about Redis Persistence.
How to connect to the Redis server?
Connect to Redis from the same computer where it is installed with the redis-cli client tool. Replace the YOURPASSWORD placeholder with the value of your password:
$ redis-cli -a YOURPASSWORD 127.0.0.1:6379>
Once connected, use the Redis get and set commands to save and retrieve keys and their values. Here's an example:
127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> get foo "bar"
How to connect to Redis from a different machine?
IMPORTANT: Making this application's network ports public is a significant security risk. You are strongly advised to only allow access to those ports from trusted networks. If, for development purposes, you need to access from outside of a trusted network, please do not allow access to those ports via a public IP address. Instead, use a secure channel such as a VPN or an SSH tunnel. Follow these instructions to remotely connect safely and reliably.
To connect to Redis from a different machine, you must open port 6379 for remote access. Refer to the FAQ for more information on this.
You can now connect to Redis using a command like the one below. Replace the YOURPASSWORD placeholder with the value of your password:
$ redis-cli -h SERVER-IP -a YOURPASSWORD
How to create a Redis cluster?
From Redis 3.x, it is possible to create a cluster of Redis nodes in master-slave mode. The minimum configuration required is a 6-node cluster with 3 masters and 3 slaves. This section will introduce you to creating a simple redis cluster. For further information, please refer to http://redis.io/topics/cluster-tutorial and http://redis.io/topics/cluster-spec.
Ruby must be installed with the Redis gem. Install these components with the commands below:
$ sudo apt-get install ruby $ sudo gem install redis
$ sudo yum install ruby $ sudo yum install redis
Use the following template and place it in your desired location.
Create the following folder structure:
$ mkdir -p my-cluster $ cd my-cluster $ mkdir PORT-1 PORT-2 PORT-3 PORT-4 PORT-5 PORT-6
In every PORT-* folder, create a configuration file. Name it whatever you wish (eg. PORT-x.conf) and fill it with this content:
port PORT-x cluster-enabled yes cluster-config-file nodes-PORT-x.conf cluster-node-timeout 5000 appendonly yes
Start a Redis server (node) in each port. This will open the normal port for client connection and the cluster-bus port (10000 + PORT-x), used between nodes to communicate.
$ cd PORT-x $ ../redis-server ./PORT-x.conf
Execute the cluster manager (redis-trib.rb) located in the /opt/bitnami/redis/bin directory:
$ ./redis-trib.rb create --replicas 1 127.0.0.1:PORT1 127.0.0.1:PORT-2 127.0.0.1:PORT-3 127.0.0.1:PORT-4 127.0.0.1:PORT-5 127.0.0.1:PORT-6
Connect to each node in the cluster as usual.
Redis comes with a tool named create-cluster, located at /opt/bitnami/scripts/create-cluster. This allows you to avoid the manual configuration described above.
By default, this utility will create 6 nodes with 1 replica and will start creating nodes on port 30000. In order to not modify the utility, is recommended to create a config.sh script in the same folder as create-cluster with the following content:
PORT=STARTING-PORT-NUMBER TIMEOUT=2000 NODES=6 REPLICAS=1
Start the node and create the cluster:
$ ./create-cluster start $ ./create-cluster create
Stop the cluster:
$ ./create-cluster stop
Clean up the folder:
$ ./create-cluster clean
How to load a module to Redis?
You can extend your Redis solution using the Redis modules. Thus, you can implement new features and add new functionalities to your application.
Follow the instructions below to load a module to Redis:
|IMPORTANT: Before follow the steps below, you must compile the source code of the module. Check the Redis Modules official documentation for more information on this.|
Once you have the compiled module library (module.so), the recommended way to load the module is during the startup of Redis by adding the following configuration directive to the configuration file /opt/bitnami/redis/etc/redis.conf:
The configuration will take effect in the next time you restart Redis.
(Optional) To load the module at runtime (with no downtime), execute the following command. Replace the YOURPASSWORD placeholder with the value of your password:
$ redis-cli -a YOURPASSWORD MODULE LOAD /path/to/the/module.so
For more ways to load modules, please check the oficial documentation Redis Modules: Loading modules.
In order to improve the security of this component, we have renamed the commands CONFIG, FLUSHDB and FLUSHALL to empty strings. This is done to avoid potentially harmful actions with those commands. To modify this behavior, edit the Redis configuration file redis.conf.
What is the default configuration?
By default, the Redis server is configured to run on the default port 6379. You can connect to the server locally or remotely using the redis-cli command line tool Replace the YOURPASSWORD placeholder with the value of your password.
$ redis-cli -h SERVER-IP -a YOURPASSWORD
The Redis server is configured following the security guidelines from the official Redis documentation. Although you use a password to connect to the server, it is strongly recommended to change your firewall policies to only accept connections from the IP address that you are using to connect to the Redis server.
In order to check which Redis version your machine is running, execute the following command from the console:
$ redis-server -v
Redis configuration file
The Redis configuration file is located at /opt/bitnami/redis/etc/redis.conf.
The default port on which Redis listens is 6379.
Redis log file
The Redis log file is created at /opt/bitnami/redis/var/log/redis-server.log.
How to test Redis with a client?
|NOTE: To use the example script below, you must have PHP installed on the server. You can check this by executing php -v. If you don't have it available, you can easily install PHP on Debian with the command sudo apt-get install php5 or on CentOS with the command sudo yum install php.|
Begin by extracting the contents of the downloaded archive and creating a simple script named example.php.
$ cd predis-1.0 $ nano example.php
The script begins by including the class autoloader file and instantiating an object of the class:
require 'autoload.php'; $client = new Predis\Client(array( 'host' => '127.0.0.1', 'port' => 6379, 'password' => 'PASSWORD' ));
Notice that it configures the client object by defining the Redis server host, port and password. Replace these values with actual values for your server.
You can now use the object's set() and get() methods to add or remove values from the cache. In this example, the set() method stores the value 'cowabunga' using the key 'foo'. The key can then be used with the get() method to retrieve the original value whenever needed.
$client->set('foo', 'cowabunga'); $response = $client->get('foo');
Here's the complete code for the example.php script:
<?php require 'autoload.php'; $client = new Predis\Client(array( 'host' => '127.0.0.1', 'port' => 6379, 'password' => 'PASSWORD' )); $client->set('foo', 'cowabunga'); $response = $client->get('foo'); echo $response; ?>
Save the file and run it.
$ php example.php
The script will connect to your Redis server, save the value to the key 'foo', then retrieve and display it.