redis安装-启动
安装:
从官网(http://download.redis.io/)下载redis 包 redis-stable.tar.gz,
解压安装:
tar -xzvf redis-stable.tar.gz
cd redis-stable
make
编译之后:
执行程序在redis-stable/src下:
redis-server:redis服务器启动程序
redis-cli :redis 命令行客户端
( 可以将这两个程序copy 出来,这两个程序对源文件redis-stable没有依赖。)
默认配置文件redis-stable/src下:
redis.conf 可以配置 redis 启动端口 数据库文件存储路径等
redis启动:
redis-server //(直接启动 默认端口6379 数据库文件在当前路径下dump.rab)
redis-server --port 6838 //(指定端口启动 数据库文件在当前路径下dump.rab)
redis-server /路径/redis.conf //指定配置文件方式启动
redis停止:
方式一: redis-cli shutdown //连接 127.0.0.1 端口为6379的redis server ,并发送shutdown请求
方式二: redis-cli -h ip -p port 通过ip和port 连接redis server ,连接成功后,在提示符下输出 shutdown
redis server 接收到停止信息,先断掉连接,再持久化数据到数据库文件,最后退出
配置redis:
配置方式:
方式一:redis-cli 连接到redis-server后,在命令提示符下执行 config set field value ,不必重启redis-server
方式二:启动redis-server时指定配置文件 如:redis-server /redis_home/etc/redis.conf
redis.conf 基本配置项:
daemonize 默认值 no 是否为守护进程
pidfile 默认值 /var/run/redis.pid pid文件路径
port 默认值 6379 redis-server 端口号
databases 默认值 16 redis 数据库个数
dbfilename 默认值 dump.rdb 持久化文件名
dir 默认值 ./ 持久化文件路径
配置redis目录结构:
/app/redis> ls -lR
drwxr-xr-x 2 13:58 bin
drwxr-xr-x 2 13:47 data
drwxr-xr-x 2 09:31 etc
drwxr-xr-x 2 09:31 logs
drwxr-xr-x 2 09:31 pid
./bin://redis 可执行程序及启动脚本
-rwxr-xr-x 1 15:56 redis-6379 //启动脚本
-rwxr-xr-x 1 13:52 redis-cli //redis命令行客户端
-rwxr-xr-x 1 13:52 redis-server//redis 服务器
./data:
-rw-r--r-- 1 13:47 dump_6379.rdb //redis 持久化文件
./etc:
-rw-r--r-- 1 redis_6379.conf //redis 配置文件
-rw------- 1 redis.conf //redis包中自带的redis配置文件
./pid:
-rw-r--r-- 1 vgop vgop 6 2014-12-02 09:31 redis_6379.pid //redis pidfile
./logs:
-rw-r--r-- 1 vgop vgop 2072 2014-12-11 14:54 redis_6379.log //日志文件
redis-6379脚本:
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDIS_HOME=/app/redis
REDISPORT=6379
EXEC=${REDIS_HOME}/bin/redis-server
CLIEXEC=${REDIS_HOME}/bin/redis-cli
PIDFILE=${REDIS_HOME}/pid/redis_${REDISPORT}.pid
CONF=${REDIS_HOME}/etc/redis_${REDISPORT}.conf
LOGFILE=${REDIS_HOME}/logs/redis_${REDISPORT}.log
DBFILE=${REDIS_HOME}/data/dump_${REDISPORT}.rdb
echo "pid file :${PIDFILE}"
echo "conf file:${CONF}"
echo "log file:${LOGFILE}"
echo "rdb file:${DBFILE}"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
client)
echo "connect to reids server :host=localhost ,port=$REDISPORT"
$CLIEXEC -h localhost -p $REDISPORT
;;
*)
echo "Please use start or stop as first argument"
;;
esac
启动方式:redis-6379 start
停止方式:redis-6379 stop
redis客户端连接服务端:redis-6379 client
配置文件redis_6379.conf中:
#引入默认配置
include /app/redis/etc/redis.conf
#以下覆盖默认配置
daemonize yes
port 6379
dir /app/redis/data/
dbfilename dump_6379.rdb
pidfile /app/redis/pid/redis_6379.pid
logfile /app/redis/logs/redis_6379.log
注意:配置文件要与redis-6379中pid、logfile、dbfile路径相对应
1.启动 redis服务器
Usage: ./redis-server
./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
2.redis命令行客户端
Usage: redis-cli ]]
-h <hostname> Server hostname (default: 127.0.0.1).
-p <port> Server port (default: 6379).
-s <socket> Server socket (overrides hostname and port).
-a <password> Password to use when connecting to the server.
-r <repeat> Execute specified command N times.
-i <interval> When -r is used, waits <interval> seconds per command.
It is possible to specify sub-second times like -i 0.1.
-n <db> Database number.
-x Read last argument from STDIN.
-d <delimiter> Multi-bulk delimiter in for raw formatting (default: \n).
-c Enable cluster mode (follow -ASK and -MOVED redirections).
--raw Use raw formatting for replies (default when STDOUT is
not a tty).
--no-raw Force formatted output even when STDOUT is not a tty.
--csv Output in CSV format.
--latency Enter a special mode continuously sampling latency.
--latency-historyLike --latency but tracking latency changes over time.
Default time interval is 15 sec. Change it using -i.
--slave Simulate a slave showing commands received from the master.
--rdb <filename> Transfer an RDB dump from remote server to local file.
--pipe Transfer raw Redis protocol from stdin to server.
--pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
no reply is received within <n> seconds.
Default timeout: 30. Use 0 to wait forever.
--bigkeys Sample Redis keys looking for big keys.
--scan List all keys using the SCAN command.
--pattern <pat> Useful with --scan to specify a SCAN pattern.
--intrinsic-latency <sec> Run a test to measure intrinsic system latency.
The test will run for the specified amount of seconds.
--eval <file> Send an EVAL command using the Lua script at <file>.
--help Output this help and exit.
--version Output version and exit.
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*'
(Note: when using --eval the comma separates KEYS[] from ARGV[] items)
When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands.
页:
[1]