CentOS6.4 – installation configuration redis

First, the environment

CentOS6.4×64 minimize system installation

redis-m 192.168.1.13

redis-s 192.168.1.14

Second, install redis

Download software

[Root @ redis-m ~] # wget http://download.redis.io/releases/redis-2.8.9.tar.gz

Installation
[Root @ redis-m ~] # tar xf redis-2.8.9.tar.gz
[Root @ redis-m ~] # cd redis-2.8.9
[Root @ redis-m redis-2.8.9] # make
[Root @ redis-m redis-2.8.9] # make PREFIX = / usr / local / redis-2.8.9 install
[Root @ redis-m redis-2.8.9] # ln -s /usr/local/redis-2.8.9/ / usr / local / redis

# The default file
[Root @ redis-m ~] # ll / usr / local / redis / bin
total 13912
-rwxr-xr-x 1 root root 4172184 Jul 26 18:08 redis-benchmark #redis performance testing tools
-rwxr-xr-x 1 root root 22177 Jul 26 18:08 redis-check-aof # to update log appendonly.aof check availability
-rwxr-xr-x 1 root root 45411 Jul 26 18:08 redis-check-dump # is used to check the local database rdb file
-rwxr-xr-x 1 root root 4265375 Jul 26 18:08 redis-cli #redis command line tool
-rwxr-xr-x 1 root root 5728711 Jul 26 18:08 daemon launcher redis-server # Server

Start redis
# Add the environment variable
[Root @ redis-m ~] # echo ‘PATH = $ PATH: / usr / local / redis / bin’ >> / etc / profile
[Root @ redis-m ~] # source / etc / profile
[Root @ redis-m ~] # which redis-server
/ Usr / local / redis / bin / redis-server

[Root @ redis-m ~] # redis-server -h
Usage: ./redis-server [/path/to/redis.conf] [options]
./redis-server – (read config from stdin)
./redis-server -v or –version
./redis-server -h or –help
./redis-server –test-memory <megabytes>

Examples:
./redis-server (run the server with default conf)
./redis-server /etc/redis/6379.conf
./redis-server –port 7777
./redis-server –port 7777 –slaveof 127.0.0.1 8888
./redis-server /etc/myredis.conf –loglevel verbose

Sentinel mode:
./redis-server /etc/sentinel.conf –sentinel

# Start the service, because the default is no configuration file, so one of our configuration file from the installation directory cp
[Root @ redis-m ~] # mkdir / usr / local / redis / conf
[Root @ redis-m ~] # cp /root/redis-2.8.9/redis.conf / usr / local / redis / conf /

# Started in the background
[Root @ redis-m ~] # redis-server /usr/local/redis/conf/redis.conf &
[1] 5870
[Root @ redis-m ~] # [5870] 26 Jul 18: 24: 24.771 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.- __ ” -._
_.- . _. ” -._ Redis 2.8.9 (00000000/0) 64 bit
.-.- \ / _., _ '' -._
(',.-
|,) Running in stand alone mode
. |
-._ -...- __…- -._ | ‘ _.-' | Port: 6379
|
-._ ._ / _.- '| PID: 5870
-._ -._ -. / _.- ‘_.-‘
| -._ -._ -.__.- '_.-'_.-' |
|
-._ -._ _.-'_.- '| Http://redis.io
-._ -._ -.__.-‘_.- ‘_.-‘
| -._ -._ -.__.- '_.-'_.-' |
|
-._ -._ _.-'_.- '|
-._ -._ -.__.-‘_.- ‘_.-‘
-._ -.__.- ‘_.-‘
-._ _.- '
-.__.- ‘

[5870] 26 Jul 18: 24: 24.774 # Server started, Redis version 2.8.9
[5870] 26 Jul 18: 24:!. 24.774 # WARNING overcommit_memory is set to 0 Background save may fail under low memory condition To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory = 1’ for this to take effect.
[5870] 26 Jul 18: 24: 24.774 * The server is now ready to accept connections on port 6379

[Root @ redis-m ~] # netstat -tunlp | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5870 / redis-server *
tcp 0 0 ::: 6379 ::: * LISTEN 5870 / redis-server *

# Resolve warning warning
[Root @ redis-m ~] # sysctl vm.overcommit_memory = 1
vm.overcommit_memory = 1
[Root @ redis-m ~] # echo “vm.overcommit_memory = 1” >> / etc / sysctl.conf
[Root @ redis-m ~] # sysctl -p

# Close redis Service
[Root @ redis-m ~] # redis-cli shutdown
[5870] 26 Jul 18: 32: 13.908 # User requested shutdown …
[5870] 26 Jul 18: 32: 13.908 * Saving the final RDB snapshot before exiting.
[5870] 26 Jul 18: 32: 13.926 * DB saved on disk
[5870] 26 Jul 18: 32: 13.926 # Redis is now ready to exit, bye bye …
[1] + Done redis-server /usr/local/redis/conf/redis.conf
[Root @ redis-m ~] # netstat -tunlp | grep redis

