1. redis-server
redis-server
脚本用来启动redis服务,可以使用-h
参数来查看其用法:
$ 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 --slaveof 127.0.0.1 8888
./redis-server /etc/myredis.conf --loglevel verbose
Sentinel mode:
./redis-server /etc/sentinel.conf --sentinel
1.1. 直接启动
1.1.1. 启动
#加上`&`号使redis以后台程序方式运行
$ ./redis-server &
redis-server
使用默认配置文件进行启动
1.1.2. 检测
#检测后台进程是否存在
$ ps -ef | grep redis
0 37777 1 0 3:18下午 ?? 0:00.01 redis-server 127.0.0.1:6379
501 37783 36827 0 3:19下午 ttys003 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn redis
#检测6379端口是否在监听
netstat -lntp | grep 6379
#使用`redis-cli`客户端检测连接是否正常
./redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set key "hello world"
OK
127.0.0.1:6379> get key
"hello world"
1.1.3. 停止
可以使用redis-cli shutdown
命令来停止redis服务,或者直接使用kill -9 pid
杀掉进程;
#使用客户端
redis-cli shutdown
#因为Redis可以妥善处理SIGTERM信号,所以直接kill -9也是可以的
kill -9 PID
1.2. 指定配置文件启动
1.2.1. 配置文件
可为redis服务启动指定配置文件,配置文件redis.conf在Redis根目录下。
# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
#
# ./redis-server /path/to/redis.conf
# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.
1.2.2. 启动时指定配置文件
Usage: ./redis-server [/path/to/redis.conf] [options]
$ sudo redis-server ../etc/redis.conf
Password:
$ ps -ef | grep redis
0 37777 1 0 3:18下午 ?? 0:00.01 redis-server 127.0.0.1:6379
501 37783 36827 0 3:19下午 ttys003 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn redis
配置文件是非常重要的配置工具,随着使用的逐渐深入将显得尤为重要,推荐在一开始就使用配置文件。
1.3. 使用Redis启动脚本设置开机自启动
1.3.1. 启动脚本
推荐在生产环境中使用启动脚本方式启动redis服务。启动脚本redis_init_script
位于位于Redis的/utils/目录下。
#redis服务器监听的端口
REDISPORT=6379
#服务端所处位置,在make install后默认存放与`/usr/local/bin/redis-server`,如果未make install则需要修改该路径,下同
EXEC=/usr/local/bin/redis-server
#客户端位置
CLIEXEC=/usr/local/bin/redis-cli
#Redis的PID文件位置
PIDFILE=/var/run/redis_${REDISPORT}.pid
#配置文件位置,需要修改
CONF="/etc/redis/${REDISPORT}.conf"
大致浏览下该启动脚本,发现redis习惯性用监听的端口名作为配置文件等命名,我们后面也遵循这个约定。
1.3.2. 配置环境
- 根据启动脚本要求,将修改好的配置文件以端口为名复制一份到指定目录。需使用root用户。
mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf
- 将启动脚本复制到/etc/init.d目录下,本例将启动脚本命名为redisd(通常都以d结尾表示是后台自启动服务)。
cp redis_init_script /etc/init.d/redisd
- 设置为开机自启动
此处直接配置开启自启动chkconfig redisd on将报错误:service redisd does not support chkconfig 参照此篇文章,在启动脚本开头添加如下两行注释以修改其运行级别:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
#
再设置即可成功。
#设置为开机自启动服务器
chkconfig redisd on
#打开服务
service redisd start
#关闭服务
service redisd stop