datadir="/var/lib/proxysql"

# ProxySQL admin configuration section
admin_variables=
{
    admin_credentials="admin:<%= @admin_password%>;cluster_admin:<%= @cluster_admin_password %>"
    mysql_ifaces="0.0.0.0:6032"
    refresh_interval=2000
    web_enabled=true
    web_port=6080
    stats_credentials="stats:<%= @admin_password %>"
    cluster_username="cluster_admin"
    cluster_password="<%= @cluster_admin_password %>"
    cluster_check_interval_ms=200
    cluster_check_status_frequency=100
    cluster_mysql_query_rules_save_to_disk=true
    cluster_mysql_servers_save_to_disk=true
    cluster_mysql_users_save_to_disk=true
    cluster_proxysql_servers_save_to_disk=true
    cluster_mysql_query_rules_diffs_before_sync=3
    cluster_mysql_servers_diffs_before_sync=3
    cluster_mysql_users_diffs_before_sync=3
    cluster_proxysql_servers_diffs_before_sync=3
}

# MySQL/MariaDB related section
mysql_variables=
{
    threads=4
    max_connections=2048
    default_query_delay=0
    default_query_timeout=36000000
    have_compress=true
    poll_timeout=2000
    interfaces="0.0.0.0:3306;/tmp/proxysql.sock"
    default_schema="information_schema"
    stacksize=1048576
    server_version="10.5.5"
    connect_timeout_server=10000
    monitor_history=60000
    monitor_connect_interval=2000
    monitor_ping_interval=2000
    ping_interval_server_msec=10000
    ping_timeout_server=200
    commands_stats=true
    sessions_sort=true
    monitor_username="proxysql"
    monitor_password="<%= @monitor_password %>"
    monitor_galera_healthcheck_interval=2000
    monitor_galera_healthcheck_timeout=800
}


# Specify all ProxySQL hosts here
proxysql_servers =
(
<%- index = 0 -%>
<%- @nextcloud_ip.each do |appserver| -%>
    <%- index += 1 -%>
    { 
        hostname="<%= appserver %>"
        port=6032
        comment="proxysql<%= index %>"
    },
<%- end -%>
)

# HG10 - single-writer
# HF30 - multi-writer
mysql_galera_hostgroups =
(
    {

        writer_hostgroup=10
        backup_writer_hostgroup=20
        reader_hostgroup=30
        offline_hostgroup=9999
        max_writers=1
        writer_is_also_reader=1
        max_transactions_behind=0
        active=1
    }
)

# List all MariaDB Galera nodes here
mysql_servers =
(
<%- @db_ip.each do |db| -%>
  { 
      address="<%= db %>"
      port=3306
      hostgroup=10
      max_connections=100
  },
<%- end -%>
)

# Default query rules:
#  - All writes -> HG10 (single-writer)
#  - All reads  -> HG30 (multi-writer)
mysql_query_rules =
(
    {
        rule_id=100
        active=1
        match_pattern="^SELECT .* FOR UPDATE"
        destination_hostgroup=10
        apply=1
    },
    {
        rule_id=200
        active=1
        match_pattern="^SELECT .*"
        destination_hostgroup=30
        apply=1
    },
    {
        rule_id=300
        active=1
        match_pattern=".*"
        destination_hostgroup=10
        apply=1
    }
)

# All MySQL user that you want to pass through this instance
#  - The MySQL user must be created first in the DB server and grant it to access from this ProxySQL host
mysql_users =
(

<%- index = 0 -%>
<%- @allnames.each do |customer| -%>
  { 
      username="nextcloud_<%= customer %>"
      password="<%= @passwords[index] %>"
      default_hostgroup=10
      transaction_persistent=<%= @transaction_persistent %>
      active=1
  },
<%- index += 1 -%>
<%- end -%>
)