Linux下编译安装Redis以及主从复制配置
Redis的安装配置很简单,而且很早之前就装过Redis,可这几天再次安装时居然又遗忘了一些细节,看来好记性不如烂笔头,还是在博客记录一下比较好,至少不用总是抱度娘大腿了。
今天编译安装了几次,发现居然没在prefix指定目录生成文件??看了半天结果发现PREFIX我用了小写字母。。。
看来还是得记录一次正确的操作步骤,免得再次出现这种窘迫。
一、选择版本
前往官方网站:http://www.redis.io/download
选择一个适合的稳定版本,比如最新的redis-3.0稳定版(stable),获得下载地址:
http://download.redis.io/releases/redis-3.0.0.tar.gz
二、编译安装
cd /usr/local/src
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
tar zxvf redis-3.0.0.tar.gz
cd redis-3.0.0
make
#可选执行命令:make test
#这里记得PREFIX是大写,小写不生效!
make PREFIX=/usr/local/redis-3.0.0 install
#建立软链接
ln -s /usr/local/redis-3.0.0 /usr/local/redis
#创建目录并拷贝默认配置文件过去
mkdir -p /usr/local/redis/{etc,var}
cp redis.conf /usr/local/etc/
#如果是对其他机器提供服务,推荐在redis.conf中设置监听IP为0.0.0.0,启动就可以开一个进程,默认是2个,还有个127.0.0.1
bind 0.0.0.0
cd /usr/local/src
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
tar zxvf redis-3.0.0.tar.gz
cd redis-3.0.0
make
#可选执行命令:make test
#这里记得PREFIX是大写,小写不生效!
make PREFIX=/usr/local/redis-3.0.0 install
#建立软链接
ln -s /usr/local/redis-3.0.0 /usr/local/redis
#创建目录并拷贝默认配置文件过去
mkdir -p /usr/local/redis/{etc,var}
cp redis.conf /usr/local/etc/
#如果是对其他机器提供服务,推荐在redis.conf中设置监听IP为0.0.0.0,启动就可以开一个进程,默认是2个,还有个127.0.0.1
bind 0.0.0.0
安装完成后,redis目录结构如下:
[root@cache-ns-4 ~]# tree /usr/local/redis
/usr/local/redis
├── bin #bin下面存放各种执行文件
│ ├── redis-benchmark
│ ├── redis-check-aof
│ ├── redis-check-dump
│ ├── redis-cli #redis客户端执行文件
│ ├── redis-sentinel -> /usr/local/redis-3.0.0/bin/redis-server
│ └── redis-server #redis服务端执行文件
├── dump.rdb #启动后默认生成的数据文件,可以在redis.conf中设置dir的路径参数指定到其他目录
├── etc
│ └── redis.conf
└── var
[root@cache-ns-4 ~]# tree /usr/local/redis
/usr/local/redis
├── bin #bin下面存放各种执行文件
│ ├── redis-benchmark
│ ├── redis-check-aof
│ ├── redis-check-dump
│ ├── redis-cli #redis客户端执行文件
│ ├── redis-sentinel -> /usr/local/redis-3.0.0/bin/redis-server
│ └── redis-server #redis服务端执行文件
├── dump.rdb #启动后默认生成的数据文件,可以在redis.conf中设置dir的路径参数指定到其他目录
├── etc
│ └── redis.conf
└── var
三、注册服务
①、编写服务控制脚本
vi /etc/init.d/redis
#!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 80 12
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /usr/local/redis/etc/redis.conf
# pidfile: /usr/local/redis/var/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
#使用中偶尔发现服务器启动后居然不转入后台,所以在最后加了一个“&”
daemon $BIN/$prog $CONFIG &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
#!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 80 12
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /usr/local/redis/etc/redis.conf
# pidfile: /usr/local/redis/var/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
#使用中偶尔发现服务器启动后居然不转入后台,所以在最后加了一个“&”
daemon $BIN/$prog $CONFIG &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
②、注册服务与启动
#给执行权限
chmod +x /etc/init.d/redis
#开机启动
chkconfig redis on
#启动服务
service redis start
#下面是启动记录
[root@localhost ~]# service redis start
Starting Redis Server:
[root@localhost ~]# _._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.0 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 28584
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[28584] 16 Apr 23:48:55.614 # Server started, Redis version 3.0.0
[28584] 16 Apr 23:48:55.614 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[28584] 16 Apr 23:48:55.614 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
[28584] 16 Apr 23:48:55.614 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[28584] 16 Apr 23:48:55.615 * DB loaded from disk: 0.000 seconds
[28584] 16 Apr 23:48:55.615 * The server is now ready to accept connections on port 6379
#给执行权限
chmod +x /etc/init.d/redis
#开机启动
chkconfig redis on
#启动服务
service redis start
#下面是启动记录
[root@localhost ~]# service redis start
Starting Redis Server:
[root@localhost ~]# _._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.0 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 28584
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[28584] 16 Apr 23:48:55.614 # Server started, Redis version 3.0.0
[28584] 16 Apr 23:48:55.614 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[28584] 16 Apr 23:48:55.614 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
[28584] 16 Apr 23:48:55.614 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[28584] 16 Apr 23:48:55.615 * DB loaded from disk: 0.000 seconds
[28584] 16 Apr 23:48:55.615 * The server is now ready to accept connections on port 6379
四、主从配置
①、配置
在从机上按照上面的步骤安装redis,然后在从机的redis.conf里面新增如下配置项目:
#指定redis主机的IP地址和端口(默认未设置密码认证)
slaveof 192.168.10.124 6379
#指定redis主机的IP地址和端口(默认未设置密码认证)
slaveof 192.168.10.124 6379
保存后启动redis即可完成简单的主从配置。
②、测试
测试很简单,先在主机上通过客户端 redis-cli 执行新增键值命令:
[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> set newkey slavetest
OK
[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> set newkey slavetest
OK
然后,登录从机上同样执行 redis-cli 执行查询命令:
[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> get newkey
"slavetest"
[root@localhost ~]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> get newkey
"slavetest"
很明显主机上新增的键值已经自动同步到了从机,主从同步成功!
本文只是记录一下基本的编译安装和主从配置,当然,redis还有其可以继续进行自定义设置或优化的项目,后续有机会再继续整理补充一下。
Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里