Radis--基础操作
目录
- 运行环境
- 启动和停止初始配置
- 启动
- 停止
- 安全配置
- Mac配置redis.conf
- 在线查看和修改配置
运行环境
centos7.9 + redis3.0.2
启动和停止
启动
Redis安装成功后,有以下几种启动方式:
- 简单启动
- 动态参数启动
- 配置文件启动
简单启动
就是使用redis-server以默认配置的方式启动。生产环境用的不多。
[root@cs ~]# redis-server
启动后,默认监听本机6379端口。
动态参数启动
[root@cs redis]# redis-server --port 6380
启动一个新的Redis实例,监听6380端口,跟之前的6379不冲突:
[root@cs redis]# ps -ef | grep redis-server root 8622 1 0 10:28 ? 00:00:00 redis-server *:6379 root 8637 8548 0 10:37 pts/1 00:00:00 redis-server *:6380 root 8641 8074 0 10:38 pts/0 00:00:00 grep --color=auto redis-server
配置文件启动
默认的,Redis的配置文件,在Redis的安装目录下,我们可以在启动时使用配置文件启动:
[root@cs redis]# pwd /opt/software/redis [root@cs redis]# ls |grep redis.conf redis.conf [root@cs redis]# redis-server redis.conf
如上示例,使用默认配置启动Redis服务。这里只做演示,工作中我们需要自己维护配置文件。
小结
启动后,可以通过下面命令来查看Redis服务:
ps -ef | grep redis netstat -antpl|grep redis redis-cli -h ip -p port ping
对于上述三种启动方式,建议:
- 生产环境建议选择配置文件启动
- 单机多实例配置文件可以用端口进行区分
客户端的返回值说明
当Redis服务启动后,就可以通过Redis客户端连接它了:
redis-cli -h 10.0.0.200 -p 6379 > ping PONG > set hello world OK > get hello "world" > exit # 退出客户端,或者CTRL + C也行
在与客户端的交互中,通常由以下几种形式的回复。
状态回复:
> ping
PONG
错误回复:
127.0.0.1:6379> set hello world OK 127.0.0.1:6379> get hello "world" 127.0.0.1:6379> hget hello field (error) WRONGTYPE Operation against a key holding the wrong kind of value
整数回复:
127.0.0.1:6379> incr hello
(integer) 1
字符串回复:
> get hello "world"
多行字符串回复:
> mget hello foo "world" "bar"
停止
可以通过客户端的命令来启动。
# 方式1,直接调用命令 [root@cs src]# redis-cli shutdown # 方式2,进入客户端,执行命令 [root@cs src]# redis-cli 127.0.0.1:6379> shutdown 4878:M 26 Dec 19:47:02.368 # User requested shutdown... 4878:M 26 Dec 19:47:02.368 * Saving the final RDB snapshot before exiting. 4878:M 26 Dec 19:47:02.370 * DB saved on disk 4878:M 26 Dec 19:47:02.370 # Redis is now ready to exit, bye bye...
初始配置
详细配置参考:https://www.cnblogs.com/Neeo/articles/13952586.html
通常,单机多实例环境下,都是通过端口号来区分各实例,所以我们需要自己维护配置文件,我们通常在Redis目录建立两个目录:
[root@cs redis]# mkdir -p /data/redis_data/6379 [root@cs redis]# vim /data/redis_data/6379/redis.conf
现在只需要这些初始配置即可:
daemonize yes port 6379 logfile "/data/redis_data/6379/redis.log" dir "/data/redis_data/6379" dbfilename dump.rdb
其中:
daemonize:是否后台运行。port:Redis对外端口号,默认是6379。在单机多实例中,必须配置。logfile:Redis系统日志。dir:Redis工作目录。dbfilename:RDB持久化数据文件。
现在再次使用配置文件启动:
[root@cs redis]# redis-server /data/redis_data/6379/redis.conf
安全配置
我们可以通过设置配置文件来决定谁能操作Redis:
bind 127.0.0.1 10.0.0.200
requirepass 1234
其中:
bind控制谁能访问,多个IP以空格分割。requirepass控制访问密码。
注意,Redis没有用户名的概念,而且谁都可以登录到Redis,但想操作Redis的话,就要通过Redis的认证了。
所以,要想通过认证,有两种方式,第一种,登录和认证一起:
[root@cs 6379]# redis-cli -h 127.0.0.1 -p 6379 -a 1234
第二种, 先登录,完事再认证:
[root@cs 6379]# redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> set hello world # 认证之前,没有操作权限 (error) NOAUTH Authentication required. 127.0.0.1:6379> auth 1234 # 认证 OK 127.0.0.1:6379> set hello world OK 127.0.0.1:6379> get hello "world"
Mac配置redis.conf
首先将redis的src目录添加到~/.bash_profile环境变量。
# 编辑 vim /etc/redis.conf bind 0.0.0.0 requirepass 1234 # 重启,然后以配置文件启动 redis-server /etc/redis.conf # 客户端登录进去 redis-cli 127.0.0.1:6379> auth 123
在线查看和修改配置
127.0.0.1:6379> CONFIG GET bind # 查看指定配置 1) "bind" 2) "127.0.0.1 10.0.0.200" 127.0.0.1:6379> CONFIG GET * # 查看所有的配置,Redis中大约有70个配置 127.0.0.1:6379> CONFIG GET b* # 支持模糊查询 127.0.0.1:6379> CONFIG SET requirepass 1234 # 在线修改配置
注意,这种修改是临时,当重启Redis服务时,配置将失效,如果要永久生效,还是要将配置写入到配置文件中。

浙公网安备 33010602011771号