Linux下安装Redis及单设备集群配置

一、安装Redis

输入下载命令:

wget http://download.redis.io/releases/redis-4.0.1.tar.gz

部分日志:

--2019-02-25 21:15:07--  http://download.redis.io/releases/redis-4.0.1.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1711660 (1.6M) [application/x-gzip]
Saving to: ‘redis-4.0.1.tar.gz.1’

29% [=====================>                                                            ] 512,283     12.9KB/s  eta 77s

等待下载完成。

二、解压

1、输入解压命令:

tar -zxvf redis-4.0.1.tar.gz

2、解压完成后进入安装文件夹:

cd redis-4.0.1

3、新建redis文件夹:

mkdir redis

4、进入redis文件夹:

cd redis

5、把安装目录下所有文件移动到新建的redis文件夹下:

mv ../* .

6、执行编译命令(如果编译时报gcc命令找不到,则先执行安装gcc命令:yum install gcc-c++):

make

部分日志:

cd src && make all
make[1]: Entering directory `/peerless/redis-4.0.1/redis/src'
    CC Makefile.dep
等待编译完成

7、编译完成后可以看到在src目录下多了几个新文件

-rwxr-xr-x 1 root root 5741872 Feb 25 22:05 redis-server
-rwxr-xr-x 1 root root 5741872 Feb 25 22:05 redis-sentinel
-rw-r--r-- 1 root root  371424 Feb 25 22:05 redis-cli.o
-rwxr-xr-x 1 root root 2605792 Feb 25 22:06 redis-cli
-rw-r--r-- 1 root root  109040 Feb 25 22:06 redis-benchmark.o
-rwxr-xr-x 1 root root 2451504 Feb 25 22:06 redis-benchmark
-rwxr-xr-x 1 root root 5741872 Feb 25 22:06 redis-check-rdb
-rwxr-xr-x 1 root root 5741872 Feb 25 22:06 redis-check-aof

8、为了使用的方便,我们要把这几个文件加到usr/local/bin目录下去,执行命令:

make install

 

这样我们就可以在任意地方执行这些命令。

9、启动redis,执行命令:

redis-server

日志:

4792:C 25 Feb 22:23:31.474 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4792:C 25 Feb 22:23:31.474 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=4792, just started
4792:C 25 Feb 22:23:31.474 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )         Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |       PID: 4792
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4792:M 25 Feb 22:23:31.476 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4792:M 25 Feb 22:23:31.476 # Server initialized
4792:M 25 Feb 22:23:31.476 # 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.
4792:M 25 Feb 22:23:31.476 # 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.
4792:M 25 Feb 22:23:31.476 * Ready to accept connections
 

10、也可以通过ps命令来检查redis是否启动,执行命令:

ps aux | grep redis

11、停止redis服务,执行命令:

redis-cli shutdown

 

三、配置redis集群

 1、修改redis.conf文件,先拷贝一份再做修改

cp redis.conf redis.conf2

vi redis.conf

屏蔽限制本地访问,在bind 127.0.0.1之前加#

protected-mode后的yes改为no

配置后台启动,daemonize no 修改为: daemonize yes

去掉cluster-enabled yes前面的#

去掉cluster-node-timeout 15000前面的#

修改完这五项后保存文件

 按esc键后 :wq 保存文件

2、在redis的同级目录下新建cluster文件夹,再在cluster下新建7000和7001文件夹

[root@izbp1iatx redis-4.0.1]# mkdir cluster
[root@izbp1iatx redis-4.0.1]# mkdir cluster/7000
[root@izbp1iatx redis-4.0.1]# mkdir cluster/7001

 把修改完成的redis.conf文件夹下所以文件拷贝一份到7000和7001文件夹

[root@izbp1iatx redis-4.0.1]# cp -r redis/* cluster/7000
[root@izbp1iatxz redis-4.0.1]# cp -r redis/* cluster/7001

再次修改7000和7001文件夹下的redis.conf文件,修改port端口号为7000和7001

[root@izbp1iatxredis-4.0.1]# cd cluster/7000
[root@izbp1iatxz 7000]# vi redis.conf 

3、分别去对应目录下启动redis服务

[root@izbp1iatx 7000]# redis-server redis.conf
6670:C 25 Feb 23:02:51.485 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6670:C 25 Feb 23:02:51.485 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=6670, just started
6670:C 25 Feb 23:02:51.485 # Configuration loaded
[root@izbp1iatxz 7000]# cd ../7001
[root@izbp1iatx 7001]# redis-server redis.conf
6681:C 25 Feb 23:03:03.453 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6681:C 25 Feb 23:03:03.453 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=6681, just started
6681:C 25 Feb 23:03:03.453 # Configuration loaded
[root@izbp1iatx 7001]# cd ../../redis/
[root@izbp1iatxyz redis]# redis-server redis.conf
6691:C 25 Feb 23:03:17.447 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6691:C 25 Feb 23:03:17.447 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=6691, just started
6691:C 25 Feb 23:03:17.447 # Configuration loaded
 

4、检查端口号,看redis是否启动成功

[root@izbp1iatx redis]# ps aux | grep redis
root      6671  0.2  0.1 149376  2792 ?        Ssl  Jul11   0:00 redis-server *:7000 [cluster]
root      6781  0.0  0.1 153472  2812 ?        Ssl  Jul11   0:00 redis-server *:7001 [cluster]
root      6790  0.0  0.1 149376  2792 ?        Ssl  Jul11   0:00 redis-server *:6379 [cluster]
root      6801  0.0  0.0 116800  1016 pts/0    R+   Jul11   0:00 grep --color=auto redis 

5、配置集群

进入7001目录下的src目录,执行集群创建语句:

./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:7000 127.0.0.1:7001

发现报错

127.0.0.1:6379 127.0.0.1:7000 127.0.0.1:7001

/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)

 原因是没有安装redis第三方接口,执行命令安装

 [root@izbp1iatx src]# gem install redis
Fetching: redis-4.1.0.gem (100%)
ERROR:  Error installing redis:
    redis requires Ruby version >= 2.2.2.

 原先安装的ruby版本太低(没有安装ruby的需先安装ruby),所以升级一下ruby版本:

[root@izbp1iatx src]# ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
[root@izbp1iatx src]# yum remove ruby
 

 卸载完成后重新安装ruby

下载ruby安装包

[root@izbp1iatx src]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.gz

解压下载的ruby安装包

[root@izbp1iatx src]# tar -xvf ruby-2.5.0.tar.gz

创建要安装ruby的目录

[root@izbp1iatx src]# mdkir -p /usr/local/ruby

进入刚才解压的ruby目录,配置并制定要安装ruby的目录

[root@izbp1iatx src]# ./configure --prefix=/usr/local/ruby

还是在此目录下,执行安装命令(安装时间要比较久)

[root@izbp1iatx src]# make install 

建立快捷方式

[root@izbp1iatx src]# ln -s /usr/local/ruby/bin/ruby /usr/local/bin/ruby 

查看是否安装成功

[root@izbp1iatx src]# ruby -v

ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux] 

到此ruby安装完成 

如果此时配置redis集群,会报如下错误:

Traceback (most recent call last):
    2: from ./redis-trib.rb:25:in `<main>'
    1: from /usr/local/ruby/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'

此时要先执行命令

gem install redis

此时又发现gem命令未安装(真的是一个接一个的坑),用yum命令安装

yum install -y rubygems  

安装完后继续 gem install redis, 安装完后接着之前的集群安装

./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:7000 127.0.0.1:7001

看到集群常见成功的提示,至此redis集群全部安装完毕。

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值