CentOS 7 安装Redis单机版和集群

╰+攻爆jí腚メ 2022-05-13 08:20 317阅读 0赞

Redis下载

http://download.redis.io/releases/redis-4.0.11.tar.gz

一、单机版搭建

1.上传压缩包到 /usr/download (我自定义放安装包的目录)

2.安装gcc编译环境,命令 yum install gcc-c++

3.解压 redis-4.0.11.tar.gz 文件,命令 tar -zxvf redis-4.0.11.tar.gz (此时会解压到当前目录,我的是在 usr/download下,文件夹名是redis-4.0.11

4.将解压出来的 redis-4.0.11 移动到 /usr/local 下并且命名为 redis ,命令 mv /usr/download/redis-4.0.11 /usr/local/redis

5.进入redis文件夹,cd /usr/local/redis ,执行命令make install PREFIX=/usr/local/redis(指定安装路径为/usr/local/redis),可以看到在src目录下生成了几个新的文件,如图(/usr/local/redis目录下):

70

6.启动

方式一:进入redis的src目录下,执行 ./redis-server

方式二:修改redis.conf,进入编辑模式 vim /usr/local/redis/redis.conf 修改daemonize yes(默认是 no ),:wq (保存退出),使用命令启动:./src/redis-server redis.conf(在安装目录/usr/local/redis/执行)

方式三:为了使用方便,我们需要将上面第5步make后生成的这个几个文件加到/usr/local/bin目录下去。这个目录在Path下面的话,就可以直接执行这几个命令了。

  1. [root@localhost redis]# make install
  2. cd src && make install
  3. make[1]: Entering directory `/usr/local/redis/src'
  4. CC Makefile.dep
  5. make[1]: Leaving directory `/usr/local/redis/src'
  6. make[1]: Entering directory `/usr/local/redis/src'
  7. Hint: It's a good idea to run 'make test' ;)
  8. INSTALL install
  9. INSTALL install
  10. INSTALL install
  11. INSTALL install
  12. INSTALL install
  13. make[1]: Leaving directory `/usr/local/redis/src'
  14. [root@localhost redis]# cd ..
  15. [root@localhost local]# ll
  16. 总用量 44
  17. drwxr-xr-x. 2 root root 4096 9 3 10:16 bin
  18. drwxr-xr-x. 2 root root 4096 9 3 10:04 data
  19. drwxr-xr-x. 2 root root 4096 9 23 2011 etc
  20. drwxr-xr-x. 2 root root 4096 9 23 2011 games
  21. drwxr-xr-x. 2 root root 4096 9 23 2011 include
  22. drwxr-xr-x. 2 root root 4096 9 23 2011 lib
  23. drwxr-xr-x. 2 root root 4096 9 23 2011 libexec
  24. drwxrwxr-x. 6 root root 4096 7 24 22:58 redis
  25. drwxr-xr-x. 2 root root 4096 9 23 2011 sbin
  26. drwxr-xr-x. 5 root root 4096 4 1 04:48 share
  27. drwxr-xr-x. 2 root root 4096 9 23 2011 src
  28. [root@localhost local]# ll bin
  29. 总用量 30908
  30. -rwxr-xr-x. 1 root root 4985275 9 3 10:16 redis-benchmark
  31. -rwxr-xr-x. 1 root root 7185836 9 3 10:16 redis-check-aof
  32. -rwxr-xr-x. 1 root root 7185836 9 3 10:16 redis-check-rdb
  33. -rwxr-xr-x. 1 root root 5092431 9 3 10:16 redis-cli
  34. lrwxrwxrwx. 1 root root 12 9 3 10:16 redis-sentinel -> redis-server
  35. -rwxr-xr-x. 1 root root 7185836 9 3 10:16 redis-server
  36. [root@localhost local]#

可以看到,这几个文件就已经被加载到bin目录下了,下面启动服务器,来看看安装是否成功。使用redis-server命令。

  1. [root@localhost local]# redis-server
  2. 9190:C 03 Sep 10:19:09.291 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  3. 9190:C 03 Sep 10:19:09.292 # Redis version=4.0.1, bits=32, commit=00000000, modified=0, pid=9190, just started
  4. 9190:C 03 Sep 10:19:09.292 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
  5. 9190:M 03 Sep 10:19:09.295 * Increased maximum number of open files to 10032 (it was originally set to 1024).
  6. 9190:M 03 Sep 10:19:09.312 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
  7. _._
  8. _.-``__ ''-._
  9. _.-`` `. `_. ''-._ Redis 4.0.1 (00000000/0) 32 bit
  10. .-`` .-```. ```\/ _.,_ ''-._
  11. ( ' , .-` | `, ) Running in standalone mode
  12. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
  13. | `-._ `._ / _.-' | PID: 9190
  14. `-._ `-._ `-./ _.-' _.-'
  15. |`-._`-._ `-.__.-' _.-'_.-'|
  16. | `-._`-._ _.-'_.-' | http://redis.io
  17. `-._ `-._`-.__.-'_.-' _.-'
  18. |`-._`-._ `-.__.-' _.-'_.-'|
  19. | `-._`-._ _.-'_.-' |
  20. `-._ `-._`-.__.-'_.-' _.-'
  21. `-._ `-.__.-' _.-'
  22. `-._ _.-'
  23. `-.__.-'
  24. 9190:M 03 Sep 10:19:09.316 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
  25. 9190:M 03 Sep 10:19:09.316 # Server initialized
  26. 9190:M 03 Sep 10:19:09.318 # 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.
  27. 9190:M 03 Sep 10:19:09.318 * Ready to accept connections

看到这个界面的话,就表示安装成功了。

  下面通过在启动一个linux客户端,通过server-cli shutdown来关闭服务器。

  1. [root@localhost ~]# redis-cli shutdown
  2. [root@localhost ~]#
  3. 下面是刚刚服务器启动的客户端的log
  4. 9190:M 03 Sep 10:20:45.566 # User requested shutdown...
  5. 9190:M 03 Sep 10:20:45.566 * Saving the final RDB snapshot before exiting.
  6. 9190:M 03 Sep 10:20:45.631 * DB saved on disk
  7. 9190:M 03 Sep 10:20:45.631 # Redis is now ready to exit, bye bye...
  8. [root@localhost local]#
  9. 此处参考:[https://www.cnblogs.com/zerotomax/p/7468833.html][https_www.cnblogs.com_zerotomax_p_7468833.html]

方式四:设置redis开机自启动

(1)在/etc 目录下新建redis目录

  1. mkdir redis

(2)将/usr/local/redis/redis.conf文件拷贝一份到/etc/redis目录下,并命名6379.conf

  1. cp /usr/local/redis/redis.conf /etc/redis/6379.conf

(3)将redis的启动脚本复制一份放到/etc/init.d目录下

  1. cp /usr/local/redis/utils/redis_init_script /etc/init.d/redisd

(4)先切换到/etc/init.d目录下,然后执行自启命令

  1. chkconfig redisd on

service redisd does not support chkconfig

看结果是redisd不支持chkconfig

解决方法:

使用vim编辑redisd文件,在第一行加入如下两行注释,保存退出

  1. # chkconfig: 2345 90 10
  2. # description: Redis is a persistent key-value database

注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

70 1

(5)再次执行开机自启命令,成功

  1. chkconfig redisd on

现在可以直接已服务的形式启动和关闭redis了

启动:

  1. service redisd start

关闭:

  1. service redisd stop

到此,开机自启设置完成。。。

7.查看是否启动 ps aux | grep redis

70 2

到此,单机版安装完成。下面是集群搭建。

二、集群搭建

Redis 集群至少要有三个节点,每个节点有一备份节点,需要6台服务器。

1.创建一个文件夹:mkdir redis-cluster (我是在/usr/local/下执行命令)
2.将单机版的redis拷贝到redis-cluster文件夹下:cp redis redis-cluster/ -r (我是在/usr/local/下执行命令)
3.将redis-cluster下的redis重命名为redis01,使用命令mv redis redis01 (我是在/usr/local/redis-cluster/下执行命令)
4.删除redis01文件夹下的dump.rdb,使用命令rm dump.rdb(/usr/local/redis-cluster/redis01/)
5.修改配置文件,使用命令vi redis.conf打开配置文件,使用insert键切换到可编辑状态,依次修改以下三个地方:

  1. ①开启后台启动:

70 3

  1. ②修改端口:

70 4

  1. ③打开集群注释:

70 5

6.再拷贝5个单机版redis,分别修改配置文件,更改端口由7002至7006

  1. cp -r redis01/ redis02
  2. cp -r redis01/ redis03
  3. cp -r redis01/ redis04
  4. cp -r redis01/ redis05
  5. cp -r redis01/ redis06

7.需要一个ruby脚本,在redis源码文件夹下的src目录下(/usr/local/tools/redis/).将redis-trib.rb拷贝到redis-cluster文件夹下,使用命令

  1. cp redis-trib.rb /usr/local/redis-cluster/(在/usr/local/tools/redis-3.0.0/路径下执行命令)

8.执行ruby脚本之前先安装ruby环境:

①先执行yum install ruby(此命令相当于安装了一个ruby虚拟机,用来解释ruby脚本)
②安装成功后执行yum install rubygems(此命令是ruby的包管理器)
③安装redis-trib.rb运行依赖的ruby的包,执行命令进行安装gem install redis,提示:

  1. gem install redis
  2. ERROR: Error installing redis:
  3. redis requires Ruby version >= 2.2.2.
  4. 原因,ruby版本小于了2.2.2
  5. 1)安装RVM:
  6. gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
  7. curl -L get.rvm.io | bash -s stable
  8. find / -name rvm -print

/usr/local/rvm
/usr/local/rvm/src/rvm
/usr/local/rvm/src/rvm/bin/rvm
/usr/local/rvm/src/rvm/lib/rvm
/usr/local/rvm/src/rvm/scripts/rvm
/usr/local/rvm/bin/rvm
/usr/local/rvm/lib/rvm
/usr/local/rvm/scripts/rvm

  1. source /usr/local/rvm/scripts/rvm

(2)查看rvm库中已知的ruby版本

  1. rvm list known

# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
ruby-head
……

(3)安装一个Ruby版本

  1. rvm install 2.5.1

Searching for binary rubies, this might take some time.
No binary rubies available for: centos/7/x86_64/ruby-2.5.1.
Continuing with compilation. Please read ‘rvm help mount’ to get more information on binary rubies.
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: patch, automake, bison, libffi-devel, libtool, patch, readline-devel, sqlite-devel, zlib-devel, openssl-devel………………….
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.5.1, this may take a while depending on your cpu(s)…
ruby-2.5.1 - #downloading ruby-2.5.1, this may take a while depending on your connection…
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13.3M 100 13.3M 0 0 888k 0 0:00:15 0:00:15 —:—:— 1199k
ruby-2.5.1 - #extracting ruby-2.5.1 to /usr/local/rvm/src/ruby-2.5.1…..
ruby-2.5.1 - #configuring………………………………………………………….
ruby-2.5.1 - #post-configuration..
ruby-2.5.1 - #compiling…………………………………………………………………………
ruby-2.5.1 - #installing………………………..
ruby-2.5.1 - #making binaries executable..
ruby-2.5.1 - #downloading rubygems-2.7.7
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 894k 100 894k 0 0 125k 0 0:00:07 0:00:07 —:—:— 142k

……
(4)使用当前安装的Ruby

  1. rvm use 2.5.1

Using /usr/local/rvm/gems/ruby-2.5.1

  1. 5)设置默认版本
  2. rvm use 2.5.1 --default

Using /usr/local/rvm/gems/ruby-2.5.1

  1. 6)卸载一个已知版本
  2. rvm remove 2.0.0
  3. 7)查看Ruby版本
  4. ruby --version

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

  1. 8)安装redis
  2. gem install redis

Fetching: redis-4.0.2.gem (100%)
Successfully installed redis-4.0.2
Parsing documentation for redis-4.0.2
Installing ri documentation for redis-4.0.2
Done installing documentation for redis after 1 seconds
1 gem installed

  1. 9)上面的步骤完事了,接下来运行一下redis-trib.rb

70 6

9.启动所有的redis实例

①在安装集群的路径下(我的是/usr/local/redis-cluster),编写一个启动脚本,vi start-all.sh,写完之后按ESC键输入:wq保存。

  1. cd redis01
  2. ./src/redis-server redis.conf
  3. cd ..
  4. cd redis02
  5. ./src/redis-server redis.conf
  6. cd ..
  7. cd redis03
  8. ./src/redis-server redis.conf
  9. cd ..
  10. cd redis04
  11. ./src/redis-server redis.conf
  12. cd ..
  13. cd redis05
  14. ./src/redis-server redis.conf
  15. cd ..
  16. cd redis06
  17. ./src/redis-server redis.conf
  18. cd ..

②给start-all.sh执行的权限,使用命令chmod +x start-all.sh,授权前后对比

70 7

③使用脚本启动redis实例,使用命令./start-all.sh

70 8

执行命令:./redis-trib.rb create —replicas 1 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 127.0.0.1:7006,(这里直接写在脚本里了)结果如下:

70 9

70 10

10.连接集群 redis-cli -c -h 你的IP -p 端口 或者 ./redis01/src/redis-cli -c -h 你的IP -p 端口

70 11

11.查看集群中的所有主节点信息 redis-cli -c -p 7001 cluster nodes | grep master

  1. redis-cli -c -p 7001 cluster nodes | grep master

到此,Redis就安装完毕了。

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读