奋斗
努力

redis 服务+ MySQL 服务需要什么配置?

云计算

在部署 Redis 和 MySQL 服务时,需要根据业务需求、数据规模和高可用性要求进行合理配置。以下是关键配置建议:


一、Redis 服务配置

1. 基础配置

  • 内存管理
    • maxmemory:设置最大内存(如 4GB),避免 OOM。
    • maxmemory-policy:内存满时的淘汰策略(如 volatile-lru)。
  • 持久化(根据数据安全性选择):
    • RDBsave 900 1(15分钟至少1次修改触发快照)。
    • AOFappendonly yes + appendfsync everysec(平衡性能与安全)。
  • 网络
    • bind:绑定安全的内网 IP(如 10.0.0.1),避免暴露公网。
    • requirepass:设置密码认证。

2. 高可用(可选)

  • 主从复制
    • 主节点配置 replica-read-only no,从节点配置 replicaof <master-ip> <port>
  • 哨兵模式(Sentinel)
    • 配置 sentinel monitor mymaster <ip> <port> 2(2个哨兵确认故障转移)。
  • Redis Cluster
    • 分片部署,至少 3 主 3 从,配置 cluster-enabled yes

3. 性能优化

  • tcp-keepalive 300:防止连接断开。
  • client-output-buffer-limit:调整客户端缓冲区限制(尤其针对大量订阅场景)。

二、MySQL 服务配置

1. 基础配置

  • 存储引擎
    • 默认 InnoDB,支持事务和行锁。
  • 连接池
    • max_connections=200(根据并发调整)。
    • wait_timeout=300:闲置连接超时。
  • 缓存
    • innodb_buffer_pool_size:设为物理内存的 50%-70%(如 4GB)。
    • query_cache_size(仅读多场景启用)。

2. 高可用(可选)

  • 主从复制
    • 主节点配置 binlog_format=ROW,从节点配置 server-id=2 + read_only=1
  • 组复制(Group Replication)
    • 配置 group_replication_start_on_boot=ON
  • 集群方案
    • Galera Cluster 或 InnoDB Cluster。

3. 性能优化

  • innodb_flush_log_at_trx_commit=1(严格持久化)或 2(性能优先)。
  • sync_binlog=1(确保二进制日志安全)。
  • innodb_file_per_table=ON:表独立表空间。

三、联合部署建议

  1. 网络隔离
    • Redis 和 MySQL 部署在同一内网,降低延迟。
    • 使用防火墙规则限制访问(如仅允许应用服务器 IP)。
  2. 连接池管理
    • 应用层使用连接池(如 HikariCP for MySQL,JedisPool for Redis)。
  3. 数据一致性
    • 考虑使用延时双删消息队列同步缓存与数据库。
  4. 监控
    • Redis:INFO 命令或 Redis Exporter + Prometheus。
    • MySQL:SHOW STATUSPercona Monitoring and Management

四、硬件建议

  • Redis:优先大内存、低延迟 SSD(尤其 AOF 场景)。
  • MySQL:高速磁盘(NVMe SSD)、充足 CPU 核心(多线程查询)。

五、安全配置

  • Redis
    • 禁用高危命令:rename-command FLUSHDB ""
    • 启用 TLS(若需公网传输)。
  • MySQL
    • 限制 root 远程登录,创建最小权限用户。
    • 启用 SSL 连接。

六、配置文件示例

Redis (redis.conf)

bind 10.0.0.1
requirepass yourpassword
maxmemory 4gb
maxmemory-policy volatile-lru
appendonly yes
appendfsync everysec

MySQL (my.cnf)

[mysqld]
server-id=1
binlog_format=ROW
innodb_buffer_pool_size=4G
innodb_flush_log_at_trx_commit=1
sync_binlog=1

根据实际业务压力测试调整参数,并定期备份数据(Redis 的 RDB/AOF,MySQL 的 mysqldump 或 XtraBackup)。

未经允许不得转载:云服务器 » redis 服务+ MySQL 服务需要什么配置?