Redis 설치 및 명령어 정리

1. 1.Mac OS Redis

Redis 설치하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$brew install redis

Warning: Treating redis as a formula. For the cask, use homebrew/cask/redis
==> Downloading https://homebrew.bintray.com/bottles/redis-6.0.9.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/673b1485f012e3c9a509c913d175e18451ddab50eca0
######################################################################## 100.0%
==> Pouring redis-6.0.9.catalina.bottle.tar.gz
==> Caveats
To have launchd start redis now and restart at login:
brew services start redis
Or, if you don't want/need a background service you can just run:
redis-server /usr/local/etc/redis.conf
==> Summary
🍺 /usr/local/Cellar/redis/6.0.9: 13 files, 3.9MB

다음과 같이 명령어를 진행하면 Redis설치가 진행됩니다. 아직 Homebrew가 없으신분들은 반드시 설치를 하셔야합니다.

Redis가 설치된 경로

Redis의 실행파일은 다음과 같은 경로에 저장됩니다.

1
/usr/local/bin/redis-server

Redis 설정파일은 다음과 같은 경로에 저장됩니다.

1
/usr/local/etc/redis.conf

2. 2. Redis 실행하기

이제 brew를 통하여 Redis를 성공적으로 설치를 하였으니 Redis서버를 실행하여봅시다.

1
$redis-server

다음과 같은 명령어를 사용하면

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
10529:C 31 Jan 2021 01:08:11.335 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
10529:C 31 Jan 2021 01:08:11.335 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=10529, just started
10529:C 31 Jan 2021 01:08:11.335 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
10529:M 31 Jan 2021 01:08:11.337 * Increased maximum number of open files to 10032 (it was originally set to 256).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: ----
| `-._ `._ / _.-' | PID: ----
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

10529:M 31 Jan 2021 01:08:11.338 # Server initialized
10529:M 31 Jan 2021 01:08:11.338 * Loading RDB produced by version 6.0.9
10529:M 31 Jan 2021 01:08:11.338 * RDB age 64 seconds
10529:M 31 Jan 2021 01:08:11.338 * RDB memory usage when created 0.96 Mb
10529:M 31 Jan 2021 01:08:11.338 * DB loaded from disk: 0.000 seconds
10529:M 31 Jan 2021 01:08:11.338 * Ready to accept connections
redis-cli
^C10529:signal-handler (1612022976) Received SIGINT scheduling shutdown...
10529:M 31 Jan 2021 01:09:36.132 # User requested shutdown...
10529:M 31 Jan 2021 01:09:36.132 * Saving the final RDB snapshot before exiting.
10529:M 31 Jan 2021 01:09:36.135 * DB saved on disk
10529:M 31 Jan 2021 01:09:36.135 # Redis is now ready to exit, bye bye...

다음과같은 하드디스크같은 그림이 뜨면서 Redis가 정상적으로 동작한것을 확인할 수 있습니다.

3. 3. Redis-Cli

이제 다음과 같이 서버가 동작하게 되었으므로, 해당 터미널은 띄워둔채 다른 터미널을 하나 키신후 다음과 같은 명령어를 진행합니다.

1
2
$redis-cli
127.0.0.1:6379>

하나의 콘솔 cli창이 보여지게 되면서 이제 Redis관련 동작을 command로 제어할 수 있게됩니다.

4. 4. Redis Command

redis 키 생성 및 조회

1
2
3
4
5
127.0.0.1:6379> set gwan www.github.com
OK
127.0.0.1:6379>
127.0.0.1:6379> get gwan
"www.github.com"

모든 키값 조회하기

1
2
127.0.0.1:6379> keys *
1) "gwan"

저장된 키값 조회

1
2
$127.0.0.1:6379> keys *gw*
1) "gwan"

키 존재여부 확인

1
2
127.0.0.1:6379[15]> exists gwan
(integer) 0

키 삭제하기

1
2
3
4
127.0.0.1:6379[15]> del gwan
(integer) 0
127.0.0.1:6379[15]> keys *
(empty array)

