linuxs下redis的安装及其集群的使用

1. [root@localhost ~]# yum install -y gcc-c++

2.[root@localhost ~]# cd /usr/local/tmp
[root@localhost tmp]# ls
apache-tomcat-7.0.68         demo.class  jdk1.7.0_80                mysql-5.6.31-linux-glibc2.5-x86_64         redis-3.0.0-rc2.tar.gz
apache-tomcat-7.0.68.tar.gz  demo.java   jdk-7u80-linux-x64.tar.gz  mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz
[root@localhost tmp]# tar zxf redis-3.0.0-rc2.tar.gz
[root@localhost tmp]# ls
apache-tomcat-7.0.68         demo.class  jdk1.7.0_80                mysql-5.6.31-linux-glibc2.5-x86_64         redis-3.0.0-rc2
apache-tomcat-7.0.68.tar.gz  demo.java   jdk-7u80-linux-x64.tar.gz  mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz  redis-3.0.0-rc2.tar.gz
[root@localhost tmp]# cd redis-3.0.0-rc2
[root@localhost redis-3.0.0-rc2]# make

3.[root@localhost redis-3.0.0-rc2]# cd ..
[root@localhost tmp]# cd ..
[root@localhost local]# ls
bin  etc  games  include  jdk7  lib  lib64  libexec  mysql  redis  sbin  share  src  tmp  tomcat
[root@localhost local]# cd redis
[root@localhost redis]# ls
bin
[root@localhost redis]# cd bin
[root@localhost bin]# ls
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-sentinel  redis-server
[root@localhost bin]# ./redis-server

[root@localhost bin]# cp /usr/local/tmp/redis-3.0.0-rc2/redis.conf /usr/local/redis/bin
[root@localhost bin]# ls
dump.rdb  redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis.conf  redis-sentinel  redis-server
[root@localhost bin]# vim redis-conf
[root@localhost bin]# vim redis.conf
[root@localhost bin]# ./redis-server
38943:C 26 Jun 15:27:19.925 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
38943:M 26 Jun 15:27:19.926 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.9.102 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 38943
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

38943:M 26 Jun 15:27:19.928 # Server started, Redis version 2.9.102
38943:M 26 Jun 15:27:19.928 # 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.
38943:M 26 Jun 15:27:19.929 # 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.
38943:M 26 Jun 15:27:19.929 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
38943:M 26 Jun 15:27:19.929 * DB loaded from disk: 0.000 seconds
38943:M 26 Jun 15:27:19.929 * The server is now ready to accept connections on port 6379
^C38943:signal-handler (1561588069) Received SIGINT scheduling shutdown...
38943:M 26 Jun 15:27:49.158 # User requested shutdown...
38943:M 26 Jun 15:27:49.158 * Saving the final RDB snapshot before exiting.
38943:M 26 Jun 15:27:49.167 * DB saved on disk
38943:M 26 Jun 15:27:49.167 # Redis is now ready to exit, bye bye...
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# ps aux|grep redis
root      38947  0.1  0.7 137436  7444 ?        Ssl  15:27   0:00 ./redis-server *:6379    
root      38951  0.0  0.0 103252   828 pts/1    S+   15:28   0:00 grep redis
[root@localhost bin]# ./redis-cli
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set name hello
OK
127.0.0.1:6379> get name
"hello"
127.0.0.1:6379>

其中[root@localhost bin]# vim redis-conf

这样做的目的使redis在后台运行,前台继续编代码。否则一旦退出,redis就关闭了。

window下的可视化redis连接工具redis-desktop-manager-0.7.6.15.exe,安装后可以window下连接linuxs。

[root@localhost bin]# vim /etc/sysconfig/iptables

开放防火墙的阻拦,否则连接不上。

[root@localhost bin]# service iptables stop    //关闭防火墙

[root@localhost bin]# service iptables restart    //开启防火墙

如果要为redis配置连接密码,则在vim /usr/local/redis/bin/redis.conf

找到里面的password进行修改即可。

一般不要设置,没必要,而且设置了以后对于后期命令的使用比较麻烦。

[root@localhost bin]# ./redis.cli -h 192.168.43.127  -p 6379  -a  密码   //表示连接别的电脑

默认不要密码

linuxs下的redis集群步骤:

1.1 后面需要用到 ruby 脚本

yum install ruby -y

1.1 安装 ruby 包管理器

2.# yum install rubygems -y

3.1 脚本需要 ruby 其他包,所以安装这个 redis.gem

