redis 安装

本文详细介绍了在CentOS 6.1环境下安装Redis 4.0.6的步骤,包括下载安装包、依赖安装、编译安装以及三种启动方式:直接执行、后台进程启动和设置开机自启动。通过本文,读者可以全面了解Redis的安装配置过程。

一、 安装环境信息

Linux 操作系统: centos6.1

Redis版本:redis-4.0.6.tar.gz

二、redis 安装

第一步:获取redis安装包到路径
1)官网地址 :https://redis.io/ ,下载reids安装包

2)通过wget http://download.redis.io/releases/redis-4.0.6.tar.gz 在线下载redis安装包

以下用wget获取安装包并安装redis服务:

[root@zero tools]# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
--2019-07-01 13:51:30--  http://download.redis.io/releases/redis-4.0.6.tar.gz
Resolving download.redis.io... 109.74.203.151
Connecting to download.redis.io|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1723533 (1.6M) [application/x-gzip]
Saving to: “redis-4.0.6.tar.gz”

100%[===================================================================================================================>] 1,723,533   10.2K/s   in 2m 50s  

2019-07-01 13:54:21 (9.88 KB/s) - “redis-4.0.6.tar.gz” saved [1723533/1723533]



第二步:解压压缩包

[root@zero tools]# tar -zxvf  redis-4.0.6.tar.gz

[root@zero tools]# ll
total 56124
drwxrwxr-x. 6 root root     4096 Dec  5  2017 redis-4.0.6
-rw-r--r--. 1 root root  1723533 Dec  5  2017 redis-4.0.6.tar.gz


第三步:redis安装需要gcc依赖,需要先安装完成在安装reids服务,通过在线安装gcc依赖,直接通过yum安装gcc依赖

[root@zero tools]# yum install gcc

Dependencies Resolved

=============================================================================================================================================================
 Package                                 Arch                            Version                                      Repository                        Size
=============================================================================================================================================================
Installing:
 gcc                                     x86_64                          4.4.7-23.el6                                 base                              10 M
Installing for dependencies:
 cloog-ppl                               x86_64                          0.15.7-1.2.el6                               base                              93 k
 cpp                                     x86_64                          4.4.7-23.el6                                 base                             3.7 M
 glibc-devel                             x86_64                          2.12-1.212.el6_10.3                          updates                          991 k
 glibc-headers                           x86_64                          2.12-1.212.el6_10.3                          updates                          620 k
 kernel-headers                          x86_64                          2.6.32-754.15.3.el6                          updates                          4.6 M
 mpfr                                    x86_64                          2.4.1-6.el6                                  base                             157 k
 ppl                                     x86_64                          0.10.2-11.el6                                base                             1.3 M
Updating for dependencies:
 glibc                                   x86_64                          2.12-1.212.el6_10.3                          updates                          3.8 M
 glibc-common                            x86_64                          2.12-1.212.el6_10.3                          updates                           14 M
 libgcc                                  x86_64                          4.4.7-23.el6                                 base                             104 k
 libgomp                                 x86_64                          4.4.7-23.el6                                 base                             135 k
 tzdata                                  noarch                          2019a-1.el6                                  updates                          513 k

Transaction Summary
=============================================================================================================================================================
Install       8 Package(s)
Upgrade       5 Package(s)
ownloading Packages:
(1/13): cloog-ppl-0.15.7-1.2.el6.x86_64.rpm                                                                                           |  93 kB     00:00     
(2/13): cpp-4.4.7-23.el6.x86_64.rpm                                                                                                   | 3.7 MB     00:06     
(3/13): gcc-4.4.7-23.el6.x86_64.rpm                                                                                                   |  10 MB     00:17     
(4/13): glibc-2.12-1.212.el6_10.3.x86_64.rpm                                                                                          | 3.8 MB     00:06     
(5/13): glibc-common-2.12-1.212.el6_10.3.x86_64.rpm                                                                                   |  14 MB     00:27     
(6/13): glibc-devel-2.12-1.212.el6_10.3.x86_64.rpm                                                                                    | 991 kB     00:01     
(7/13): glibc-headers-2.12-1.212.el6_10.3.x86_64.rpm                                                                                  | 620 kB     00:01     
(8/13): kernel-headers-2.6.32-754.15.3.el6.x86_64.rpm                                                                                 | 4.6 MB     00:08     
(9/13): libgcc-4.4.7-23.el6.x86_64.rpm                                                                                                | 104 kB     00:00     
(10/13): libgomp-4.4.7-23.el6.x86_64.rpm                                                                                              | 135 kB     00:00     
(11/13): mpfr-2.4.1-6.el6.x86_64.rpm                                                                                                  | 157 kB     00:00     
(12/13): ppl-0.10.2-11.el6.x86_64.rpm                                                                                                 | 1.3 MB     00:01     
(13/13): tzdata-2019a-1.el6.noarch.rpm  
Installed:
  gcc.x86_64 0:4.4.7-23.el6                                                                                                                                  

