# linux下redis的使用

### redis服务启动和关闭

redis服务启动：redis-server /myredis/redis.conf –>指定redis配置文件路径； –>在/usr/local/bin路径下有redis-server命令；否则，就只能去redis安装路径的src路径下执行命令；可以手动将src下的redis-server命令复制到/usr/local/bin路径下，并使用“chmod u+x redis-\*”命令修改权限；

* redis服务关闭：redis-cli -p 6379 shutdown
* 远程连接redis：redis-cli -h host -p port -a password
* 查看redis线程：ps aux | grep redis
* 查看主从关系：info replication
* 设置成为从节点：slaveof 127.0.0.1 6379
* 取消从节点身份：slaveof no one
* 查看当前的redis连接数：info clients
* 查看 redis允许的最大连接数：config get maxclients

### redis基本设置：

* daemonize 如果需要将Redis服务以守护进程在后台运行，则把该项的值改为yes
* pidfile 配置多个pid的地址，默认在/var/run/redis/pid
* bind 绑定ip，设置后只接受来自该ip的请求
* port 监听端口，默认是6379
* timeout 客户端连接超时的设定，单位是秒
* loglevel 分为4级，debug、verbose、notice、warning
* logfile 配置log文件地址
* databases 设置数据库的个数，默认使用的数据库为0
* save 设置redis进行数据库镜像的频率
* rdbcompression 在进行镜像备份时，是否进行压缩
* Dbfilename 镜像备份文件的文件名
* Dir 数据库镜像备份文件的存放路径
* Slaveof 设置数据库为其他数据库的从数据库
* Masterauth 主数据库连接需要的密码验证
* Requirepass 设置登录时，需要使用的密码
* Maxclients 设置同时连接的最大客户端数量
* Maxmemory 设置redis能够使用的最大内存
* Appendonly 开启append only模式
* Appendfsync 设置对appendonly.aof文件同步的频率

### 1.增加单个键值

```bash
set key value
```

### 2.增加多个键值

```bash
mset key1 value1 key2 value2
```

### 3.查询单个键值

```bash
get key
```

### 4.查询多个键值

```bash
mget key1 key2 key3
```

### 5.删除单或多键值

```bash
del key1
del key1 key2 key3
```

### 6.增加数据到原有的键值对中

```bash
append key1 hahaha  ---> 例如: 向键为 key1 中追加值 hahaha 
```

### 7.查询所有键

```bash
keys *
```

### 8.查看名称中包含某个字符的键

```bash
keys 'a*'      --->查看名称中包含a的键
```

### 9.判断键值是否存在

```bash
exists key1   ---> 存在返回1，不存在返回0
```

### 10.查看键对应的 value 类型

```bash
type key
```

### 11.设置过期时间

```bash
expire key seconds
```

### 12.查看有效时间

```bash
ttl key
```

### 13.查看当前库中key的数量

```bash
dbsize
```

### 14.查看所有库中key的数量

```bash
info keyspace
```

### 15.删除当前数据库中的所有Key

```bash
flushdb
```

### 16.删除所有数据库中的key

```bash
key：flushall
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/linux-ji-cao/linux-xia-redis-de-shi-yong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
