{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":[]},"redocly_category":"Integrations","type":"markdown"},"seo":{"title":"MySQL Tables Export Integration CLI","description":"Treasure Data Product Documentation · Collect and Unify · Segment and Activate · Experiment and Analyze · Decisioning Automate with AI Scale and Trust.","siteUrl":"https://docs.treasuredata.com","lang":"en-US","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"mysql-tables-export-integration-cli","__idx":0},"children":["MySQL Tables Export Integration CLI"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["You can use the CLI to issue queries and output results. The following instructions show you how to format the query output results using the CLI."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"td-query-command-usage","__idx":1},"children":["TD Query Command Usage"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To output the result of a single query to a MySQL server add the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["--result"]}," option to the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["td query"]}," command. After the job is finished, the results are written into your database:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"$ td query --result 'mysql://user:password@host/database/table' \\\n  -w -d testdb \\\n  \"SELECT code, COUNT(1) FROM www_access GROUP BY code\"\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"scheduled-jobs","__idx":2},"children":["Scheduled Jobs"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To create a scheduled query whose output is systematically written to MySQL add the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["--result"]}," option when creating the schedule through ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["td sched:create"]}," command. Every time the job runs, the results are written into ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["mytbl"]},"."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"$ td result:create mydb mysql://user:password@host/database\n$ td sched:create hourly_count_example \"0 * * * *\" -d testdb \"select count(*) from www_access\" --result mydb:mytbl\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"result-output-url-format","__idx":3},"children":["Result Output URL Format"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The result output target is represented by URL with the following format:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mysql://username:password@hostname:port/database/table\n"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["where:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["mysql"]}," is identified for result output to MySQL;"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["username"]}," and ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["password"]}," are the credentials to the MySQL server;"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["hostname"]}," is the host name of theMySQL server;"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["port"]}," is the port number through which theMySQL server is accessible. “:” is optional and assumed to be 3306 by default;"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["database"]}," is the name of the destination database;"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["table"]}," is the name of a table within the above mentioned database. It may not exist at the moment the query output is execute, in which case a table with the specified name will be created for the user."]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"options","__idx":4},"children":["Options"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Writing Job Results to the MySQL server supports the following options."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"modes-to-modify-data-appendreplacetruncateupdate","__idx":5},"children":["Modes to Modify Data (Append/Replace/Truncate/Update)"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["You can add or delete data in four ways."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mysql://user:password@host/database/table               # append\nmysql://user:password@host/database/table?mode=append   # append\nmysql://user:password@host/database/table?mode=replace  # replace\nmysql://user:password@host/database/table?mode=truncate # truncate\nmysql://user:password@host/database/table?mode=update   # update\n"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["All four modes are ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["atomic"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["In the “host” section, you can specify the port as well."]}," Here is an example:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mysql://superadmin:superstrongpassword@data.science.com:27013/transaction/user_info\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"append","__idx":6},"children":["APPEND"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This is the default mode. The query results are appended to a table. If the table does not exist, a new table will be created."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"replace","__idx":7},"children":["REPLACE"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If the table already exists, the rows of the existing table are replaced with the query results. If the table does not exist, a new table will be created."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["We achieve ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["atomicity"]}," (so that a consumer of the table always has consistent data) by performing the following three steps in a ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["single transaction"]},"."]},{"$$mdtype":"Tag","name":"ol","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Create a temporary table."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Write to the temporary table."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Replace the existing table with the temporary table using ALTER TABLE RENAME."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Using replace mode may change the schema of the MySQL table. To avoid schema changes to the target table, select 'Preserve schema?' option."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"truncate","__idx":8},"children":["TRUNCATE"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The system first truncates the existing table, then inserts the query results. If the table does not exist, a new table will be created."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Unlike REPLACE, TRUNCATE retains the indexes of your table."]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"update","__idx":9},"children":["UPDATE"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This mode inserts new data and update existing data by performing the following steps."]},{"$$mdtype":"Tag","name":"ol","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Create a temporary table."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Write to the temporary table."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Update data from temporary table to the destination table."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Insert the rest of the data from temporary table to the destination table."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To specify a unique key, you can set ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["unique"]}," parameter, which is comma-separated list of keys. If the values for all keys match all existing record, that record will be replaced. Here is an example:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mysql://superadmin:superstrongpassword@data.science.com:27013/transaction/user_infomode=update&unique=column1%2Ccolumn2  \n"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["On CLI, Comma in the parameter must be URL encoded. (\",\" should be \"%2C\")"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"additional-options","__idx":10},"children":["Additional Options"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Writing Job Results to the MySQL server supports following additional options that can be specified as optional URL parameters."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"ssl-option","__idx":11},"children":["ssl option"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["ssl"]}," option determines whether to use SSL or not for connecting to the MySQL server."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"ssltrue","__idx":12},"children":["ssl=true"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Use SSL from Treasure Data to the MySQL server connection. The MySQL server must be configured to accept SSL connection."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mysql://user:password@host/database/table?ssl=true\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"sslfalse-default","__idx":13},"children":["ssl=false (default)"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Do not use SSL from Treasure Data to the MySQL server."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mysql://user:password@host/database/table?ssl=false\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"character-encoding","__idx":14},"children":["Character Encoding"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Treasure Data always uses UTF-8 as character encoding. You also need to setup your database to use UTF-8 by default. If you came across any encoding related issues, check the following configurations."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"header":{"controls":{"copy":{}}},"source":"mysql> show variables like 'char%';\n+--------------------------+----------------------------+\n| Variable_name | Value |\n+--------------------------+----------------------------+\n| character_set_client | utf8 |\n| character_set_connection | utf8 |\n| character_set_database | utf8 |\n| character_set_filesystem | binary |\n| character_set_results | utf8 |\n| character_set_server | utf8 |\n| character_set_system | utf8 |\n| character_sets_dir | /usr/share/mysql/charsets/ |\n+--------------------------+----------------------------+\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"int-unsigned-type-of-mysql","__idx":15},"children":["INT UNSIGNED type of MySQL"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Job Results into MySQL doesn't support INT UNSIGNED type of MySQL. Its value will be converted to an unexpected value."]}]},"headings":[{"value":"MySQL Tables Export Integration CLI","id":"mysql-tables-export-integration-cli","depth":1},{"value":"TD Query Command Usage","id":"td-query-command-usage","depth":2},{"value":"Scheduled Jobs","id":"scheduled-jobs","depth":2},{"value":"Result Output URL Format","id":"result-output-url-format","depth":2},{"value":"Options","id":"options","depth":2},{"value":"Modes to Modify Data (Append/Replace/Truncate/Update)","id":"modes-to-modify-data-appendreplacetruncateupdate","depth":3},{"value":"APPEND","id":"append","depth":4},{"value":"REPLACE","id":"replace","depth":4},{"value":"TRUNCATE","id":"truncate","depth":4},{"value":"UPDATE","id":"update","depth":4},{"value":"Additional Options","id":"additional-options","depth":3},{"value":"ssl option","id":"ssl-option","depth":2},{"value":"ssl=true","id":"ssltrue","depth":3},{"value":"ssl=false (default)","id":"sslfalse-default","depth":3},{"value":"Character Encoding","id":"character-encoding","depth":4},{"value":"INT UNSIGNED type of MySQL","id":"int-unsigned-type-of-mysql","depth":4}],"frontmatter":{"seo":{"title":"MySQL Tables Export Integration CLI"}},"lastModified":"2026-03-18T02:21:08.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/int/mysql-tables-export-integration-cli","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}