redis startup script
[Root @ redis-m ~] # cat redis.sh
#! / Bin / bash
# Chkconfig: 2345 50 30
#
# Description: Redis service
#
#Script: Redis command

Redisserver = / usr / local / redis / bin / redis-server
Rediscli = / usr / local / redis / bin / redis-cli
Redisconf = / usr / local / redis / conf / redis.conf

function_start ()
{
printf “start redis-server …”
$ Redisserver $ Redisconf &> / dev / null &
if [$ -eq 0?]; then
echo “runing”
fi
}

function_stop ()
{
printf “stop redis-server …”
$ Rediscli -p 6379 shutdown
if [$ -eq 0?]; then
echo “stop”
fi
}

function_restart ()
{
function_start
function_stop
}

function_kill ()
{
killall redis-server
}

function_status ()
{
a = ps -A | grep" redis-server \> "-c
if [$ a -ge 1]; then
echo -e “The Redis is [\ e [0; 32; 5m runing \ e [0m]”
else
echo -e “The Redis is [\ e [0; 31; 5m not run \ e [0m]”
fi
}

case “$ 1” in
start)
function_start
;;
stop)
function_stop
;;
restart)
function_stop
function_start
;;
kill)
function_kill
;;
status)
function_status
;;
*)
echo “Usage: /etc/init.d/redis {start | stop | restart | kill | status}”

esac

exit

Third, the operation redis
[Root @ redis-m ~] # redis-cli
# Add a value set key value
127.0.0.1:6379> set id 001
OK
127.0.0.1:6379> get id
“001”
127.0.0.1:6379> del id
(Integer) 1 # returns 1, indicating that the operation was successful
127.0.0.1:6379> get id
(Nil)
127.0.0.1:6379> exists id # determine whether there is a key
(Integer) 0 # 0 indicates there is no return

# Switch databases, default there are 16 libraries are numbered from 0-15, where we switched to the 1st library
127.0.0.1:6379> select 1
OK
127.0.0.1:6379 [1]> keys *
(Empty list or set)
127.0.0.1:6379 [1]> set name lyao
OK
127.0.0.1:6379 [1]> get name
“Lyao”

# Cut back 0 Number library
127.0.0.1:6379 [1]> select 0
OK
127.0.0.1:6379> get name # The key have only library in the 1st
(Nil)

# Other connection methods
[Root @ redis-m ~] # redis-cli -h 192.168.1.13 -p 6379
192.168.1.13:6379>
-h # Specify the host address
-p # specify the port number

#redis Help
[Root @ redis-m ~] # redis-cli
127.0.0.1:6379> help
redis-cli 2.8.9
Type: “help @ <group>” to get a list of commands in <group>
“Help <command>” for help on <command>
“Help <tab>” to get a list of possible help topics
“Quit” to exit
127.0.0.1:6379> help set # query with the help of a command

SET key value [EX seconds] [PX milliseconds] [NX | XX]
summary: Set the string value of a key
since: 1.0.0
group: string

# Query class of command
127.0.0.1:6379> helpstring

APPEND key value
summary: Append a value to a key
since: 2.0.0

BITCOUNT key [start] [end]
summary: Count set bits in a string
since: 2.6.0

BITOP operation destkey key [key …]
summary: Perform bitwise operations between strings
since: 2.6.0

DECR key
summary: Decrement the integer value of a key by one
since: 1.0.0

DECRBY key decrement
summary: Decrement the integer value of a key by the given number
since: 1.0.0

GET key
summary: Get the value of a key
since: 1.0.0

GETBIT key offset
summary: Returns the bit value at offset in the string value stored at key
since: 2.2.0

GETRANGE key start end
summary: Get a substring of the string stored at a key
since: 2.4.0

GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0

INCR key
summary: Increment the integer value of a key by one
since: 1.0.0

INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0

INCRBYFLOAT key increment
summary: Increment the float value of a key by the given amount
since: 2.6.0

MGET key [key …]
summary: Get the values ​​of all the given keys
since: 1.0.0

MSET key value [key value …]
summary: Set multiple keys to multiple values
since: 1.0.1

MSETNX key value [key value …]
summary: Set multiple keys to multiple values, only if none of the keys exist
since: 1.0.1

PSETEX key milliseconds value
summary: Set the value and expiration in milliseconds of a key
since: 2.6.0

SET key value [EX seconds] [PX milliseconds] [NX | XX]
summary: Set the string value of a key
since: 1.0.0

SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0

SETEX key seconds value
summary: Set the value and expiration of a key
since: 2.0.0

