【數(shù)據(jù)庫】Redis安裝部署

一、Redis簡介

Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。
Redis通常運行在Linux/Unix/macOS下,對于Windows操作系統(tǒng),微軟開源技術(shù)小組曾經(jīng)維護了一個Windows的Redis發(fā)行版,但是由于種種原因,停留在了比較舊的版本。

二、下載和安裝

1.1準(zhǔn)備工作

Redis的安裝運行方式有兩種:

1.二進制可執(zhí)行文件,大部分操作系統(tǒng)都會在其軟件倉庫中預(yù)置Redis的二進制可執(zhí)行文件,但版本迭代具有延后性。

2.源碼編譯安裝,源碼執(zhí)行安裝是比較常用的安裝方式,需要在編譯環(huán)境中安裝GCC編譯器和LIBC。

Redis官方地址:https://redis.io 上可以獲取Redis及基本的安裝步驟。

1.2操作步驟
1.1.1 安裝編譯工具
$ sudo yum install gcc
1.1.2 獲取redis(https://redis.io/
$ cd ~
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
1.1.3 解壓Redis
$ tar -zxvf redis-5.0.5.tar.gz
1.1.4 編譯依賴項
$ cd ~/redis-5.0.5/deps
$ make hiredis lua jemalloc linenoise
1.1.5 編譯Redis
$ cd ~/redis-5.0.5
$ make
1.1.6 安裝Redis
$ make PREFIX=/usr/local/redis install

至此redis完成安裝,路徑/usr/local/redis,該目錄下僅有bin目錄,bin目錄中會有一些可執(zhí)行文件

文件名 描述 備注
redis-server Redis服務(wù)端
redis-sentinel Redis Sentinel redis-server的軟鏈接
redis-cli Redis命令行工具
redis-check-rdb Redis RDB檢查工具
redis-check-aof Redis Append Only Files(AOF)檢查工具
redis-benchmark Redis基準(zhǔn)/性能測試工具

此外還需手動從源目錄拷貝redis配置文件到安裝目錄上

$ mkdir /usr/local/redis/conf
$ cp ~/redis-5.0.5/redis.conf /usr/local/redis/conf
1.1.7 添加全局環(huán)境變量
$ sudo vim /etc/profile
#在文本內(nèi)容后面追加內(nèi)容:
export PATH=/usr/local/redis/bin:$PATH
#刷新環(huán)境變量:
sudo source /etc/profile

三、啟動和停止Redis

前面我們已經(jīng)將redis的運行目錄(即bin)添加到了系統(tǒng)的環(huán)境變量里面,因此我們可以直接通過執(zhí)行redis命令來完成相關(guān)操作,而無需到對應(yīng)目錄去執(zhí)行。

1.1使用默認(rèn)配置啟動一個redis實例
$ redis-server
31391:C 13 Jul 2019 12:43:23.515 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31391:C 13 Jul 2019 12:43:23.515 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=31391, just started
31391:C 13 Jul 2019 12:43:23.515 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
31391:M 13 Jul 2019 12:43:23.515 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 31391
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

31391:M 13 Jul 2019 12:43:23.518 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
31391:M 13 Jul 2019 12:43:23.518 # Server initialized
31391:M 13 Jul 2019 12:43:23.518 # 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.
31391:M 13 Jul 2019 12:43:23.518 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
31391:M 13 Jul 2019 12:43:23.518 * Ready to accept connections
^C31391:signal-handler (1562993057) Received SIGINT scheduling shutdown...
31391:M 13 Jul 2019 12:44:17.681 # User requested shutdown...
31391:M 13 Jul 2019 12:44:17.681 * Saving the final RDB snapshot before exiting.
31391:M 13 Jul 2019 12:44:17.682 * DB saved on disk
31391:M 13 Jul 2019 12:44:17.682 # Redis is now ready to exit, bye bye...
1.2 若要根據(jù)指定配置文件運行,可以在啟動服務(wù)命令后追加配置文件
$ redis-server /usr/local/redis/conf/redis.conf

默認(rèn)配置中,redis服務(wù)運行在前置窗口,關(guān)閉窗口redis服務(wù)便關(guān)閉,若要以守護進程的方式在后臺運行,可以編輯配置文件并將daemonize參數(shù)設(shè)為yes并使用該配置文件啟動

$ vim /usr/local/redis/conf/redis.conf
daemonize yes
$ redis-server /usr/local/redis/conf/redis.conf
1.3 停止Redis服務(wù)

停止redis服務(wù)可以通過Kill+PID:

$ kill `pidof redis-server`

也可以通過redis-cli調(diào)用shutdown命令:

$ redis-cli shutdown
1.4 使用redis-cli連接到Redis

通過redis-cli命令連接到redis服務(wù)

$ redis-cli -h <hostname> -p <port> -a <password>
127.0.0.1:6379>

我們可以發(fā)送一些簡單的命令進行測試:

#1.設(shè)置兩個字符鍵值對:
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
#2.獲取剛剛設(shè)置的值:
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> get key2
"value2"

四、獲取服務(wù)器信息

通過redis-cli的INFO命令可以獲得當(dāng)前Redis實例最全面和重要的信息。

1.1 使用redis-cli連接到一個Redis實例,然后執(zhí)行INFO(不區(qū)分大小寫)命令:
[root@zhan ~]# redis-cli
127.0.0.1:6379> INFO

單實例和集群的INFO信息不一樣,當(dāng)前可得單實例以下INFO信息(附描述):

# Server 關(guān)于Redis服務(wù)器的基本信息
redis_version:5.0.5         #redis服務(wù)版本
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:a0c26467f84225b8
redis_mode:standalone       #redis運行模式(獨立,哨兵或集群)
os:Linux 3.10.0-957.el7.x86_64 x86_64   #托管Redis服務(wù)器的操作系統(tǒng)
arch_bits:64                #架構(gòu)(32位或64位)
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:71140            #服務(wù)器進程的PID
run_id:e818f36b08cef33ac2679118b1ea593777f961d4
tcp_port:6379               #TCP/IP偵聽端口
uptime_in_seconds:1267      #Redis服務(wù)器啟動后的秒數(shù)
uptime_in_days:0            #以天表示的相同值
hz:10                       #服務(wù)器的頻率設(shè)置
configured_hz:10
lru_clock:2795106           #時鐘遞增每分鐘,用于LRU管理
executable:/root/redis-server           #服務(wù)器可執(zhí)行文件的路徑
config_file:/usr/local/redis/conf/redis.conf #配置文件的路徑

# Clients 客戶端連接的狀態(tài)和指標(biāo)
connected_clients:1         #客戶端連接數(shù)(不包括來自副本的連接)
client_recent_max_input_buffer:4        #當(dāng)前客戶端連接中最長的輸出列表
client_recent_max_output_buffer:0       #當(dāng)前客戶端連接中最大的輸入緩沖區(qū)
blocked_clients:0           #阻塞呼叫中待處理的客戶端數(shù)量

# Memory 大致的內(nèi)存消耗指標(biāo)
used_memory:854576          #Redis使用其分配器分配的總字節(jié)數(shù)
used_memory_human:834.55K   #以前值的人類可讀表示
used_memory_rss:17223680    
used_memory_rss_human:16.43M 
used_memory_peak:4954216     #Redis消耗的峰值內(nèi)存
used_memory_peak_human:4.72M #上一值的人類可讀表示
used_memory_peak_perc:17.25% 
used_memory_overhead:841174
used_memory_startup:791368   #Redis在啟動時消耗的初始內(nèi)存量
used_memory_dataset:13402
used_memory_dataset_perc:21.20%
allocator_allocated:1427904
allocator_active:1806336
allocator_resident:16568320
total_system_memory:1907970048  #Redis主機具有的內(nèi)存總量
total_system_memory_human:1.78G #上一值的人類可讀表示
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配置指令的值
maxmemory_human:0B          #上一值的人類可讀表示
maxmemory_policy:noeviction 
allocator_frag_ratio:1.27
allocator_frag_bytes:378432
allocator_rss_ratio:9.17
allocator_rss_bytes:14761984
rss_overhead_ratio:1.04
rss_overhead_bytes:655360
mem_fragmentation_ratio:21.20
mem_fragmentation_bytes:16411104
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0 #內(nèi)存分配器,在編譯時選擇
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence 數(shù)據(jù)持久化相關(guān)的狀態(tài)和指標(biāo)
loading:0   #指示轉(zhuǎn)儲文件的加載是否正在進行的標(biāo)志
rdb_changes_since_last_save:0 #自上次轉(zhuǎn)儲以來的更改數(shù)
rdb_bgsave_in_progress:0      #表示RDB保存的標(biāo)志正在進行中
rdb_last_save_time:1563075828 #上次成功RDB保存的時間戳
rdb_last_bgsave_status:ok     #最后一次RDB保存操作的狀態(tài)
rdb_last_bgsave_time_sec:0    #最后一次RDB保存操作的持續(xù)時間
rdb_current_bgsave_time_sec:-1 #正在進行的RDB保存操作的持續(xù)時間
rdb_last_cow_size:4300800     #上次RBD保存操作期間寫時復(fù)制分配的字節(jié)大小
aof_enabled:0   #表示AOF記錄的標(biāo)志已激活
aof_rewrite_in_progress:0     #表示AOF重寫操作的標(biāo)志正在進行中
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1  #最后一次AOF重寫操作的持續(xù)時間
aof_current_rewrite_time_sec:-1 #正在進行的AOF重寫操作的持續(xù)時間
aof_last_bgrewrite_status:ok  #最后一次AOF重寫操作的狀態(tài)
aof_last_write_status:ok      #最后一次寫入操作到AOF的狀態(tài)
aof_last_cow_size:0           

# Stats 總體統(tǒng)計數(shù)據(jù)
total_connections_received:3  #服務(wù)器接受的連接總數(shù)
total_commands_processed:9    #服務(wù)器處理的命令總數(shù)
instantaneous_ops_per_sec:0   #每秒處理的命令數(shù)
total_net_input_bytes:209     #從網(wǎng)絡(luò)讀取的總字節(jié)數(shù)
total_net_output_bytes:41083  #寫入網(wǎng)絡(luò)的總字節(jié)數(shù)
instantaneous_input_kbps:0.00 #網(wǎng)絡(luò)的每秒讀取速率,以KB /秒為單位
instantaneous_output_kbps:0.00 #網(wǎng)絡(luò)的每秒寫入速率,以KB /秒為單位
rejected_connections:0        #由于maxclients限制而拒絕的連接數(shù)
sync_full:0                   #具有副本的完整重新同步的數(shù)量
sync_partial_ok:0             #已接受的部分重新同步請求的數(shù)量
sync_partial_err:0            #拒絕部分重新同步請求的數(shù)量
expired_keys:0                #密鑰到期事件的總數(shù)
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0                #由于maxmemory限制而被驅(qū)逐的密鑰數(shù)量
keyspace_hits:2
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:175659
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

# Replication 主從復(fù)制相關(guān)的狀態(tài)和指標(biāo)
role:master                 #如果是單實例的即"master"
connected_slaves:0          #聯(lián)機的從redis實例數(shù)量
master_replid:acfd45747c106739c8f4f28ee3108d71acf023aa
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 CPU使用情況
used_cpu_sys:2.455878        #Redis服務(wù)器消耗的系統(tǒng)CPU
used_cpu_user:1.653499       #Redis服務(wù)器消耗的用戶CPU
used_cpu_sys_children:0.208633  #后臺進程占用的系統(tǒng)CPU
used_cpu_user_children:0.001086 #后臺進程占用的用戶CPU

# Cluster Redis Cluster的狀態(tài)
cluster_enabled:0

# Keyspace 數(shù)據(jù)庫相關(guān)的統(tǒng)計數(shù)據(jù)
db0:keys=2,expires=0,avg_ttl=0

【未完待續(xù).....】

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容