docs/src/main/sphinx/connector/mysql.md
The MySQL connector allows querying and creating tables in an external MySQL instance. This can be used to join data between different systems like MySQL and Hive, or between two different MySQL instances.
To connect to MySQL, you need:
To configure the MySQL connector, create a catalog properties file in
etc/catalog named, for example, example.properties, to mount the MySQL
connector as the mysql catalog. Create the file with the following contents,
replacing the connection properties as appropriate for your setup:
connector.name=mysql
connection-url=jdbc:mysql://example.net:3306
connection-user=root
connection-password=secret
The connection-url defines the connection information and parameters to pass
to the MySQL JDBC driver. The supported parameters for the URL are
available in the MySQL Developer Guide.
For example, the following connection-url allows you to require encrypted
connections to the MySQL server:
connection-url=jdbc:mysql://example.net:3306?sslMode=REQUIRED
The connection-user and connection-password are typically required and
determine the user credentials for the connection, often a service user. You can
use {doc}secrets </security/secrets> to avoid actual values in the catalog
properties files.
(mysql-tls)=
If you have TLS configured with a globally-trusted certificate installed on your
data source, you can enable TLS between your cluster and the data
source by appending a parameter to the JDBC connection string set in the
connection-url catalog configuration property.
For example, with version 8.0 of MySQL Connector/J, use the sslMode
parameter to secure the connection with TLS. By default the parameter is set to
PREFERRED which secures the connection if enabled by the server. You can
also set this parameter to REQUIRED which causes the connection to fail if
TLS is not established.
You can set the sslMode parameter in the catalog configuration file by
appending it to the connection-url configuration property:
connection-url=jdbc:mysql://example.net:3306/?sslMode=REQUIRED
For more information on TLS configuration options, see the MySQL JDBC security documentation.
You can have as many catalogs as you need, so if you have additional
MySQL servers, simply add another properties file to etc/catalog
with a different name, making sure it ends in .properties. For
example, if you name the property file sales.properties, Trino
creates a catalog named sales using the configured connector.
(mysql-fte-support)=
The connector supports {doc}/admin/fault-tolerant-execution of query
processing. Read and write operations are both supported with any retry policy.
Table property usage example:
CREATE TABLE person (
id INT NOT NULL,
name VARCHAR,
age INT,
birthday DATE
)
WITH (
primary_key = ARRAY['id']
);
The following are supported MySQL table properties:
:::{list-table} :widths: 30, 10, 60 :header-rows: 1
primary_keyNOT NULL.
:::(mysql-type-mapping)=
Because Trino and MySQL each support types that the other does not, this
connector {ref}modifies some types <type-mapping-overview> when reading or
writing data. Data types may not map the same way in both directions between
Trino and the data source. Refer to the following sections for type mapping in
each direction.
The connector maps MySQL types to the corresponding Trino types following this table:
:::{list-table} MySQL to Trino type mapping :widths: 30, 30, 40 :header-rows: 1
BITBOOLEANBOOLEANTINYINTTINYINTTINYINTTINYINT UNSIGNEDSMALLINTSMALLINTSMALLINTSMALLINT UNSIGNEDINTEGERINTEGERINTEGERINTEGER UNSIGNEDBIGINTBIGINTBIGINTBIGINT UNSIGNEDDECIMAL(20, 0)DOUBLE PRECISIONDOUBLEFLOATREALREALREALDECIMAL(p, s)DECIMAL(p, s) or NUMBERDECIMAL when p ≤ 38. Otherwise, maps to NUMBER.CHAR(n)CHAR(n)VARCHAR(n)VARCHAR(n)TINYTEXTVARCHAR(255)TEXTVARCHAR(65535)MEDIUMTEXTVARCHAR(16777215)LONGTEXTVARCHARENUM(n)VARCHAR(n)BINARY, VARBINARY, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOBVARBINARYJSONJSONDATEDATETIME(n)TIME(n)DATETIME(n)TIMESTAMP(n)TIMESTAMP(n)TIMESTAMP(n) WITH TIME ZONE:::
No other types are supported.
The connector maps Trino types to the corresponding MySQL types following this table:
:::{list-table} Trino to MySQL type mapping :widths: 30, 30, 40 :header-rows: 1
BOOLEANTINYINTTINYINTTINYINTSMALLINTSMALLINTINTEGERINTEGERBIGINTBIGINTREALREALDOUBLEDOUBLE PRECISIONDECIMAL(p, s)DECIMAL(p, s)CHAR(n)CHAR(n)VARCHAR(n)VARCHAR(n)JSONJSONDATEDATETIME(n)TIME(n)TIMESTAMP(n)DATETIME(n)TIMESTAMP(n) WITH TIME ZONETIMESTAMP(n):::
No other types are supported.
MySQL TIMESTAMP types are mapped to Trino TIMESTAMP WITH TIME ZONE.
To preserve time instants, Trino sets the session time zone
of the MySQL connection to match the JVM time zone.
As a result, error messages similar to the following example occur when
a timezone from the JVM does not exist on the MySQL server:
com.mysql.cj.exceptions.CJException: Unknown or incorrect time zone: 'UTC'
To avoid the errors, you must use a time zone that is known on both systems, or install the missing time zone on the MySQL server.
The MySQL connector provides a schema for every MySQL database.
You can see the available MySQL databases by running SHOW SCHEMAS:
SHOW SCHEMAS FROM example;
If you have a MySQL database named web, you can view the tables
in this database by running SHOW TABLES:
SHOW TABLES FROM example.web;
You can see a list of the columns in the clicks table in the web database
using either of the following:
DESCRIBE example.web.clicks;
SHOW COLUMNS FROM example.web.clicks;
Finally, you can access the clicks table in the web database:
SELECT * FROM example.web.clicks;
If you used a different name for your catalog properties file, use
that catalog name instead of example in the above examples.
(mysql-sql-support)=
The connector provides read access and write access to data and metadata in the MySQL database. In addition to the globally available and read operation statements, the connector supports the following features:
(mysql-insert)=
(mysql-update)=
(mysql-delete)=
(mysql-merge)=
(mysql-procedures)=
(mysql-table-functions)=
The connector provides specific {doc}table functions </functions/table> to
access MySQL.
(mysql-query-function)=
query(varchar) -> tableThe query function allows you to query the underlying database directly. It
requires syntax native to MySQL, because the full query is pushed down and
processed in MySQL. This can be useful for accessing native features which are
not available in Trino or for improving query performance in situations where
running a query natively may be faster.
For example, query the example catalog and group and concatenate all
employee IDs by manager ID:
SELECT
*
FROM
TABLE(
example.system.query(
query => 'SELECT
manager_id, GROUP_CONCAT(employee_id)
FROM
company.employees
GROUP BY
manager_id'
)
);
The connector includes a number of performance improvements, detailed in the following sections.
(mysql-table-statistics)=
The MySQL connector can use {doc}table and column statistics </optimizer/statistics> for {doc}cost based optimizations </optimizer/cost-based-optimizations>, to improve query processing performance
based on the actual data in the data source.
The statistics are collected by MySQL and retrieved by the connector.
The table-level statistics are based on MySQL's INFORMATION_SCHEMA.TABLES
table. The column-level statistics are based on MySQL's index statistics
INFORMATION_SCHEMA.STATISTICS table. The connector can return column-level
statistics only when the column is the first column in some index.
MySQL database can automatically update its table and index statistics. In some cases, you may want to force statistics update, for example after creating new index, or after changing data in the table. You can do that by executing the following statement in MySQL Database.
ANALYZE TABLE table_name;
:::{note} MySQL and Trino may use statistics information in different ways. For this reason, the accuracy of table and column statistics returned by the MySQL connector might be lower than that of others connectors. :::
Improving statistics accuracy
You can improve statistics accuracy with histogram statistics (available since MySQL 8.0). To create histogram statistics execute the following statement in MySQL Database.
ANALYZE TABLE table_name UPDATE HISTOGRAM ON column_name1, column_name2, ...;
Refer to MySQL documentation for information about options, limitations and additional considerations.
(mysql-pushdown)=
The connector supports pushdown for a number of operations:
join-pushdownlimit-pushdowntopn-pushdown{ref}Aggregate pushdown <aggregation-pushdown> for the following functions:
avgcountmaxminsumstddevstddev_popstddev_sampvariancevar_popvar_samp