CentOS7 安装redis6.2,主从复制,配置成服务,自启动,replicaof ,slaveof

该文详细介绍了如何在Linux系统中下载并安装Redis6.2.11,包括安装GCC,解压Redis源码,编译安装到自定义目录,处理启动时的警告,配置文件的修改,以及设置服务自启动。同时,文中还涉及了Redis的启动、客户端访问、主从复制的配置及解决复制过程中的错误问题。
摘要由CSDN通过智能技术生成

 linux版下载地址

Index of /releases/

windows版下载地址

Releases · microsoftarchive/redis · GitHub

安装gcc

rpm -q gcc

rpm -q gcc-c++

yum -y install gcc gcc-c++

一、安装

上传redis-6.2.11.tar.gz到/opt下

[root@bogon opt]# tar -zxvf redis-6.2.11.tar.gz
[root@bogon opt]# cd redis-6.2.11/


[root@bogon redis-6.2.11]# make

指定安装目录PREFIX
[root@bogon redis-6.2.11]# make install PREFIX=/opt/redis6
[root@bogon opt]# cd /opt/redis6
[root@localhost redis6]# ll
总用量 0
drwxr-xr-x. 2 root root 150 3月   6 03:10 bin
[root@bogon redis6]# cd bin
[root@bogon bin]# ll
总用量 18936
-rw-r--r--. 1 root root      93 3月   6 03:10 dump.rdb
-rwxr-xr-x. 1 root root 4830144 3月   6 03:03 redis-benchmark
lrwxrwxrwx. 1 root root      12 3月   6 03:03 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root      12 3月   6 03:03 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root 5004256 3月   6 03:03 redis-cli
lrwxrwxrwx. 1 root root      12 3月   6 03:03 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 9547400 3月   6 03:03 redis-server

二、启动服务

[root@localhost bin]# ./redis-server
29106:C 06 Mar 2023 03:10:55.776 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29106:C 06 Mar 2023 03:10:55.776 # Redis version=6.2.11, bits=64, commit=00000000, modified=0, pid=29106, just started
29106:C 06 Mar 2023 03:10:55.776 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
29106:M 06 Mar 2023 03:10:55.778 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.11 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 29106
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

29106:M 06 Mar 2023 03:10:55.781 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
29106:M 06 Mar 2023 03:10:55.781 # Server initialized
29106:M 06 Mar 2023 03:10:55.781 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. 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.
29106:M 06 Mar 2023 03:10:55.782 * Ready to accept connections

^C29106:signal-handler (1678090257) Received SIGINT scheduling shutdown...
29106:M 06 Mar 2023 03:10:57.989 # User requested shutdown...
29106:M 06 Mar 2023 03:10:57.989 * Saving the final RDB snapshot before exiting.
29106:M 06 Mar 2023 03:10:58.027 * DB saved on disk
29106:M 06 Mar 2023 03:10:58.027 # Redis is now ready to exit, bye bye...

解决两个警告

警告:无法强制执行TCP backlog设置511,因为/proc/sys/net/core/somaxconn设置为较低的值128。

 解决:redis优化/proc/sys/net/core/somaxconn

警告:内存设置为0!在内存不足的情况下,后台保存可能会失败。若要解决此问题,请将“vm.overcommit_memory =1”添加到/etc/sysctl.conf,然后重新启动或运行命令“sysctl vm.overcommit_memory =1”使其生效。

[root@bogon ~]# vi /etc/sysctl.conf
加入:
net.core.somaxconn = 2048
vm.overcommit_memory = 1
使之生效
[root@bogon ~]# sysctl -p

三、客户端访问

打卡新窗口
[root@bogon ~]# cd /opt/redis6/bin/
[root@bogon bin]# ./redis-cli
127.0.0.1:6379> get foo
(nil)
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> del foo
(integer) 1
127.0.0.1:6379> get foo
(nil)
127.0.0.1:6379> exit
[root@bogon bin]# ./redis-cli 
127.0.0.1:6379> quit

四、配置文件

创建目录(非必须),配置文件目录,数据库文件目录,日志文件目录

[root@bogon bin] cd /opt/redis6
[root@bogon redis6]# mkdir conf db logs

拷贝配置文件
[root@bogon redis6]# cp /opt/redis-6.2.11/redis.conf /opt/redis6    #/conf

编辑配置文件
[root@bogon redis6]# vi /opt/redis6/redis.conf

注释bind,或加入#bind 0.0.0.0(绑定本机可以接受访问的IP)
#bind 127.0.0.1 -::1
#bind 0.0.0.0

#关闭保护模式
#protected-mode yes
protected-mode no

守护进程,在后台运行
#daemonize no
daemonize yes


设置pid文件路径
#pidfile /var/run/redis_6379.pid
pidfile /opt/redis6/redis_6379.pid


#设置数据库文件路径
dbfilename dump.rdb

#dir ./
dir /opt/redis6    #/db

