linux版下载地址
windows版下载地址
Releases · microsoftarchive/redis · GitHub
安装gcc
yum -y install gcc gcc-c++
rpm -q gcc
rpm -q gcc-c++
一、安装
上传redis-6.2.3.tar.gz到/opt下
[root@bogon opt]# tar -zxvf redis-6.2.3.tar.gz
[root@bogon opt]# cd redis-6.2.3/
[root@bogon redis-6.2.3]# make
指定安装目录PREFIX
[root@bogon redis-6.2.3]# make install PREFIX=/opt/redis6
[root@bogon opt]# cd /opt/redis6
[root@bogon redis6]# ll
总用量 0
drwxr-xr-x. 2 root root 134 5月 10 23:29 bin
[root@bogon redis6]# cd bin
[root@bogon bin]# ll
总用量 18872
-rwxr-xr-x. 1 root root 4829416 5月 10 23:29 redis-benchmark
lrwxrwxrwx. 1 root root 12 5月 10 23:29 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root 12 5月 10 23:29 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root 5002832 5月 10 23:29 redis-cli
lrwxrwxrwx. 1 root root 12 5月 10 23:29 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 9484192 5月 10 23:29 redis-server
二、启动服务
[root@bogon bin]# ./redis-server
59122:C 11 May 2021 18:16:40.791 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
59122:C 11 May 2021 18:16:40.791 # Redis version=6.2.3, bits=64, commit=00000000, modified=0, pid=59122, just started
59122:C 11 May 2021 18:16:40.791 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
59122:M 11 May 2021 18:16:40.792 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 59122
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
59122:M 11 May 2021 18:16:40.794 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
59122:M 11 May 2021 18:16:40.794 # Server initialized
59122:M 11 May 2021 18:16:40.794 # 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.
59122:M 11 May 2021 18:16:40.794 * Ready to accept connections
^C59122:signal-handler (1620728333) Received SIGINT scheduling shutdown...
59122:M 11 May 2021 18:18:53.485 # User requested shutdown...
59122:M 11 May 2021 18:18:53.485 * Saving the final RDB snapshot before exiting.
59122:M 11 May 2021 18:18:53.576 * DB saved on disk
59122:M 11 May 2021 18:18:53.576 # 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.3/redis.conf /opt/redis6/conf
编辑配置文件
[root@bogon redis6]# vi /opt/redis6/conf/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/logs/redis.log
#设置连接密码
# requirepass foobared
requirepass redis623
#设置主密码(主从复制时使用,主从切换会用到)
# masterauth <master-password>
masterauth redis623
设置最大内存
# maxmemory <bytes>
maxmemory 10gb
五、应用
启动redis服务
[root@bogon redis6]# /opt/redis6/bin/redis-server /opt/redis6/conf/redis.conf
打开redis客户端,测试
[root@bogon ~]# /opt/redis6/bin/redis-cli -h 127.0.0.1 -p 6379 -a redis623
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 redis623 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/logs/redis.log
性能测试
[root@dev bin]# /opt/redis6/bin/redis-benchmark -n 1000 -a redis623
六、主从复制
在从的配置文件加入
新版本
# replicaof <masterip> <masterport>
replicaof 10.16.10.172 6379
老版本
slaveof 10.16.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.16.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.16.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.16.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/redis_6379.service
[Unit]
Description=redis_6379
After=network.target
[Service]
Type=forking
ExecStart=/opt/redis6/bin/redis-server /opt/redis6/conf/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/redis6/bin/redis-cli -a redis623 shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
ExecStop这么写比ExecStop=/bin/kill -s QUIT $MAINPID友好点,停止时会做处理,会有日志。
配置项中的意义可以百度搜索:Systemd Unit文件。
启动停止服务
[root@bogon ~]# systemctl start redis_6379
[root@bogon ~]# systemctl stop redis_6379
配置成自启动
[root@bogon ~]# systemctl enable redis_6379
Created symlink from /etc/systemd/system/multi-user.target.wants/redis_6379.service to /usr/lib/systemd/system/redis_6379.service.
配置文件修改后。
Warning: redis_6379.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