schema.xml Overview

schema.xml is MyCat’s core configuration file, managing logical databases, logical tables, data nodes, and data hosts.

Core Components

1. Schema Tag (Logical Database)

Define virtual database, decouple applications from physical databases:

<schema name="edu_order" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1">
    <table name="orders" primaryKey="order_id" dataNode="dn1,dn2" rule="mod-long"/>
</schema>

2. Table Tag (Logical Table)

Specify virtual table structure and support three types:

  • Sharded table: Distributed to multiple nodes using rules
  • Global table: Replicated to all nodes
  • Non-sharded table: Stored on single node

3. DataNode Tag

Map logical tables to physical database instances:

<dataNode name="dn1" dataHost="edu_order_1" database="edu_order_1" />

4. DataHost Tag

Configure physical database connections:

  • Connection pool settings (maxCon/minCon)
  • Read-write separation (balance attribute)
  • High availability (switchType)
  • Heartbeat detection

5. Heartbeat Tag

Specify health check SQL query for database monitoring:

  • MySQL: SELECT user()
  • Oracle: SELECT 1 FROM dual

6. WriteHost/ReadHost Tag

Define master (write) and slave (read) database instances.

Typical Application Scenarios

  • Order table sharded by time or user ID
  • Global table for product information synchronization
  • Log table partitioned by date range

Configuration Notes

  1. Need to cooperate with rule.xml for sharding rules
  2. Need to restart MyCat or reload after modification
  3. Keep logical configuration synchronized with actual database structure