Linux(debian)安装Redis教程

4 篇文章 0 订阅

redis安装

ps:文档记录大部分来自万能的网络资源,感谢众多前辈大咖的分享,此只是个人记录自己的安装过程

一、概述

内存数据库,key-value存储系统,是当前比较热门的NOSQL系统之一。

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

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

三、部署与配置

1. 方便管理,将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启动、停止、开机启动

  • 在/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
  • 创建软链接

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

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

  • 刷新配置

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

systemctl daemon-reload

  • 修改profile文件:
# vi /etc/profile

# 在最后行添加:

export PATH="$PATH:/usr/local/redis/bin"

# 然后马上应用这个文件:

# source /etc/profile
  • 启动、重启、停止

启动redis

systemctl start redis

重启redis

systemctl restart redis

停止redis

systemctl stop redis

  • 开机自启动

redis服务加入开机启动

systemctl enable redis

禁止开机启动

systemctl disable redis

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目前,Redis 7 还没有正式发布,但是 Redis 7 的预览版已经发布了。以下是在 Linux 系统上安装 Redis 7 预览版的详细步骤: 1. 下载 Redis 7 预览版 可以从 Redis 官方网站下载 Redis 7 预览版的源代码:https://redis.io/download。选择最新版本的 Redis 7 预览版,下载并解压缩。 2. 安装 Redis 7 依赖 Redis 7 预览版依赖于 gcc 编译器、jemalloc 内存分配器和 tcl 测试框架。可以使用以下命令安装这些依赖项: Ubuntu/Debian: ``` sudo apt-get update sudo apt-get install build-essential tcl sudo apt-get install libjemalloc-dev ``` CentOS/RHEL: ``` sudo yum update sudo yum install gcc jemalloc-devel tcl ``` 3. 编译和安装 Redis 7 进入 Redis 7 源代码目录,执行以下命令编译和安装 Redis 7: ``` make sudo make install ``` 如果需要指定安装路径,可以使用以下命令: ``` make PREFIX=/usr/local/redis sudo make PREFIX=/usr/local/redis install ``` 4. 配置 Redis 7 Redis 7 预览版的配置文件位于 Redis 安装目录下的 redis.conf 文件。可以编辑该文件进行配置。 注意:Redis 7 预览版的配置文件与 Redis 6 的配置文件不兼容。需要参考 Redis 7 预览版的文档进行配置。 5. 启动 Redis 7 可以使用以下命令启动 Redis 7: ``` redis-server /path/to/redis.conf ``` 其中,/path/to/redis.conf 是 Redis 7 的配置文件路径。如果忽略该参数,Redis 7 将使用默认的配置文件。 6. 测试 Redis 7 可以使用以下命令连接到 Redis 7 服务器并执行一些命令: ``` redis-cli ``` 如果连接成功,将看到以下提示: ``` 127.0.0.1:6379> ``` 可以执行一些 Redis 命令,例如: ``` set mykey "Hello Redis 7" get mykey ``` 如果能够正常执行命令并返回结果,则说明 Redis 7 已经安装并运行成功。 以上就是在 Linux 系统上安装 Redis 7 预览版的步骤,希望对你有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值