redis使用详细步骤

redis

1.安装配置jdk
/home         放置压缩包
/usr/local    安装目录
/usr/local/src  源码包

解压jdk
tar -zxvf jdk-8u151-linux-x64.tar.gz -C /usr/local

更改jdk文件名
mv jdk1.8.0_151/ jdk

配置环境变量
vim ~/.bash_profile
    需要配置的信息:(最后一行写)
        export JAVA_HOME=/usr/local/jdk
        export PATH=$JAVA_HOME/bin:$PATH
    保存退出:
        1.  esc
        2.  :wq!
    刷新文件:
        source ~/.bash_profile
    查看javac:
        javac
    查看jdk版本号;
        java -version

2.安装redis

解压缩
tar -zxvf redis-2.8.0.tar.gz -C /usr/local/src

进入文件:
[root@centos home]# cd /usr/local
[root@centos local]# ls
bin  etc  games  include  jdk  lib  lib64  libexec  sbin  share  src
[root@centos local]# cd src
[root@centos src]# ls
redis-2.8.0
[root@centos src]# cd redis-2.8.0
[root@centos redis-2.8.0]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README  redis.conf  runtest  sentinel.conf  src  tests  utils
[root@centos redis-2.8.0]# cd src
[root@centos src]# ls
adlist.c     anet.c       config.h      endianconv.h  lzfP.h  .......

make --> 报错,缺少c语言的编译器

在线安装gcc编译器:
yum install gcc -y

看到一下信息:则安装gcc成功!
    Dependency Updated:
      glibc.x86_64 0:2.17-260.el7           glibc-common.x86_64 0:2.17-260.el7           libgcc.x86_64 0:4.8.5-36.el7           libgomp.x86_64 0:4.8.5-36.el7          

    Complete!

进行make 预编译:
[root@centos src]# make MALLOC=libc

之后:
[root@centos src]# cd ../
[root@centos redis-2.8.0]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README  redis.conf  runtest  sentinel.conf  src  tests  utils
[root@centos redis-2.8.0]# make install PREFIX=/usr/local/redis
cd src && make install
make[1]: Entering directory `/usr/local/src/redis-2.8.0/src'

Hint: To run 'make test' is a good idea ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/src/redis-2.8.0/src'
[root@centos redis-2.8.0]# cd /usr/local/
[root@centos local]# ls         (发现redis已经编译好了)
bin  etc  games  include  jdk  lib  lib64  libexec  redis  sbin  share  src
[root@centos local]# cd redis
[root@centos redis]# ls
bin
[root@centos redis]# cd bin
[root@centos bin]# ls
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-server


另打开一个终端:进行拷贝一个配置文件
[root@centos ~]# cd /usr/local/src
[root@centos src]# ls
redis-2.8.0
[root@centos src]# cd redis-2.8.0
[root@centos redis-2.8.0]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README  redis.conf  runtest  sentinel.conf  src  tests  utils
[root@centos redis-2.8.0]# cp redis.conf /usr/local/redis/bin


在第一个终端查看redis-2.8.0中存在redis.conf配置文件
[root@centos bin]# ls
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis.conf  redis-server


前端启动redis服务:
./redis-server redis.conf

[49590] 16 Jan 23:27:53.018 * Max number of open files set to 10032
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.8.0 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 49590
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

[49590] 16 Jan 23:27:53.080 # Server started, Redis version 2.8.0
[49590] 16 Jan 23:27:53.081 # 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.
[49590] 16 Jan 23:27:53.081 * The server is now ready to accept connections on port 6379
        
前端启动成功!

停止前端启动:
ctrl+c

查看是否启动:
ps -ef | grep redis

root      49590  26700  0 23:27 pts/3    00:00:00 ./redis-server *:6379
root      49603  26389  0 23:29 pts/2    00:00:00 grep --color=auto redis

链接一下:(客户端)
[root@centos redis-2.8.0]# cd /usr/local/redis/bin
[root@centos bin]# ls
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis.conf  redis-server
[root@centos bin]# ./redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name baixin
OK
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> get name
"baixin"
127.0.0.1:6379> 

修改配置文件redis.conf,改为后台运行
vim redis.conf
/daemonize      /用来快速查询信息
将daemonize 改为yes
daemonize yes
退出编辑模式:
esc 
保存退出:
:wq!

查看服务启动:(服务端)
[root@centos bin]# ls
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis.conf  redis-server
[root@centos bin]# ./redis-server redis.conf
[root@centos bin]# ps -ef | grep redis
root      49702  26389  0 23:37 pts/2    00:00:00 ./redis-cli
root      49781      1  0 23:43 ?        00:00:00 ./redis-server *:6379
root      49785  26700  0 23:44 pts/3    00:00:00 grep --color=auto redis

--------与Java交互---------
6.x
查看防火墙:
service iptables status

7.x
查看防火墙:
systemctl status firewalld.service


------集群banredis---------
redis3以上才支持集群

解压缩:
tar -zxvf redis-3.0.5.tar.gz -C /usr/local/src

进入文件:
[root@centos home]# cd /usr/local/src
[root@centos src]# ls
redis-2.8.0  redis-3.0.5
[root@centos src]# cd redis-3.0.5/
[root@centos redis-3.0.5]# cd src
[root@centos src]# ls
adlist.c     anet.h       config.c  endianconv.c   ......

进行make 预编译:
[root@centos src]# make MALLOC=libc


进入文件:编译文件到redis3中
[root@centos src]# cd ../
[root@centos redis-3.0.5]# make install PREFIX=/usr/local/redis3
cd src && make install
make[1]: Entering directory `/usr/local/src/redis-3.0.5/src'

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

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/src/redis-3.0.5/src'
[root@centos redis-3.0.5]# cd /usr/local/
[root@centos local]# ls
bin  etc  games  include  jdk  lib  lib64  libexec  redis  redis3  sbin  share  src
[root@centos local]# cd redis3
[root@centos redis3]# ls
bin
[root@centos redis3]# cd bin
[root@centos bin]#


