Centos安装Redis(保姆级)

文章目录

  • NoSQL
  • 适用场景
  • 不适用场景
  • Redis
    • 概述
    • 安装
      • 下载Redis
      • 安装C语言的编译环境
      • 解压redis压缩文件
      • 进入解压后的文件夹内
      • 编译文件
      • 安装
      • 安装目录
      • 前台启动(不推荐)
      • 后台启动(推荐)
      • 访问redis测试通讯状态
      • 退出redis-cli
      • 关闭redis
      • 设置密码
      • 配置Redis可远程访问
      • 注释redis默认的只允许本机访问
      • 注释redis默认的开启保护模式

NoSQL

Not Only SQL:泛指非关系型数据库,不依赖业务逻辑方式存储,而是简单的key-value模式存储,因此大大增加了数据库的扩展能力;

  • 不遵循SQL标准
  • 不支持ACID(四大特性)
  • 远超于SQL的性能

适用场景

  • 堆数据高并发的读写
  • 海亮数据的读写
  • 对数据的高可扩展性

不适用场景

  • 需要事务支持
  • 基于sql的结构化查询存储,处理复杂的关系

Redis

概述

  • Redis是一个开源的key-value存储系统
  • 支持存储value类型包括String(字符串)、list(链表)、set(集合)、zset(sorted set – 有序集合)、hash(哈希类型)
  • 所有数据类型都支持push/pop、add/remove及取交集并集差集以及更丰富的操作,且操作都是原子性的
  • Redis支持各种不同方式的排序
  • 数据都缓存在内存中,但是会周期性的把更新的数据写入磁盘或把修改操作写入追加的记录文件
  • 实现了master-slave(主从)同步

安装

下载Redis

Redis官网所有版本下载地址

将下载的tar.gz文件放入服务器
我放在opt目录下

[root@ecs-340806 /]# cd opt/
[root@ecs-340806 opt]# ll
total 2384
-rw-r--r-- 1 root root 2438367 Aug  5 15:55 redis-6.2.1.tar.gz
[root@ecs-340806 opt]# 

安装C语言的编译环境

只需要gcc环境所以可以直接安装gcc

  • 联网安装GCC
yum install gcc
  • 查看gcc版本
gcc --version
[root@ecs-340806 opt]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

解压redis压缩文件

[root@ecs-340806 opt]# tar -zxvf redis-6.2.1.tar.gz 

解压完文件名ls查看–redis-6.2.1

[root@ecs-340806 opt]# ll
total 2388
drwxrwxr-x 7 root root    4096 Mar  2  2021 redis-6.2.1
-rw-r--r-- 1 root root 2438367 Aug  5 15:55 redis-6.2.1.tar.gz

进入解压后的文件夹内

[root@ecs-340806 opt]# cd redis-6.2.1

编译文件

[root@ecs-340806 opt]# make

如果报错,请检查gcc版本,如果有gcc,执行make distclean命令把编译文件清除,再执行make

安装

编译完成后执行make install命令安装

