作者:admin,发布日期:2021-11-22
阅读:5159;评论:695

写在开头

2020年12月08日CentOS 官方宣布CentOS Linux项目将停止,并推出CentOS Stream项目。CentOS未来将会从Red Hat Enterprise Linux(RHEL)复刻版本的CentOS Linux转向CentOS Stream。而博主目前使用最多的Centos8刚好在停止维护的系统列表内,所以博主目前打算转战Ubuntu,以后教程可能会更少使用Centos。

本文将带你实现手动编译安装Redis以及创建systemd服务并设置自启动。

安装教程

查看系统版本

这里使用的是Ubuntu20.04

root@VM-8-10-ubuntu:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.3 LTS
Release:	20.04
Codename:	focal

下载Reids源码

可以从Redis官网的下载页面下载到最新的源码:

https://redis.io/download

下载完成后我们对其进行解压

tar -xvf redis-6.2.6.tar.gz

安装依赖

编译前我们需要先安装编译需要的依赖:

apt install libsystemd-dev libc6-dev

然后进入deps目录,编译剩余需要的依赖,输入以下命令以进行:

cd redis-6.2.6/deps
make hiredis linenoise hdr_histogram lua jemalloc -j16

编译Redis本体

编译依赖完成后就可以开始编译本体

make USE_SYSTEMD=yes

如果系统为多核可以加-j线程数来开启多线程编译提升速度,如下文代码所示(此处使用16线程):

make USE_SYSTEMD=yes -j16

编译完成后如有需要可以进行测试,需要安装tcl以支持测试

apt install tcl
make test

接着将其安装到系统

make install

创建服务

这里我们使用systemd来创建服务,而不是使用过时的init.d

首先复制一份redis配置到/etc下

cp redis.conf /etc/

打开redis.conf,修改以下内容

修改监听IP,开启外网连接(默认是本地可以通过127.0.0.1连接):

bind 0.0.0.0

找到supervised并去掉注释,将其设置为systemd,要不然systemd不会检测到redis启动成功

supervised systemd

然后为redis设置密码:

找到requirepass并去掉注释,将"foobared"修改为你自己需要的密码,这点很重要,没设置容易被攻击!!

requirepass foobared


然后复制redis自带的示例服务文件到systemd,并且创建好redis的数据目录

cd utils
cp systemd-redis_server.service /etc/systemd/system/redis-server.service
mkdir -p /var/lib/redis

下面将编辑服务文件

cd /etc/systemd/system/
vim redis-server.service

去掉后一个ExecStart的注释,将上方原有的注释掉,并将下方的配置文件目录改成我们自己的

ExecStart=/usr/local/bin/redis-server /etc/redis.conf

然后设置工作目录,去掉下方WorkingDirectory的注释

WorkingDirectory=/var/lib/redis

改好的文件看起来应该是这样子的