打开另一个终端:复制配置文件
[root@centos ~]# cd /usr/local/src/redis-3.0.5/
[root@centos redis-3.0.5]# ls
00-RELEASENOTES  CONTRIBUTING  deps     Makefile   README      runtest          runtest-sentinel  src    utils
BUGS             COPYING       INSTALL  MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests
[root@centos redis-3.0.5]# cp redis.conf /usr/local/redis3/bin


修改配置文件redis.conf,改为后台运行
vim redis.conf
/daemonize      /用来快速查询信息
将daemonize 改为yes
daemonize yes
将端口号改为7001
port 7001
支持集群:
cluster-enabled yes

退出编辑模式:
esc 
保存退出:
:wq!

进入文件:cd /usr/local 创建redis-cluster文件
mkdir redis-cluster

进行拷贝redis3 到redis01
cp -r /usr/local/redis3 ./redis01

同时拷贝五个文件,模拟6个机器  (三主三从)
[root@centos redis-cluster]# cp -r  redis01/ redis02
[root@centos redis-cluster]# cp -r  redis01/ redis03
[root@centos redis-cluster]# cp -r  redis01/ redis04
[root@centos redis-cluster]# cp -r  redis01/ redis05
[root@centos redis-cluster]# cp -r  redis01/ redis06


修改6台机器的端口号
[root@centos redis-cluster]# vim redis02/bin/redis.conf
[root@centos redis-cluster]# vim redis03/bin/redis.conf
[root@centos redis-cluster]# vim redis04/bin/redis.conf
[root@centos redis-cluster]# vim redis05/bin/redis.conf
[root@centos redis-cluster]# vim redis06/bin/redis.conf

创建文件
> 同时启动6台服务器
vim start-all.sh
cd /usr/local/redis-cluster/redis01/bin
./redis-server redis.conf

cd /usr/local/redis-cluster/redis02/bin
./redis-server redis.conf

cd /usr/local/redis-cluster/redis03/bin
./redis-server redis.conf

cd /usr/local/redis-cluster/redis04/bin
./redis-server redis.conf

cd /usr/local/redis-cluster/redis05/bin
./redis-server redis.conf

cd /usr/local/redis-cluster/redis06/bin
./redis-server redis.conf
>同时关闭6台服务器
 vim stop-all.sh
 cd /usr/local/redis-cluster/redis01/bin
./redis-cli -p 7001 shutdown

cd /usr/local/redis-cluster/redis02/bin
./redis-cli -p 7002 shutdown

cd /usr/local/redis-cluster/redis03/bin
./redis-cli -p 7003 shutdown

cd /usr/local/redis-cluster/redis04/bin
./redis-cli -p 7004 shutdown

cd /usr/local/redis-cluster/redis05/bin
./redis-cli -p 7005 shutdown

cd /usr/local/redis-cluster/redis06/bin
./redis-cli -p 7006 shutdown


赋予权限:chmod 777 start-all.sh stop-all.sh

执行6台机器:
./start-all.sh
ps -ef | grep redis


搭建集群:
拷贝文件:redis-trib.rb
cp redis-trib.rb /usr/local/redis-cluster

下载ruby:
yum install ruby -y
yum install rubygems -y
gem install redis-3.0.5.gem

>启动集群:
 ./redis-trib.rb create --replicas 1 192.168.121.130:7002 192.168.121.130:7003 192.168.121.130:7004 192.168.121.130:7005  192.168.121.130:7006

猜你喜欢

转载自blog.csdn.net/weixin_43552143/article/details/87175002