[root@ecs-340806 redis-6.2.1]# make install
[root@ecs-340806 redis-6.2.1]# make install
cd src && make install
make[1]: Entering directory `/opt/redis-6.2.1/src'
    CC Makefile.dep
make[1]: Leaving directory `/opt/redis-6.2.1/src'
make[1]: Entering directory `/opt/redis-6.2.1/src'

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

    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/opt/redis-6.2.1/src'
[root@ecs-340806 redis-6.2.1]# 

如果权限不够请用sudo

sudo make install

安装目录

安装完成后默认安装在/usr/local/bin

[root@ecs-340806 redis-6.2.1]# cd /usr/local/bin/
[root@ecs-340806 bin]# ls
cloud-id    cloud-init-per  jsonpatch    jsonschema  redis-benchmark  redis-check-rdb  redis-sentinel
cloud-init  jsondiff        jsonpointer  normalizer  redis-check-aof  redis-cli        redis-server
[root@ecs-340806 bin]# ll
total 18876
-rwxr-xr-x 1 root root     390 Feb 10 17:15 cloud-id
-rwxr-xr-x 1 root root     394 Feb 10 17:15 cloud-init
-rwxr-xr-x 1 root root    2108 Feb 10 17:15 cloud-init-per
-rwxr-xr-x 1 root root    1003 Feb 10 17:15 jsondiff
-rwxr-xr-x 1 root root    3858 Feb 10 17:15 jsonpatch
-rwxr-xr-x 1 root root    1837 Feb 10 17:15 jsonpointer
-rwxr-xr-x 1 root root     397 Feb 10 17:15 jsonschema
-rwxr-xr-x 1 root root     424 Feb 10 17:15 normalizer
-rwxr-xr-x 1 root root 4833352 Aug  5 16:10 redis-benchmark
lrwxrwxrwx 1 root root      12 Aug  5 16:10 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root      12 Aug  5 16:10 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 5003368 Aug  5 16:10 redis-cli
lrwxrwxrwx 1 root root      12 Aug  5 16:10 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 9450208 Aug  5 16:10 redis-server
[root@ecs-340806 bin]# 

redis-benchmark:性能测试工具,可以看看服务器性能如何
redis-check-aof:修复有问题的AOF文件
redis-check-rdb:修复有问题的dump.rdb文件
redis-sentinel:集群使用

redis-server:Redis服务器启动命令
redis-cli:客户端操作入口

前台启动(不推荐)

前台启动(不推荐)

redis-server

命令行窗口不能关闭,否则服务器停止

29679:C 05 Aug 2022 16:21:49.109 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29679:C 05 Aug 2022 16:21:49.109 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=29679, just started
29679:C 05 Aug 2022 16:21:49.109 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
29679:M 05 Aug 2022 16:21:49.110 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 29679
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

29679:M 05 Aug 2022 16:21:49.110 # Server initialized
29679:M 05 Aug 2022 16:21:49.110 # 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.
29679:M 05 Aug 2022 16:21:49.110 * Ready to accept connections

后台启动(推荐)

  • 进入/opt目录解压的文件内
[root@ecs-340806 /]# cd /opt/redis-6.2.1
[root@ecs-340806 redis-6.2.1]# ls
00-RELEASENOTES  CONTRIBUTING  INSTALL    README.md   runtest-cluster    sentinel.conf  TLS.md
BUGS             COPYING       Makefile   redis.conf  runtest-moduleapi  src            utils
CONDUCT          deps          MANIFESTO  runtest     runtest-sentinel   tests
[root@ecs-340806 redis-6.2.1]# 

  • 复制redis.conf文件(redis配置文件)到其他文件夹内
[root@ecs-340806 redis-6.2.1]# cp redis.conf /etc/redis.conf
  • 来到 /etc 目录下
[root@ecs-340806 redis-6.2.1]# cd /etc
  • 修改 /etc 目录下redis.conf文件中允许后台启动的配置项
[root@ecs-340806 etc]# vi redis.conf 
  • 搜索相关关键字
    直接 /daem
/daem
# By default, TLS session caching is enabled to allow faster and less expensive
# reconnections by clients that support it. Use the following directive to disable
# caching.
#
# tls-session-caching no

# Change the default number of TLS sessions cached. A zero value sets the cache
# to unlimited size. The default size is 20480.
#
# tls-session-cache-size 5000

# Change the default timeout of cached TLS sessions. The default timeout is 300
# seconds.
#
# tls-session-cache-timeout 60

################################# 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.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#                        requires "expect stop" in your upstart job config
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#                        on startup, and updating Redis status on a regular
#                        basis.
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous pings back to your supervisor.
#
# The default is "no". To run under upstart/systemd, you can simply uncomment
# the line below:
#
search hit BOTTOM, continuing at TOP
  • 修改配置
################################# 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.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no

将上面no 改为yes即可

按 i 进入编辑模式修改

################################# 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.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize yes

修改完后 按esc 输入 :wq! 保存退出

  • 进入安装目录 /usr/local/bin
[root@ecs-340806 etc]# cd /usr/local/bin/
[root@ecs-340806 bin]# ls
cloud-id        dump.rdb   jsonpointer  redis-benchmark  redis-cli
cloud-init      jsondiff   jsonschema   redis-check-aof  redis-sentinel
cloud-init-per  jsonpatch  normalizer   redis-check-rdb  redis-server
[root@ecs-340806 bin]# 
  • 指定配置文件启动Redis
[root@ecs-340806 bin]# redis-server /etc/redis.conf 
  • 查看redis运行
[root@ecs-340806 bin]# ps -ef | grep redis
root     30939     1  0 16:41 ?        00:00:00 redis-server 127.0.0.1:6379
root     30988 23132  0 16:42 pts/0    00:00:00 grep --color=auto redis
[root@ecs-340806 bin]# 

访问redis测试通讯状态

[root@ecs-340806 bin]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

退出redis-cli

关闭redis

方式一:

  • redis-cli客户端进入redis后输入shutdown关闭

方式二:

  • 找到redis的进程使用kill -9 port杀死进程

设置密码

客户端进入redis-cli:

[root@ecs-340806 ~]# redis-cli
127.0.0.1:6379> config set requirepass 123456
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"
127.0.0.1:6379> 

此方法临时有效

  • 建议设置密码,不然会被别人乱搞,包括且不限于被别人用来挖矿

或者可以修改配置文件将已经注释的密码那个地方注释去掉,然后将后面的值改为你需要的密码,效果一样,此方法永久有效

带密码访问redis:

redis-cli -a password

配置Redis可远程访问

修改启动的配置文件

注释redis默认的只允许本机访问

# bind 127.0.0.1 -::1

注释redis默认的开启保护模式

当开启了此模式,限制为本地访问,关闭此模式,设置为no,
允许所有外部的网络直接访问redis服务。

protected-mode no

一般线上部署如果应用在本机

#本机ip或者改成应用服务所在的ip
bind 127.0.0.1
#保护模式关闭
protected-mode no
#redis密码设置
requirepass redispwd

线下调试

#我们要允许开发环境也可以连接到redis,或者注释掉
bind 0.0.0.0
#保护模式关闭
protected-mode no
#redis密码设置
requirepass redispwd

你可能感兴趣的:(Redis,redis,centos,数据库)