MySQL

Detailed information on the MySQL state store component

Component format

To setup MySQL state store create a component of type state.mysql. See this guide on how to create and apply a state store configuration.

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: <NAME>
  namespace: <NAMESPACE>
spec:
  type: state.mysql
  version: v1
  metadata:
  - name: connectionString
    value: "<CONNECTION STRING>"
  - name: schemaName
    value: "<SCHEMA NAME>"
  - name: tableName
    value: "<TABLE NAME>"
  - name: pemPath
    value: "<PEM PATH>"

If you wish to use MySQL as an actor store, append the following to the yaml.

  - name: actorStateStore
    value: "true"

Spec metadata fields

FieldRequiredDetailsExample
connectionStringYThe connection string to connect to MySQL. Do not add the schema to the connection stringNon SSL connection: "<user>:<password>@tcp(<server>:3306)/?allowNativePasswords=true", Enforced SSL Connection: "<user>:<password>@tcp(<server>:3306)/?allowNativePasswords=true&tls=custom"
schemaNameNThe schema name to use. Will be created if schema does not exist. Defaults to "dapr_state_store""custom_schema", "dapr_schema"
tableNameNThe table name to use. Will be created if table does not exist. Defaults to "state""table_name", "dapr_state"
pemPathNFull path to the PEM file to use for enforced SSL Connection"/path/to/file.pem", "C:\path\to\file.pem"

Setup MySQL

Dapr can use any MySQL instance - containerized, running on your local dev machine, or a managed cloud service.


Run an instance of MySQL. You can run a local instance of MySQL in Docker CE with the following command:

This example does not describe a production configuration because it sets the password in plain text and the user name is left as the MySQL default of “root”.

docker run --name dapr-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest

We can use Helm to quickly create a MySQL instance in our Kubernetes cluster. This approach requires Installing Helm.

  1. Install MySQL into your cluster.

    helm repo add bitnami https://charts.bitnami.com/bitnami
    helm install dapr-mysql bitnami/mysql
    
  2. Run kubectl get pods to see the MySQL containers now running in your cluster.

  3. Next, we’ll get our password, which is slightly different depending on the OS we’re using:

    • Windows: Run [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($(kubectl get secret --namespace default dapr-mysql -o jsonpath="{.data.mysql-root-password}"))) and copy the outputted password.

    • Linux/MacOS: Run kubectl get secret --namespace default dapr-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode and copy the outputted password.

  4. With the password you can construct your connection string.


Azure MySQL

If you are using MySQL on Azure see the Azure documentation on SSL database connections, for information on how to download the required certificate.

Non SSL connection

Replace the <CONNECTION STRING> value with your connection string. The connection string is a standard MySQL connection string. For example, "<user>:<password>@tcp(<server>:3306)/?allowNativePasswords=true".

Enforced SSL connection

If your server requires SSL your connection string must end with &tls=custom for example, "<user>:<password>@tcp(<server>:3306)/?allowNativePasswords=true&tls=custom". You must replace the <PEM PATH> with a full path to the PEM file. The connection to MySQL will require a minimum TLS version of 1.2.