Linux(debian)安装Redis教程

一、安装(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

posted @ 2020-09-25 21:57  少司命  阅读(2346)  评论(0编辑  收藏  举报