[教程] Ubuntu20下手動(dòng)編譯安裝Redis6.2
寫在開(kāi)頭
2020年12月08日CentOS 官方宣布CentOS Linux項(xiàng)目將停止,并推出CentOS Stream項(xiàng)目。CentOS未來(lái)將會(huì)從Red Hat Enterprise Linux(RHEL)復(fù)刻版本的CentOS Linux轉(zhuǎn)向CentOS Stream。而博主目前使用最多的Centos8剛好在停止維護(hù)的系統(tǒng)列表內(nèi),所以博主目前打算轉(zhuǎn)戰(zhàn)Ubuntu,以后教程可能會(huì)更少使用Centos。
本文將帶你實(shí)現(xiàn)手動(dòng)編譯安裝Redis以及創(chuàng)建systemd服務(wù)并設(shè)置自啟動(dòng)。
安裝教程
查看系統(tǒng)版本
這里使用的是Ubuntu20.04
Bash
root@VM-8-10-ubuntu:~# lsb_release -aNo LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
下載Reids源碼
可以從Redis官網(wǎng)的下載頁(yè)面下載到最新的源碼:
https://redis.io/download
下載完成后我們對(duì)其進(jìn)行解壓
Bash
tar -xvf redis-6.2.6.tar.gz
安裝依賴
編譯前我們需要先安裝編譯需要的依賴:
Bash
apt install libsystemd-dev libc6-dev
然后進(jìn)入deps目錄,編譯剩余需要的依賴,輸入以下命令以進(jìn)行:
Bash
cd redis-6.2.6/depsmake hiredis linenoise hdr_histogram lua jemalloc -j16
編譯Redis本體
編譯依賴完成后就可以開(kāi)始編譯本體
Bash
make USE_SYSTEMD=yes
如果系統(tǒng)為多核可以加-j線程數(shù)來(lái)開(kāi)啟多線程編譯提升速度,如下文代碼所示(此處使用16線程):
Bash
make USE_SYSTEMD=yes -j16
編譯完成后如有需要可以進(jìn)行測(cè)試,需要安裝tcl以支持測(cè)試
Bash
apt install tclmake test
接著將其安裝到系統(tǒng)
Bash
make install
創(chuàng)建服務(wù)
這里我們使用systemd來(lái)創(chuàng)建服務(wù),而不是使用過(guò)時(shí)的init.d
首先復(fù)制一份redis配置到/etc下
Bash
cp redis.conf /etc/
打開(kāi)redis.conf,修改以下內(nèi)容:
修改監(jiān)聽(tīng)I(yíng)P,開(kāi)啟外網(wǎng)連接(默認(rèn)是本地可以通過(guò)127.0.0.1連接):
Bash
bind 0.0.0.0
找到supervised并去掉注釋,將其設(shè)置為systemd,要不然systemd不會(huì)檢測(cè)到redis啟動(dòng)成功
Bash
supervised systemd
然后為redis設(shè)置密碼:
找到requirepass并去掉注釋,將"foobared"修改為你自己需要的密碼,這點(diǎn)很重要,沒(méi)設(shè)置容易被攻擊!!
Bash
requirepass foobared
然后復(fù)制redis自帶的示例服務(wù)文件到systemd,并且創(chuàng)建好redis的數(shù)據(jù)目錄
Bash
cd utilscp systemd-redis_server.service /etc/systemd/system/redis-server.servicemkdir -p /var/lib/redis
下面將編輯服務(wù)文件
Bash
cd /etc/systemd/system/
vim redis-server.service
去掉后一個(gè)ExecStart的注釋,將上方原有的注釋掉,并將下方的配置文件目錄改成我們自己的
Bash
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
然后設(shè)置工作目錄,去掉下方WorkingDirectory的注釋
Bash
WorkingDirectory=/var/lib/redis
改好的文件看起來(lái)應(yīng)該是這樣子的
Bash
[Unit]Description=Redis data structure server
Documentation=https://redis.io/documentation#Before=your_application.service another_example_application.service#AssertPathExists=/var/lib/redisWants=network-online.target
After=network-online.target[Service]#ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize no## Alternatively, have redis-server load a configuration file:ExecStart=/usr/local/bin/redis-server /etc/redis.conf
LimitNOFILE=10032
NoNewPrivileges=yes#OOMScoreAdjust=-900#PrivateTmp=yesType=notify
TimeoutStartSec=infinity
TimeoutStopSec=infinity
UMask=0077#User=redis#Group=redisWorkingDirectory=/var/lib/redis[Install]WantedBy=multi-user.target
開(kāi)啟服務(wù)并設(shè)置開(kāi)機(jī)啟動(dòng)
首先看一下服務(wù)配的對(duì)不對(duì),如果status顯示Active則正常。
Bash
systemctl start redis-server.service
systemctl status redis-server.service
正常之后直接設(shè)置開(kāi)機(jī)啟動(dòng)即可
Bash
systemctl enable redis-server.service
或者一條命令搞定
Bash
systemctl enable --now redis-server.service
非root用戶運(yùn)行redis
首先創(chuàng)建用戶
Bash
useradd redis -s /sbin/nologin -b /var/lib
然后設(shè)置運(yùn)行用戶
Bash
vim /etc/systemd/system/redis-server.service
去掉下面兩個(gè)注釋
User=redis
Group=redis
設(shè)置完成之后重載systemd并且重啟redis-server
Bash
systemctl daemon-reload && systemctl restart redis-server.service
配置完成后可以查看下是不是redis用戶在跑,顯示User=redis就說(shuō)明配置成功
Bash
root@VM-8-10-ubuntu:/var/lib# systemctl show -pUser redis-server.service User=redis
解決overcommit_memory問(wèn)題
Nov 22 10:30:14 VM-8-10-ubuntu redis-server[25201]: 25201:M 22 Nov 2021 10:30:14.530 # WARNING overcommit_memory is set to 0!
“警告超限”內(nèi)存設(shè)置為0!在內(nèi)存不足的情況下,后臺(tái)保存可能會(huì)失敗。若要解決此問(wèn)題,請(qǐng)將“vm.overcommit_memory=1”添加到/etc/sysctl.conf,然后重新啟動(dòng)或運(yùn)行命令“sysctl vm.overcommit_memory=1”以使其生效。
我們按照提示直接設(shè)置這個(gè)內(nèi)核參數(shù)即可:
Bash
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf && sysctl -p
Redis管理工具
這里推薦RDM:
https://github.com/uglide/RedisDesktopManager
項(xiàng)目是開(kāi)源的,windows版本需要到microsoft store中購(gòu)買后(相當(dāng)于贊助作者)才可以解鎖
這里提供一個(gè)第三方大佬編譯的windows版本,可以直接安裝:
https://github.com/lework/RedisDesktopManager-Windows