키 만료시간 지정하기(Second)

1
2
127.0.0.1:6379[15]> expire gwan 100
(integer) 1

키 만료시간 지정하기(Unix Time)

1
2
3
4
127.0.0.1:6379[15]> expireat gwan 100
(integer) 1
127.0.0.1:6379[15]> ttl gwan
(integer) -2

키 만료시간 삭제

1
2
127.0.0.1:6379[15]> persist gwan
(integer) 1

키 만료시간 확인하기

1
2
3
4
127.0.0.1:6379[15]> expire gwan 1000
(integer) 1
127.0.0.1:6379[15]> ttl gwan
(integer) 997

키 타입 조회

1
2
127.0.0.1:6379[15]> type gwan
string

Redis 정보 조회

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
127.0.0.1:6379[15]> info
# Server
redis_version:6.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:26c3229b35eb3beb
redis_mode:standalone
os:Darwin 19.5.0 x86_64
arch_bits:64
multiplexing_api:kqueue
atomicvar_api:atomic-builtin
gcc_version:4.2.1
process_id:10758
run_id:91ff9f67b95c54f394234f6004cffbe07960d2b2
tcp_port:6379
uptime_in_seconds:1719
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:1412238
executable:/Users/kgh/redis-server
config_file:
io_threads_active:0

# Clients
connected_clients:1
client_recent_max_input_buffer:16
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

# Memory
used_memory:1066368
used_memory_human:1.02M
used_memory_rss:901120
used_memory_rss_human:880.00K
used_memory_peak:1123120
used_memory_peak_human:1.07M
used_memory_peak_perc:94.95%
used_memory_overhead:1019192
used_memory_startup:1001536
used_memory_dataset:47176
used_memory_dataset_perc:72.77%
allocator_allocated:1019776
allocator_active:863232
allocator_resident:863232
total_system_memory:8589934592
total_system_memory_human:8.00G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:0.85
allocator_frag_bytes:18446744073709395072
allocator_rss_ratio:1.00
allocator_rss_bytes:0
rss_overhead_ratio:1.04
rss_overhead_bytes:37888
mem_fragmentation_ratio:0.88
mem_fragmentation_bytes:-118656
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:17456
mem_aof_buffer:0
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:3
rdb_bgsave_in_progress:0
rdb_last_save_time:1612023255
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:14
instantaneous_ops_per_sec:0
total_net_input_bytes:369
total_net_output_bytes:18757
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:49
evicted_keys:0
keyspace_hits:1
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_reads_processed:15
total_writes_processed:14
io_threaded_reads_processed:0
io_threaded_writes_processed:0

# Replication
role:master
connected_slaves:0
master_replid:4c355e7154a8a46cccffd4acfbe1e1ed5e5183d9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.767543
used_cpu_user:0.575956
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Modules

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0
db15:keys=1,expires=1,avg_ttl=68029

Redis Database 변경

1
2
3
4
$select 1

select 1~15
해당 번호는 1~15 데이터베이스를 선택하겠다 라는 뜻입니다.

주의할점: select를 사용할때 1~15까지 진행하는 이유는 Redis는 하나의 인스턴스에 최대 16개의 데이터베이스를 가질 수 있습니다.

auth 접속하기

1
2
$redis-cli
127.0.0.1:6379> auth 비밀번호입력

5. Redis 명령어 정리

명령어 사용예시 기능설명
del del ${key} 키 삭제
exists exists ${key} 키 존재여부
expire expire ${key} ${second} 만료시간 지정 (초단위)
expireat expireat $(key} ${unixtime} 만료시간 지정 (유닉스 시간)
get get ${key} 해당 키의 값 출력
info info redis 정보 조회
keys keys ${key_pattern} 패턴에 부합하는 키목록 출력
persist persist ${key} 만료시간을 삭제
set set ${key} ${value} 키와 데이터를 추가
ttl ttl ${key} 만료시간 확인
type type ${key} 해당 키의 타입 확인