Creating a MySQL database using the API
This article describes how to create a MySQL database via the API.
Prerequisites
To create a MySQL database, you will need to have access to a project. Refer to the guide "create a project" for this. You will need the project ID for the subsequent operations.
List available MySQL versions
Send a request to the GET/ endpoint to get a list of available MySQL versions.
Each returned version object contains the following properties:
idcontains the unique identifier of the versionnumbercontains the version number (e.g., "8.0")namecontains a human-readable name for the versiondisabledindicates whether the version is available for use
Important: When selecting a version for your database, you must choose a version where disabled is false.
Creating a MySQL database
To create a MySQL database, send a request to the POST/ endpoint. The request body must contain a JSON object with the following properties:
Database properties
database.descriptionshould contain a human-readable description of the database. This is a required value.database.versionshould contain the version number of the database (e.g., "8.0"). This is a required value, and must match thenumberproperty of a version returned by the GET/endpoint (not thev2/ mysql-versions/ idproperty).database.characterSettings(optional) allows you to specify the character set and collation for the database.
User properties
When creating a MySQL database, you must also create an initial user. The user properties include:
user.passwordshould contain the password for the database user. This is a required value.user.accessLevelmust be set to"full"for the initial user. This is a required value.user.externalAccess(optional) indicates whether the user can access the database from outside the hosting environment.user.accessIpMask(optional) specifies IP address restrictions for external access.
POST /v2/projects/YOUR_PROJECT_ID/mysql-databases HTTP/1.1
Host: api.mittwald.de
Content-Type: application/json
{
"database": {
"description": "My Application Database",
"version": "8.0",
"characterSettings": {
"characterSet": "utf8mb4",
"collation": "utf8mb4_unicode_ci"
}
},
"user": {
"password": "your-secure-password",
"accessLevel": "full",
"externalAccess": true,
"accessIpMask": "0.0.0.0/0"
}
}
/v2/projects/{projectId}/mysql-databases/ The response will contain the following properties:
idcontains the ID of the created database.userIdcontains the ID of the created user.
Monitoring database readiness
After creating a database, it may take a few moments before it is ready to use. You can monitor the readiness status by sending a request to the GET/ endpoint.
The response contains a mainUser object with a status field. This field can have the following values:
pending- The user is being createdready- The user is ready and the database can be usederror- An error occurred during user creationterminating- The user is being deleteddisabled- The user has been disabled
Important: Wait until the mainUser.status field is "ready" before attempting to connect to the database.
Accessing the database
Once the database is ready, you can retrieve connection information by sending a request to the GET/ endpoint.
Among others, the response will contain the following values:
hostnamecontains the hostname of the database server. You can use this value to connect to the database from within your hosting environment.namecontains the name of the database.mainUser.namecontains the username for database authentication.characterSettingsallows you to inspect the character set and collation settings applied to your database.
Note: The password is set during database creation and cannot be retrieved via the API. To change the password, use the PATCH/ endpoint.