# MySQL

This data destination connector sends data to a [MySQL](https://www.mysql.com/products/) or [MariaDB](https://mariadb.org/) database.



**Before you start**

You need:

- **A MySQL or MariaDB database** to write to.
- **A user for the connector** with `CREATE`, `DROP`, `CREATE TEMPORARY TABLES`, `SELECT`, `INSERT` and `UPDATE` on that database. Create a dedicated user (#create-a-dedicated-user) has the SQL for the database and the user. Without `ALTER`, the connector can't write Storage descriptions as table and column comments; the job logs a warning and still succeeds.
- **The server variable `local_infile` turned on,** because the connector loads data with `LOAD DATA LOCAL INFILE` and stops with `local_infile is disabled on server` otherwise. It is off by default on MySQL 8 (https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_local_infile) (turn it on with `SET GLOBAL local_infile = 1` or your managed service's parameter) and on by default on MariaDB.
- **Network access from Keboola to the database:** allow the Keboola IP addresses (/components/ip-addresses/) of your stack in your firewall, or connect through an SSH tunnel (/components/extractors/database/#connecting-to-database).

What each tab needs:

- **Prompt tab:** Kai, the **Kai Agent** button in the top bar, on supported stacks. A new Free Plan project has it from the start; elsewhere an organization admin turns it on once: Get started with Kai (/kai/getting-started/).
- **UI tab:** a browser.
- **CLI / API tab:** kbagent (/cli/getting-started/), connected to your project. The checks only read.


## Create a dedicated user

We highly recommend that you create dedicated credentials for the connector in your database. You can use the following SQL code to get started:

```sql
CREATE DATABASE writer_sample;
CREATE USER 'writer_sample' IDENTIFIED BY 'Writer_sample1';
GRANT CREATE TEMPORARY TABLES, CREATE, DROP, SELECT, INSERT, UPDATE ON writer_sample.*
    TO writer_sample;
```

## Configuration
[Create a new configuration](https://help.keboola.com/components/configurations/#create-a-configuration) of the **MySQL** data destination connector.

The first step is to **Set Up Database Credentials**.
You need to provide a *host name*, *user name*, *password*, and a *database name* (MySQL [*schema*](https://dev.mysql.com/doc/refman/8.0/en/glossary.html#glos_schema)).

![Screenshot - Credentials](https://help.keboola.com/components/writers/database/mysql/mysql-2.png)

## Table Configuration
The next step is to configure the tables you want to write. Click **Add New Table** and select an existing table from Storage:

![Screenshot - Select Table](https://help.keboola.com/components/writers/database/mysql/mysql-3.png)

Then specify table configuration. Use the **preview** icon to peek at the column contents.

![Screenshot - Table Columns](https://help.keboola.com/components/writers/database/mysql/mysql-5.png)

For each column you can specify its

- **name** in the destination database; you can also use the select box in the table header to bulk convert the case of all names.
- **data type** (one of [MySQL data types](https://dev.mysql.com/doc/refman/8.0/en/data-types.html)); you can also use the select box in the table header to bulk set the type for all columns. Setting the data type to `IGNORE` means that the column will not be present in the destination table.
- **nullable**; when checked, the column will be marked as nullable and empty values (`''`) in that column will be converted to `NULL`. Use this for non-string columns with missing data.
- **default value**; the provided value will be set as the [default value of the column](https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html) in the target table.

When done configuring the columns, don't forget to **save** the settings.

### Load Options
At the top of the page, you can specify the target table name and additional load options. There are two main options how the connector
can write data to tables — **Full Load** and **Incremental Load**.

![Screenshot - Table Options](https://help.keboola.com/components/writers/database/mysql/mysql-6.png)

In the **Incremental Load** mode, the data are bulk inserted into
the destination table and the table structure must match (including the data types). That means the structure of the target table
will not be modified. If the target table doesn't exist, it will be created. If a primary key is defined on the table, the
data is [upserted](https://en.wikipedia.org/wiki/Merge_(SQL)). If no primary key is defined, the data is inserted.

In the **Full Load** mode, the table is completely overwritten including the table structure. The table is removed
using the [`DROP`](https://dev.mysql.com/doc/refman/8.0/en/drop-table.html) command and recreated. The
`DROP` command needs to acquire a [table-level lock](https://dev.mysql.com/doc/refman/8.0/en/lock-tables.html).
This means that if the database is used by other applications which acquire table-level locks, the connector may
freeze waiting for the locks to be released.

Additionally, you can specify a **primary key** of the table, a simple column **data filter**, and a filter for
[incremental processing](https://help.keboola.com/storage/tables/incremental-loading/#incremental-processing).

Don't forget to **Save** the configuration, then click **Run Component**.

**Check:**

#### Prompt

```text title="Prompt" wrap
Did the last job of the MySQL data destination configuration my-mysql-config end with success? Give its end time.
```

Kai only reads here, so it asks for no approval, and its answer links to what it found in your project.

#### UI

The job ends with **Success**, and the table appears in the target database with
the rows from the Storage table.

#### CLI / API

```bash
kbagent job list --component-id keboola.wr-db-mysql --limit 1
```

If you have several configurations, add `--config-id` with yours; the job's `status` shows `success`, and then check the destination as in the UI tab.

---
