Redis(一) Centos7 安装redis-5.0.7

Redis(一) Centos7安装redis-5.0.7
1.NoSql 简介

NoSql(Not Only Sql) 不仅仅是数据库,泛指非关系型数据库

  • 不依赖业务逻辑方式存储,e而是以key-value模式存储,因此大大的增加了数据库的扩展能力
  • 不遵循SQL 标准
  • 不支持ACID(事务)
  • 远超Sql的性能
NoSql 适用场景
  • 对数据的高并发读写
  • 海量数据的读写
  • 对数据的高可扩展性
NoSql 不适用场景
  • 需要事务支持
  • 基于sql的结构化查询存储,处理复杂的关系,需要即席查询(条件查询)

用不着Sql,Sql 无法解决的情况下,可考虑使用NoSql

常用非关系数据库

Memcached, Redis, mongoDB, Hbase…

2.Redis 简介

          Redis(全称:Remote Dictionary Server 远程字典服务)是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
          和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)list(链表)set(集合)zset(sorted set --有序集合)hash(哈希类型)
          这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,Redis支持各种不同方式的排序。
          与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是Redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

3.CentOs7 安装 redis
1) 从官网下载安装压缩包

这里下载的是 5.0.7 版本
wget http://download.redis.io/releases/redis-5.0.7.tar.gz

[root@MyCnetos7 redis]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
2) 解压redis 下载安装包
tar -zxvf redis-5.0.7.tar.gz -C 你所要放置到的目录
3) yum 安装gcc 依赖
yum install gcc
yum install gcc-c++
4) 编译安装

重新进入到Redis的目录中执行

make MALLOC=libc
[root@MyCnetos7 redis]# cd redis-5.0.7
[root@MyCnetos7 redis-5.0.7]# ll
total 272
-rw-rw-r--.  1 root root 115100 Nov 19 17:05 00-RELEASENOTES
-rw-rw-r--.  1 root root     53 Nov 19 17:05 BUGS
-rw-rw-r--.  1 root root   2381 Nov 19 17:05 CONTRIBUTING
-rw-rw-r--.  1 root root   1487 Nov 19 17:05 COPYING
drwxrwxr-x.  6 root root    124 Nov 19 17:05 deps
-rw-rw-r--.  1 root root     11 Nov 19 17:05 INSTALL
-rw-rw-r--.  1 root root    151 Nov 19 17:05 Makefile
drwxrwxr-x.  3 root root   8192 Feb 21 00:54 src
....
[root@MyCnetos7 redis-5.0.7]# make MALLOC=libc
5) 继续编译

生成了src目录文件之后,进入src

cd src
make install
[root@MyCnetos7 redis-5.0.7]# cd src
[root@MyCnetos7 src]# ll
total 28576
-rw-rw-r--. 1 root root   10291 Nov 19 17:05 adlist.c
-rw-rw-r--. 1 root root    3512 Nov 19 17:05 adlist.h
-rw-r--r--. 1 root root   20232 Feb 21 00:54 adlist.o
-rw-rw-r--. 1 root root   17645 Nov 19 17:05 ae.c
-rw-rw-r--. 1 root root    4846 Nov 19 17:05 ae_epoll.c
-rw-rw-r--. 1 root root   10939 Nov 19 17:05 ae_evport.c
-rw-rw-r--. 1 root root    5338 Nov 19 17:05 ae.h
-rw-rw-r--. 1 root root    4567 Nov 19 17:05 ae_kqueue.c
-rw-r--r--. 1 root root   38184 Feb 21 00:54 ae.o
-rw-rw-r--. 1 root root    3828 Nov 19 17:05 ae_select.c
-rw-rw-r--. 1 root root   20701 Nov 19 17:05 anet.c
...
[root@MyCnetos7 src]# make install
    CC Makefile.dep

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

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
6) 查看默认安装目录 /usr/local/bin
[root@MyCnetos7 src]# cd /usr/local/bin
[root@MyCnetos7 bin]# ll
total 13024
-rwxr-xr-x. 1 root root  353720 Feb 21 00:58 redis-benchmark
-rwxr-xr-x. 1 root root 4058360 Feb 21 00:58 redis-check-aof
-rwxr-xr-x. 1 root root 4058360 Feb 21 00:58 redis-check-rdb
-rwxr-xr-x. 1 root root  799232 Feb 21 00:58 redis-cli
lrwxrwxrwx. 1 root root      12 Feb 21 00:58 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 4058360 Feb 21 00:58 redis-server
  • Redis-benchmark:性能测试工具,可以在自己本子运行,看看自己本子性能如何(服务启动起来后执行)
  • Redis-check-aof:修复有问题的AOF文件,rdb和aof
  • Redis-check-dump:修复有问题的dump.rdb文件
  • Redis-sentinel:Redis集群使用
  • redis-server:Redis服务器启动命令
  • redis-cli:客户端,操作入口
7) redis 启动

问题: 默认前台启动,redis-server 启动redis, 启动后当前窗口无法再做操作
解决:将redis服务启动改为后台启动

-rwxr-xr-x. 1 root root 4058360 Feb 21 00:58 redis-server
[root@MyCnetos7 bin]# redis-server
2855:C 21 Feb 2020 01:02:53.582 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2855:C 21 Feb 2020 01:02:53.582 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=2855, just started
2855:C 21 Feb 2020 01:02:53.582 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2855:M 21 Feb 2020 01:02:53.583 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 2855
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

2855:M 21 Feb 2020 01:02:53.585 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2855:M 21 Feb 2020 01:02:53.585 # Server initialized
2855:M 21 Feb 2020 01:02:53.585 # 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.
2855:M 21 Feb 2020 01:02:53.586 # 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.
2855:M 21 Feb 2020 01:02:53.586 * Ready to accept connections

