查看: 49|回覆: 1

[Debian] Linux(debian)安装Redis教程

[複製鏈接]

5

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2008-10-30
發表於 2020-9-25 21:57:00 | 顯示全部樓層 |閲讀模式

一、安装(linux-debin)

1.Redis 官方下载、编译make

$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz
$ tar xzf redis-4.0.9.tar.gz
$ cd redis-4.0.9
$ make

 

2.make编译后,在src目录下,有几个可执行文件

mkreleasehdr.sh
redis-benchmark
redis-check-aof
redis-cli
redis-server

 

此时已经安装完成,但是我们还方便运维,还是需要部署下

二、部署与配置

为方便管理,将Redis文件中的conf配置文件和常用命令移动到统一文件夹中

/usr/local/redis

 

创建目录

# 存放执行脚本
mkdir -p /usr/local/redis/bin
# 存放配置文件
mkdir -p /usr/local/redis/etc

 

移动文件到/usr/local/redis/bin; /usr/local/redis/ect目录

cd /root/redis-4.0.9
mv redis.conf /usr/local/redis/etc
cd /root/redis-4.0.9/src
mv mkreleasehdr.sh /usr/local/redis/bin
mv redis-benchmark /usr/local/redis/bin
mv redis-check-aof /usr/local/redis/bin
mv redis-cli /usr/local/redis/bin
mv redis-server /usr/local/redis/bin

 

三、执行redis-server脚本,启动redis服务

1.前台启动redis

cd /usr/local/redis/bin/
./redis-server
18847:C 28 May 19:14:10.117 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18847:C 28 May 19:14:10.118 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=18847, just started
18847:C 28 May 19:14:10.118 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 18847
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'


18847:M 28 May 19:14:10.120 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18847:M 28 May 19:14:10.120 # Server initialized
18847:M 28 May 19:14:10.120 # 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.
18847:M 28 May 19:14:10.120 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
18847:M 28 May 19:14:10.120 * Ready to accept connections

 

 

直接执行redis-server启动的Redis服务,是在前台直接运行的。如果Lunix关闭当前会话,则Redis服务也随即关闭。

^C18847:signal-handler (1527506181) Received SIGINT scheduling shutdown...
18847:M 28 May 19:16:21.363 # User requested shutdown...
18847:M 28 May 19:16:21.363 * Saving the final RDB snapshot before exiting.
18847:M 28 May 19:16:21.366 * DB saved on disk
18847:M 28 May 19:16:21.366 # Redis is now ready to exit, bye bye...

 

2. 后台启动redis服务

启动Redis服务需要从后台启动,并且指定启动配置文件。编辑redis.conf文件,设置daemonize为yes,表示后台运行。

cd /usr/local/redis/etc/
vim redis.conf
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
​
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

 

配置下面的内核参数,否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上。

# /etc/sysctl.conf加上 
#vim /etc/sysctl.conf
vm.overcommit_memory = 1

 

配置完后执行命令

sysctl vm.overcommit_memory=1

 

指定配置文件(redis.conf)启动redis服务

cd /usr/local/redis/bin/
./redis-server /usr/local/redis/etc/redis.conf
18944:C 28 May 19:30:07.549 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18944:C 28 May 19:30:07.549 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=18944, just started
18944:C 28 May 19:30:07.549 # Configuration loaded

 

启动成功,查看进程

root@Chao:~# ps -ef | grep redis
root 18945 1 0 19:30 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 18958 15412 0 19:31 pts/0 00:00:00 grep redis

 

四、将redis作为linux service启动

systemctl管理Redis启动、停止、开机启动

1. 在/lib/systemd/system目录下创建一个脚本文件redis.service

vim /lib/systemd/system/redis.service
#表示基础信息
​
[Unit]
#描述
Description=Redis
#在哪个服务之后启动
After=syslog.target network.target remote-fs.target nss-lookup.target
​
#表示服务信息
[Service]
Type=forking
#和redis.conf配置文件中的信息一致
PIDFile=/var/run/redis_6379.pid
#启动服务的命令
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
​
#安装相关信息
[Install]
WantedBy=multi-user.target

 

2. 创建软链接

创建软链接是为了下一步系统初始化时自动启动服务。

ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service

 

3. 刷新配置
刚刚配置的服务需要让systemctl能识别,就必须刷新配置。

systemctl daemon-reload

 

4. 修改profile文件:

# vi /etc/profile
# 在最后行添加:
​
export PATH="$PATH:/usr/local/redis/bin"
​
# 然后马上应用这个文件:
​
# source /etc/profile

 

5. 启动、重启、停止

# 启动redis
systemctl start redis
​
# 重启redis
systemctl restart redis
​
# 停止redis
systemctl stop redis

 

6. 开机自启动

# redis服务加入开机启动
systemctl enable redis
​
# 禁止开机启动
systemctl disable redis

 

 

 


 

参考:https://blog.csdn.net/u010277446/article/details/80497539



来源:https://www.cnblogs.com/itfky/p/13732817.html
回覆

使用道具 舉報

0

主題

720

回帖

4441

積分

琼殿精英

金币
3721
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2011-10-11
發表於 2026-5-9 07:55:25 | 顯示全部樓層
顶一个!教程写得很详细,很适合新手学习~

之前一直用apt安装redis,这次跟着教程手动编译安装了一遍,确实学到不少东西。

补充几点小建议:1. 编译前最好先确认系统已经安装了gcc和make,不然会报错缺少编译工具。可以先执行:
  1. apt-get update
  2. apt-get install build-essential
複製代碼

2. 关于内核参数优化,除了教程里提到的vm.overcommit_memory,最好也把somaxconn调大一下:
  1. echo 511 > /proc/sys/net/core/somaxconn
複製代碼

3. 如果想让redis-cli直接使用,不用每次都输入完整路径,可以把/usr/local/redis/bin加到PATH环境变量里,教程里其实也有提到啦~

4. 生产环境建议给redis设置密码,在redis.conf里找到requirepass配置项取消注释就行。

另外,debian系统其实也可以用apt安装redis,版本会稍微旧一点,但是更省心:
  1. apt-get install redis-server
複製代碼

不过手动编译确实能学到更多东西,支持楼主的分享!期待更多干货~

——来自一个小透明的回复
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部