MySQL Shell API 9.2.0
Unified development interface for MySQL Products
|
Routing Guidelines operations More...
Methods | |
str | get_name () |
Returns the name of the Routing Guideline. More... | |
dict | as_json () |
Returns the Routing Guideline as a JSON document. More... | |
ShellResult | destinations () |
List destination classes defined for the Routing Guideline. More... | |
ShellResult | routes () |
List routes defined for the Routing Guideline. More... | |
None | add_destination (str name, str match, dict options) |
Adds a destination class that groups MySQL instances in the Routing Guideline. More... | |
None | add_route (str name, str match, list destinations, dict options) |
Adds a new route that defines how client sessions matching specific criteria are routed to defined destinations. More... | |
None | remove_destination (str name) |
Remove a destination class from the Routing Guideline. More... | |
None | remove_route (str name) |
Remove a route from the Routing Guideline. More... | |
None | show (dict options) |
Displays a comprehensive summary of the Routing Guideline. More... | |
None | rename (str name) |
Rename the target Routing Guideline. More... | |
None | set_destination_option (str destinationName, str option, str value) |
Updates a specific option for a named destination in the Routing Guideline. More... | |
None | set_route_option (str routeName, str option, str value) |
Updates a specific option for a named route in the Routing Guideline. More... | |
RoutingGuideline | copy (str name) |
Creates a full copy of the target Routing Guideline with a new name. More... | |
None | export (str filePath) |
Exports the Routing Guideline to a file. More... | |
Properties | |
str | name |
Returns the name of the Routing Guideline. | |
Routing Guidelines operations
A RoutingGuideline can be used to control routing behavior of MySQL Router through rules that define possible destination MySQL servers for incoming client sessions.
Routing Guidelines consist of two main components: destination classes, and routes.
Destination classes
Destination classes group MySQL server instances in the topology using expressions. These expressions define the conditions under which servers are added to a destination class. Each class can then be used to form a pool of candidate instances for routing. A server can belong to multiple destination classes simultaneously. However, only ONLINE instances in the topology are considered when forming the candidate pool.
Routes
Routes, on the other hand, specify an expression that can match incoming client sessions, and a list of destination classes to form a pool of candidate MySQL servers to route them to.
Routes specify how incoming client sessions are classified and directed. When a client connects to a MySQL Router port:
Topology monitoring
MySQL Router continuously monitors the topology of its topology. If the topology changes:
Example Use Cases for Client Classification
Routing Guidelines can classify clients based on a variety of attributes, such as:
For the full list of session-related variables to define the matching rules for routes , see \? RoutingGuideline.add_route()
Example Use Cases for Destinations
Destinations can be defined with broad or specific criteria, such as:
For the full list of server-related variables to define the destination classes matching rules, see \? RoutingGuideline.add_destination()
Practical Example
Adding Destinations and Routes
Destination classes can be added with the add_destination() method.
For example: rg.addDestination("ReadOnlyServers", "$.server.memberRole = SECONDARY")
Routes can be added with the add_route() method.
For example: rg.addRoute('priority_reads', '$.session.user = "myapp"', ['round-robin(Primary)'])
str get_name | ( | ) |
Returns the name of the Routing Guideline.
dict as_json | ( | ) |
Returns the Routing Guideline as a JSON document.
The returned Routing Guideline can be modified and fed to parse().
ShellResult destinations | ( | ) |
List destination classes defined for the Routing Guideline.
ShellResult routes | ( | ) |
List routes defined for the Routing Guideline.
None add_destination | ( | str | name, |
str | match, | ||
dict | options | ||
) |
Adds a destination class that groups MySQL instances in the Routing Guideline.
name | Name for the destination class to be added. |
match | An expression string for matching MySQL server instances. |
options | Optional dictionary with additional options. |
A destination class specifies an expression that groups together MySQL server instances from the topology. They can be used to define candidate destinations for a route to forward client sessions to. Destination classes group instances based on matching attributes, and each instance can belong to one or more destination classes.
For an overview on Routing Guidelines, see \? RoutingGuideline.
Expression Syntax
The matching expression can contain variables related to the MySQL server instances ('$.server.*') and the Router itself ('$.router.*') and must evaluate to either true or false. Each variable represents a specific attribute of the server or the router, allowing fine-grained control over grouping.
The following server-related variables are available:
The following router-related variables are available:
Examples
rg.add_destination("SecondaryInstances", "$.server.memberRole = 'SECONDARY'")
rg.add_destination("US_Instances", "$.server.address in ['us-east-1.example.com', 'us-west-2.example.com']")
rg.add_destination("MarkedRouters", "$.router.tags.mark = 'special'")
None add_route | ( | str | name, |
str | match, | ||
list | destinations, | ||
dict | options | ||
) |
Adds a new route that defines how client sessions matching specific criteria are routed to defined destinations.
name | The name of the route to be added. |
match | An expression string for matching incoming client sessions. |
destinations | An array of destination selectors in the form of `strategy( destination, ...)`. |
options | An optional dictionary with additional options. |
This command adds a route that evaluates a given expression against attributes of incoming client sessions. If the expression matches, the client session is routed to one of the specified destination instances, based on the defined routing strategies.
Individual routes can be selectively enabled or disabled using the 'enabled' flag. If disabled, the route will not be considered during routing decisions.
For an overview on Routing Guidelines, see \? RoutingGuideline.
Source Expression
The 'match' parameter specifies the client session matching rule. It can contain variables related to the incoming client session ('$.session.*') and the router itself ('$.router.*'). The expression must evaluate to either true or false.
The following session-related variables are available:
The following router-related variables are available:
The '$.session.connectAttrs' variable refers to attributes sent by the client during the connection handshake. These attributes are not simple scalars but key-value pairs that describe details about the client environment. You can use these attributes to match specific criteria for routing.
For more information on the available Client connection attributes see https://dev.mysql.com/doc/refman/en/performance-schema-connection-attribute-tables.html#performance-schema-connection-attributes-available
Destinations
The 'destinations' parameter specifies the list of candidate destination classes for routing, using one or more routing strategies. A destination list must be defined in the format '[strategy(destination, ...), ...]', where:
'strategy' is one of the supported routing strategies:
'destination' is the name of the destination class defined in the Routing Guideline. Each class is a group of instances matching a specific set of rules.
Destinations are organized in tiers, and MySQL Router will evaluate each tier in order of priority. If no servers are available in the first tier, it will proceed to the next until it finds a suitable destination or exhausts the list.
To add a new destination class, use the add_destination method.
The following options can be specified when adding a route:
Examples
guideline.addRoute('priority_reads', '$.session.user = "myapp"', ['round-robin(Primary)'])
guideline.addRoute('local_reads', '$.session.sourceIP = "192.168.1.10"', ['round-robin(Secondary)'], {enabled: true})
guideline.addRoute('backup_client', '$.session.connectAttrs.program_name = "mysqldump"', ['first-available(backup_server)'],{ connectionSharingAllowed: false, enabled: true }
guideline.addRoute('tagged', '$.router.tags.marker = "special"', ['round-robin(Special)'], { connectionSharingAllowed: true, enabled: true } );
None remove_destination | ( | str | name | ) |
Remove a destination class from the Routing Guideline.
name | Name of the destination class |
None remove_route | ( | str | name | ) |
Remove a route from the Routing Guideline.
name | Name of the route |
void show | ( | dict | options | ) |
Displays a comprehensive summary of the Routing Guideline.
options | Optional dictionary with additional options. |
This function describes the Routing Guideline by evaluating expressions and listing all destinations and routes, including the information about the servers of the target topology that match the destinations and routes. It also identifies all destinations not referenced by any route.
Destinations that rely on expressions involving the target router port or address (e.g., '$.router.port' or '$.router.address') may produce results that differ from actual behavior in MySQL Router. Since this function evaluates the guideline outside the context of the router, these expressions are not matched against live router runtime data and may not reflect the router's operational reality. On that case, the option 'router' must be set to specify the Router instance that should be used for evaluation of the guideline.
The options dictionary may contain the following attributes:
void rename | ( | str | name | ) |
Rename the target Routing Guideline.
This functions allows changing the name of an existing Routing Guideline.
None set_destination_option | ( | str | destinationName, |
str | option, | ||
str | value | ||
) |
Updates a specific option for a named destination in the Routing Guideline.
destinationName | The name of the destination whose option needs to be updated. |
option | The option to update for the specified destination. |
value | The new value for the specified option. |
This command allows modifying the properties of a destination within a Routing Guideline. It is used to change the matching rule for identifying MySQL server instances that belong to this destination class.
The following destination options are supported:
Examples
rg.set_destination_option("EU_Regions", "match", "$.server.address in ['eu-central-1.example.com', 'eu-west-1.example.com']");
rg.set_destination_option("Primary_Instances", "match", "$.server.memberRole = 'PRIMARY'");
For more information on destinations, see \? RoutingGuideline.add_destination.
For more information about Routing Guidelines, see \? RoutingGuideline.
None set_route_option | ( | str | routeName, |
str | option, | ||
str | value | ||
) |
Updates a specific option for a named route in the Routing Guideline.
routeName | The name of the route whose option needs to be updated. |
option | The option to update for the specified route. |
value | The new value for the specified option. |
This command allows modifying the properties of a route within a Routing Guideline. It is used to change the matching rule for client sessions, update the destinations, enable or disable the route, or change its priority.
The following route options are supported:
Examples
rg.set_route_option("read_traffic", "match", "$.session.user = 'readonly_user'");
rg.set_route_option("write_traffic", "enabled", true);
For more information on routes definition, see \? RoutingGuideline.add_route.
For more information about Routing Guidelines, see \? RoutingGuideline.
RoutingGuideline copy | ( | str | name | ) |
Creates a full copy of the target Routing Guideline with a new name.
name | The name for the new Routing Guideline. |
This command creates a duplicate of the target Routing Guideline, including all its routes and destination classes. The new guideline must have a unique name within the topology, and the name must follow the same naming rules defined for Routing Guidelines. This command is useful for quickly creating new guidelines based on existing ones, without manually duplicating all the definitions.
Example
rg_copy = rg.copy("newGuideline");
The example above creates a new Routing Guideline named 'newGuideline' with the same configuration as the original guideline.
For more information about Routing Guidelines, see \? RoutingGuideline.
None export | ( | str | file | ) |
Exports the Routing Guideline to a file.
file | The file path where the Routing Guideline will be saved. |
This command exports a complete copy of the Routing Guideline to a file in JSON format. The output file can then be used for backup, sharing, or applying the Routing Guideline to another topology.
For more information on Routing Guidelines, see \? RoutingGuideline.