MySQL

This provisions a new MySQL database in an existing MySQL instance. The instance must be reachable from Humanitec IPs.

Property Description
Resource type mysql
Account type None

Inputs

Values

Name Type Description
host string The IP Address or hostname that the instance is available on.
port integer The port the instance is listening on.
append_host_to_user boolean [Optional] Azure Databases for Postgres and MySQL require usernames to have @servername appended to them. Set this to true for the Driver to append this automatically. (See: Azure Database connection strings )
copy_from_name string [Optional] If provided, specifies the database in the same instance to copy data from.

Secrets

Name Type Description
dbcredentials object An object holding username and password properties for the PostgreSQL superuser.
agent_url string [Optional] Can be used to specify the Humanitec Agent URL to use to intermediate access to the MySQL instance. This is always done by supplying a placeholder. For example: ${resources["agent.default#agent-01"].outputs.url}.

Notes

Automatic population of database

MySQL does not provide any standard way of duplicating databases. Instead, the suggested approach is to “dump and restore” a database using tools such as mysqldump .

This Driver emulates a dump and restore of the database - i.e. it serially copies data from the source database to the target database. It therefore suffers from limitations of dumping and restoring a database. The main issue is that if the source database is being written to, there are no guarantees about the data integrity in the resulting database. Consider a source database with 2 tables A and B:

  1. Table A is copied to the target database.
  2. New data is then written to Table A and Table B in the source database.
  3. Table B is copied from the source database to the target database.

The target database will now have the updated to table B but not table A.

This functionality should not be used for production databases or where data integrity must be guaranteed.

Example

Set the following environment variables for the CLI and API commands:

Variable Example Description
HUMANITEC_TOKEN my-token The authentication token for accessing the Humanitec API.
HUMANITEC_ORG my-org-id The unique identifier for the organization in Humanitec.

Use the command below for the interface of your choice to create a fresh MySQL database in an instance available at dev-mysql.example.com.

  1. Create a file defining the Resource Definition you want to create:
cat << EOF > dev-mysql.yaml
apiVersion: entity.humanitec.io/v1b1
kind: Definition
metadata:
  id: dev-mysql
entity:
  name: Dev MySQL
  type: mysql
  driver_type: humanitec/mysql
  driver_inputs:
    secrets:
      dbcredentials:
        username: root
        password: 53cr3t-P455w0rd
    values:
      host: dev-mysql.example.com
      port: 3306
  criteria:
  - env_type: development
EOF
  1. Use the humctl create command to create the Resource Definition in the Organization defined by your configured context:
humctl create -f dev-mysql.yaml
rm dev-mysql.yaml

curl https://api.humanitec.io/orgs/${HUMANITEC_ORG}/resources/defs \
  -X POST \
  -H "Authorization: Bearer ${HUMANITEC_TOKEN}" \
  -H "Content-Type: application/json" \
  --data-binary '
{
  "id": "dev-mysql",
  "name": "Dev MySQL",
  "type": "mysql",
  "criteria": [
    {
      "env_type": "development"
    }
  ],
  "driver_type": "humanitec/mysql",
  "driver_inputs": {
    "values": {
      "host": "dev-mysql.example.com",
      "port": 3306
    },
    "secrets": {
      "dbcredentials": {
        "username": "root",
        "password": "53cr3t-P455w0rd"
      }
    }
  }
}'

Most MySQL implementations use a single shared Driver. For Google MySQL implementations, see the Google MySQL Driver .

Prerequisites

  • You must have a database instance/server running.
  • You must have a user defined on the instance for Workloads to use when connecting to the database.

Add a Resource Definition

  1. From the Resource Management screen, click Add resource definition.
  2. In the modal dialog click MySQL.
  3. Next, select the mysql Driver.
  4. Finally, provide the following information, then click Add MySQL.
    1. In the ID field provide a unique ID for the Resource.
    2. Provide the user or role of the database.
    3. Provide the password for the database.
    4. Provide a privilege list to be applied to the user.
    5. Choose whether to have @servername appended to the username.
    6. Provide the database hostname/IP, database name, and port.

Resource Matching

Now that the resource is defined you will need to add matching criteria.

  1. Click on the relevant row in the Resource Definition table.
  2. Then switch to the Matching Criteria tab
  3. Click + Add new Criteria.
  4. Configure the matching rules as needed.
  5. Click Save.
Top