Tag: redis

  • Redis error currently not able to persist on disk

    Redis error currently not able to persist on disk

    When doing a FLUSHALl on redis server, i get following error message

    [root@server ~]# redis-cli FLUSHALL
    (error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
    [root@server ~]#

    The error was because redis data directory was not properly configured.

    To fix, the error, edit redis config file

    vi /etc/redis/redis.conf
    

    Find

    dir ./
    

    Replace with

    dir /var/lib/redis
    

    Create a directory

    mkdir /var/lib/redis
    

    Make it owned by user redis

    chown redis:redis /var/lib/redis
    

    Restart redis server

    systemctl restart redis
    

    See Redis

  • How to flush Redis cache

    How to flush Redis cache

    To delete everything from redis cache, run the command

    redis-cli FLUSHDB
    

    This will delete all keys from the current DB.

    To delete cache from all DB, run

    redis-cli FLUSHALL
    

    If Redis is on another server or needs password, you can use the following command

    redis-cli -h IP_HERE -p PORT_HERE -a PASSWORD_HERE FLUSHDB
    

    See Redis

  • How to find Redis data directory

    How to find Redis data directory

    To find the data directory in redis, run the command

    redis-cli config get dir
    

    redis data directory

    Redis data directory is specified in the configuration file

    /etc/redis/redis.conf 
    

    The entry is

    dir /var/lib/redis
    

    redis data directory should be owned by user redis

    chown redis:redis /var/lib/redis
    

    Example

    [root@server redis]# cd /var/lib/redis/
    [root@server redis]# ls -la
    total 12
    drwxrwx---   2 redis redis 4096 Dec  2 06:44 .
    drwxr-xr-x. 37 root  root  4096 Nov 20 17:30 ..
    -rw-r--r--   1 redis redis   92 Dec  2 06:44 dump.rdb
    [root@server redis]#
    

    See Redis

  • Install latest Redis on Ubuntu/Debian Server

    Install latest Redis on Ubuntu/Debian Server

    To install the latest version of Redis on Ubuntu/Debian server, run

    curl https://packages.redis.io/gpg | sudo apt-key add -
    echo "deb https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
    sudo apt-get update
    sudo apt-get install redis
    

    Enable redis server to start on boot

    systemctl enable redis-server
    

    Start redis server

    systemctl start redis-server
    

    See Redis

  • redis commander

    redis-commander is a GUI for redis. To install, run

    npm install -g redis-commander
    redis-commander
    

    redis commander

  • Install Redis from Source

    Install Redis from Source

    To install Redis from the source, run

    cd /usr/local/src
    wget http://download.redis.io/redis-stable.tar.gz
    tar xvzf redis-stable.tar.gz
    cd redis-stable
    make
    make install

    Copy config file

    mkdir /etc/redis/
    cp /usr/local/src/redis-stable/redis.conf  /etc/redis/

    Create redis user

    groupadd -r redis
    useradd -r -g redis -s /sbin/nologin -d /var/lib/redis -c "redis Daemons" redis

    Create directory

    mkdir /var/lib/redis
    chown redis:redis /var/lib/redis
    chmod 770 /var/lib/redis

    Set data directory

    vi /etc/redis/redis.conf

    Find

    dir ./

    Replace with

    dir /var/lib/redis

    Create service file

    vi /usr/lib/systemd/system/redis.service

    Add the following content to the file

    [Unit]
    Description=Redis persistent key-value database
    After=syslog.target
    
    [Service]
    ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
    RestartSec=5s
    Restart=on-success
    User=redis
    Group=redis
    
    [Install]
    WantedBy=multi-user.target

    Enable and start Redis

    systemctl enable redis
    systemctl start redis

    To find Redis version, run

    redis-server --version

    To monitor Redis, you can use redis-cli

    Back to redis

  • Install Redis on Plesk Debian Server

    Install Redis on Plesk Debian Server

    To install Redis on Plesk Debian/Ubuntu server, run

    apt install -y redis-server redis-tools
    

    Enable and restart redis-server

    systemctl enable redis-server
    systemctl restart redis-server
    

    Now install PHP Modules

    apt install -y plesk-php70-redis plesk-php71-redis plesk-php72-redis plesk-php73-redis plesk-php74-redis
    

    After installing, you will see redis listed in phpinfo() page.

    Redis PHP Debian

  • Monitor redis with redis-cli

    On Ununtu/Debian, install it with

    apt -y install redis-tools
    

    To get stats, run

    redis-cli --stat
    

    If your redis on differnt sevrer or use non standard port, then use

    redis-cli -h HOSTNAME -p PORT --stat
    

    Example

    root@ip-10-0-0-31:~# redis-cli -h news24redis.cylrbg.0001.use1.cache.amazonaws.com --stat
    ------- data ------ --------------------- load -------------------- - child -
    keys       mem      clients blocked requests            connections          
    9287       20.58M   27      0       179271 (+0)         1208        
    9287       20.62M   27      0       179277 (+6)         1208        
    9288       20.67M   28      0       179377 (+100)       1209        
    9288       20.64M   28      0       179494 (+117)       1209        
    9292       20.71M   28      0       179823 (+329)       1210        
    9292       20.68M   28      0       179969 (+146)       1211        
    9298       20.74M   29      0       180033 (+64)        1212        
    9311       20.67M   28      0       180162 (+129)       1214        
    9314       20.69M   29      0       180198 (+36)        1215        
    9318       20.80M   30      0       180275 (+77)        1216        
    9318       20.68M   30      0       180280 (+5)         1216        
    9318       20.82M   31      0       180307 (+27)        1218        
    9319       20.74M   31      0       180321 (+14)        1218        
    9322       20.70M   29      0       180350 (+29)        1219        
    9324       20.80M   30      0       180377 (+27)        1220        
    9366       20.90M   30      0       180724 (+347)       1221        
    9368       20.77M   29      0       181086 (+362)       1221        
    9368       20.73M   27      0       181133 (+47)        1221        
    9372       20.73M   27      0       181487 (+354)       1222        
    9380       20.73M   27      0       181592 (+105)       1223        
    ------- data ------ --------------------- load -------------------- - child -
    keys       mem      clients blocked requests            connections          
    9384       20.76M   28      0       181639 (+47)        1224        
    9391       20.77M   28      0       181718 (+79)        1224        
    9391       20.79M   27      0       181731 (+13)        1224        
    9391       20.67M   25      0       181771 (+40)        1224        
    9391       20.67M   25      0       181780 (+9)         1224        
    9391       20.67M   25      0       181849 (+69)        1224        
    ^C
    root@ip-10-0-0-31:~# 
    

    INFO

    redis-cli INFO
    

    If you want to see info about clients, run

    redis-cli INFO clients
    

    Monitor

    To see what redis is doing, run

    redis-cli monitor
    

    If your redis is running on remote server, then use

    redis-cli -h REMOTE_SERVER_IP monitor
    
  • Install Redis PHP module in Ubuntu 16.04

    To install redis php module in Ubuntu 16.04, run

    apt -y install php-redis
    

    If you are using Apache, restart Apache.

    systemctl restart apache2
    

    If php-fpm restart

    systemctl restart php7.0-fpm
    

    You will see following in your phpinfo()

    php redis

  • redis

    Install Redis

    Install latest Redis on Ubuntu/Debian Server
    Install Redis on Plesk Debian Server
    Install Redis from Source

    How Tos

    Installing Redis PHP module CentOS 7
    Connect to redis from PHP
    Monitor redis with redis-cli
    redis commander – Web GUI for Redis
    How to find Redis data directory
    How to flush Redis cache
    Redis error currently not able to persist on disk

    Checking if Redis works

    # telnet 127.0.0.1 6379
    set testkey 10
    get testkey
    

    Example

    root@ip-10-0-0-31:~# telnet news24redis.cylrbg.0001.use1.cache.amazonaws.com 6379
    Trying 10.0.0.223...
    Connected to news24redis.cylrbg.0001.use1.cache.amazonaws.com.
    Escape character is '^]'.
    set testkey 10
    +OK
    get testkey
    $2
    10
    quit
    +OK
    Connection closed by foreign host.
    root@ip-10-0-0-31:~# 
    

    Count Number of Keys

    To count number of keys, use

    DBSIZE
    

    Example

    root@ip-10-0-0-31:~# telnet news24redis.cylrbg.0001.use1.cache.amazonaws.com 6379
    Trying 10.0.0.223...
    Connected to news24redis.cylrbg.0001.use1.cache.amazonaws.com.
    Escape character is '^]'.
    DBSIZE
    :4114
    

    Here DBSIZE returned 4114, that means you have that much key/value stored in redis.

    Flush ALl

    To flush all keys, use

    redis-cli flushall
    
  • Install Redis PHP Module from source

    To install Redis PHP module from source, run

    cd /usr/local/src
    git clone https://github.com/phpredis/phpredis.git
    cd /usr/local/src/phpredis
    /usr/serverok/php-7.1.12/bin/phpize
    ./configure --with-php-config=/usr/serverok/php-7.1.12/bin/php-config
    make && make install
    

    Once installed, edit your php.ini file, add following below extension_dir line

    extension=redis.so
    

    Verify redis is installed with php -m

    [root@vps154294 ~]# /usr/serverok/php-7.1.12/bin/php -m | grep redis
    redis
    [root@vps154294 ~]# 
    

    Now restart web server

    service httpd restart
    

    phpinfo() will show following

    phpinfo redis

  • Installing Redis PHP module CentOS 7

    Lets search for available redis packages

    [root@vps154294 smartfst]# yum search redis
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * epel: mirror.us.leaseweb.net
     * remi-safe: repo1.ash.innoscale.net
    ============================================================================================ N/S matched: redis ============================================================================================
    collectd-redis.x86_64 : Redis plugin for collectd
    collectd-write_redis.x86_64 : Redis output plugin for collectd
    hiredis.x86_64 : Minimalistic C client library for Redis
    hiredis-devel.x86_64 : Development files for hiredis
    hiredis-last.x86_64 : Minimalistic C client library for Redis
    opensips-redis.x86_64 : Redis connector
    pcp-pmda-redis.x86_64 : Performance Co-Pilot (PCP) metrics for Redis
    perl-Apache-Session-Redis.noarch : Redis driver for Apache::Session::NoSQL
    perl-Redis.noarch : Perl binding for Redis database
    php-nrk-Predis.noarch : PHP client library for Redis
    php-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
    php-phpiredis.x86_64 : Client extension for Redis
    php54-php-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
    php54-php-phpiredis.x86_64 : Client extension for Redis
    php55-php-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
    php55-php-phpiredis.x86_64 : Client extension for Redis
    php56-php-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
    php56-php-phpiredis.x86_64 : Client extension for Redis
    php70-php-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
    php70-php-phpiredis.x86_64 : Client extension for Redis
    php71-php-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
    php71-php-phpiredis.x86_64 : Client extension for Redis
    php72-php-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
    php72-php-phpiredis.x86_64 : Client extension for Redis
    python-redis.noarch : Python 2 interface to the Redis key-value store
    python-trollius-redis.noarch : Redis client for the Python event loop PEP3156 for Trollius.
    python2-django-redis.noarch : Full featured redis cache backend for Django
    redis-trib.noarch : Cluster management script for Redis
    rubygem-redis.noarch : A Ruby client library for Redis
    rubygem-redis-doc.noarch : Documentation for rubygem-redis
    syslog-ng-redis.x86_64 : redis support for syslog-ng
    uwsgi-logger-redis.x86_64 : uWSGI - redislog logger plugin
    uwsgi-router-redis.x86_64 : uWSGI - Plugin for Redis router support
    redis.x86_64 : A persistent key-value database
    
      Name and summary matches only, use "search all" for everything.
    [root@vps154294 smartfst]# 
    

    In this server, we have PHP 7.0 installed. So we need to install packages for PHP 7.0

    Here are the two packages for PHP 7.0

    php70-php-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
    php70-php-phpiredis.x86_64 : Client extension for Redis
    

    Install it with yum

    yum install -y php70-php-pecl-redis.x86_64 php70-php-phpiredis.x86_64
    

    Now you need to restart web server, generally restarting apache will do. In this cause PHP is running as php-fpm under nginx, so we need to restart php-fpm with

    systemctl restart php-fpm