1. 程式人生 > 其它 >記錄一次生產環境下EleasticSearch故障(cpu打滿)

記錄一次生產環境下EleasticSearch故障(cpu打滿)

Redis(一)

本文分為以下幾個部分

介紹

Linux安裝

遠端連線Redis

其他配置

總結

介紹

Redis是一種非關係型資料庫(NoSQL)

NoSQL

NoSQL = not only SQL (不僅僅是sql),並不是沒有SQL,泛指非關係型資料庫。(關係型資料庫為表格型別)

特點

  1. 方便擴充套件(資料之間沒有關係,很好擴充套件)
  2. 大資料高效能(Redis 一秒寫 8w 次,讀取 11w 次,NoSQL 的快取記錄級,是一種細粒度的快取,效能高)
  3. 資料型別多樣(不需要事先設計資料庫,隨取隨用)
  • 從上面的話中,看到Redis官方是不支援windows版本的

Redis 為單執行緒

Redis 將所有資料全部放在記憶體中,使用單執行緒去操作效率最高,多執行緒CPU上下文切換會耗時間,對於記憶體來說,如果沒有上下文切換就是效率最高!

Linux安裝

將官網下載的Redis壓縮包傳至linux伺服器。

使用 tar -zxvf redis-6.2.4.tar.gz 命令解壓檔案(後面為自己redis的壓縮包名稱)

切換至Redis解壓目錄,使用make命令進行編譯

等待完成,切換到該目錄的src目錄下,啟動redis,輸入redis-server

[root@qundd src]# redis-server 
2223:C 15 Jul 2021 10:04:40.035 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2223:C 15 Jul 2021 10:04:40.035 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=2223, just started
2223:C 15 Jul 2021 10:04:40.035 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2223:M 15 Jul 2021 10:04:40.035 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2223
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2223:M 15 Jul 2021 10:04:40.036 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2223:M 15 Jul 2021 10:04:40.036 # Server initialized
2223:M 15 Jul 2021 10:04:40.036 # 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.
2223:M 15 Jul 2021 10:04:40.036 * Ready to accept connections

預設埠為6379,新建一個視窗,同樣切換至redis的src目錄下,啟動redis-cli

[root@qundd src]# redis-cli 
127.0.0.1:6379> ping	#輸入ping,顯示pong,則說明連線成功
PONG
127.0.0.1:6379> exit
[root@qundd src]# 

遠端連線Redis

使用連線網路、埠號方式連線redis-server

#redis-cli -h IP地址 -p 埠號 -a redis的密碼
[root@qundd src]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
[root@qundd src]# 

ifconfig 檢視linux ip地址,將上方127.0.0.1換成ip地址,模擬外網訪問,發現無法ping通

[root@qundd src]# redis-cli -h 172.31.43.147 -p 6379
172.31.43.147:6379> ping
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
172.31.43.147:6379> exit
[root@qundd src]# 

修改配置檔案,更換地址等資訊,切換至Redis目錄(src上一層),裡面有個官方的 redis.conf (也可以自己寫一個配置檔案)配置檔案。

在network中,有一個

bind 127.0.0.1 -::1

註釋之,修改成

#bind 127.0.0.1 -::1
bind 127.0.0.1 172.31.43.147	#172.31.43.147 為我的ip,需要自己修改成自己的ip

wq儲存,使用配置檔案啟動redis,也可以切換至src目錄下,筆者懶得切換,直接使用src下的redis-server

#redis-server 配置檔案
[root@qundd redis-6.2.4]# src/redis-server /root/redis-6.2.4/redis.conf
2242:C 15 Jul 2021 10:19:13.037 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2242:C 15 Jul 2021 10:19:13.038 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=2242, just started
2242:C 15 Jul 2021 10:19:13.038 # Configuration loaded
2242:M 15 Jul 2021 10:19:13.038 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2242
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2242:M 15 Jul 2021 10:19:13.039 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2242:M 15 Jul 2021 10:19:13.039 # Server initialized
2242:M 15 Jul 2021 10:19:13.039 # 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.
2242:M 15 Jul 2021 10:19:13.039 * Ready to accept connections

再次使用redis-cli連線

[root@qundd src]# redis-cli -h 172.31.43.147 -p 6379
172.31.43.147:6379> ping
PONG
172.31.43.147:6379> exit

其他配置

部分後面文章會了解

單位

  1. 配置檔案Unit,大小寫不敏感

INCLUDES 包含

  1. 可以將多個redis conf組合成一個,就好比spring的import,jsp include

NETWORK 網路

bind 127.0.0.1 172.31.43.147	#繫結ip
protected-mode yes	#保護模式,預設yes
port 6379	#埠設定

GENERAL 通用

daemonize yes後臺方式執行

daemonize no	#是否以守護(後臺)程序方式執行,預設為no,需要手動改為yes
pidfile /var/run/redis_6379.pid	#如果以後臺方式執行,需要指定pid程序檔案

# 日誌
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
logfile ""	#日誌檔案位置
databases 16	#資料庫數量
always-show-logo no	#是否總是顯示logo

SNAPSHOTTING 快照

持久化,預設為rdb持久化

在規定的時間內,執行了多少次操作,則會持久化到檔案 .rdb .aof

redis 是記憶體資料庫,如果沒有持久化,斷電即失

# 如果3600s內,如果至少有 1個 key進行了修改,我們就進行持久化操作,3600s才會去儲存
# save 3600 1
# 如果300s內,如果至少有 100個 key進行了修改,我們就進行持久化操作,300s才會去儲存
# save 300 100
# 如果60s內,如果至少有 10000個 key進行了修改,我們就進行持久化操作
# save 60 10000
stop-writes-on-bgsave-error yes	#持久化如果出錯,是否還需要繼續工作
rdbcompression yes	#是否壓縮rdb檔案,需要消耗CPU資源
rdbchecksum yes	#儲存rdb檔案時候,進行錯誤的檢查校驗
dir ./	#rbd儲存路徑,與redis-server相同目錄

REPLICATION 複製

SECURITY

requirepass &sjy	#設定密碼

CLIENTS 限制客戶端

# maxclients 10000	#能連線上的最大客戶端數量
# maxmemory <bytes>	#最大的容量
# maxmemory-policy noeviction	#記憶體到達上限之後的處理策略

APPEND ONLY MODE aof配置(也是一個持久化的配置)

appendonly no	#預設不開啟aof模式,預設是rdb方式持久化,大部分情況下,rdb就能處理
appendfilename "appendonly.aof"	#持久化檔案的名字
# appendfsync always	#每次修改,都會同步
appendfsync everysec	#每秒執行一次,sync,可能會丟失這1s資料
# appendfsync no		#不執行同步,這個時候作業系統自己同步資料,速度最快

總結

  • Redis 官方並不支援windows。
  • Redis 是單執行緒的。
  • 遠端連線 Redis 需要修改配置檔案的 bind 屬性,並且使用配置檔案啟動。
  • 後臺啟動需要修改配置檔案中 GENERAL daemonize yes
  • Redis 密碼可以使用配置檔案配置 REPLICATION 中的 requirepass,也可以使用命令設定。