#设置日志文件路径
#logfile ""
#logfile /opt/redis6/redis.log
logfile /opt/redis6/redis.log

#设置主密码(主从复制时使用,主从切换会用到)
# masterauth <master-password>
masterauth redis6211

#设置连接密码
# requirepass foobared
requirepass redis6211

设置最大内存
# maxmemory <bytes>
maxmemory 10gb


#开启AOF持久化(做缓存,为了提高效率,这个可以不开)
#appendonly no

五、应用

启动redis服务

[root@bogon redis6]# /opt/redis6/bin/redis-server /opt/redis6/redis.conf

/opt/redis6/bin/redis-server /opt/redis6/6479/redis.conf
/opt/redis6/bin/redis-server /opt/redis6/6579/redis.conf


打开redis客户端,测试
[root@bogon ~]# /opt/redis6/bin/redis-cli -h 127.0.0.1 -p 6379 -a redis6211

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> del foo
(integer) 1
127.0.0.1:6379> quit (退出)

停止redis服务
[root@dev redis]# /opt/redis6/bin/redis-cli -h 127.0.0.1 -p 6379 -a redis6211 shutdown

杀进程
[root@bogon redis6]# ps -ef|grep redis
root     300865      1  0 17:16 ?        00:00:00 /opt/redis6/bin/redis-server 127.0.0.1:6379
root     300871 300392  0 17:16 pts/0    00:00:00 grep --color=auto redis

[root@dev bin]# kill -9 300865

查看日志
[root@dev redis]tail -f /opt/redis6/redis.log

性能测试

[root@dev bin]# /opt/redis6/bin/redis-benchmark -n 1000 -a redis6211

六、主从复制

在从的配置文件加入
新版本
# replicaof <masterip> <masterport>
replicaof 10.10.10.172 6379

老版本
slaveof 10.10.10.172 6379

报错的解决
1.需要设置主密码masterauth
59518:S 11 May 2021 18:46:53.749 * Retrying with SYNC...
59518:S 11 May 2021 18:46:53.750 # MASTER aborted replication with an error: NOAUTH Authentication required.
59518:S 11 May 2021 18:46:53.750 * Reconnecting to MASTER 10.10.10.172:6379 after failure
59518:S 11 May 2021 18:46:53.750 * MASTER <-> REPLICA sync started
59518:S 11 May 2021 18:46:53.750 * Non blocking connect for SYNC fired the event.
59518:S 11 May 2021 18:46:53.750 * Master replied to PING, replication can continue...
59518:S 11 May 2021 18:46:53.750 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
59518:S 11 May 2021 18:46:53.750 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
59518:S 11 May 2021 18:46:53.750 * Partial resynchronization not possible (no cached master)
59518:S 11 May 2021 18:46:53.750 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.

2.设置主密码后,requirepass和masterauth密码得一致
59657:S 11 May 2021 18:53:31.556 * Connecting to MASTER 10.10.10.172:6379
59657:S 11 May 2021 18:53:31.556 * MASTER <-> REPLICA sync started
59657:S 11 May 2021 18:53:31.556 * Non blocking connect for SYNC fired the event.
59657:S 11 May 2021 18:53:31.556 * Master replied to PING, replication can continue...
59657:S 11 May 2021 18:53:31.556 # Unable to AUTH to MASTER: -WRONGPASS invalid username-password pair or user is disabled.
 

3.注释bind 127.0.0.1 -::1或加:bind 0.0.0.0,解决

59817:S 11 May 2021 19:04:56.115 * Connecting to MASTER 10.10.10.172:6379
59817:S 11 May 2021 19:04:56.115 * MASTER <-> REPLICA sync started
59817:S 11 May 2021 19:04:56.115 # Error condition on socket for SYNC: Connection refused

七、配置成服务,自启动

[root@bogon ~]# vim /usr/lib/systemd/system/redis6.service

[Unit]
Description=redis6
After=network.target
 
[Service]
Type=forking
LimitNOFILE=infinity
ExecStart=/opt/redis6/bin/redis-server /opt/redis6/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/redis6/bin/redis-cli -a redis6211 shutdown
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

ExecStop这么写比ExecStop=/bin/kill -s QUIT $MAINPID友好点,停止时会做处理,会有日志。

配置项中的意义可以百度搜索:Systemd Unit文件。

启动停止服务
[root@bogon ~]# systemctl start redis6
[root@bogon ~]# systemctl stop redis6

配置成自启动
[root@bogon ~]# systemctl enable redis6
Created symlink from /etc/systemd/system/multi-user.target.wants/redis6.service to /usr/lib/systemd/system/redis6.service.

配置文件修改后。
Warning: redis6.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@bogon ~]# systemctl daemon-reload

另外以下的两个文件都可以配置自启动

 /opt/redis-6.2.3/utils/redis_init_script
/opt/redis-6.2.3/utils/install_server.sh

redis5 集群自启动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值