redis 从入门到实战(1)-redis 安装
1.redis 是什么?
REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统。
Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。
它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。
2. 安装 redis
本次安装的 redis 为稳定版:redis-5.0.5。想要了解当前最新稳定版本为多少,访问 redis 官网。更多其他历史版本请访问 redis 历史版本
2.1 按照步骤如下
1. 下载安装包: $ wget http://download.redis.io/releases/redis-5.0.5.tar.gz 或者 curl -O http://download.redis.io/releases/redis-5.0.5.tar.gz 2. 解压压缩包: $ tar xzf redis-5.0.5.tar.gz 3. 进入redis-5.0.5的目录 $ cd redis-5.0.5 4. 开始构建: $ make
经过以上步骤,生成的 redis 程序放在 src 目录。下面我们开始运行 redis:
# src/redis-server
12961:C 20 Oct 2019 21:11:24.643 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12961:C 20 Oct 2019 21:11:24.643 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12961, just started
12961:C 20 Oct 2019 21:11:24.643 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 12961
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
12961:M 20 Oct 2019 21:11:24.646 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
12961:M 20 Oct 2019 21:11:24.646 # Server initialized
12961:M 20 Oct 2019 21:11:24.646 * Ready to accept connections
上面是我运行的结果。
显然程序是放在前台执行,我们如果想要连接上 redis 的话,那就要再打开一个终端窗口,然后执行以下命令:
# src/redis-cli 127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> get foo "bar" 127.0.0.1:6379>
现在,我们的安装就已经完成。
3. 让 redis 在后台运行
虽然我们已经安装好了 redis,但是只能在前台运行,一旦关闭了终端窗口,redis 也就停止运行了。那么,我们应该怎么让 redis 在后台运行呢?
3.1 把 redis 添加到环境变量 PATH 中
为了能够在任意目录都能启动 redis 程序,我们把 redis 程序安装到 PATH 环境变量指向的目录。操作如下:
make install
which redis-server
可以看到 redis-server 安装到了 /usr/local/bin/redis-server
。再看一下,可以发现 redis 有以下程序:
$ ll /usr/local/bin/redis-* -rwxr-xr-x. 1 root root 4366600 Oct 20 21:17 /usr/local/bin/redis-benchmark -rwxr-xr-x. 1 root root 8111808 Oct~~~~ 20 21:17 /usr/local/bin/redis-check-aof -rwxr-xr-x. 1 root root 8111808 Oct 20 21:17 /usr/local/bin/redis-check-rdb -rwxr-xr-x. 1 root root 4806832 Oct 20 21:17 /usr/local/bin/redis-cli lrwxrwxrwx. 1 root root 12 Oct 20 21:17 /usr/local/bin/redis-sentinel -> redis-server -rwxr-xr-x. 1 root root 8111808 Oct 20 21:17 /usr/local/bin/redis-server
3.2 准备配置文件:redis.conf
如果你留心第 2 节运行 redis-server 时的输出信息,你会发现这么一句话:
Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
那么我们为 redis 准备一个配置文件 redis.conf,如下:
# 只有本机能访问: bind 127.0.0.1 # yes表示以后台模式启动redis: daemonize yes # 持久化数据时,开启appendonly模式: appendonly yes
准备好配置文件后,下面我们启动 redis:
# redis-server ./redis.conf 1850:C 20 Oct 2019 21:34:27.334 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1850:C 20 Oct 2019 21:34:27.334 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1850, just started 1850:C 20 Oct 2019 21:34:27.334 # Configuration loaded
可以看到,redis 已经是在后台启动了。下面我们再次连接 redis 看下是否正常:
# 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"
可以看到已经正常。
注意,在下载的安装包中,已经有一个 redis.conf 的官方配置文件,每一项参数都有具体的说明。感兴趣的读者可以先去看看,或者跟着我的脚步,后面一个一个再了解。
3.3 关闭 redis
redis 后台服务器的关闭命令:redis-cli shutdown
4. 一些参考命令
# redis-server --help Usage: ./redis-server [/path/to/redis.conf] [options] ./redis-server - (read config from stdin) ./redis-server -v or --version ./redis-server -h or --help ./redis-server --test-memory <megabytes> Examples: ./redis-server (run the server with default conf) ./redis-server /etc/redis/6379.conf ./redis-server --port 7777 ./redis-server --port 7777 --replicaof 127.0.0.1 8888 ./redis-server /etc/myredis.conf --loglevel verbose Sentinel mode: ./redis-server /etc/sentinel.conf --sentinel # redis-cli --help Examples: cat /etc/passwd | redis-cli -x set mypasswd redis-cli get mypasswd redis-cli -r 100 lpush mylist x redis-cli -r 100 -i 1 info | grep used_memory_human: redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3 redis-cli --scan --pattern '*:12345*' $ redis-cli -h host -p port -a password 有时候会有中文乱码,要在 redis-cli 后面加上 --raw 如: redis-cli --raw
5. 一个安装 redis 脚本:
#!/bin/bash cd /usr/local curl -O http://download.redis.io/releases/redis-5.0.7.tar.gz tar xzf redis-5.0.7.tar.gz cd redis-5.0.7 make -j `cat /proc/cpuinfo | grep processor | wc -l` >/dev/null 2>&1 if [ $? -ne 0 ]; then make MALLOC=libc make -j `cat /proc/cpuinfo | grep processor | wc -l` >/dev/null 2>&1 fi #查找redis可执行文件 #find / -type f \( -perm -04000 -o -perm -02000 \) -exec ls -lg {} \;` find src -type f -perm 0755 -exec cp {} /usr/bin \;
6. 下一节是?
下一节我们讨论下如何使用 redis。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于