redis入门——安装篇

来源:互联网 发布:破解炒股软件 编辑:程序博客网 时间:2024/06/06 23:18

redis入门——安装篇

@(Redis)[redis, 入门, 安装]

  • redis入门安装篇
    • Redis基本介绍
    • redis的安装
      • windows版
      • mac版
      • linux版
    • redis的启动以linux为例
    • Redis的关闭以linux为例
    • redis配置认证密码

Redis基本介绍

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

Redis是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。

Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。

——参考《百度百科》

PS:Redis还支持集群,通过最少6个redis服务器,两个一组,一主一从,一共三组服务器,就能搭建出一个最小型的Redis服务器集群了。

redis的安装

windows版

测试学习使用,生产环境不使用。
参考:Windows 64位下安装Redis详细教程

mac版

使用brew工具的brew install redis安装,如果没有brew命令行工具,可以到官网Homebrew配置。

linux版

linux版的可以到https://redis.io/或者是http://www.redis.cn/ 下载需要版本的源代码。

因为是源代码且底层是c开发的,所以需要c的编译环境。

如果make命令能够使用,则证明已经有了c环境了。没有c环境则可以使用yum install gcc-c++

安装步骤
1. 把redis的源码包上传到linux系统,如果直接在Linux环境下下载的,不需要这步。
2. 使用tar -zxvf redis-3.x.x.tar.gz解压源码包
3. 进入解压出来的目录redis-3.x.x
4. 使用make install PREFIX=编译源码,生成可执行文件
- PREFIX参数:指定安装目录

redis的启动(以linux为例)

使用cd /usr/local/redis/bin命令进入到redis的命令目录。

[root@iZ28xuvyce3Z bin]# pwd/usr/local/redis/bin[root@iZ28xuvyce3Z bin]# lsredis-benchmark  redis-check-dump  redis-sentinelredis-check-aof  redis-cli         redis-server[root@iZ28xuvyce3Z bin]# 
  1. 前端启动模式,默认的启动方式。会占用一个终端启动,一般不使用。

    • redis-server:就是服务端。
      • PS:使用ctrl+c可以关闭服务器。
    • redis-cli:redis的客户端
  2. 后端启动模式,推荐使用此模式。

    • 从刚刚解压出来的redis源码包中复制一个文件redis.conf,复制到安装目录
      • 使用cp ${redis解压目录}/redis.conf /usr/local/redis/redis.conf
[root@iZ28xuvyce3Z bin]# lsredis-benchmark  redis-check-dump  redis.conf      redis-serverredis-check-aof  redis-cli         redis-sentinel
    • 修改redis.conf
# 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

PS:因为我这台服务器上已经装了redis,且使用了默认端口6379了,所以我将配置文件中的端口修改为6401

# Accept connections on the specified port, default is 6379.# If port 0 is specified Redis will not listen on a TCP socket.port 6401
    • 启动redis
[root@iZ28xuvyce3Z bin]# ./redis-server redis.conf
    • 查看进程
[root@iZ28xuvyce3Z bin]# ps aux | grep redisroot       975  0.0  0.7 140840  7456 ?        Ssl  14:43   0:00 ./redis-server 127.0.0.1:6401root       993  0.0  0.0 110216   900 pts/0    S+   14:43   0:00 grep --color=auto redisroot      1892  0.1  0.6 140836  6572 ?        Ssl  Aug19 146:40 /usr/local/redis/bin/redis-server 127.0.0.1:6379

PS:可以看到,第一个进程就是刚刚开启的redis服务器进程。

Redis的关闭(以linux为例)

[root@iZ28xuvyce3Z bin]# kill 975[root@iZ28xuvyce3Z bin]# ps aux | grep redisroot      1105  0.0  0.0 110216   896 pts/0    S+   15:10   0:00 grep --color=auto redisroot      1892  0.1  0.6 140836  6572 ?        Ssl  Aug19 146:41 /usr/local/redis/bin/redis-server 127.0.0.1:6379[root@iZ28xuvyce3Z bin]# 

PS:也可以使用命令关闭使用命令关闭:

[root@iZ28xuvyce3Z bin]# ./redis-server redis.conf [root@iZ28xuvyce3Z bin]# ps aux | grep redisroot      1121  0.0  0.7 140840  7460 ?        Ssl  15:14   0:00 ./redis-server 127.0.0.1:6401root      1139  0.0  0.0 110216   900 pts/0    S+   15:14   0:00 grep --color=auto redisroot      1892  0.1  0.6 140836  6572 ?        Ssl  Aug19 146:42 /usr/local/redis/bin/redis-server 127.0.0.1:6379[root@iZ28xuvyce3Z bin]# ./redis-cli -p 6401 shutdown[root@iZ28xuvyce3Z bin]# ps aux | grep redisroot      1189  0.0  0.0 110216   896 pts/0    S+   15:14   0:00 grep --color=auto redisroot      1892  0.1  0.6 140836  6572 ?        Ssl  Aug19 146:42 /usr/local/redis/bin/redis-server 127.0.0.1:6379[root@iZ28xuvyce3Z bin]# 

PS:注意,如果不是默认关闭默认端口上的redis服务器,请使用-p命令指定对应端口的服务器。

redis配置认证密码

  • 修改redis配置文件redis.conf
# Require clients to issue AUTH <PASSWORD> before processing any other# commands.  This might be useful in environments in which you do not trust# others with access to the host running redis-server.## This should stay commented out for backward compatibility and because most# people do not need auth (e.g. they run their own servers).## 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.## requirepass foobared

# requirepass foobared前面的#去掉,并修改后面的密码为你想要设置的密码。
比如说

requirepass 123456
  • 重启redis
    关闭redis,再使用该配置文件启动redis服务

  • 用密码登陆redis

redis-cli -p 6401 -a 123456  
  • master配置了密码,slave如何配置
    若master配置了密码则slave也要配置相应的密码参数否则无法进行正常复制的。

再次进入redis配置文件redis.conf

# If the master is password protected (using the "requirepass" configuration# directive below) it is possible to tell the slave to authenticate before# starting the replication synchronization process, otherwise the master will# refuse the slave request.## masterauth <master-password>

slave中配置文件内找到masterauth <master-password>,移除注释,修改密码即可,具体如下:

masterauth 123456  

参考:redis配置认证密码

0 0