CentOS安装redis
1.安装第一个redis实例
#查看已安装的redis版本
/usr/local/redis/bin/redis-server -v
#yum查看redis各版本
yum search --showduplicates redis
1. yum install -y gcc
2.该机器如果创建2个redis实例(redis、redis1),则先指定每个实例的log位置,并赋予写权限
mkdir /data1/redis/log -p
touch /data1/redis/log/redis.log
chmod 766 /data1/redis/log/redis.log
mkdir /data1/redis1/log -p
touch /data1/redis1/log/redis1.log
chmod 766 /data1/redis1/log/redis1.log
3.下载redis并解压
mkdir /data1/software
wget -P /data1/software http://download.redis.io/releases/redis-3.0.7.tar.gz
cd /data1/software
tar -zxvf redis-3.0.7.tar.gz
cd /data1/software/redis-3.0.7
#编译,然后安装到/usr/local目录下
make
make install PREFIX=/usr/local/redis
4.创建软连接
ln -s /usr/local/redis/bin/redis-server /usr/bin/redis-server
5.修改redis每个实例的配置文件/usr/local/bin/redis.conf
daemonize yes pidfile /var/run/redis.pid #多实例要修改不同名称,最好是放到/data1/redis目录下,这样方便赋权 bind 0.0.0.0 port 6379 unixsocket /tmp/redis.sock #多实例要修改不同名称,最好放到/data1/redis目录下,方便赋权 timeout 0 tcp-keepalive 0 databases 16 loglevel warning logfile /data1/redis/log/redis.log #多实例要修改不同名称 dbfilename dump.rdb dir /data1/redis/data #多实例要修改不同名称
#redis cluster启用 cluster-enabled yes cluster-config-file /usr/local/redis/cluster/nodes.conf cluster-node-timeout 15000 cluster-migration-barrier 1 cluster-require-full-coverage yes appendonly no save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes maxmemory 6096mb maxclients 30000 appendfsync everysec no-appendfsync-on-rewrite yes auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 1024 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes
6.新建redis开机自启动脚本/etc/init.d/redis,注意修改配置文件位置及配置文件端口
chmod a+x /etc/init.d/redis
chkconfig redis on
/etc/init.d/redis内容如下,注意修改实例服务及配置文件路径
#!/bin/bash #chkconfig: 2345 55 25 #description: Starts,stops and restart the redis-server #usage: ./script_name {start|stop|status|restart} # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check networking is up. [ "$NETWORKING" = "no" ] && exit 0 RETVAL=0 REDIS="/usr/local/redis/bin/redis-server" PROG=$(basename $REDIS) CONF="/usr/local/redis/bin/redis.conf" if [ "$1" = "-f" ];then CONF=$2 fi if [ ! -f $CONF ]; then echo -n $"$CONF not exist.";warning;echo exit 1 fi REDIS_PORT=$(/bin/grep port ${CONF} | /bin/cut -d ' ' -f2) PID_FILE=$(/bin/grep "pidfile" ${CONF}|cut -d ' ' -f2) PID_FILE=${PID_FILE:=/var/run/redis.pid} LOCKFILE="/var/lock/subsys/redis-${REDIS_PORT}" if [ ! -x $REDIS ]; then echo -n $"$REDIS not exist.";warning;echo exit 0 fi start() { echo never > /sys/kernel/mm/transparent_hugepage/enabled echo -n $"Starting $PROG: " # $REDIS $CONF # su nobody -s "/bin/sh" -c "$REDIS $CONF" daemon --user nobody "$REDIS $CONF" RETVAL=$? if [ $RETVAL -eq 0 ]; then success;echo;touch $LOCKFILE else failure;echo fi return $RETVAL } stop() { echo -n $"Stopping $PROG: " if [ -f $PID_FILE ] ;then read PID < "$PID_FILE" else failure;echo; echo -n $"$PID_FILE not found.";failure;echo return 1; fi if checkpid $PID; then kill -TERM $PID >/dev/null 2>&1 RETVAL=$? if [ $RETVAL -eq 0 ] ;then success;echo echo -n "Waiting for Redis to shutdown .." while checkpid $PID;do echo -n "." sleep 1; done success;echo;rm -f $LOCKFILE else failure;echo fi else echo -n $"Redis is dead and $PID_FILE exists.";failure;echo RETVAL=7 fi return $RETVAL } restart() { stop start } case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status -p ${PID_FILE} $PROG RETVAL=$? ;; *) #echo $"Usage: $0 {start|stop|status|restart}" echo $"Usage: $0 -f [redis configfile] {start|stop|status|restart}" RETVAL=1 esac exit $RETVAL
其他实例安装,则只需要将 /usr/local/redis 复制即可,并新建开机自启动脚本,还有修改配置文件必要参数
redis cluster配置:
参考:http://www.linuxe.cn/post-375.html
按上面修改redis.conf,启用cluster,然后再 chmod a+w /usr/local/redis/cluster/nodes.conf #增加写权限,否则service redis start会失败,如果不用cluster功能,则无需cluster/nodes.conf
默认redis以nobody权限运行,所以要对 redis.log和db.rdb文件都赋予写权限 chmod -R a+w /data1/redis
sysctl vm.overcommit_memory=1 #这个也启用
redis5.0版本之前,需要安装ruby依赖才能使用redis-trib.rb命令来创建集群,Redis 5开始可以使用redis-cli --cluster来创建集群,命令语法和redis-trib.rb脚本一样,不再需要ruby
yum install ruby rubygems #安装ruby和ruby软件仓库 yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel gem source -l #安装完成后可以查看当前ruby源 gem sources --remove https://rubygems.org/ #去掉官方源 gem sources --remove http://rubygems.org/ #去掉官方源 gem sources -a https://gems.ruby-china.com/ gem install redis -v 3.3.5
以上只需要在一台机器上安装即可,完成后运行如下命令将redis实例加入群集:
redis-trib.rb create --replicas 1 192.168.1.101:6379 192.168.1.102:6379 192.168.1.103:6379 192.168.1.104:6379 192.168.1.105:6379 192.168.1.106:6379 #create 创建集群 #replicas 代表每个主节点有几个从节点 #后面跟上的IP和端口是所有master和slave的节点信息
查看cluster状态:
./redis-cli -p 6380 cluster nodes #可使用任一端口
安装完成后,测试如下:
redis-cli -c -h 10.10.201.72 -p 6379
然后可以连接到另外一台机器 get name,可以取到数值则表示成功
Redis cluster常用命令:https://www.cnblogs.com/kevingrace/p/7910692.html