安装redis

Posted 技术是条不归路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安装redis相关的知识,希望对你有一定的参考价值。

安装redis

官网下载redis,解压缩,让进目录,make完事。[[email protected] data]# tar redis-3.2.12.tar.gz 

[[email protected] data]# cd redis-3.2.12
[[email protected] redis-3.2.12]# make
[[email protected] redis-3.2.12]# pwd
/data/redis-3.2.12
[[email protected] redis-3.2.12]# mkdir redis6380
[[email protected] redis-3.2.12]# pwd
/data/redis-3.2.12
[[email protected] redis-3.2.12]# cp -rf redis.conf /data/redis6380/
[[email protected] src]# pwd
/data/redis-3.2.12/src
[[email protected] src]# cp src/redis-server /data/redis6380/
[[email protected] redis6380]# pwd
/data/redis6380
[[email protected] redis6380]# grep ^[^$|#;] redis.conf > redis.conf2
[[email protected] redis6380]# mv redis.conf redis.confbak
[[email protected] redis6380]# mv redis.conf2 redis.conf
[[email protected] redis6380]# ll
total 5124
-rw-r--r-- 1 root root    1170 Sep  4 20:24 redis.conf
-rw-r--r-- 1 root root   46695 Sep  4 20:16 redis.confbak
-rwxr-xr-x 1 root root 5192400 Sep  4 20:16 redis-server
[[email protected] redis6380]# vim redis.conf
[[email protected] redis6380]# ./redis-server redis.conf
                _._                                                  
           _.-``__ ‘‘-._                                             
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.12 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ‘‘-._                                   
 (          ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|` _.-|     Port: 6380
 |    `-._   `._    /     _.-    |     PID: 30112
  `-._    `-._  `-./  _.-    _.-                                   
 |`-._`-._    `-.__.-    _.-_.-|                                  
 |    `-._`-._        _.-_.-    |           http://redis.io        
  `-._    `-._`-.__.-_.-    _.-                                   
 |`-._`-._    `-.__.-    _.-_.-|                                  
 |    `-._`-._        _.-_.-    |                                  
  `-._    `-._`-.__.-_.-    _.-                                   
      `-._    `-.__.-    _.-                                       
  [[email protected] redis6380]# vim redis.conf
[[email protected] redis6380]# ./redis-server redis.conf
[[email protected] redis6380]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      27045/redis-server  
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      30117/./redis-serve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1199/sshd           
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      23187/master        
tcp6       0      0 :::80                   :::*                    LISTEN      1671/docker-proxy-c 
tcp6       0      0 :::25                   :::*                    LISTEN      23187/master        
[[email protected] redis6380]# ll
total 5128
-rw-r--r-- 1 root root      77 Sep  4 20:26 dump.rdb
-rw-r--r-- 1 root root    1171 Sep  4 20:26 redis.conf
-rw-r--r-- 1 root root   46695 Sep  4 20:16 redis.confbak
-rwxr-xr-x 1 root root 5192400 Sep  4 20:16 redis-server
[[email protected] redis6380]# whereis redis-cli
redis-cli: /usr/bin/redis-cli /usr/share/man/man1/redis-cli.1.gz
[[email protected] redis6380]# redis-cli 
dump.rdb       redis.conf     redis.confbak  redis-server   
[[email protected] redis6380]# redis-cli --help
redis-cli 3.2.12

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
  -r <repeat>        Execute specified command N times.
  -i <interval>      When -r is used, waits <interval> seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n <db>            Database number.
  -x                 Read last argument from STDIN.
  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: 
).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
  [[email protected] redis6380]# redis-cli
127.0.0.1:6379> 
[[email protected] redis6380]# redis-cli -p 6380
127.0.0.1:6380> set foo sb
OK
127.0.0.1:6380> get foo
"sb"                  

redis集群

脚本创建6个节点,并修改配置文件

