ubuntu 14.04 install Redis

About redis

redis is data structure server It’s opensource, Key-value database ,networked, in-memory.
Data types is maps keys to types of values.
Redis supports strings and abstract data types.

timthumb
Install redis on ubuntu 14.04

  1. install redis server
    sudo apt-get install redis-server
  2. you can check progress and check port
    ps -aux|grep redis
    netstat -nlt|grep 6379
  3. check status
    /etc/init.d/redis-server status
    or
    service redis-server status

CLI

documentation: http://redis.io/commands

  1. run cli
    redis-cli
  2. help tip

    help
    Type: “help @” to get a list of commands in
    “help ” for help on
    “help ” to get a list of possible help topics
    “quit” to exit

  3. show all key list

    keys *

  4. add key value

    set key1 “hi cake!”
    OK

  5. show key value

    get key1
    “hi cake!”

  6. add key number

    set key2 1
    OK

  7. Increment number

    INCR key2
    (integer) 2

  8. add list data

    LPUSH key3 jeff
    LPUSH key3 cake
    LPUSH key3 ken

  9. show list data

    LRANGE key3 0 3

  10. add hash tables

    HSET key4 name “golden cake”
    HSET key4 email “jeff@golden-cake.com

  11. get hash tables by key

    HGET key4 name
    “golden cake”

  12. get all hash tables

    HGETALL key4
    1) “name”
    2) “golden cake”
    3) “email”
    4) “jeff@golden-cake.com

  13. create hash tables by more key and value

    HMSET key5 username jeff password 123456 mail jeff@golden-cake.com

  14. show name and mail

    HMGET key5 username mail

  15. show all

    HGETALL key5
    1) “username”
    2) “jeff”
    3) “password”
    4) “123456”
    5) “mail”
    6) “jeff@golden-cake.com

  16. show all key

    keys *
    1) “key2”
    2) “key5”
    3) “key4”
    4) “key1”
    5) “key3”

  17. remove key1 and key5

    del key1 key5

  18. remove all key

    FLUSHALL

set password

  1. vim /etc/redis/redis.conf
  2. enable requirepass [yourpassword]
  3. service redis-server restart
  4. >redis-cli -a [yourpassword]

If you want to remotely access the database

  1. vim /etc/redis/redis.conf
  2. disable bind 127.0.0.1
  3. service redis-server restart
  4. redis-cli -a [yourpassword] -h xxx.xxx.xxx.xxx

How to working on php7-fpm

  1. download phpredis source code

    git clone https://github.com/phpredis/phpredis.git
    cd phpredis

  2. Switch to php7 branch

    git checkout php7

  3. Compile

    phpize
    ./configure
    make && make install
    cd ..
    rm -rf phpredis

  4. create php7 config :vim /etc/php/7.0/fpm/conf.d/30-redis.ini

    extension=redis.so

  5. restart php7-fpm and check phpinfo

    service php7.0-fpm restart

Test connection Redis

documentation: https://github.com/phpredis/phpredis#classes-and-methods

  1. connect
    ”’
    <?php
    \(redis = new Redis();
    \)redis->connect(‘127.0.0.1′, 6379);
    echo “Connection to server sucessfully”;
    echo “Server is running: ” . $redis->ping();
    ”’

Working With Node.js and Redis

documentation: http://redis.js.org/

  1. install node_redis

    npm install redis

  2. connect
    ”’
    var redis = require(“redis”),
    client = redis.createClient();//port and ip
    //client = redis.createClient(‘6379’, ‘127.0.0.1’);//auth
    // client.auth(“passowrd”);

//error
client.on(“error”, function (err) {
console.log(“Error ” + err);
});
”’

分享

發佈留言

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料