Redis官网下载:Redishttps://redis.io/download
root@ubuntu:/home/test/downLoadResource# wget https://download.redis.io/releases/redis-6.2.6.tar.gz
--2022-02-28 20:52:55-- https://download.redis.io/releases/redis-6.2.6.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2476542 (2.4M) [application/octet-stream]
Saving to: ‘redis-6.2.6.tar.gz’
redis-6.2.6.tar.gz 100%[===================================================================================================>] 2.36M 48.6KB/s in 50s
2022-02-28 20:53:46 (48.8 KB/s) - ‘redis-6.2.6.tar.gz’ saved [2476542/2476542]
root@ubuntu:/home/test/downLoadResource# ls
apache-tomcat-8.5.75.tar.gz jdk-8u321-linux-x64.tar.gz redis-6.2.6.tar.gz
root@ubuntu:/home/test/downLoadResource# ll
total 156156
drwxrwxr-x 2 test test 4096 Feb 28 20:52 ./
drwxr-xr-x 5 test test 4096 Feb 28 19:52 ../
-rw-rw-r-- 1 test test 10595855 Feb 28 20:31 apache-tomcat-8.5.75.tar.gz
-rw-rw-r-- 1 test test 146815279 Feb 28 19:48 jdk-8u321-linux-x64.tar.gz
-rw-r--r-- 1 root root 2476542 Oct 4 19:19 redis-6.2.6.tar.gz
root@ubuntu:/home/test/downLoadResource# cp ./redis-6.2.6.tar.gz ../../../usr/local/
root@ubuntu:/home/test/downLoadResource# cd ../../../usr/local/
root@ubuntu:/usr/local# ls
apache-tomcat-8.5.75 apache-tomcat-8.5.75.tar.gz bin etc games include lib man redis-6.2.6.tar.gz sbin share src
root@ubuntu:/usr/local# ll
total 12812
drwxr-xr-x 11 root root 4096 Feb 28 20:57 ./
drwxr-xr-x 10 root root 4096 Sep 29 12:10 ../
drwxr-xr-x 9 root root 4096 Feb 28 20:35 apache-tomcat-8.5.75/
-rw-r--r-- 1 root root 10595855 Feb 28 20:32 apache-tomcat-8.5.75.tar.gz
drwxr-xr-x 2 root root 4096 Aug 11 2020 bin/
drwxr-xr-x 2 root root 4096 Aug 11 2020 etc/
drwxr-xr-x 2 root root 4096 Aug 11 2020 games/
drwxr-xr-x 2 root root 4096 Aug 11 2020 include/
drwxr-xr-x 3 root root 4096 Sep 29 12:10 lib/
lrwxrwxrwx 1 root root 9 Sep 29 12:10 man -> share/man/
-rw-r--r-- 1 root root 2476542 Feb 28 20:57 redis-6.2.6.tar.gz
drwxr-xr-x 2 root root 4096 Aug 11 2020 sbin/
drwxr-xr-x 6 root root 4096 Sep 29 12:15 share/
drwxr-xr-x 2 root root 4096 Aug 11 2020 src/
root@ubuntu:/usr/local# tar -xzvf ./redis-6.2.6.tar.gz
root@ubuntu:/usr/local# yum install gcc-c++ 可以用yum 命令安装
(Linux系统自带C语言环境)
root@ubuntu:/usr/local# cd redis-6.2.6/
root@ubuntu:/usr/local# make
root@ubuntu:/usr/local/redis-6.2.6# cd ../
root@ubuntu:/usr/local# ll
total 12816
drwxr-xr-x 12 root root 4096 Feb 28 21:00 ./
drwxr-xr-x 10 root root 4096 Sep 29 12:10 ../
drwxr-xr-x 9 root root 4096 Feb 28 20:35 apache-tomcat-8.5.75/
-rw-r--r-- 1 root root 10595855 Feb 28 20:32 apache-tomcat-8.5.75.tar.gz
drwxr-xr-x 2 root root 4096 Aug 11 2020 bin/
drwxr-xr-x 2 root root 4096 Aug 11 2020 etc/
drwxr-xr-x 2 root root 4096 Aug 11 2020 games/
drwxr-xr-x 2 root root 4096 Aug 11 2020 include/
drwxr-xr-x 4 root root 4096 Feb 28 21:22 lib/
lrwxrwxrwx 1 root root 9 Sep 29 12:10 man -> share/man/
drwxrwxr-x 7 root root 4096 Oct 4 18:59 redis-6.2.6/
-rw-r--r-- 1 root root 2476542 Feb 28 20:57 redis-6.2.6.tar.gz
drwxr-xr-x 2 root root 4096 Aug 11 2020 sbin/
drwxr-xr-x 6 root root 4096 Sep 29 12:15 share/
drwxr-xr-x 2 root root 4096 Aug 11 2020 src/
root@ubuntu:/usr/local# mkdir redis
root@ubuntu:/usr/local# ll
total 12820
drwxr-xr-x 13 root root 4096 Feb 28 21:29 ./
drwxr-xr-x 10 root root 4096 Sep 29 12:10 ../
drwxr-xr-x 9 root root 4096 Feb 28 20:35 apache-tomcat-8.5.75/
-rw-r--r-- 1 root root 10595855 Feb 28 20:32 apache-tomcat-8.5.75.tar.gz
drwxr-xr-x 2 root root 4096 Aug 11 2020 bin/
drwxr-xr-x 2 root root 4096 Aug 11 2020 etc/
drwxr-xr-x 2 root root 4096 Aug 11 2020 games/
drwxr-xr-x 2 root root 4096 Aug 11 2020 include/
drwxr-xr-x 4 root root 4096 Feb 28 21:22 lib/
lrwxrwxrwx 1 root root 9 Sep 29 12:10 man -> share/man/
drwxr-xr-x 2 root root 4096 Feb 28 21:29 redis/
drwxrwxr-x 7 root root 4096 Oct 4 18:59 redis-6.2.6/
-rw-r--r-- 1 root root 2476542 Feb 28 20:57 redis-6.2.6.tar.gz
drwxr-xr-x 2 root root 4096 Aug 11 2020 sbin/
drwxr-xr-x 6 root root 4096 Sep 29 12:15 share/
drwxr-xr-x 2 root root 4096 Aug 11 2020 src/
root@ubuntu:/usr/local# make install PREFIX=/usr/local/redis
make: *** No rule to make target 'install'. Stop.
root@ubuntu:/usr/local# make install PREFIX=/usr/local/redis
make: *** No rule to make target 'install'. Stop.
root@ubuntu:/usr/local# cd redis-6.2.6/
root@ubuntu:/usr/local/redis-6.2.6# make install PREFIX=/usr/local/redis
cd src && make install
make[1]: Entering directory '/usr/local/redis-6.2.6/src'
/bin/sh: 1: pkg-config: not found
CC Makefile.dep
/bin/sh: 1: pkg-config: not found
Hint: It's a good idea to run 'make test' ;)
INSTALL redis-server
INSTALL redis-benchmark
INSTALL redis-cli
make[1]: Leaving directory '/usr/local/redis-6.2.6/src'
root@ubuntu:/usr/local/redis-6.2.6#
拷贝redis 配置文件到安装目录下
root@ubuntu:/usr/local/redis-6.2.6# cp ./redis.conf ../redis/bin/
root@ubuntu:/usr/local/redis-6.2.6# cd ../redis/bin/
root@ubuntu:/usr/local/redis/bin# ll
root@ubuntu:/usr/local/redis/bin# cat redis.conf
root@ubuntu:/usr/local/redis/bin# vi ./redis.conf
(如果有多个redis 启动要指定配置文件)
root@ubuntu:/usr/local/redis/bin# ./redis-server ./redis.conf
22449:C 28 Feb 2022 21:47:33.785 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
22449:C 28 Feb 2022 21:47:33.785 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=22449, just started
22449:C 28 Feb 2022 21:47:33.785 # Configuration loaded
22449:M 28 Feb 2022 21:47:33.786 * Increased maximum number of open files to 10032 (it was originally set to 1024).
22449:M 28 Feb 2022 21:47:33.786 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 22449
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
22449:M 28 Feb 2022 21:47:33.787 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
22449:M 28 Feb 2022 21:47:33.787 # Server initialized
22449:M 28 Feb 2022 21:47:33.787 # 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.
22449:M 28 Feb 2022 21:47:33.787 * Ready to accept connections
(如果有多个redis 停止也要指定具体的redis ip和端口 )
root@ubuntu:/usr/local/redis/bin# ./redis-cli -h 127.0.1 -p 6379 shutdown
22449:M 28 Feb 2022 21:52:45.406 * Removing the pid file.
22449:M 28 Feb 2022 21:52:45.406 # Redis is now ready to exit, bye bye...
root@ubuntu:/usr/local/redis/bin#
maven库引入所需的包
redis.clients
jedis
2.7.2
创建Java类
public static void main(String[] args) {
//创建对象指定ip和端口号
// Jedis jedis=new Jedis("192.168.65.132",6379);
// jedis.set("Hello","redis");
// String value = jedis.get("Hello");
// System.out.println("输出:"+value);
// jedis.close();
//使用JedisPool 链接池访问
JedisPool jedisPool=new JedisPool("192.168.65.132",6379);
Jedis jedis1=jedisPool.getResource();
String value1 = jedis1.get("Hello");
System.out.println("输出:"+value1);
jedis1.close();
}
控制台打印:
Connected to the target VM, address: '127.0.0.1:56202', transport: 'socket'
输出:redis
Disconnected from the target VM, address: '127.0.0.1:56202', transport: 'socket'
Process finished with exit code 0
注:在访问Redis前先要在redis配置文件中修改ip地址,然后关闭防火墙
修改配置文件之后需要关闭Redis,然后再重新开启
关闭防火墙(注意各Linux系统,关闭防火墙命令存在不一样的情况)
root@ubuntu:/usr/local/redis/bin# systemctl stop firewalld. service
禁止防火墙开机启动:
root@ubuntu:/usr/local/redis/bin# systemctl disable firewalld. service
首先创建六个文件夹,redis 配置文件redis.conf 拷贝到六个文件夹下,并修改配置文件ip地址和端口号,并将 cluster-enabled yes 设置为yes
root@ubuntu:/usr/local# mkdir 8881
root@ubuntu:/usr/local# mkdir 8882
root@ubuntu:/usr/local# mkdir 8883
root@ubuntu:/usr/local# mkdir 8884
root@ubuntu:/usr/local# mkdir 8885
root@ubuntu:/usr/local# mkdir 8886
root@ubuntu:/usr/local# ll
total 12844
drwxr-xr-x 19 root root 4096 Mar 1 20:13 ./
drwxr-xr-x 10 root root 4096 Sep 29 12:10 ../
drwxr-xr-x 2 root root 4096 Mar 1 20:13 8881/
drwxr-xr-x 2 root root 4096 Mar 1 20:13 8882/
drwxr-xr-x 2 root root 4096 Mar 1 20:13 8883/
drwxr-xr-x 2 root root 4096 Mar 1 20:13 8884/
drwxr-xr-x 2 root root 4096 Mar 1 20:13 8885/
drwxr-xr-x 2 root root 4096 Mar 1 20:13 8886/
drwxr-xr-x 9 root root 4096 Feb 28 20:35 apache-tomcat-8.5.75/
-rw-r--r-- 1 root root 10595855 Feb 28 20:32 apache-tomcat-8.5.75.tar.gz
drwxr-xr-x 2 root root 4096 Aug 11 2020 bin/
drwxr-xr-x 2 root root 4096 Aug 11 2020 etc/
drwxr-xr-x 2 root root 4096 Aug 11 2020 games/
drwxr-xr-x 2 root root 4096 Aug 11 2020 include/
drwxr-xr-x 4 root root 4096 Feb 28 21:22 lib/
lrwxrwxrwx 1 root root 9 Sep 29 12:10 man -> share/man/
drwxr-xr-x 3 root root 4096 Feb 28 21:34 redis/
drwxrwxr-x 7 root root 4096 Oct 4 18:59 redis-6.2.6/
-rw-r--r-- 1 root root 2476542 Feb 28 20:57 redis-6.2.6.tar.gz
drwxr-xr-x 2 root root 4096 Aug 11 2020 sbin/
drwxr-xr-x 6 root root 4096 Sep 29 12:15 share/
drwxr-xr-x 2 root root 4096 Aug 11 2020 src/
root@ubuntu:/usr/local# cd bin/
root@ubuntu:/usr/local/bin# cd ../
root@ubuntu:/usr/local# cd redis/bin/
root@ubuntu:/usr/local/redis/bin# ll
total 19612
drwxr-xr-x 2 root root 4096 Mar 1 19:27 ./
drwxr-xr-x 3 root root 4096 Feb 28 21:34 ../
-rw-r--r-- 1 root root 92 Feb 28 21:52 dump.rdb
-rwxr-xr-x 1 root root 4952472 Feb 28 21:34 redis-benchmark*
lrwxrwxrwx 1 root root 12 Feb 28 21:34 redis-check-aof -> redis-server*
lrwxrwxrwx 1 root root 12 Feb 28 21:34 redis-check-rdb -> redis-server*
-rwxr-xr-x 1 root root 5170456 Feb 28 21:34 redis-cli*
-rw-r--r-- 1 root root 93724 Mar 1 19:26 redis.conf
lrwxrwxrwx 1 root root 12 Feb 28 21:34 redis-sentinel -> redis-server*
-rwxr-xr-x 1 root root 9843064 Feb 28 21:34 redis-server*
root@ubuntu:/usr/local/redis/bin# cp ./redis.conf ../../8881
root@ubuntu:/usr/local/redis/bin# cd ../../
root@ubuntu:/usr/local# cd /8881
bash: cd: /8881: No such file or directory
root@ubuntu:/usr/local# //
bash: //: Is a directory
root@ubuntu:/usr/local# cd 8881
root@ubuntu:/usr/local/8881# ll
total 100
drwxr-xr-x 2 root root 4096 Mar 1 20:16 ./
drwxr-xr-x 19 root root 4096 Mar 1 20:13 ../
-rw-r--r-- 1 root root 93724 Mar 1 20:16 redis.conf
root@ubuntu:/usr/local/8881#
进入Redis安装包解压缩文件夹下,并将src文件目录整个拷贝到先前创建的六个文件夹下
root@ubuntu:/usr/local# cd redis-6.2.6
root@ubuntu:/usr/local/redis-6.2.6# ll
total 264
drwxrwxr-x 7 root root 4096 Oct 4 18:59 ./
drwxr-xr-x 19 root root 4096 Mar 1 20:13 ../
-rw-rw-r-- 1 root root 33624 Oct 4 18:59 00-RELEASENOTES
-rw-rw-r-- 1 root root 51 Oct 4 18:59 BUGS
-rw-rw-r-- 1 root root 5026 Oct 4 18:59 CONDUCT
-rw-rw-r-- 1 root root 3384 Oct 4 18:59 CONTRIBUTING
-rw-rw-r-- 1 root root 1487 Oct 4 18:59 COPYING
drwxrwxr-x 7 root root 4096 Feb 28 21:23 deps/
drwxrwxr-x 4 root root 4096 Oct 4 18:59 .github/
-rw-rw-r-- 1 root root 483 Oct 4 18:59 .gitignore
-rw-rw-r-- 1 root root 11 Oct 4 18:59 INSTALL
-rw-rw-r-- 1 root root 151 Oct 4 18:59 Makefile
-rw-rw-r-- 1 root root 6888 Oct 4 18:59 MANIFESTO
-rw-rw-r-- 1 root root 21567 Oct 4 18:59 README.md
-rw-rw-r-- 1 root root 93724 Oct 4 18:59 redis.conf
-rwxrwxr-x 1 root root 275 Oct 4 18:59 runtest*
-rwxrwxr-x 1 root root 279 Oct 4 18:59 runtest-cluster*
-rwxrwxr-x 1 root root 1079 Oct 4 18:59 runtest-moduleapi*
-rwxrwxr-x 1 root root 281 Oct 4 18:59 runtest-sentinel*
-rw-rw-r-- 1 root root 13768 Oct 4 18:59 sentinel.conf
drwxrwxr-x 3 root root 12288 Feb 28 21:34 src/
drwxrwxr-x 11 root root 4096 Oct 4 18:59 tests/
-rw-rw-r-- 1 root root 3055 Oct 4 18:59 TLS.md
drwxrwxr-x 9 root root 4096 Oct 4 18:59 utils/
root@ubuntu:/usr/local/redis-6.2.6# cp -r ./src ../8881/
root@ubuntu:/usr/local/8881# cp -r ./src ../8882/
root@ubuntu:/usr/local/8881# cp -r ./src ../8883
root@ubuntu:/usr/local/8881# cp -r ./src ../8884
root@ubuntu:/usr/local/8881# cp -r ./src ../8885
root@ubuntu:/usr/local/8881# cp -r ./src ../8886
root@ubuntu:/usr/local/8881# cd ../8882
root@ubuntu:/usr/local/8882# ls
redis.conf src
root@ubuntu:/usr/local/8882#
分别启动六个文件加下的redis
root@ubuntu:/usr/local# ps -ef|grep redis
root 1767 1753 0 19:33 pts/0 00:00:11 ./redis-server 192.168.65.132:6379
root 1974 1795 0 20:59 pts/1 00:00:00 ./redis-server 192.168.65.132:8881 [cluster]
root 2002 1990 0 21:01 pts/2 00:00:00 ./redis-server 192.168.65.132:8882 [cluster]
root 2028 2018 0 21:02 pts/3 00:00:00 ./redis-server 192.168.65.132:8883 [cluster]
root 2054 2044 0 21:03 pts/4 00:00:00 ./redis-server 192.168.65.132:8884 [cluster]
root 2081 2071 0 21:03 pts/5 00:00:00 ./redis-server 192.168.65.132:8885 [cluster]
root 2107 2097 0 21:04 pts/6 00:00:00 ./redis-server 192.168.65.132:8886 [cluster]
root 2134 2123 0 21:04 pts/7 00:00:00 grep --color=auto redis
root@ubuntu:/usr/local#
将六个redis创建成集群,使用 ./redis-trib.rb
root@ubuntu:/usr/local/8881/src# ./redis-trib.rb create --replicas 1 192.168.65.132:8881 192.168.65.132:8882 192.168.65.132:8883 192.168.65.132:8884 192.168.65.132:8885 192.168.65.132:888
6
/usr/bin/env: ‘ruby’: No such file or directory
root@ubuntu:/usr/local/8881/src#
/usr/bin/env: ‘ruby’: No such file or directory
出现以上错误,需要先去安装 ruby 环境(搭建集群需要ruby环境)
yum install ruby (centos 系统可以这么用,我的是ubuntu的,所以用下面的命令)
apt-get install ruby
root@ubuntu:/usr/local# yum install ruby
There are no enabled repos.
Run "yum repolist all" to see the repos you have.
You can enable repos with yum-config-manager --enable
root@ubuntu:/usr/local# apt-get install ruby
root@ubuntu:/usr/local# apt-get install rubygems
启动集群 [OK] All 16384 slots covered.代表成功
root@ubuntu:/usr/local/8881/src# ./redis-trib.rb create --replicas 1 192.168.65.132:8881 192.168.65.132:8882 192.168.65.132:8883 192.168.65.132:8884 192.168.65.132:8885 192.168.65.132:8886
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 192.168.65.132:8881 192.168.65.132:8882 192.168.65.132:8883 192.168.65.132:8884 192.168.65.132:8885 192.168.65.132:8886 --cluster-replicas 1
To get help about all subcommands, type:
redis-cli --cluster help
root@ubuntu:/usr/local/8881/src# ./redis-cli --cluster create 192.168.65.132:8881 192.168.65.132:8882 192.168.65.132:8883 192.168.65.132:8884 192.168.65.132:8885 192.168.65.132:8886 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.65.132:8885 to 192.168.65.132:8881
Adding replica 192.168.65.132:8886 to 192.168.65.132:8882
Adding replica 192.168.65.132:8884 to 192.168.65.132:8883
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 48765dbf9b6656b133c5ebcad3679e866017eca1 192.168.65.132:8881
slots:[0-5460] (5461 slots) master
M: 5a7a109c5802a8798830c65d6e534f2b603d8073 192.168.65.132:8882
slots:[5461-10922] (5462 slots) master
M: 0a1987db957bbaa5af251345d96456622e12dada 192.168.65.132:8883
slots:[10923-16383] (5461 slots) master
S: 06054ff4e3444d1c814fe55576a7b9ee378ab2bb 192.168.65.132:8884
replicates 0a1987db957bbaa5af251345d96456622e12dada
S: bd2034206c5c84b7bc958a44fd5e2b122ef16d65 192.168.65.132:8885
replicates 48765dbf9b6656b133c5ebcad3679e866017eca1
S: bf2c6c0233807fdd533a9d96137305d406c688b6 192.168.65.132:8886
replicates 5a7a109c5802a8798830c65d6e534f2b603d8073
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.65.132:8881)
M: 48765dbf9b6656b133c5ebcad3679e866017eca1 192.168.65.132:8881
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 06054ff4e3444d1c814fe55576a7b9ee378ab2bb 192.168.65.132:8884
slots: (0 slots) slave
replicates 0a1987db957bbaa5af251345d96456622e12dada
M: 5a7a109c5802a8798830c65d6e534f2b603d8073 192.168.65.132:8882
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: bd2034206c5c84b7bc958a44fd5e2b122ef16d65 192.168.65.132:8885
slots: (0 slots) slave
replicates 48765dbf9b6656b133c5ebcad3679e866017eca1
S: bf2c6c0233807fdd533a9d96137305d406c688b6 192.168.65.132:8886
slots: (0 slots) slave
replicates 5a7a109c5802a8798830c65d6e534f2b603d8073
M: 0a1987db957bbaa5af251345d96456622e12dada 192.168.65.132:8883
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@ubuntu:/usr/local/8881/src#