redis单机,集群搭建教程

环境准备:

Linux 版本:Centos 7.0.2009

Redis版本:redis-5.0.3.tar.gz

一、redis是什么?

通常而言目前的数据库分类有几种,包括 SQL/NSQL,,关系数据库,键值数据库等等 等,分类的标准也不以,Redis本质上也是一种键值数据库的,但它在保持键值数据库简单快捷特点的同时,又吸收了部分关系数据库的优点。从而使它的位置处于关系数据库和键值数 据库之间。Redis不仅能保存Strings类型的数据,还能保存Lists类型(有序)和Sets类型(无序)的数据,而且还能完成排序(SORT) 等高级功能,在实现INCR,SETNX等功能的时候,保证了其操作的原子性,除此以外,还支持主从复制等功能。
 

二、单机搭建步骤

1.环境准备

查看Linux版本和上传Redis压缩包,并解压 :

#查看Centos版本
[root@VM-0-14-centos wonxfey]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
#查看Gcc版本,redis安装需要Gcc,如果没有安装就用 yum install gcc-c++ 安装命令
[root@VM-0-14-centos bin]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
[root@VM-0-14-centos wonxfey]# pwd
/usr/local/wonxfey
[root@VM-0-14-centos wonxfey]# tar -vxf redis-5.0.3.tar.gz
[root@VM-0-14-centos wonxfey]# ls
redis-5.0.3  redis-5.0.3.tar.gz

2.安装步骤

#redis编译
[root@VM-0-14-centos redis-5.0.3]# make

