Launch DB Using REST API
This walkthrough explains how to launch database services and manage the lifecycle of database services using the MariaDB Cloud DBaaS REST API.
Launch a Service
Generate API Key
Go to the MariaDB Cloud API Key management page and generate an API key
Export the value from the token field to an environment variable $API_KEY
export API_KEY='... key data ...'The
API_KEYenvironment variable will be used in the subsequent steps.
Use it on subsequent requests, for example:
curl --request GET 'https://api.skysql.com/provisioning/v1/services' \
--header "X-API-Key: $API_KEY"Determine the Client IP Address
When your new service is created, your client can only connect through the service's firewall if the client's IP address is in the service's IP allowlist.
Before creating the new service, determine the public IP address of your client host and save it to the SKYSQL_CLIENT_IP environment variable.
If you are not sure of your public IP address, you can use a lookup service, such as checkip.amazonaws.com:
export SKYSQL_CLIENT_IP=`curl -sS checkip.amazonaws.com`Launch a Service
To launch a service:
Prepare a request body containing the desired service options in a file called
request-service.json:
cat > request-service.json <<EOF
{
"service_type": "transactional",
"topology": "es-single",
"provider": "gcp",
"region": "us-central1",
"architecture": "amd64",
"size": "sky-2x8",
"storage": 100,
"nodes": 1,
"name": "skysql-quickstart",
"ssl_enabled": true,
"allow_list": [
{
"comment": "Describe the IP address",
"ip": "${SKYSQL_CLIENT_IP}/32"
}
]
}
EOFThis configuration is suitable for a quick test, but a more customized configuration should be selected for performance testing or for alignment to the needs of production workloads:
For
service_type, choose a Service Type SelectionFor
topology, choose a Topology SelectionFor
provider, choose a Cloud Provider Selection (aws,gcporazure)For
region, choose a Region SelectionFor
architecture, choose a Hardware Architecture SelectionFor
size, choose an Instance Size SelectionFor
storage, choose a Transactional Storage Size SelectionFor
nodes, choose a node countFor
version, choose the Software Version SelectionFor
name, choose a name between 4-24 characters for the new serviceFor
allow_list, set the client IP address using CIDR notation, so that the client can connect through the firewall
Provide the request to the
/provisioning/v1/servicesAPI endpoint to create (launch) a new database service and save the response to theresponse-service.jsonfile:
curl -sS --location --request POST \
--header "X-API-Key: ${API_KEY}" \
--header "Accept: application/json" \
--header "Content-type: application/json" \
--data '@request-service.json' \
https://api.skysql.com/provisioning/v1/services \
| tee response-service.json | jq .Upon success, the command will return JSON with details about the new service.
Read the service ID for the new service and save the value in the
SKYSQL_SERVICEenvironment variable:$ export SKYSQL_SERVICE=`jq -r .id response-service.json`
Check Service State
Before advancing, check the service state using the /provisioning/v1/services/${SKYSQL_SERVICE} API endpoint:
curl -sS --location --request GET \
--header "X-API-Key: ${API_KEY}" \
--header "Accept: application/json" \
https://api.skysql.com/provisioning/v1/services/${SKYSQL_SERVICE} \
| tee response-state.json | jq .statusWhen the service is still being launched, the JSON payload will contain "pending_create" or "pending_modifying" as the service status.
When the service has been launched, the JSON payload contains "ready", and you can continue with the next steps. Keep in mind that some of the following values will not be populated in the JSON data until this ready status has been achieved.
Obtain Connection Details
Obtain the connection credentials for the new MariaDB Cloud service by executing the following commands:
Obtain the hostname and port of the service and save them to the
SKYSQL_FQDNandSKYSQL_PORTenvironment variables:The hostname is specified with the
"fqdn"key.export SKYSQL_FQDN=`jq -r .fqdn response-state.json`Available TCP ports are specified in the
"endpoints"array. For this test, connect to the"port"where"name"is"readwrite".export SKYSQL_PORT=`jq '.endpoints[0].ports[] | select(.name=="readwrite") | .port' response-state.json`
Obtain the default username and password for the service using the
/provisioning/v1/services/${SKYSQL_SERVICE}/security/credentialsAPI endpoint and save the response to theresponse-credentials.jsonfile:
curl -sS --location --request GET \
--header "X-API-Key: ${API_KEY}" \
--header "Accept: application/json" \
--header "Content-type: application/json" \
https://api.skysql.com/provisioning/v1/services/${SKYSQL_SERVICE}/security/credentials \
| tee response-credentials.json | jq .The default username and password will not be available until the service state is "ready".
Set the file's mode to only allow the current user to read its contents:
$ chmod 600 response-credentials.jsonRead the username and password from
response-credentials.jsonand save them to theSKYSQL_USERNAMEandSKYSQL_PASSWORDenvironment variables:$ export SKYSQL_USERNAME=`jq -r .username response-credentials.json` $ export SKYSQL_PASSWORD=`jq -r .password response-credentials.json`
Connect
Connect to the database using the host, port, and default credentials using the mariadb client:
mariadb --host ${SKYSQL_FQDN} --port ${SKYSQL_PORT} \
--user ${SKYSQL_USERNAME} --password="${SKYSQL_PASSWORD}" \
--ssl-verify-server-cert If you don't want the password to appear on the command-line, specify the --password command-line option without an argument to be prompted for a password.
Save Connection Information
To connect to your MariaDB Cloud service easily, it is possible to create a .my.cnf file in your home directory that contains all the details of your connection.
Use the following command to create a new
.my.cnffile or overwrite an existing one and populates it with the connection information that was collected in the previous steps:
cat > ~/.my.cnf <<EOF
[client]
host=${SKYSQL_FQDN}
port=${SKYSQL_PORT}
user=${SKYSQL_USERNAME}
password="${SKYSQL_PASSWORD}"
EOFSet the file system permissions for the
.my.cnffile to ensure that other users can't read it:$ chmod 600 ~/.my.cnfWhen all the connection parameters are in your
~/.my.cnffile, the mariadb client can connect without specifying any command-line options:$ mariadb
Resources
Last updated
Was this helpful?