7)redis 后台启动

修改redis.conf文件中的一项配置 daemonize 将no 改为yes,代表后台启动
注:修改配置文件之前尽量先备份一份

[root@MyCnetos7 redis-5.0.7]# cp redis.conf ./redis.conf.bk
vi redis.conf

后台启动:

daemonize yes

在这里插入图片描述

wq #保存退出
redis-server ./redis.conf 指定文件名启动
[root@MyCnetos7 redis-5.0.7]# redis-server ./redis.conf
2894:C 21 Feb 2020 01:18:38.360 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2894:C 21 Feb 2020 01:18:38.360 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=2894, just started
2894:C 21 Feb 2020 01:18:38.360 # Configuration loaded
9) 客户端访问

redis-cli

  • 如果有多个Redis同时启动,则需指定端口号访问 redis-cli -h 主机地址 -p 端口号
    eg: redis-cli -h 127.0.0.1 -p 6379
  • 退出客户端: ctrl+c 或者 exit
[root@MyCnetos7 redis-5.0.7]# redis-cli
127.0.0.1:6379> ping
PONG
5.远程连接
1) 开放6379 端口

firewall-cmd --zone=public --add-port=6379/tcp --permanent   永久开放6379 端口
firewall-cmd --reload    配置立即生效
firewall-cmd --zone=public --list-ports    查看已开放端口

[root@MyCnetos7 redis-5.0.7]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@MyCnetos7 redis-5.0.7]# firewall-cmd --reload
success
[root@MyCnetos7 redis-5.0.7]# firewall-cmd --zone=public --list-ports
22/tcp 8080/tcp 3306/tcp 1521/tcp 6379/tcp
2) 修改ip绑定

vi redis.conf
找到 bind 127.0.0.1

  • 将bind 127.0.0.1 改为bind 0.0.0.0或者注释该行
#bind 127.0.0.1 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FiGcLc80-1582254013124)(./QQ截图20200221105153.png)]

3) 允许外网访问
 # 把yes改成no,允许外网访问
protected-mode no 

在这里插入图片描述

4) 设置连接密码

找到 #requirepass foobared 这一行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Eo5cDMu-1582254013127)(./QQ截图20200221105001.png)]

5) 测试连接

redisdesktopmanager 远程连接
redisdesktopmanager 免费下载
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kFqHvBjz-1582254013128)(./QQ截图20200221105422.png)]

6)命令行密码登录
[root@MyCnetos7 redis-5.0.7]# redis-server
127.0.0.1:6379> auth "123456"
OK
127.0.0.1:6379> 
7) redis 服务关闭

进入客户端:直接 shutdown

[root@MyCnetos7 redis-5.0.7]# redis-cli shutdown

127.0.0.1:6379> auth "123456"
OK
127.0.0.1:6379> shutdown
not connected> 
6. 设置开机自启

1) 新建文件

vim /etc/init.d/redis

输入下面内容:

#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_6379.pid

CONF="/home/leyou/opt/redis/redis-5.0.7/redis.conf"

case "$1" in  
    start)  
        if [ -f $PIDFILE ]  
        then  
                echo "$PIDFILE exists, process is already running or crashed"  
        else  
                echo "Starting Redis server..."  
                $EXEC $CONF  
        fi  
        if [ "$?"="0" ]   
        then  
              echo "Redis is running..."  
        fi  
        ;;  
    stop)  
        if [ ! -f $PIDFILE ]  
        then  
                echo "$PIDFILE does not exist, process is not running"  
        else  
                PID=$(cat $PIDFILE)  
                echo "Stopping ..."  
                $REDIS_CLI -p $REDISPORT SHUTDOWN  
                while [ -x ${PIDFILE} ]  
               do  
                    echo "Waiting for Redis to shutdown ..."  
                    sleep 1  
                done  
                echo "Redis stopped"  
        fi  
        ;;  
   restart|force-reload)  
        ${0} stop  
        ${0} start  
        ;;  
  *)  
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2  
        exit 1  
esac
wq

注意:以下信息需要根据安装目录进行调整:

EXEC=/usr/local/bin/redis-server # 执行脚本的地址

REDIS_CLI=/usr/local/bin/redis-cli # 客户端执行脚本的地址

PIDFILE=/var/run/redis_6379.pid # 进程id文件地址

CONF="/home/leyou/opt/redis/redis-5.0.7" #配置文件地址

2)设置权限

chmod 755 /etc/init.d/redis

3)启动测试

/etc/init.d/redis start

启动成功会提示如下信息:

Starting Redis server...
Redis is running...

4)设置开机自启动

chkconfig --add /etc/init.d/redis
chkconfig redis on

5)关闭redis 服务

/etc/init.d/redis stop

报错

Stopping ...
(error) NOAUTH Authentication required.
Redis stopped

解决:

vi /etc/init.d/redis 

在这里插入图片描述
其他命令:

/etc/init.d/redis start  启动
/etc/init.d/redis restart  重新启动

6)创建软连接–快速执行命令

ln -s /etc/init.d/redis /usr/bin/redis
[root@hadoop01 redis-5.0.7]# ln -s /etc/init.d/redis /usr/bin/redis
[root@hadoop01 redis-5.0.7]# redis start
Starting Redis server...
42786:C 23 Apr 2020 07:09:49.856 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
42786:C 23 Apr 2020 07:09:49.856 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=42786, just started
42786:C 23 Apr 2020 07:09:49.856 # Configuration loaded
Redis is running...
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值