本文主要介绍使用yum和源码两种方式部署redis服务
更新于 2021-05-02
yum方式部署
依赖安装
1
| yum install -y epel-release
|
安装Redis
1 2
| yum install -y redis redis-cli --version
|
直接yum安装的redis不是最新的版本,如果要安装最新版本需要执行下面的步骤:
1 2
| yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm yum --enablerepo=remi install redis -y
|
设置配置文件
修改/etc/redis.conf
中的bind
参数为下面的值,监听所有IP地址:
设置系统参数
1 2
| sysctl vm.overcommit_memory=1 echo "sysctl vm.overcommit_memory=1" >> /etc/rc.local
|
vm.overcommit_memory
是控制内存分配策略的参数:
- 1:内核分配所有的物理内存而不管当前内存状态;
- 0:内核检查是否有足够的内存共当前进程使用,没有则会返回错误给进程;
- 2:内核允许分配超过物理内存和交换空间总和的内存;
启动Redis
1 2 3
| systemctl start redis systemctl enable redis systemctl status redis
|
源码方式安装
安装依赖
1
| yum install -y gcc gcc-c++
|
下载并编译
1 2 3 4
| wget https://github.com/antirez/redis/archive/5.0.3.tar.gz tar zxf 5.0.3.tar.gz cd redis-5.0.3 make PREFIX=/usr/local/redis install
|
设置环境变量和系统参数
1 2
| echo "PATH=$PATH:/usr/local/redis/bin" >> /etc/profile source /etc/profile
|
安装后会在/usr/local/redis
下生成安装目录,安装的命令有如下:
1 2 3 4 5 6
| # /usr/local/redis/bin redis-benchmark:性能测试工具 redis-check-aof:文件修复工具 redis-check-dump:rbd文件检查工具 redis-cli:客户端命令行工具 redis-server:服务启动命令
|
设置系统参数:
1 2
| sysctl vm.overcommit_memory=1 echo "sysctl vm.overcommit_memory=1" >> /etc/rc.local
|
vm.overcommit_memory
是控制内存分配策略的参数:
- 1:内核分配所有的物理内存而不管当前内存状态;
- 0:内核检查是否有足够的内存共当前进程使用,没有则会返回错误给进程;
- 2:内核允许分配超过物理内存和交换空间总和的内存;
设置配置文件
编辑配置文件/usr/local/redis/redis.conf
,修改daemonize
参数为下面的值,让redis在后台运行:
如果没有redis.conf
,可以拷贝源码包中的配置文件过去
启动Redis
1 2 3 4 5 6 7
| redis-server /usr/loca/redis/redis.conf
ps -ef | grep redis root 10983 1 0 13:57 ? 00:00:00 redis-server 127.0.0.1:6379
netstat -ntlp | grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 10983/redis-server
|
连接Redis
1 2
| redis-cli 127.0.0.1:6379>
|
停止Redis