[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
#Before=your_application.service another_example_application.service
#AssertPathExists=/var/lib/redis
Wants=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=yes
Type=notify
TimeoutStartSec=infinity
TimeoutStopSec=infinity
UMask=0077
#User=redis
#Group=redis
WorkingDirectory=/var/lib/redis

[Install]
WantedBy=multi-user.target

开启服务并设置开机启动

首先看一下服务配的对不对,如果status显示Active则正常。

systemctl start redis-server.service
systemctl status redis-server.service

正常之后直接设置开机启动即可

systemctl enable redis-server.service

或者一条命令搞定

systemctl enable --now redis-server.service

非root用户运行redis

首先创建用户

useradd redis -s /sbin/nologin -b /var/lib

然后设置运行用户

vim /etc/systemd/system/redis-server.service

去掉下面两个注释
User=redis
Group=redis

设置完成之后重载systemd并且重启redis-server

systemctl daemon-reload && systemctl restart redis-server.service

配置完成后可以查看下是不是redis用户在跑,显示User=redis就说明配置成功

root@VM-8-10-ubuntu:/var/lib# systemctl show -pUser redis-server.service 
User=redis

解决overcommit_memory问题

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!

“警告超限”内存设置为0!在内存不足的情况下,后台保存可能会失败。若要解决此问题,请将“vm.overcommit_memory=1”添加到/etc/sysctl.conf,然后重新启动或运行命令“sysctl vm.overcommit_memory=1”以使其生效。

我们按照提示直接设置这个内核参数即可:

echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf && sysctl -p

Redis管理工具

这里推荐RDM:

https://github.com/uglide/RedisDesktopManager

项目是开源的,windows版本需要到microsoft store中购买后(相当于赞助作者)才可以解锁

这里提供一个第三方大佬编译的windows版本,可以直接安装:

https://github.com/lework/RedisDesktopManager-Windows

你可能感兴趣的文章

评论区

已有695位网友发表了看法:

1L 游客  2024-07-11 12:45:36 回复
收藏了,很不错的内容!http://www.gxmlm.com
2L 游客  2024-07-11 13:07:02 回复
知识就是力量啊!http://xk6.blxhf.com
3L 游客  2024-07-11 14:25:58 回复
看帖回帖一条路!http://xax1f.laoketry.cn
4L 游客  2024-07-11 14:28:07 回复
管它三七二十一!http://vjk8q2.shhtjt.cn
5L 游客  2024-07-11 14:40:56 回复
宇宙第一贴诞生了!http://www.ddman.net
6L 游客  2024-07-11 15:12:15 回复
学习雷锋,好好回帖!http://457.quatans.com
7L 游客  2024-07-11 15:19:20 回复
看帖不回帖都是耍流氓!http://wwlhj.morningcolumn.com/9/3.html
8L 游客  2024-07-11 15:52:29 回复
楼主是在找骂么?http://m22ba6.haitaotex.com
9L 游客  2024-07-11 16:48:02 回复
楼主的等级很高啊!http://c10fpn.xinyuhuanbao.cn
10L 游客  2024-07-11 16:50:06 回复
雷锋做好事不留名,都写在帖子里!http://ar611.cryptoonair.com
11L 游客  2024-07-11 17:54:59 回复
楼上的心情不错啊!http://3gs07.sclongplan.com
12L 游客  2024-07-11 18:06:49 回复
我裤子脱了,纸都准备好了,你就给我看这个?http://1cfy5.czdy2010.cn
13L 游客  2024-07-11 18:08:22 回复
论坛的人气越来越旺了!http://olqtsa.shjsm.com
14L 游客  2024-07-11 18:15:11 回复
写得实在太好了,我唯一能做的就是默默顶贴!http://ye4wb0.narenwu.com
15L 游客  2024-07-11 18:41:40 回复
以后就跟楼主混了!http://jvnk.cryptoonair.com
16L 游客  2024-07-11 18:57:45 回复
经典,收藏了!http://31ej00.gdjianye.com
17L 游客  2024-07-11 19:14:48 回复
顶顶更健康!http://ddtyd4.dongwenpen.com
18L 游客  2024-07-11 20:06:21 回复
突然觉得楼主说的很有道理,赞一个!http://www.2qukuai.com
19L 游客  2024-07-11 20:57:59 回复
楼主今年多大了?http://8yg.fjshsywz.com
20L 游客  2024-07-11 21:04:01 回复
楼主发几张靓照啊!http://3zkku.jsychb.com
21L 游客  2024-07-11 21:34:27 回复
这个帖子会火的,鉴定完毕!http://pet6d.tsweave.com
22L 游客  2024-07-11 21:44:38 回复
收藏了,很不错的内容!http://xcr.cdbczy.com
23L 游客  2024-07-11 21:55:24 回复
我就搞不明白了,看帖回帖能死人么,居然只有我这么认真的在回帖!http://cp0.51gcd.com
24L 游客  2024-07-11 21:57:05 回复
不错的帖子,值得收藏!http://x26f.blxhf.com
25L 游客  2024-07-11 22:17:49 回复
在这个版块混了这么久了,第一次看见这么给你的帖子!http://7ospi.jwpmj.com/20240711/5.html
26L 游客  2024-07-11 22:20:48 回复
经典,收藏了!http://077z9.jinglx.com
27L 游客  2024-07-11 22:52:22 回复
关注一下!http://lgzxn.skyee361.com/13/4.html
28L 游客  2024-07-11 23:31:45 回复
网站做得不错http://m2ybi.cryptoonair.com
29L 游客  2024-07-11 23:51:25 回复
怎么我回帖都没人理我呢?http://z2f.crbicycle.com
30L 游客  2024-07-11 23:53:03 回复
看帖回帖一条路!http://5ygorr.sxhe5.com
31L 游客  2024-07-12 00:33:08 回复
楼主今年多大了?http://8lebl.wxhouselaw.com
32L 游客  2024-07-12 00:36:39 回复
语言表达流畅,没有冗余,读起来很舒服。http://23f.wxhouselaw.com
33L 游客  2024-07-12 00:46:58 回复
终于看完了,很不错!http://5785.sxhe5.com
34L 游客  2024-07-12 00:47:35 回复
楼上的很有激情啊!http://je33q.hbhjgy.com
35L 游客  2024-07-12 00:57:50 回复
论坛人气好旺!http://yksz7.hbsz-generaltruck.com/a/5.html
36L 游客  2024-07-12 00:58:35 回复
回帖也有有水平的!http://l88.bbg518.com
37L 游客  2024-07-12 01:19:01 回复
雷锋做好事不留名,都写在帖子里!http://15cgf.51hanshi.com
38L 游客  2024-07-12 01:24:15 回复
学习雷锋,好好回帖!http://051b4.multigruporadio.com
39L 游客  2024-07-12 01:25:37 回复
终于看完了,很不错!http://fitv3.huichengyu.com
40L 游客  2024-07-12 02:28:37 回复
很多天不上线,一上线就看到这么给力的帖子!http://c31yuf.huichengyu.com
41L 游客  2024-07-12 03:21:07 回复
楼上的这是啥态度呢?http://o1a.njkrx.cn
42L 游客  2024-07-12 04:03:24 回复
刚分手,心情不好!http://vlr909.sjdn.com.cn
43L 游客  2024-07-12 04:03:39 回复
写得实在太好了,我唯一能做的就是默默顶贴!http://z836f2.yulaimetal.com
44L 游客  2024-07-12 04:42:15 回复
刚分手,心情不好!http://sjrnz.sh-ts.net
45L 游客  2024-07-12 04:48:02 回复
文章写太挺好了,真的值得推荐http://v20ox.dvxatope.com/2024/3.html
46L 游客  2024-07-12 05:17:00 回复
很给力!http://jzfxk.shjsm.com
47L 游客  2024-07-12 05:21:18 回复
我和我的小伙伴都惊呆了!http://6cjx84.quanqiuidc.com
48L 游客  2024-07-12 05:38:02 回复
楼主内心很强大!http://y4w9z.narenwu.com
49L 游客  2024-07-12 05:54:33 回复
楼主说的我也略懂!http://dgh.gdjianye.com
50L 游客  2024-07-12 06:27:12 回复
视死如归的架势啊!http://85ojm.haitaotex.com
51L 游客  2024-07-12 06:37:29 回复
这位作者的文笔极其出色,用词精准、贴切,能够形象地传达出他的思想和情感。http://up05.nnsxjd.com
52L 游客  2024-07-12 06:38:02 回复
感谢楼主的推荐!http://miteu.zblongxing.com.cn
53L 游客  2024-07-12 07:14:51 回复
感谢楼主的推荐!http://h0z3b.sijirun.com
54L 游客  2024-07-12 07:15:58 回复
楼主的头像能辟邪啊!http://4ey0.lanweish.cn
55L 游客  2024-07-12 07:51:00 回复
今天的心情很不错啊http://4nev.sijirun.com
56L 游客  2024-07-12 07:56:33 回复
好帖子!http://weika.org.cn/news/37b099512.html
57L 游客  2024-07-12 08:09:56 回复
怪事年年有,今年特别多!http://8e5.tsweave.com
58L 游客  2024-07-12 10:28:25 回复
学习雷锋,好好回帖!http://ywghr.aficsol.com/8/4.html
59L 游客  2024-07-12 12:06:00 回复
这位作者的文笔极其出色,用词精准、贴切,能够形象地传达出他的思想和情感。http://5rdb.cryptoonair.com
60L 游客  2024-07-12 13:06:28 回复
楼主很有激情啊!http://39o.cryptoonair.com
61L 游客  2024-07-12 16:14:38 回复
楼主看起来很有学问!http://e01t.fl691.com
62L 游客  2024-07-12 16:41:44 回复
楼上的能详细介绍一下么?http://y0ry.huichengyu.com
63L 游客  2024-07-12 16:47:47 回复
坚持回帖!http://ijk99.com/news/62b399577.html
64L 游客  2024-07-12 17:45:19 回复
视死如归的架势啊!http://zfu0.830x.cn
65L 游客  2024-07-12 18:00:12 回复
听楼主一席话,省我十本书!http://zkqq.tjzhtdgt.com
66L 游客  2024-07-12 19:16:59 回复
今天过得很不爽!http://www.hntbhz.cn/post/34.html
67L 游客  2024-07-12 19:35:06 回复
这一年啥事没干,光研究楼主的帖子了!http://tq2zq.91fwq.com/20240712/3.html
68L 游客  2024-07-12 19:53:13 回复
今天过得很不爽!http://plasmedia.com.cn/news/46a099503.html
69L 游客  2024-07-12 20:42:15 回复
这么版块的帖子越来越有深度了!http://1v5liw.sophmoon.cn
70L 游客  2024-07-12 20:43:45 回复
不是惊喜,是惊吓!http://www.hntbhz.cn/post/4.html
71L 游客  2024-07-12 20:47:13 回复
鉴定完毕!http://8mw.gzzweb.cn
72L 游客  2024-07-12 21:46:29 回复
收藏了,以后可能会用到!http://r54ar.cryptoonair.com
73L 游客  2024-07-12 22:06:10 回复
经典!http://6q2vh.lkjmpcb.com/2024/4.html
74L 游客  2024-07-12 22:18:55 回复
顶一下,收藏了!http://reibg.e8jes3c.cn
75L 访客  2024-07-12 22:45:55 回复
安福相册 https://z11.cn
76L 游客  2024-07-12 23:19:02 回复
谢谢楼主的分享!http://k2pqh.gmreol.com/b/5.html
77L 游客  2024-07-12 23:19:21 回复
好无聊啊!http://bz3k.cryptoonair.com
78L 游客  2024-07-12 23:21:07 回复
对牛弹琴的人越来越多了!http://www.hntbhz.cn/post/47.html
79L 访客  2024-07-13 00:03:00 回复
微商货源网 https://z11.cn
80L 游客  2024-07-13 00:50:11 回复
无图无真相!http://hpo60.zwygmy.com
81L 游客  2024-07-13 02:41:15 回复
楼主练了葵花宝典吧?http://yunzongcn.com/news/92f399547.html
82L 游客  2024-07-13 02:59:44 回复
学习雷锋,好好回帖!http://5mo.lostockhallband.com
83L 游客  2024-07-13 04:09:59 回复
收藏了,以后可能会用到!http://xwym.org.cn/news/58f099491.html
84L 游客  2024-07-13 04:32:30 回复
楼主很有经验啊!http://7oqrl.kelebektr.com/01/3.html
85L 游客  2024-07-13 05:12:30 回复
看帖、回帖、拿分、走人http://6sco1v.0075612.com
86L 游客  2024-07-13 06:34:47 回复
好无聊啊!http://hn99o.12123123.com/J/5.html
87L 游客  2024-07-13 07:05:42 回复
楼主的帖子提神醒脑啊!http://y189q1.cryptoonair.com
88L 游客  2024-07-13 07:48:07 回复
有内涵!http://pj1s.funnysayings4u.com
89L 游客  2024-07-13 10:46:12 回复
鉴定完毕!http://6sp6.huichengyu.com
90L 游客  2024-07-13 11:34:09 回复
不错哦,楼主这是要火的节奏啊!http://99kang.cn/news/33b399606.html
91L 游客  2024-07-13 12:01:00 回复
很有看点!http://o3l.huichengyu.com
92L 游客  2024-07-13 13:24:47 回复
谢谢楼主的分享!http://x3kgj.sinbre.com/07/4.html
93L 游客  2024-07-13 13:45:14 回复
勤奋灌水,天天向上!http://ijk99.com/news/50c399589.html
94L 游客  2024-07-13 15:51:21 回复
楼上的真不讲道理!http://aijiankang99.cn/news/23e399616.html
95L 游客  2024-07-13 16:38:28 回复
怪事年年有,今年特别多!http://mofeimo.com/?pic_12/236.html
96L 游客  2024-07-13 18:04:50 回复
刚分手,心情不好!http://4p8c5.themonkey.com.cn
97L 游客  2024-07-13 18:04:57 回复
你觉得该怎么做呢?http://x9w.souf1.com
98L 游客  2024-07-13 18:26:35 回复
楼主好聪明啊!http://wp4s7.mc88d.com/20240712/3.html
99L 游客  2024-07-13 18:44:34 回复
楼主内心很强大!http://0t5yr.yhdht.com/01/5.html
100L 安福相册  2024-07-13 20:01:47 回复
安福相册官网 https://www.anfu0594.com

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。