Redis單例項安裝

小陈运维發表於2024-06-16

Redis單例項安裝

Redis(Remote Dictionary Server)是一個開源的記憶體資料庫,遵守 BSD 協議,它提供了一個高效能的鍵值(key-value)儲存系統,常用於快取、訊息佇列、會話儲存等應用場景。

  • 效能極高:Redis 以其極高的效能而著稱,能夠支援每秒數十萬次的讀寫操作24。這使得Redis成為處理高併發請求的理想選擇,尤其是在需要快速響應的場景中,如快取、會話管理、排行榜等。
  • 豐富的資料型別:Redis 不僅支援基本的鍵值儲存,還提供了豐富的資料型別,包括字串、列表、集合、雜湊表、有序集合等。這些資料型別為開發者提供了靈活的資料操作能力,使得Redis可以適應各種不同的應用場景。
  • 原子性操作:Redis 的所有操作都是原子性的,這意味著操作要麼完全執行,要麼完全不執行。這種特性對於確保資料的一致性和完整性至關重要,尤其是在高併發環境下處理事務時。
  • 持久化:Redis 支援資料的持久化,可以將記憶體中的資料儲存到磁碟中,以便在系統重啟後恢復資料。這為 Redis 提供了資料安全性,確保資料不會因為系統故障而丟失。
  • 支援釋出/訂閱模式:Redis 內建了釋出/訂閱模式(Pub/Sub),允許客戶端之間透過訊息傳遞進行通訊。這使得 Redis 可以作為訊息佇列和實時資料傳輸的平臺。
  • 單執行緒模型:儘管 Redis 是單執行緒的,但它透過高效的事件驅動模型來處理併發請求,確保了高效能和低延遲。單執行緒模型也簡化了併發控制的複雜性。
  • 主從複製:Redis 支援主從複製,可以透過從節點來備份資料或分擔讀請求,提高資料的可用性和系統的伸縮性。
  • 應用場景廣泛:Redis 被廣泛應用於各種場景,包括但不限於快取系統、會話儲存、排行榜、實時分析、地理空間資料索引等。
  • 社群支援:Redis 擁有一個活躍的開發者社群,提供了大量的文件、教程和第三方庫,這為開發者提供了強大的支援和豐富的資源。
  • 跨平臺相容性:Redis 可以在多種作業系統上執行,包括 Linux、macOS 和 Windows,這使得它能夠在不同的技術棧中靈活部署。

安裝編譯環境

# ubuntu
apt install make gcc
# centos
yum install make gcc

安裝 Redis

# 檢視 Redis 版本
http://download.redis.io/releases/

# 下載 Redis
wget http://download.redis.io/releases/redis-7.2.5.tar.gz

# 解壓
tar xvf redis-7.2.5.tar.gz 
cd redis-7.2.5/

# 進行編譯
make && make install

配置服務

cat << EOF > /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --supervised systemd
ExecStop=/usr/local/redis/redis-shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

配置停止指令碼

mkdir /usr/local/redis
vim /usr/local/redis/redis-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=redis
fi
# Get the proper config file based on service name
CONFIG_FILE="/usr/local/redis/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
    PORT=${PORT:-6379}
else
    PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
        $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
        $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi

授權啟動服務

chmod +x /usr/local/redis/redis-shutdown
useradd -s /sbin/nologin redis

cp /root/redis-7.2.5/redis.conf /usr/local/redis/ && chown -R redis:redis /usr/local/redis
mkdir -p /usr/local/redis/data && chown -R redis:redis /usr/local/redis/data

修改配置

bind 0.0.0.0 -::1                # 監聽ip,多個ip用空格分隔
daemonize yes               # 允許後臺啟動
logfile "/usr/local/redis/redis.log"        # 日誌路徑
dir /usr/local/redis/data                   # 資料庫備份檔案存放目錄
requirepass 123123              # 設定連線密碼
appendonly yes                  # 在/usr/local/redis/data目錄生成appendonly.aof檔案,將每一次寫操作請求都追加到appendonly.aof 檔案中

修改linux核心引數

# 臨時生效
sysctl  -w  vm.overcommit_memory=1
# 永久生效
echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf && sysctl -p
### 可選值:0,1,2。
# 0,:表示核心將檢查是否有足夠的可用記憶體供應用程序使用;如果有足夠的可用記憶體,記憶體申請允許;否則,記憶體申請失敗,並把錯誤返回給應用程序。
# 1:表示核心允許分配所有的實體記憶體,而不管當前的記憶體狀態如何。
# 2: 表示核心允許分配超過所有實體記憶體和交換空間總和的記憶體。

啟動 Redis

systemctl daemon-reload
systemctl enable redis
systemctl start redis
systemctl status redis

檢視叢集

# 互動式
redis-cli -h 192.168.1.21 -a 123123
192.168.1.21:6379> info replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:9d6563f8b2cf7300bc82890838b877eceae2d8bf
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
192.168.1.21:6379> 


# 互動式
redis-cli -h 192.168.1.21
192.168.1.21:6379> 
192.168.1.21:6379> info replication
NOAUTH Authentication required.
192.168.1.21:6379> 
192.168.1.21:6379> 
192.168.1.21:6379> auth 123123
OK
192.168.1.21:6379> 
192.168.1.21:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:9d6563f8b2cf7300bc82890838b877eceae2d8bf
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
192.168.1.21:6379> 
192.168.1.21:6379>

# 非互動式
redis-cli -h 192.168.1.21 -a 123123 info replication

壓測