Dependency Installed:
  cloog-ppl.x86_64 0:0.15.7-1.2.el6           cpp.x86_64 0:4.4.7-23.el6 glibc-devel.x86_64 0:2.12-1.212.el6_10.3 glibc-headers.x86_64 0:2.12-1.212.el6_10.3
  kernel-headers.x86_64 0:2.6.32-754.15.3.el6 mpfr.x86_64 0:2.4.1-6.el6 ppl.x86_64 0:0.10.2-11.el6              

Dependency Updated:
  glibc.x86_64 0:2.12-1.212.el6_10.3      glibc-common.x86_64 0:2.12-1.212.el6_10.3      libgcc.x86_64 0:4.4.7-23.el6      libgomp.x86_64 0:4.4.7-23.el6     
  tzdata.noarch 0:2019a-1.el6            

Complete!


第三步:编译安装

[root@zero redis]# make MALLOC=libc
cd src && make all
make[1]: Entering directory `/usr/local/redis/src'
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb
    INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/local/redis/src'


三、启动redis的三种方式

切换当前到目录到redis/src 路径下:

[root@zero redis]# cd src/
[root@zero src]# 


1、直接执行./redis-server 启动redis服务

[root@zero src]# ./redis-server 
19836:C 27 Jun 16:02:05.388 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
19836:C 27 Jun 16:02:05.388 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=19836, just started
19836:C 27 Jun 16:02:05.388 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
19836:M 27 Jun 16:02:05.388 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 19836
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

19836:M 27 Jun 16:02:05.389 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
19836:M 27 Jun 16:02:05.389 # Server initialized
19836:M 27 Jun 16:02:05.389 # 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.
19836:M 27 Jun 16:02:05.389 * DB loaded from disk: 0.000 seconds
19836:M 27 Jun 16:02:05.389 * Ready to accept connections


备注:最直接到启动方式,但这种方式不建议使用,启动服务以后服务redis窗口就不能关闭,窗口需要一直开着,导致关闭窗口就会停止redis服务

2、以后台进程方式启动redis服务

第一步:修改redis.conf文件
打开redis到配置文件redis.conf文件,找到daemonize属性值

[root@zero redis]# vim  redis.comf 

################################# 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 ,默认配置no不为默认守护进程
daemonize yes  


:wq



修改配置文件保存:wq 保存退出当前文件

第二步:启动redis ,通过制定配置文件redis.conf

[root@zero redis]# src/redis-server redis.conf 
20169:C 27 Jun 16:09:38.423 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20169:C 27 Jun 16:09:38.423 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=20169, just started
20169:C 27 Jun 16:09:38.423 # Configuration loaded


查看当前reids是否启动成功

[root@zero redis]# ps -ef|grep redis
root     20170     1  0 16:09 ?        00:00:00 src/redis-server 127.0.0.1:6379
root     20175 16254  0 16:09 pts/1    00:00:00 grep redis
[root@zero redis]# 


可以看到有一个20170到进程,同时可以通过redis客户端操作看是否正到启动成功

[root@zero redis]# ./src/redis-cli 
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1 
"value1"
127.0.0.1:6379> 



以上操作可以对redis 数据到添加和获取

使用kill命令结束刚刚启动到redis后台进程

[root@zero redis]# kill  -9  20170      



3、设置redis开机自启动,也是推荐到一种启动方式

1、在/etc目录下新建redis目录

[root@zero etc]# mkdir redis   



2、将/usr/local/redis-4.0.6/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf

[root@zero etc]# cp /usr/local/redis/redis.conf  redis/6379.conf


3、将redis的启动脚本复制一份放到/etc/init.d目录下

[root@zero etc]# cp /usr/local/redis/utils/redis_init_script init.d/redisd


4、设置redis开机自启动,先切换到/etc/init.d目录下执行自启命令

[root@zero etc]# chkconfig redisd on
service redisd does not support chkconfig


 
结果显示是redisd不支持chkconfig

解决方法:

使用vim编辑redisd文件,在第一行加入如下两行注释,保存退出

 

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database


注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

再次执行开机自启命令,成功

[root@zero etc]# chkconfig redisd on



现在可以直接已服务的形式启动和关闭redis了启动:

service redisd start

[root@zero etc]# service redisd start
Starting Redis server...
20898:C 27 Jun 16:23:30.305 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20898:C 27 Jun 16:23:30.305 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=20898, just started
20898:C 27 Jun 16:23:30.305 # Configuration loaded
[root@zero etc]# ps -ef|grep redis
root     20899     1  0 16:23 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379      
root     20904 16254  0 16:23 pts/1    00:00:00 grep redis

 
关闭:

service redisd stop
root@zero etc]# service redisd stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped


 

### 安装 Redis 数据库 Redis 是一个高性能的开源内存数据库,支持多种数据结构,适用于缓存、实时分析、消息队列等应用场景。以下是 Redis 在不同操作系统上的安装方法。 #### 在 Linux 系统上安装 Redis 1. **下载 Redis** 首先,访问 Redis 官网下载最新版本的源代码包。使用 `wget` 命令下载: ```bash wget https://download.redis.io/redis-stable.tar.gz ``` 2. **解压文件** 解压下载的压缩包,并进入解压后的目录: ```bash tar -zxvf redis-stable.tar.gz cd redis-stable ``` 3. **编译 Redis** 使用 `make` 命令编译 Redis 源代码。如果系统中没有安装 `gcc` 编译器,可以先通过以下命令安装: ```bash sudo apt-get install build-essential ``` 然后进行编译: ```bash make ``` 4. **安装 Redis** 编译完成后,执行 `make install` 命令将 Redis 安装到系统目录中: ```bash sudo make install ``` 5. **配置 Redis** 默认情况下,Redis 的配置文件 `redis.conf` 位于源代码目录中。可以将其复制到 `/etc/redis` 目录下,并修改配置文件中的 `daemonize` 参数为 `yes`,以便以后台模式运行 Redis: ```bash sudo cp redis.conf /etc/redis/ sudo nano /etc/redis/redis.conf ``` 6. **启动 Redis** 修改完配置文件后,可以通过以下命令启动 Redis: ```bash redis-server /etc/redis/redis.conf ``` 7. **测试 Redis** 使用 `redis-cli` 工具连接到 Redis 服务器,并执行简单的命令进行测试: ```bash redis-cli ping ``` 如果返回 `PONG`,则表示 Redis 服务器已经成功启动。 #### 在 Windows 系统上安装 Redis 1. **下载 Redis Windows 版本** Redis 官方并不直接提供 Windows 版本的安装包,但可以从 Microsoft 的 GitHub 仓库下载适用于 Windows 的 Redis: ```bash https://github.com/microsoftarchive/redis/releases ``` 2. **安装 Redis** 下载完成后,解压压缩包,并将解压后的文件夹移动到一个合适的位置,例如 `C:\Redis`。 3. **启动 Redis** 进入 Redis 目录,执行 `redis-server.exe` 文件启动 Redis 服务器: ```bash cd C:\Redis redis-server.exe ``` 如果没有指定配置文件,Redis 将使用默认配置启动。为了避免错误,建议创建一个 `redis.conf` 配置文件,并通过以下命令启动 Redis: ```bash redis-server.exe redis.conf ``` 4. **测试 Redis** 使用 `redis-cli.exe` 工具连接到 Redis 服务器,并执行简单的命令进行测试: ```bash redis-cli.exe ping ``` 如果返回 `PONG`,则表示 Redis 服务器已经成功启动。 #### 在 macOS 系统上安装 Redis 1. **使用 Homebrew 安装 Redis** 如果你使用的是 macOS,并且已经安装了 Homebrew,可以直接通过以下命令安装 Redis: ```bash brew install redis ``` 2. **启动 Redis** 安装完成后,可以通过以下命令启动 Redis 服务: ```bash brew services start redis ``` 3. **测试 Redis** 使用 `redis-cli` 工具连接到 Redis 服务器,并执行简单的命令进行测试: ```bash redis-cli ping ``` 如果返回 `PONG`,则表示 Redis 服务器已经成功启动。 #### 注意事项 - 在生产环境中,建议使用后台模式启动 Redis,并配置合适的持久化策略,以确保数据的安全性[^3]。 - 如果在 Windows 上启动 Redis 时遇到端口冲突问题,可以检查是否有其他程序占用了 6379 端口,或者尝试更改 Redis 的监听端口[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值