CentOS 6.4 설치 설정 redis
계통 CentOS 6.4x 64 최소 화 설치
redis-m 192.168.1.13
설치 redis
소프트웨어 를 다운로드 하 다
[root@redis-m ~]# wget http://download.redis.io/releases/redis-2.8.9.tar.gz
설치 하 다.
[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
#
[root@redis-m ~]# ll /usr/local/redis/bin
total 13912
-rwxr-xr-x 1 root root 4172184 Jul 26 18:08 redis-benchmark #redis
-rwxr-xr-x 1 root root 22177 Jul 26 18:08 redis-check-aof # appendonly.aof ,
-rwxr-xr-x 1 root root 45411 Jul 26 18:08 redis-check-dump # rdb
-rwxr-xr-x 1 root root 4265375 Jul 26 18:08 redis-cli #redis
-rwxr-xr-x 1 root root 5728711 Jul 26 18:08 redis-server # daemon
시작 redis
#
[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
# , , cp
[root@redis-m ~]# mkdir /usr/local/redis/conf
[root@redis-m ~]# cp /root/redis-2.8.9/redis.conf /usr/local/redis/conf/
#
[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 *
# 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
# redis
[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 시작 스 크 립 트
[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
3.조작 redis
[root@redis-m ~]# redis-cli
# 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 # 1,
127.0.0.1:6379> get id
(nil)
127.0.0.1:6379> exists id # key
(integer) 0 # 0
# , 16 , 0-15, 1
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"
# 0
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get name # key 1
(nil)
#
[root@redis-m ~]# redis-cli -h 192.168.1.13 -p 6379
192.168.1.13:6379>
-h #
-p #
#redis
[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 #
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
#
127.0.0.1:6379> help @string
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
4.redis 권한
redis 클 라 이언 트 에 외부 연결 암 호 를 설정 합 니 다.
[root@redis-m ~]# vim /usr/local/redis/conf/redis.conf
requirepass lyao36843 # 326 , 150k
# redis
[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. #
# , , 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" #
#
[root@redis-m ~]# redis-cli -a lyao36843
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
5.redis 데이터 형식
데이터 형식 은 string,list,set,hash 등 몇 가지 가 있 습 니 다.
string 형식
127.0.0.1:6379> set mykey "my binary safe value"
OK
127.0.0.1:6379> get mykey
"my binary safe value"
#
127.0.0.1:6379> set counter 1
OK
127.0.0.1:6379> incr counter #
(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 #
(integer) 3
127.0.0.1:6379> decr counter
(integer) 2
#getset
127.0.0.1:6379> set name lyao
OK
127.0.0.1:6379> getset name lyao36843 # name , name lyao36843
"lyao"
127.0.0.1:6379> get name
"lyao36843"
#
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"
#
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"
6.redis 다 중 인 스 턴 스
[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
# 6380
[root@redis-m ~]# vim /data/6380/redis.conf
dir /data/6380/data # dump
port 6380 #
pidfile /data/6380/redis.pid # pid
# 6381
[root@redis-m ~]# vim /data/6381/redis.conf
pidfile /data/6381/redis.pid
port 6381
dir /data/6381/data
#
[root@redis-m ~]# redis-server /data/6380/redis.conf &
[root@redis-m ~]# redis-server /data/6381/redis.conf &
#
[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 *
# ,6379,6380,6381 3 redis
# 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
# 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
7.주종 설정
주종 동기 화 기본 원 리 는 다음 과 같다.
1.slave 서버 master 서버 에 연결
2.slave 서버 에서 sync 명령 보 내기
3.master.rdb 파일 에 데 이 터 를 백업 합 니 다.
4.master 는.rdb 파일 을 slave 서버 에 전송 합 니 다.
5.slave.rdb 파일 을 데이터베이스 에 가 져 오기
아래 의 마스터 설정 은 redis 인 스 턴 스 2 개 를 사용 하여 진행 합 니 다.
# 6379,6380
[root@redis-m ~]# netstat -tunlp |grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1634/redis-server *
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 1647/redis-server *
tcp 0 0 :::6379 :::* LISTEN 1634/redis-server *
tcp 0 0 :::6380 :::* LISTEN 1647/redis-server *
# 6379 ,6380 , 6380
[root@redis-m ~]# vim /data/6380/redis.conf
slaveof 192.168.1.13 6379 # master IP
masterauth <master-password> # master ,
# 6380 redis
[root@redis-m ~]# redis-cli -p 6380 shutdown
[1647] 28 Jul 14:25:48.209 # User requested shutdown...
[1647] 28 Jul 14:25:48.210 * Saving the final RDB snapshot before exiting.
[1647] 28 Jul 14:25:48.223 * DB saved on disk
[1647] 28 Jul 14:25:48.225 # Redis is now ready to exit, bye bye...
[root@redis-m ~]# netstat -tunlp |grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 1634/redis-server *
tcp 0 0 :::6379 :::* LISTEN 1634/redis-server *
[root@redis-m ~]# redis-server /data/6380/redis.conf &
[2] 1676
[root@redis-m ~]# [1676] 28 Jul 14:26:40.304 * 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: 6380
| `-._ `._ / _.-' | PID: 1676
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[1676] 28 Jul 14:26:40.307 # Server started, Redis version 2.8.9
[1676] 28 Jul 14:26:40.308 * DB loaded from disk: 0.000 seconds
[1676] 28 Jul 14:26:40.308 * The server is now ready to accept connections on port 6380
[1676] 28 Jul 14:26:41.308 * Connecting to MASTER 192.168.1.13:6379
[1676] 28 Jul 14:26:41.308 * MASTER <-> SLAVE sync started
[1676] 28 Jul 14:26:41.308 * Non blocking connect for SYNC fired the event.
[1676] 28 Jul 14:26:41.308 * Master replied to PING, replication can continue...
[1676] 28 Jul 14:26:41.308 * Partial resynchronization not possible (no cached master)
[1634] 28 Jul 14:26:41.308 * Slave asks for synchronization
[1634] 28 Jul 14:26:41.308 * Full resync requested by slave.
[1634] 28 Jul 14:26:41.308 * Starting BGSAVE for SYNC
[1676] 28 Jul 14:26:41.310 * Full resync from master: 5e26f07d111a362449cd1fecfc4d720019e66339:1
[1634] 28 Jul 14:26:41.318 * Background saving started by pid 1679
[1679] 28 Jul 14:26:41.349 * DB saved on disk
[1679] 28 Jul 14:26:41.350 * RDB: 6 MB of memory used by copy-on-write
[1634] 28 Jul 14:26:41.367 * Background saving terminated with success
[1634] 28 Jul 14:26:41.368 * Synchronization with slave succeeded
#
[1676] 28 Jul 14:26:41.368 * MASTER <-> SLAVE sync: receiving 150 bytes from master
[1676] 28 Jul 14:26:41.368 * MASTER <-> SLAVE sync: Flushing old data
[1676] 28 Jul 14:26:41.368 * MASTER <-> SLAVE sync: Loading DB in memory
[1676] 28 Jul 14:26:41.368 * MASTER <-> SLAVE sync: Finished with success
# redis
[root@redis-m ~]# redis-cli -p 6379
127.0.0.1:6379> set docker kvm # key docker, kvm
OK
127.0.0.1:6379> get docker
"kvm"
# redis
[root@redis-m ~]# redis-cli -p 6380
127.0.0.1:6380> get docker # ,
"kvm"
# redis slave
127.0.0.1:6380> set ldap lyao
(error) READONLY You can't write against a read only slave. # slave
# slave replication
[root@redis-m ~]# redis-cli -p 6380 info replication
# Replication
role:slave
master_host:192.168.1.13
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:2004
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# master replication
[root@redis-m ~]# redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.13,port=6380,state=online,offset=2074,lag=1
master_repl_offset:2074
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:2073
8.redis 설정 key 만 료 시간
[root@redis-m ~]# redis-cli -p 6379
127.0.0.1:6379> keys *
1) "age"
2) "counter"
3) "sex"
4) "mykey"
5) "k1"
6) "name"
7) "docker"
8) "name1"
9) "anme1"
10) "k"
127.0.0.1:6379> flushdb #
OK
127.0.0.1:6379> keys *
(empty list or set)
# key name, lyao
127.0.0.1:6379> set name lyao
OK
127.0.0.1:6379> ttl name
(integer) -1 #-1
127.0.0.1:6379> expire name 10 # key 10s
(integer) 1
127.0.0.1:6379> ttl name
(integer) 8
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name #key
(nil)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.