MaxScale 无从属状态集

MaxScale no Slave State set

我们想使用 MaxScale 和两个带有 docker-compose 的 MariaDB 数据库。

我们遇到了无法通过 maxscale 实现数据库复制的问题。 写入权限可通过 MaxScale 在两个数据库上获得。通过 maxscale 容器中的命令 maxscale list servers,我们可以看到两台服务器。第一台服务器的状态为 Master, Running,第二台服务器的状态仅为 Running.

我的docker-compose.yaml:

version: '3'
services:

  # Application
  app:
    build:
      context: .
      dockerfile: app.dockerfile
    working_dir: /var/www/project
    volumes:
      - ./project:/var/www/project
      - ./php.ini:/usr/local/etc/php/php.ini
    links:
      - database:database
    environment:
      - "DATABASE_HOST=database"
      - "DATABASE_PORT=4006"

  # Web server
  web:
    image: nginx:latest
    volumes:
      - ./vhost.conf:/etc/nginx/conf.d/default.conf
      - ./nginx-logs:/var/log/nginx
      # Inherit from app container
      - ./project:/var/www/project
      - ./php.ini:/usr/local/etc/php/php.ini
    ports:
      - 0.0.0.0:8021:80
    links:
      - app:app

  # Database
  database:
    image: mariadb:latest
    ports:
      - 0.0.0.0:3306:3306
    volumes:
      - ./database:/var/lib/mysql
      - ./database-config:/etc/mysql/
    command: mysqld --log-bin=mariadb-bin --binlog-format=ROW --server-id=3001 --log-slave-updates
    environment:
      - "MYSQL_ROOT_PASSWORD=secretDummyPassword"
      - "MYSQL_DATABASE=database"
      - "MYSQL_USER=database"
      - "MYSQL_PASSWORD=secretDummyPassword"
      - "skip-networking=0"

  #Max Scale
  maxscale:
    image: mariadb/maxscale:6.2.3
    depends_on:
      - database
    volumes:
      - ./maxscale.cnf:/etc/maxscale.cnf
    ports:
      - 0.0.0.0:4006:4006 # readwrite port
      - 0.0.0.0:4008:4008 # readonly port
      - 0.0.0.0:8989:8989 # REST API port
    links:
      - database:database

volumes:
  app: {}

我的maxscale.cnf:

[maxscale]
threads=auto

[MariaDB-Monitor]
type=monitor
module=mariadbmon
servers=server1,server2
user=database
password=secretDummyPassword
auto_failover=true
auto_rejoin=true
enforce_read_only_slaves=1

[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2
user=database
password=secretDummyPassword
master_failure_mode=fail_on_write

[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MariaDBClient
port=4006

[server1]
type=server
address=195.XXX.123.22
port=3306
protocol=MariaDBBackend

[server2]
type=server
address=142.XXX.186.188
port=3306
protocol=MariaDBBackend

如果您还没有手动配置复制,您可以在 Maxscale 容器中使用以下命令来设置服务器之间的复制:

maxctrl call command mariadbmon reset-replication MariaDB-Monitor server1

这会导致为 MariaDB-Monitor 配置的所有其他服务器从 server1 开始复制。

注意:此命令会重置 GTID 位置,因此不应在实时系统上使用。如果您使用的是实时系统,请使用 CHANGE MASTER TO command with the correct GTID coordinates. It won't touch the data but you'll lose the history (it does a RESET MASTER).


如果您希望在容器首次启动时自动配置复制,您可以在 /docker-entrypoint-initdb.d 处挂载一个包含 SQL 命令的文件,MariaDB 将在启动期间执行这些命令。对于自动化系统,这可能是一个更好的解决方案,并且对于测试设置来说非常方便。