1. 安装
[root@VM_0_3_centos ~]# yum -y install redis
2. 启动/关闭/重启
# 配置文件启动 &表示后台运行
[root@VM_0_3_centos ~]# redis-server /etc/redis.conf &
# 关闭
[root@VM_0_3_centos ~]# redis-cli shutdown
# 或者
[root@VM_0_3_centos ~]# ps -ef | grep redis
redis 30440 1 0 15:04 ? 00:00:00 /usr/bin/redis-server *:6379
root 30898 28840 0 15:07 pts/1 00:00:00 grep --color=auto redis
# 直接杀死进程
[root@VM_0_3_centos ~]# kill -9 30440
3. 访问
[root@VM_0_3_centos ~]# redis-cli
127.0.0.1:6379> set userName "admin"
OK
127.0.0.1:6379> get userName
"admin"
127.0.0.1:6379> exit
[root@VM_0_3_centos ~]#
# 说明可以正常使用。
4. Redis 密码设置
# 其实设置密码有两种方式: ① 命令 ② 修改配置,先尝试第一种
# 我们通过命令来设置密码, 默认是没有密码的
127.0.0.1:6379> CONFIG get requirepass
1) "requirepass"
2) ""
127.0.0.1:6379>
# 设置密码
127.0.0.1:6379> CONFIG set requirepass "123123"
OK
# 验证密码
127.0.0.1:6379> auth "123123"
127.0.0.1:6379> CONFIG get requirepass
1) "requirepass"
2) "123123"
# 密码设置完毕, 测试一下
[root@VM_0_3_centos ~]# redis-cli
127.0.0.1:6379> get userName
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123123
OK
127.0.0.1:6379> get userName
"admin"
127.0.0.1:6379> exit
[root@VM_0_3_centos ~]#
# 通过修改配置文件来修改密码, 同样的找到
[root@VM_0_3_centos ~]# vim /etc/redis.conf
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass 123123
# 这应该不言而喻了吧
5. Windows用户可以安装redis-desktop-manager,Linux用户想必大家也没必要用桌面工具,网盘地址
- 安装之后准备连接
- 如果连接失败,修改配置文件
# 打开redis配置文件, 找到 bind 127.0.0.1 将其注释, 所在位置上面点。 [root@VM_0_3_centos ~]# vim /etc/redis.conf # 注释bind 127.0.0.1或者改为0.0.0.0 # bind 127.0.0.1 # 找到 protected-mode yes 将其改为 no protected-mode no # 然后重启 [root@VM_0_3_centos ~]# systemctl restart redis.service # 最后还要设置防火墙规则, 开放端口, centos 7如果用的iptables就如下配置。 iptables -A INPUT -p tcp --dport 6379 -j ACCEPT # 保存防火墙规则 [root@VM_0_3_centos ~]# service iptables save # 用firewall防火墙的朋友 firewall-cmd --zone=public --add-port=6379/tcp --permanent # 生效配置 firewall-cmd --complete-reload
-
再次连接。连接成功