简单总结下 Linux 环境下,编译安装 redis 的步骤。
1. 下载 redis 安装包并解压
切换到 root 用户,在 /usr/local/ 路径下(也可以选其他路径),下载 redis 安装包,解压文件并将文件夹重命名为 redis。
$ cd /usr/local/
$ pwd
/usr/local
$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
$ tar -xvf redis-4.0.8.tar.gz
$ mv redis-3.0.2 redis
2. 编译安装 redis
进入 redis 目录,编译 redis :
$ cd redis
$ make
出现如下提示语,说明编译通过:
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/usr/local/redis/src'
安装 redis:
$ make install
cd src && make install
make[1]: Entering directory `/usr/local/redis/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/usr/local/redis/src'
出现上述语句,说明安装成功。
3. 修改 redis.conf 配置
$ vi redis.conf
对 redis.conf 文件作如下修改,并保存:
# 把 bind 127.0.0.1 这一行注释掉,这里的 bind 指的是只有指定的网段才能远程访问这个redis,注释掉后,就没有这个限制了。
# bind 127.0.0.1
# protected-mode 值默认是yes,防止了远程访问,此处允许远程访问
protected-mode no
# redis 端口,默认是6379
port 6379
# daemonize 是否后台运行,no 不是,yes 是
daemonize yes
# 日志文件地址
logfile /var/log/redis.log
# 设置 redis 访问密码是123456
requirepass 123456
4. 启动 redis
redis-server /usr/local/redis/redis.conf
查看 redis 进程:
$ ps -ef|grep redis
root 24892 1 0 13:56 ? 00:00:06 redis-server *:6379
root 25205 25130 0 15:56 pts/3 00:00:00 grep --color=auto redis
查看 6379 端口是否监听:
$ netstat -lntp | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 24892/redis-server
tcp6 0 0 :::6379 :::* LISTEN 24892/redis-server
启动 redis 客户端(2种方式):
$ redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 12345
(error) ERR invalid password
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set testKey1 "hello"
OK
127.0.0.1:6379> get testKey1
"hello"
127.0.0.1:6379> del testKey1
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
或者 redis-cli -h [ip] -p [port] -a [password]
$ redis-cli -h localhost -p 6379 -a 123456
localhost:6379> keys *
1) "key"
2) "testKey1"
localhost:6379> del key
(integer) 1
localhost:6379> del testKey1
(integer) 1
localhost:6379> keys *
(empty list or set)
localhost:6379> exit
5. 查看 redis 安装路径
$ whereis redis
redis: /usr/local/redis
$ whereis redis-server
redis-server: /usr/local/bin/redis-server
$ whereis redis-cli
redis-cli: /usr/local/bin/redis-cli
$ find /usr/local/redis/ -name redis.conf
/usr/local/redis/redis.conf
6. 停止 redis 服务
- 可通过查找 redis 进程号后,执行 kill [PID] 或 kill -9 [PID] 命令来杀掉 redis 进程。
- 若 redis 未设置密码,可执行如下命令停止 redis 服务:
$ redis-cli shutdown
- 若 redis 设置了密码,可执行如下命令停止 redis 服务
$ redis-cli -a 123 shutdown
(error) NOAUTH Authentication required.
$ redis-cli -a 123456 shutdown
$ ps -ef|grep redis
root 25344 25264 0 16:36 pts/4 00:00:00 grep --color=auto redis