3.# gem install redis-3.0.0.gem

Xshell:>
Connecting to 192.168.43.128:22...
Connection established.
Escape character is '^@]'.

Last login: Wed Jun 26 11:37:43 2019 from 192.168.43.1
[root@localhost ~]# cd /usr/local
[root@localhost local]# ls
bin  etc  games  include  jdk7  lib  lib64  libexec  mysql  redis  redis-cluster  sbin  share  src  tmp  tomcat
[root@localhost local]# rm -r redis-cluster
rm: descend into directory `redis-cluster'? y
rm: descend into directory `redis-cluster/redis04'? y
rm: remove regular file `redis-cluster/redis04/redis-cli'? y
rm: remove regular file `redis-cluster/redis04/redis-server'? y
rm: remove regular file `redis-cluster/redis04/nodes.conf'? y
rm: remove regular file `redis-cluster/redis04/redis-check-dump'? ^C
[root@localhost local]# rm -rf redis-cluster
[root@localhost local]# mkdir redis-cluster
[root@localhost local]# ls
bin  etc  games  include  jdk7  lib  lib64  libexec  mysql  redis  redis-cluster  sbin  share  src  tmp  tomcat
[root@localhost local]# cd redis
[root@localhost redis]# cp -r bin ../redis-cluster/redis01
[root@localhost redis]# cd ../redis-cluster/
[root@localhost redis-cluster]# ls
redis01
[root@localhost redis-cluster]# cd redis01
[root@localhost redis01]# ls
dump.rdb  redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis.conf  redis-sentinel  redis-server
[root@localhost redis01]# rm -rf dump.rdb
[root@localhost redis01]# ls
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis.conf  redis-sentinel  redis-server
[root@localhost redis01]# vim redis.conf
[root@localhost redis01]# cp -r redis01 redis02
cp: cannot stat `redis01': No such file or directory
[root@localhost redis01]# cd ..
[root@localhost redis-cluster]# cp -r redis01 redis02
[root@localhost redis-cluster]# cp -r redis01 redis03
[root@localhost redis-cluster]# cp -r redis01 redis04
[root@localhost redis-cluster]# cp -r redis01 redis05
[root@localhost redis-cluster]# cp -r redis01 redis06
[root@localhost redis-cluster]# vim redis02/redis.conf
[root@localhost redis-cluster]# vi redis03/redis.conf
[root@localhost redis-cluster]# vim redis04/redis.conf
[root@localhost redis-cluster]# vim redis05/redis.conf
[root@localhost redis-cluster]# vim redis06/redis.conf
[root@localhost redis-cluster]# ls
redis01  redis02  redis03  redis04  redis05  redis06
[root@localhost redis-cluster]# cd /usr/local/tmp/redis-3.0.0-rc2/bin/src
-bash: cd: /usr/local/tmp/redis-3.0.0-rc2/bin/src: No such file or directory
[root@localhost redis-cluster]# cd /usr/local/tmp/redis-3.0.0-rc2/
[root@localhost redis-3.0.0-rc2]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README  redis.conf  runtest  runtest-cluster  runtest-sentinel  sentinel.conf  src  tests  utils
[root@localhost redis-3.0.0-rc2]# cd src
[root@localhost src]# cp *.rb /usr/local/redis-cluster/
[root@localhost src]# cd ..
[root@localhost redis-3.0.0-rc2]# cd /usr/local
[root@localhost local]# ls
bin  etc  games  include  jdk7  lib  lib64  libexec  mysql  redis  redis-cluster  sbin  share  src  tmp  tomcat
[root@localhost local]# cd redis-cluster/
[root@localhost redis-cluster]# vim startall.sh
[root@localhost redis-cluster]# chmod a+x startall.sh
[root@localhost redis-cluster]# ./startall.sh
[root@localhost redis-cluster]# ps aux|grep redis
root       2572  0.2  0.7 137436  7480 ?        Ssl  02:46   0:00 ./redis-server *:7001 [cluster]
root       2574  0.1  0.7 137436  7480 ?        Ssl  02:46   0:00 ./redis-server *:7002 [cluster]
root       2578  0.2  0.7 137436  7480 ?        Ssl  02:46   0:00 ./redis-server *:7003 [cluster]
root       2582  0.1  0.7 137436  7484 ?        Ssl  02:46   0:00 ./redis-server *:7004 [cluster]
root       2586  0.2  0.7 137436  7476 ?        Ssl  02:46   0:00 ./redis-server *:7005 [cluster]
root       2590  0.1  0.7 137436  7480 ?        Ssl  02:46   0:00 ./redis-server *:7006 [cluster]
root       2596  0.0  0.0 103252   828 pts/0    S+   02:46   0:00 grep redis
[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.43.128:7001 192.168.43.128:7002 192.168.43.128:7003 192.168.43.128:7004 192.168.43.128:7005 192.168.43.128:7006
>>> Creating cluster
Connecting to node 192.168.43.128:7001: OK
Connecting to node 192.168.43.128:7002: OK
Connecting to node 192.168.43.128:7003: OK
Connecting to node 192.168.43.128:7004: OK
Connecting to node 192.168.43.128:7005: OK
Connecting to node 192.168.43.128:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.43.128:7001
192.168.43.128:7002
192.168.43.128:7003
Adding replica 192.168.43.128:7004 to 192.168.43.128:7001
Adding replica 192.168.43.128:7005 to 192.168.43.128:7002
Adding replica 192.168.43.128:7006 to 192.168.43.128:7003
M: 615bce850f389c281b66edd264c340efdbacca6b 192.168.43.128:7001
   slots:0-5460 (5461 slots) master
M: b8e98f5c4d5474f3fac04e806a5bd7cc3d1eae87 192.168.43.128:7002
   slots:5461-10922 (5462 slots) master
M: 625715b54be3f84645ec114174cf6e036619c659 192.168.43.128:7003
   slots:10923-16383 (5461 slots) master
S: ddcfc2ca8a1415f4bcf627698d7893edcc6c47db 192.168.43.128:7004
   replicates 615bce850f389c281b66edd264c340efdbacca6b
S: 30ca12fb75045792a4a70b74f06a1ea99ab34226 192.168.43.128:7005
   replicates b8e98f5c4d5474f3fac04e806a5bd7cc3d1eae87
S: eb2238dbb83ad1adf2009593c38ae53eed6d9e2d 192.168.43.128:7006
   replicates 625715b54be3f84645ec114174cf6e036619c659
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join......
>>> Performing Cluster Check (using node 192.168.43.128:7001)
M: 615bce850f389c281b66edd264c340efdbacca6b 192.168.43.128:7001
   slots:0-5460 (5461 slots) master
M: b8e98f5c4d5474f3fac04e806a5bd7cc3d1eae87 192.168.43.128:7002
   slots:5461-10922 (5462 slots) master
M: 625715b54be3f84645ec114174cf6e036619c659 192.168.43.128:7003
   slots:10923-16383 (5461 slots) master
M: ddcfc2ca8a1415f4bcf627698d7893edcc6c47db 192.168.43.128:7004
   slots: (0 slots) master
   replicates 615bce850f389c281b66edd264c340efdbacca6b
M: 30ca12fb75045792a4a70b74f06a1ea99ab34226 192.168.43.128:7005
   slots: (0 slots) master
   replicates b8e98f5c4d5474f3fac04e806a5bd7cc3d1eae87
M: eb2238dbb83ad1adf2009593c38ae53eed6d9e2d 192.168.43.128:7006
   slots: (0 slots) master
   replicates 625715b54be3f84645ec114174cf6e036619c659
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-cluster]# cd redis01
[root@localhost redis01]# ./redis-cli -h 7001 -c
Could not connect to Redis at 7001:6379: Invalid argument
not connected>
[root@localhost redis01]# ./redis-cli -p 7001 -c
127.0.0.1:7001> set age 15
OK
127.0.0.1:7001> set name susu
-> Redirected to slot [5798] located at 192.168.43.128:7002
OK
192.168.43.128:7002> set x 123
-> Redirected to slot [16287] located at 192.168.43.128:7003
OK
192.168.43.128:7003> set a 1
OK
192.168.43.128:7003> get x
"123"
192.168.43.128:7003> get age
-> Redirected to slot [741] located at 192.168.43.128:7001
"15"
192.168.43.128:7001>
[root@localhost redis01]# vim /etc/sysconfig/iptables
[root@localhost redis01]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]

集群的优点是可以把一个人干的或分给多个人干,在高访问量的情况下,减小服务器压力。

redis集群最少要建立6台虚拟机,采取一主一备模式,防止其当掉,当服务器中有》=1/2台虚拟机当掉时,整个集群才会当掉。

有什么问题,欢迎一起交流(2967254652)

原文地址:https://www.cnblogs.com/aasu/p/11094717.html