以下操作在debian11、deiban12下演示,ubuntu通用,其它系列l(wèi)inux發(fā)行版自行安裝依賴,其余一樣
以7.2.1版本為例進(jìn)行安裝
最后的bash腳本是對(duì)前面的操作的封裝,一鍵即配置完成,即可使用
一、編譯安裝
1.安裝依賴
apt-get install build-essential autoconf automake
2.下載
redis官方下載頁面
redis github頁面
cd /usr/local
如果下載不下來,復(fù)制鏈接用迅雷這類工具下載后上傳到服務(wù)器
wget https://github.com/redis/redis/archive/7.2.1.tar.gz
3.解壓
tar -zxvf /usr/local/7.2.1.tar.gz
4.編譯安裝
cd redis-7.2.1
安裝到/usr/local/redis
make && make install
5.配置文件
配置文件在源碼包里面,復(fù)制配置文件到/etc
mkdir /etc/redis
cp /usr/local/redis-7.2.1/redis.conf /etc/redis/redis.conf
下面的命令測(cè)試運(yùn)行,ctrl+c退出
/usr/local/bin/redis-server /etc/redis/redis.conf
6.修改配置文件
vi /etc/redis/redis.conf
守護(hù)模式開啟
找到daemonize no,修改為daemonize yes(這個(gè)參數(shù)當(dāng)適用systemctl管理時(shí)失效)
修改安全設(shè)置
requirepass xxx:設(shè)定密碼訪問(如果只是做緩存公開信息的話,可以不用密碼)
bind 127.0.0.1:綁定的ip,在保護(hù)模式開啟的情況下,只有綁定的ip才可以訪問redis服務(wù),建議添加#號(hào)注釋,否則外部的設(shè)備無法訪問,只能安裝redis的訪問。
protected-mode yes:保護(hù)模式,如果為yes,綁定的ip或輸入密碼才能訪問。如果為no,則所有保護(hù)措施失效。
port:redis服務(wù)端口號(hào),默認(rèn)是6379,如果需要修改redis的訪問端口,就通過修改該值實(shí)現(xiàn)
7.systemctl腳本管理
vi /usr/lib/systemd/system/redis.service
內(nèi)容如下
[Unit]
Description=redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存:wq
啟動(dòng)并開啟啟動(dòng)
systemctl daemon-reload && systemctl start redis && systemctl enable redis && systemctl status redis
8.遠(yuǎn)程連接
使用redisinsight連接,不寫賬號(hào),只寫上面設(shè)定的密碼即可
如果后期優(yōu)化,可以設(shè)定賬號(hào)密碼,這里不再涉及
二、編譯安裝的一鍵腳本
1.創(chuàng)建腳本文件
vi /usr/local/redis7.sh
2.編輯
可能會(huì)卡在下載redis源碼包上,可以注釋掉wget這一行,然后上傳其它地方下載的上傳到/usr/local
#!/bin/bash
# 常量設(shè)置
redis_version="7.2.1" # redis版本
install_path="/usr/local" # 安裝、操作目錄
# 輸入redis密碼,如果為空則沒有
read -p -"請(qǐng)輸入一個(gè)redis密碼:" redis_password
# 安裝依賴
echo "......正在安裝依賴......"
apt-get install -y build-essential autoconf automake
echo "......依賴安裝完成......"
# 下載redis源碼包
echo "......正在下載源碼包......"
wget -P ${install_path} https://github.com/redis/redis/archive/${redis_version}.tar.gz
echo "......源碼包下載完成......"
# 解壓縮
echo "......正在解壓縮源碼包......"
cd ${install_path}
tar -zxf ${install_path}/${redis_version}.tar.gz
echo "......源碼包解壓縮完成......"
# 編譯安裝
echo "......正在編譯安裝......"
cd ${install_path}/redis-${redis_version} && make && make install
echo "......編譯安裝完成......"
# 配置文件
echo "......正在修改配置文件......"
## 創(chuàng)建配置文件
mkdir /etc/redis
cp ${install_path}/redis-${redis_version}/redis.conf /etc/redis/redis.conf
## 修改配置文件(/etc/redis/redis.conf)
sed -i 's/daemonize no/daemonize yes/g' /etc/redis/redis.conf
## 修改默認(rèn)密碼,如果為空則開啟所有ip都可以訪問,如果不為空則使用密碼訪問
if [ ${#redis_password} -eq 0 ];
then
echo "未輸入密碼,開啟所有IP都能訪問"
sed -i 's/bind 127.0.0.1 -::1/# bind 127.0.0.1 -::1/g' /etc/redis/redis.conf
sed -i 's/protected-mode yes/protected-mode no/g' /etc/redis/redis.conf
else
echo "已設(shè)定密碼"
sed -i 's/bind 127.0.0.1 -::1/# bind 127.0.0.1 -::1/g' /etc/redis/redis.conf
sed -i 's/# requirepass foobared/requirepass '"${redis_password}"'/g' /etc/redis/redis.conf
fi
## 持久化設(shè)定
echo "......修改配置文件完成......"
# 配置systemctl腳本
echo "......正在配置systemctl腳本......"
echo "......正在配置systemctl腳本......"
cat>/usr/lib/systemd/system/redis.service<<EOF
[Unit]
Description=redis
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start redis
systemctl enable redis
systemctl status redis
echo "......systemctl腳本配置完成......"
echo "......!!!腳本運(yùn)行完成!!!......"
3.運(yùn)行
sh /usr/local/redis7.sh
三、相關(guān)問題
1.報(bào)錯(cuò): WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition.
解決方法文章來源:http://www.zghlxwxcb.cn/news/detail-588731.html
echo 1 > /proc/sys/vm/overcommit_memory
2.exact Failed to connect to any host resolved for DNS name
解決方法,注銷配置文件中的綁定ip文章來源地址http://www.zghlxwxcb.cn/news/detail-588731.html
到了這里,關(guān)于redis7.0編譯安裝 + bash安裝腳本的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!