Redis基础目录
mkdir -p /usr/local/redis
mkdir -p /usr/local/redis/data
mkdir -p /usr/local/redis/conf
mkdir -p /usr/local/redis/logs
Redis 下载源码
wget https:
安装make
apt-get install -y make gcc pkg-config
make
make install
主节点配置
cat > /usr/local/redis/conf/redis.conf << EOF
port 6379
daemonize yes
pidfile /usr/local/redis/redis.pid
dir /usr/local/redis/data
logfile "/usr/local/redis/logs/redis.log"
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
save 900 1
save 300 10
save 60 10000
dbfilename dump.rdb
requirepass YourStrongPassword
masterauth YourStrongPassword
timeout 300
tcp-keepalive 300
maxclients 10000
maxmemory 2gb
maxmemory-policy allkeys-lru
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
bind 0.0.0.0
protected-mode yes
EOF
从节点配置文件
cat > /usr/local/redis/conf/redis.conf << EOF
port 6379
daemonize yes
pidfile /usr/local/redis/redis.pid
dir /usr/local/redis/data
logfile "/usr/local/redis/logs/redis.log"
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
save 900 1
save 300 10
save 60 10000
dbfilename dump.rdb
replicaof 192.168.1.100 6379
requirepass YourStrongPassword
masterauth YourStrongPassword
timeout 300
tcp-keepalive 300
maxclients 10000
maxmemory 2gb
maxmemory-policy allkeys-lru
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
bind 0.0.0.0
protected-mode yes
EOF
系统服务
cat > /etc/systemd/system/redis.service << EOF
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/redis-server /usr/local/redis/conf/redis.conf
ExecStop=/usr/local/redis/redis-cli -h 127.0.0.1 -p 6379 -a YourStrongPassword shutdown
Restart=always
User=redis
Group=redis
[Install]
WantedBy=multi-user.target
EOF
创建用户组
useradd -r -s /bin/false redis
chown -R redis:redis /usr/local/redis
设置权限
chown -R root:root /usr/local/redis
chmod -R 755 /usr/local/redis
开机启动
systemctl daemon-reload
systemctl start redis
systemctl enable redis
systemctl status redis
测试主从复制
redis-cli -h 192.168.1.100 -p 6379 -a YourStrongPassword set test "Hello World"
从节点读取配置
redis-cli -h 192.168.1.101 -p 6379 -a YourStrongPassword get test