root@cby:~# redis-benchmark -t set,get -n 100000 -a 123123 -h 192.168.1.21
====== SET ======                                                     
  100000 requests completed in 0.85 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1
  host configuration "save": 3600 1 300 100 60 10000
  host configuration "appendonly": yes
  multi-thread: no

Latency by percentile distribution:
0.000% <= 0.095 milliseconds (cumulative count 13)
50.000% <= 0.287 milliseconds (cumulative count 52749)
75.000% <= 0.343 milliseconds (cumulative count 77482)
87.500% <= 0.367 milliseconds (cumulative count 88051)
93.750% <= 0.383 milliseconds (cumulative count 94598)
96.875% <= 0.399 milliseconds (cumulative count 97691)
98.438% <= 0.407 milliseconds (cumulative count 98450)
99.219% <= 0.423 milliseconds (cumulative count 99272)
99.609% <= 0.455 milliseconds (cumulative count 99612)
99.805% <= 0.599 milliseconds (cumulative count 99816)
99.902% <= 0.911 milliseconds (cumulative count 99903)
99.951% <= 1.039 milliseconds (cumulative count 99952)
99.976% <= 1.303 milliseconds (cumulative count 99977)
99.988% <= 1.343 milliseconds (cumulative count 99988)
99.994% <= 1.367 milliseconds (cumulative count 99995)
99.997% <= 1.375 milliseconds (cumulative count 99997)
99.998% <= 1.383 milliseconds (cumulative count 99999)
99.999% <= 1.391 milliseconds (cumulative count 100000)
100.000% <= 1.391 milliseconds (cumulative count 100000)

Cumulative distribution of latencies:
0.016% <= 0.103 milliseconds (cumulative count 16)
13.574% <= 0.207 milliseconds (cumulative count 13574)
59.956% <= 0.303 milliseconds (cumulative count 59956)
98.450% <= 0.407 milliseconds (cumulative count 98450)
99.708% <= 0.503 milliseconds (cumulative count 99708)
99.825% <= 0.607 milliseconds (cumulative count 99825)
99.868% <= 0.703 milliseconds (cumulative count 99868)
99.877% <= 0.807 milliseconds (cumulative count 99877)
99.899% <= 0.903 milliseconds (cumulative count 99899)
99.938% <= 1.007 milliseconds (cumulative count 99938)
99.966% <= 1.103 milliseconds (cumulative count 99966)
99.967% <= 1.207 milliseconds (cumulative count 99967)
99.977% <= 1.303 milliseconds (cumulative count 99977)
100.000% <= 1.407 milliseconds (cumulative count 100000)

Summary:
  throughput summary: 117508.81 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.285     0.088     0.287     0.391     0.423     1.391
====== GET ======                                                     
  100000 requests completed in 0.80 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1
  host configuration "save": 3600 1 300 100 60 10000
  host configuration "appendonly": yes
  multi-thread: no

Latency by percentile distribution:
0.000% <= 0.039 milliseconds (cumulative count 1)
50.000% <= 0.255 milliseconds (cumulative count 51084)
75.000% <= 0.311 milliseconds (cumulative count 76787)
87.500% <= 0.343 milliseconds (cumulative count 90043)
93.750% <= 0.359 milliseconds (cumulative count 95251)
96.875% <= 0.375 milliseconds (cumulative count 97337)
98.438% <= 0.391 milliseconds (cumulative count 98520)
99.219% <= 0.415 milliseconds (cumulative count 99259)
99.609% <= 0.519 milliseconds (cumulative count 99611)
99.805% <= 0.639 milliseconds (cumulative count 99808)
99.902% <= 0.911 milliseconds (cumulative count 99903)
99.951% <= 1.895 milliseconds (cumulative count 99952)
99.976% <= 1.991 milliseconds (cumulative count 99977)
99.988% <= 2.031 milliseconds (cumulative count 99988)
99.994% <= 2.055 milliseconds (cumulative count 99994)
99.997% <= 2.071 milliseconds (cumulative count 99998)
99.998% <= 2.079 milliseconds (cumulative count 100000)
100.000% <= 2.079 milliseconds (cumulative count 100000)

Cumulative distribution of latencies:
0.052% <= 0.103 milliseconds (cumulative count 52)
27.094% <= 0.207 milliseconds (cumulative count 27094)
73.309% <= 0.303 milliseconds (cumulative count 73309)
99.140% <= 0.407 milliseconds (cumulative count 99140)
99.577% <= 0.503 milliseconds (cumulative count 99577)
99.780% <= 0.607 milliseconds (cumulative count 99780)
99.832% <= 0.703 milliseconds (cumulative count 99832)
99.855% <= 0.807 milliseconds (cumulative count 99855)
99.899% <= 0.903 milliseconds (cumulative count 99899)
99.933% <= 1.007 milliseconds (cumulative count 99933)
99.938% <= 1.207 milliseconds (cumulative count 99938)
99.947% <= 1.407 milliseconds (cumulative count 99947)
99.950% <= 1.503 milliseconds (cumulative count 99950)
99.954% <= 1.903 milliseconds (cumulative count 99954)
99.981% <= 2.007 milliseconds (cumulative count 99981)
100.000% <= 2.103 milliseconds (cumulative count 100000)

Summary:
  throughput summary: 125628.14 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.259     0.032     0.255     0.359     0.407     2.079

root@cby:~# 

關於

https://www.oiox.cn/

https://www.oiox.cn/index.php/start-page.html

CSDN、GitHub、51CTO、知乎、開源中國、思否、部落格園、掘金、簡書、華為雲、阿里雲、騰訊雲、嗶哩嗶哩、今日頭條、新浪微博、個人部落格

全網可搜《小陳運維》

文章主要釋出於微信公眾號

相關文章