#安装并指定安装目录
[root@VM-0-14-centos redis-5.0.3]# make install PREFIX=/usr/local/redis
cd src && make install
make[1]: Entering directory `/usr/local/wonxfey/redis-5.0.3/src'
    CC Makefile.dep
make[1]: Leaving directory `/usr/local/wonxfey/redis-5.0.3/src'
make[1]: Entering directory `/usr/local/wonxfey/redis-5.0.3/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/wonxfey/redis-5.0.3/src'

#执行查看安装是否报错
[root@VM-0-14-centos redis-5.0.3]# echo $?
0

3.启动方式

(1)前台启动(退出界面就会关闭redis)

#进入安装目录
[root@VM-0-14-centos redis-5.0.3]# cd /usr/local/redis/bin/
#启动命令
[root@VM-0-14-centos bin]# ./redis-server
26164:C 24 Jan 2022 14:50:46.380 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26164:C 24 Jan 2022 14:50:46.380 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=26164, just started
26164:C 24 Jan 2022 14:50:46.380 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
26164:M 24 Jan 2022 14:50:46.381 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 26164
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

26164:M 24 Jan 2022 14:50:46.382 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
26164:M 24 Jan 2022 14:50:46.382 # Server initialized
26164:M 24 Jan 2022 14:50:46.382 # 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.
26164:M 24 Jan 2022 14:50:46.382 # 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.
26164:M 24 Jan 2022 14:50:46.382 * Ready to accept connections

(2)后台启动方式

复制redis源码目录的配置文件redis.conf到redis安装目录,修改daemonize no改成daemonize yes

#复制文件
[root@VM-0-14-centos bin]# cp /usr/local/wonxfey/redis-5.0.3/redis.conf /usr/local/redis/bin/

#修改文件
[root@VM-0-14-centos bin]# vim /usr/local/redis/bin/redis.conf 

#启动命令
[root@VM-0-14-centos bin]# ./redis-server redis.conf 
28968:C 24 Jan 2022 15:05:33.593 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28968:C 24 Jan 2022 15:05:33.593 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=28968, just started
28968:C 24 Jan 2022 15:05:33.593 # Configuration loaded

#查看是否启动
[root@VM-0-14-centos bin]# ps -ef|grep redis
root     28969     1  0 15:05 ?        00:00:00 ./redis-server 127.0.0.1:6379
root     28994 18931  0 15:05 pts/0    00:00:00 grep --color=auto redis

(3)创建一个软链接,方便使用redis

[root@VM-0-14-centos bin]# ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
[root@VM-0-14-centos bin]# redis
127.0.0.1:6379>

三、集群搭建步骤(在一台机子搭建一个伪集群)

(1)直接使用编译过后的源码,放在一个redis集群包中,基于单机make(编译)后的src包即可,并且复制源码中的配置文件过来,放到每个节点包里面去

[root@VM-0-14-centos local]# mkdir redis-cluster
[root@VM-0-14-centos local]# cd redis-cluster/
[root@VM-0-14-centos redis-cluster]# cp -r /usr/local/wonxfey/redis-5.0.3/src src
[root@VM-0-14-centos redis-cluster]# cp -r /usr/local/wonxfey/redis-5.0.3/redis.conf redis.conf

(2)修改配置,复制多个src文件分别以端口命令

[root@VM-0-14-centos 7000]# vim redis.conf 
    pidfile /var/run/redis_7000.pid
	port 7000
	cluster-enabled yes
	cluster-config-file 7000.conf
	cluster-node-timeout 5000
	appendonly yes
	#内网保护
	protected-mode no
	密码 (如果对集群设置密码,那么requirepass和masterauth都需要设置,否则发生主从切换时,就会遇到授权问题)
	requirepass 
	意思是该redis使用该IP来接受外部的连接(注意:bind的意思不是绑定外部服务器的IP,而是绑定本机可以接受访问的IP)
	bind

[root@VM-0-14-centos redis-cluster]# cp -r 7000 7001
[root@VM-0-14-centos redis-cluster]# vim 7001/redis.conf 
[root@VM-0-14-centos redis-cluster]# cp -r 7001 7002
[root@VM-0-14-centos redis-cluster]# vim 7002/redis.conf
[root@VM-0-14-centos redis-cluster]# cp -r 7002 7003
[root@VM-0-14-centos redis-cluster]# vim 7003/redis.conf 
[root@VM-0-14-centos redis-cluster]# cp -r 7003 7004
[root@VM-0-14-centos redis-cluster]# vim 7004/redis.conf 
[root@VM-0-14-centos redis-cluster]# cp -r 7004 7005
[root@VM-0-14-centos redis-cluster]# vim 7005/redis.conf 
[root@VM-0-14-centos redis-cluster]# cp -r 7005 7006
[root@VM-0-14-centos redis-cluster]# vim 7006/redis.conf

(3)修改完成之后,为了方便启动,创建一个启动的脚本,并启动

[root@VM-0-14-centos redis-cluster]# vim starts.sh

cd /usr/local/redis-cluster/7000
./redis-server redis.conf
cd /usr/local/redis-cluster/7001
./redis-server redis.conf
cd /usr/local/redis-cluster/7002
./redis-server redis.conf
cd /usr/local/redis-cluster/7003
./redis-server redis.conf
cd /usr/local/redis-cluster/7004
./redis-server redis.conf
cd /usr/local/redis-cluster/7005
./redis-server redis.conf

[root@VM-0-14-centos redis-cluster]# chmod 755 starts.sh

[root@VM-0-14-centos redis-cluster]# vim starts.sh 
[root@VM-0-14-centos redis-cluster]# ./starts.sh 
8290:C 24 Jan 2022 16:07:08.309 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8290:C 24 Jan 2022 16:07:08.309 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8290, just started
8290:C 24 Jan 2022 16:07:08.309 # Configuration loaded
8293:C 24 Jan 2022 16:07:08.314 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8293:C 24 Jan 2022 16:07:08.314 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8293, just started
8293:C 24 Jan 2022 16:07:08.314 # Configuration loaded
8295:C 24 Jan 2022 16:07:08.318 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8295:C 24 Jan 2022 16:07:08.318 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8295, just started
8295:C 24 Jan 2022 16:07:08.318 # Configuration loaded
8297:C 24 Jan 2022 16:07:08.327 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8297:C 24 Jan 2022 16:07:08.327 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8297, just started
8297:C 24 Jan 2022 16:07:08.327 # Configuration loaded
8302:C 24 Jan 2022 16:07:08.331 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8302:C 24 Jan 2022 16:07:08.331 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8302, just started
8302:C 24 Jan 2022 16:07:08.331 # Configuration loaded
8304:C 24 Jan 2022 16:07:08.341 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8304:C 24 Jan 2022 16:07:08.341 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8304, just started
8304:C 24 Jan 2022 16:07:08.341 # Configuration loaded
[root@VM-0-14-centos redis-cluster]# ps -ef|grep redis
root      8291     1  0 16:07 ?        00:00:00 ./redis-server *:7000 [cluster]
root      8294     1  0 16:07 ?        00:00:00 ./redis-server *:7001 [cluster]
root      8296     1  0 16:07 ?        00:00:00 ./redis-server *:7002 [cluster]
root      8301     1  0 16:07 ?        00:00:00 ./redis-server *:7003 [cluster]
root      8303     1  0 16:07 ?        00:00:00 ./redis-server *:7004 [cluster]
root      8311     1  0 16:07 ?        00:00:00 ./redis-server *:7005 [cluster]
root      8348 18931  0 16:07 pts/0    00:00:00 grep --color=auto redis

(4)搭建redis集群

#{ip} 指得是自己得服务器IP地址
[root@VM-0-14-centos redis-cluster]# ./redis-cli --cluster create {ip}:7000 {ip}:7001 {ip}:7002 {ip}:7003 {ip}:7004 {ip}:7005 --cluster-replicas 1 -a test


[root@VM-0-14-centos redis-cluster]# cp /usr/localhost/wonxfey/redis-5.0.3/src/redis-cli /usr/local/bin/
#添加配置,方便直接访问redis集群
[root@VM-0-14-centos opt]# echo "PATH=$PATH:/usr/local/bin" >>/etc/profile
[root@VM-0-14-centos opt]# source /etc/profile

[root@VM-0-14-centos opt]# redis-cli -c -p 7000 -a test
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7000> 


总结

新手搭建,仅供参考,遇到问题欢迎一起讨论。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值