1.应用场景

主要用于Linux下安装Redis server.

2.学习/操作

环境

阿里云ECS  腾讯云ECS   // 两者相同配置, 均是centOS7  【7.5/7.8】


方式一:编译安装


1. 使用以下命令下载,提取和编译Redis

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

Note:

这里进入/usr/local/bin 目录下 [只是推荐, 不是强制要求]

linux怎么查看redis服务起来了没 查看redis状态 linux_redis

安装出现失败,

原因: gcc的版本低于5, 当前gcc版本为 4.8 

解决办法: 先升级gcc版本  具体操作过程后补.

详见: http://www.redis.cn/download.html


升级步骤

请先检查gcc的版本是否低于5,如果是请先升级,可以使用以下命令:
sudo yum install centos-release-scl
sudo yum install devtoolset-7-gcc*
scl enable devtoolset-7 bash

下面已经升级到7.3

linux怎么查看redis服务起来了没 查看redis状态 linux_Linux_02


2.src 目录 中现在提供了已编译的二进制文件  // 上面编译后的产物,源文件中是没有的

使用以下命令运行Redis

$ src/redis-server

3. 您可以使用内置客户端与Redis进行交互:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

4. 配置

参见:下方 7.2 配置


官网提供了在线交互式教程。[Try Redis]

如下:

linux怎么查看redis服务起来了没 查看redis状态 linux_Redis_03


Note:

在腾讯云ECS配置下安装Redis, 

下载压缩包速度明显慢.

linux怎么查看redis服务起来了没 查看redis状态 linux_Redis_04


方式二: yum 安装

1. sudo yum install epel-release

linux怎么查看redis服务起来了没 查看redis状态 linux_安装_05

linux怎么查看redis服务起来了没 查看redis状态 linux_redis_06


2. sudo yum install redis

linux怎么查看redis服务起来了没 查看redis状态 linux_Linux_07


3. sudo systemctl start redis.service  // 启动服务

linux怎么查看redis服务起来了没 查看redis状态 linux_Redis_08

4. sudo systemctl enable redis  // 开机启动服务

linux怎么查看redis服务起来了没 查看redis状态 linux_安装_09


5. 查看redis进程

ps -ef | grep redis

linux怎么查看redis服务起来了没 查看redis状态 linux_linux_10


6. 命令行下交互测试

redis-cli 

set name williamning

get name

linux怎么查看redis服务起来了没 查看redis状态 linux_linux_11


7.设置远程连接

7.1 关闭防火墙 //关闭之前面, 查看防火墙服务状态

systemctl status firewalld

firewall-cmd --state

linux怎么查看redis服务起来了没 查看redis状态 linux_linux_12


Note:

linux怎么查看redis服务起来了没 查看redis状态 linux_安装_13

由上可知:

之前防火墙一直处于关闭状态,相当于所有端口都是开放状态,[即如: 80, 3306,6379 ]  //这种操作相当危险, 很容易被攻击

打开防火墙之后, 查看80端口尚未开放,需要另外操作80端口, 3306端口开放.


打开防火墙,之后尚未开放80,3306端口的结果, 截图如下:

可以看到数据库可视化工具不能远程连接到数据库服务器,同时web页面也出现500报错。//这里都是因为3306端口尚未开放

linux怎么查看redis服务起来了没 查看redis状态 linux_Redis_14

linux怎么查看redis服务起来了没 查看redis状态 linux_Redis_15


解决办法:

开放3306端口即可,然后重启防火墙

firewall-cmd --permanent --add-port=3306/tcp 

service firewalld restart

linux怎么查看redis服务起来了没 查看redis状态 linux_Linux_16


结果如下:

linux怎么查看redis服务起来了没 查看redis状态 linux_linux_17

linux怎么查看redis服务起来了没 查看redis状态 linux_linux_18


但是:

如果远程连接访问redis server

需要开放6379与80端口


7.2 修改配置  //此时仍然不能远程连接redis server 因为配置文件中默认限制只有本机才可以访问redis

7.2.1 vi /etc/redis.conf  进入编辑模式

注释掉 bind 127.0.0.1 ,否则只有本机才能访问.

linux怎么查看redis服务起来了没 查看redis状态 linux_安装_19

注释后:

linux怎么查看redis服务起来了没 查看redis状态 linux_安装_20

7.2.2 保护模式修改为no 以及 端口修改[默认为6357 可不改]

文档中原话://简单理解就是:一层保护层,防止被访问与利用

Protected mode is a layer of security protection, in order to avoid that
Redis instances left open on the internet are accessed and exploited

linux怎么查看redis服务起来了没 查看redis状态 linux_Linux_21

修改后:

linux怎么查看redis服务起来了没 查看redis状态 linux_Linux_22


7.2.3 修改密码为 xxxxx  //该密码为登陆redis所需要的密码,请记好, 另外请使用强密码

Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.


警告:由于Redis是相当快的外部用户可以尝试
每秒150k个密码。
这意味着你应该
使用强密码,否则很容易被破解。

linux怎么查看redis服务起来了没 查看redis状态 linux_linux_23

修改后: 

省略.


补充://20200824 

设置最大内存大小以及缓存淘汰策略

linux怎么查看redis服务起来了没 查看redis状态 linux_linux_24


7.2.4 保存并退出编辑重启redis

systemctl restart redis

linux怎么查看redis服务起来了没 查看redis状态 linux_linux_25


7.2.5 重新检测是否能正常与Redis交互

本机交互:

redis-cli -h 127.0.0.1 -p 6379 -a password

简写:

redis-cli -a xxx 回车即可

linux怎么查看redis服务起来了没 查看redis状态 linux_Redis_26

成功交互.


客户端远程连接 [redisClient]

linux怎么查看redis服务起来了没 查看redis状态 linux_Redis_27


成功连接.




后续补充

...

3.问题/补充

1.配置Redis过程中出现, Failed to start Redis persistent key-value database. 报错

原因: TBD

解决办法: 最合适的办法暂时为找到.

最后重新卸载, 然后重新安装实验,结果不再报错.

//Linux Yum


Note:

安装出现失败原因: gcc的版本低于5, 当前gcc版本为 4.8 

解决办法: 先升级gcc版本  具体操作过程后补.

详见: ttp://www.redis.cn/download.html

4.参考

Redis  //官网 - 下载 以及编译安装步骤

centos7 - Unable to install latest stable Release of Redis on CentOS 7 - Stack Overflow  //yum 安装

后续补充

...