[[email protected] data]# more cluster.sh 
#!/bin/bash
rm -rf /data/700*
for no in 0 1 2 3 4 5;do cp -r redis6380/ 700$no;done
for no in 0 1 2 3 4 5;do rm -rf 700$no/dump.rdb;done
for no in 0 1 2 3 4 5
do
        cd /data/700$no
    sed -i "s/port 6380/port 700$no/g" redis.conf
    echo "cluster-enabled yes" >> redis.conf
    echo "cluster-config-file nodes.conf" >> redis.conf
    echo "cluster-node-timeout 5000" >> redis.conf 
    sed -i "s/appendonly no/appendonly yes/g" redis.conf
    sed -i "s/redis_6380.pid/redis_700$no.pid/g" redis.conf
    sed -i "/^dir/d" redis.conf
    echo dir "./" >> redis.conf
    
done
[[email protected] data]# 

启动6个节点:

[[email protected] data]# for no in 0 1 2 3 4 5;do cd /data/700$no && ./redis-server ./redis.conf;done

[[email protected] data]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 2536/./redis-server 
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 30492/./redis-serve 
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 30494/./redis-serve 
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1199/sshd 
tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 2504/./redis-server 
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 30486/./redis-serve 
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 23187/master 
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 30488/./redis-serve 
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 2536/./redis-server 
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 30492/./redis-serve 
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 30494/./redis-serve 
tcp 0 0 0.0.0.0:17000 0.0.0.0:* LISTEN 2504/./redis-server 
tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 30486/./redis-serve 
tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 30488/./redis-serve 
tcp6 0 0 :::80 :::* LISTEN 1671/docker-proxy-c 
tcp6 0 0 :::25 :::* LISTEN 23187/master

创建集群脚本是ruby写的,需要系统安装高于2.2.以上版本的ruby,ruby版本低会报以下错误。

创建redis集群报错 /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require‘: cannot load suc

升级ruby步骤:

1、sudo yum install curl  安装curl
2、下载rvm   curl -sSL https://get.rvm.io | bash -s stable 
3、查找配置文件 find / -name rvm.sh 
4、配置文件生效 source /etc/profile.d/rvm.sh 
5、下载rvm依赖 rvm requirements 
6、查看rvm库ruby版本 rvm list known
7、安装ruby指定版本 rvm install ruby-2.4.1
8、使用ruby版本默认 rvm use 2.4.1 default
9、gem install redis

然后从新创建集群输入yes成功,查看集群节点状态。有master和slave

[[email protected] data]# /data/redis-3.2.12/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
[[email protected] data]# redis-cli -c -h 127.0.0.1 -p 7002
127.0.0.1:7002> set foo2 www
-> Redirected to slot [1044] located at 127.0.0.1:7003
OK
127.0.0.1:7003> get foo2
"www"
127.0.0.1:7003> 
[[email protected] data]# redis-cli -p 7000 cluster nodes
7a295ffed325031866332c1f686c7eb26f7c6123 127.0.0.1:7001 master - 0 1536073555336 2 connected 5461-10922
9a711fa99b06b625abe31a1ce721b469152801e8 127.0.0.1:7004 slave 7a295ffed325031866332c1f686c7eb26f7c6123 0 1536073553834 5 connected
701a613bdaeeafca902595d8a75f61c174577879 127.0.0.1:7005 slave 20364419bce097b62c957d816c326cc4bc634505 0 1536073553333 6 connected
20364419bce097b62c957d816c326cc4bc634505 127.0.0.1:7002 master - 0 1536073554836 3 connected 10923-16383
302ae0e7b28c656dca312cc2b2eb860729a5cb98 127.0.0.1:7003 master - 0 1536073554836 7 connected 0-5460
2433185edac6e587ec4942c4608948c8b898c1aa 127.0.0.1:7000 myself,slave 302ae0e7b28c656dca312cc2b2eb860729a5cb98 0 0 1 connected
[[email protected] data]# 

 

以上是关于安装redis的主要内容,如果未能解决你的问题,请参考以下文章

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

PHP代码-psysh调试代码片段工具

spring boot 整合 redis

VIM 代码片段插件 ultisnips 使用教程

redis存储session配制方法

Android 插件化VirtualApp 源码分析 ( 目前的 API 现状 | 安装应用源码分析 | 安装按钮执行的操作 | 返回到 HomeActivity 执行的操作 )(代码片段