SETNX key value
summary: Set the value of a key, only if the key does not exist
since: 1.0.0

SETRANGE key offset value
summary: Overwrite part of a string at key starting at the specified offset
since: 2.2.0

STRLEN key
summary: Get the length of the value stored in a key
since: 2.2.0

Four, redis Permissions

redis client set up an external connection password
[Root @ redis-m ~] # vim /usr/local/redis/conf/redis.conf
requirepass lyao36843 # instructions about 326 lines per second can be the number of password attempts 150k

# Restart redis Service
[Root @ redis-m ~] # redis-cli shutdown
[5892] 26 Jul 19: 49: 03.756 # User requested shutdown …
[5892] 26 Jul 19: 49: 03.756 * Saving the final RDB snapshot before exiting.
[5892] 26 Jul 19: 49: 03.789 * DB saved on disk
[5892] 26 Jul 19: 49: 03.789 # Redis is now ready to exit, bye bye …
[1] + Done redis-server /usr/local/redis/conf/redis.conf
[Root @ redis-m ~] # redis-server /usr/local/redis/conf/redis.conf &
[Root @ redis-m ~] # redis-cli
127.0.0.1:6379> set k v
(Error) NOAUTH Authentication required. # Prompt enough authority

# After setting a password before performing command operations, the need for certification auth
127.0.0.1:6379> auth lyao36843
OK
127.0.0.1:6379> set k v
OK
127.0.0.1:6379> get k
“V” # normal increase in value

# Another connection
[Root @ redis-m ~] # redis-cli -a lyao36843
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
“V1”

Five, redis data type

Data types are string, list, set, these types of hash

string type
127.0.0.1:6379> set mykey “my binary safe value”
OK
127.0.0.1:6379> get mykey
“My binary safe value”

# To count
127.0.0.1:6379> set counter 1
OK
127.0.0.1:6379> incr counter # increment
(Integer) 2
127.0.0.1:6379> incr counter
(Integer) 3
127.0.0.1:6379> incr counter
(Integer) 4
127.0.0.1:6379> decr counter # decrement
(Integer) 3
127.0.0.1:6379> decr counter
(Integer) 2

#getset Usage
127.0.0.1:6379> set name lyao
OK
127.0.0.1:6379> getset name lyao36843 # get the name of the first value, and then updates the value of the name lyao36843
“Lyao”
127.0.0.1:6379> get name
“Lyao36843”

# Batch operation
127.0.0.1:6379> mset name1 tom age 26 sex male
OK
127.0.0.1:6379> mget name1 age sex
1) “tom”
2) “26”
3) “male”

# To the value of the additional content
127.0.0.1:6379> get name1
“Tom”
127.0.0.1:6379> append name1 “teacher”
(Integer) 10
127.0.0.1:6379> get name1
“Tomteacher”

Six, redis multiple instances
[Root @ redis-m ~] # mkdir -p / data / 6380 / data
[Root @ redis-m ~] # mkdir -p / data / 6381 / data
[Root @ redis-m ~] # cp /usr/local/redis/conf/redis.conf / data / 6380
[Root @ redis-m ~] # cp /usr/local/redis/conf/redis.conf / data / 6381

# Edit profiles 6380
[Root @ redis-m ~] # vim /data/6380/redis.conf
dir / data / 6380 / data # modify data directory dump file
port 6380 # modify the port number
pidfile /data/6380/redis.pid # modify pid file path

#Edit 6381 profile
[Root @ redis-m ~] # vim /data/6381/redis.conf
pidfile /data/6381/redis.pid
port 6381
dir / data / 6381 / data

# Start multiple instances
[Root @ redis-m ~] # redis-server /data/6380/redis.conf &
[Root @ redis-m ~] # redis-server /data/6381/redis.conf &

# Display port
[Root @ redis-m ~] # netstat -tunlp | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5956 / redis-server *
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 6156 / redis-server *
tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 6161 / redis-server *
tcp 0 0 ::: 6379 ::: * LISTEN 5956 / redis-server *
tcp 0 0 ::: 6380 ::: * LISTEN 6156 / redis-server *
tcp 0 0 ::: 6381 ::: * LISTEN 6161 / redis-server *
# From the results above we can see, these three redis 6379,6380,6381 instance running up

Examples # Connect to 6380
[Root @ redis-m ~] # redis-cli -p 6380
127.0.0.1:6380> auth lyao36843
OK
127.0.0.1:6380> set name k1
OK
127.0.0.1:6380> get name
“K1”
127.0.0.1:6380> quit

Examples # Connect to 6381
[Root @ redis-m ~] # redis-cli -p 6381
127.0.0.1:6381> auth lyao36843
OK
127.0.0.1:6381> set web httpd
OK
127.0.0.1:6381> get web
“Httpd”
127.0.0.1:6381> quit