Accessing a Tanzu MySQL for Kubernetes Instance
Warning: VMware Tanzu™ SQL with MySQL for Kubernetes is currently in beta and is intended for evaluation and test purposes only. Do not use this product in a production environment. If you discover any bugs, contact Support.
Page last updated:
This topic describes how to access a VMware Tanzu™ SQL with MySQL for Kubernetes instance.
Prerequisites
Before you access a Tanzu MySQL for Kubernetes instance, you must have:
The Kubernetes Command Line Interface (kubectl) installed. For more information, see the Kubernetes documentation.
An existing Tanzu MySQL for Kubernetes instance
(Optional) Verify MySQL Server Settings
To see all MySQL server settings configured:
Log in to the Kubernetes namespace by running:
kubectl -n YOUR-NAMESPACE exec --stdin --tty pod/INSTANCE-NAME-0 -- /bin/bash
Where:
YOUR-NAMESPACE
is the namespace you are logging in to.INSTANCE-NAME
is the name of the instance you are targeting.
For example:
$ kubectl -n my-namespace exec --stdin --tty pod/my-instance-0 -- /bin/bash
Review the configuration files named
/etc/mysql/conf.d/base.cnf
and/etc/mysql/conf.d/autotune.cnf
.
Log in to the MySQL Server
To log in to the MySQL server:
Log in to the Kubernetes namespace by running:
kubectl -n YOUR-NAMESPACE exec --stdin --tty pod/INSTANCE-NAME-0 -- /bin/bash
Where:
YOUR-NAMESPACE
is the namespace you are logging in to.INSTANCE-NAME
is the name of the instance you are targeting.
For example:
$ kubectl -n my-namespace exec --stdin --tty pod/my-instance-0 -- /bin/bash
Log in to the MySQL server by running:
mysql -uroot -p$(cat $MYSQL_ROOT_PASSWORD_FILE)
For example:
$ mysql -uroot -p$(cat $MYSQL_ROOT_PASSWORD_FILE)
Connect to the MySQL Server with the Kubernetes API Server
Note: Connecting with kubectl port-forward
proxies
the connection through the API server. It is intended for debugging access, and
its performance is slower than a direct connection.
To connect to the MySQL server with the Kubernetes API server:
Get the root password. The root user password is available in the Kubernetes Secret named after the TanzuMySQL instance suffixed with
-credentials
.MYSQL_ROOT_PASSWORD=$(kubectl get secret INSTANCE-NAME-credentials -o go-template='{{.data.rootPassword | base64decode}}')
Where
INSTANCE-NAME
is the name of your instance.Start a port-forwarding proxy with
kubectl port-forward
. For more information about the command, see Kubernetes documentation.kubectl port-forward service/INSTANCE-NAME 3306 &
For example:
$ kubectl port-forward service/tanzumysql-sample 3306 &
Forwarding from 127.0.0.1:3306 -> 3306 Forwarding from [::1]:3306 -> 3306Connect to the service through the poxy port. For example, to connect with the MySQL client:
$ mysql -h 127.0.0.1 -u root -P 3306 -p${MYSQL_ROOT_PASSWORD}
When you have finished with the connection, stop the proxy by stopping the kubectl process.
For example:
$ jobs [1]+ Running kubectl port-forward service/tanzumysql-sample 3306 &
$ kill %1
Connect an In-Cluster App to the MySQL Server
Tanzu MySQL for Kubernetes creates a Kubernetes Service that is used to connect apps to the database from within the Kubernetes cluster. The name of the Service is the same as the name of the Tanzu MySQL for Kubernetes instance.
Log in to the MySQL server. See Log in to the MySQL Server above.
Create a database and user for the app to use.
Provide the DNS name of the Tanzu MySQL for Kubernetes instance service to the app, along with the username and password created for the app user.
Example Deployment of Bitnami Wordpress with Tanzu MySQL for Kubernetes
You can configure the Bitnami Wordpress Helm chart to connect to an external database instead of deploying its own database. For more information about the Helm chart, see charts/bitnami/wordpress in GitHub.
See the following example of setting up Tanzu MySQL for Kubernetes with a database and user that you can use with the Wordpress Helm chart:
$ kubectl exec -it tanzumysql-sample-0 -c mysql -- bash mysql@tanzumysql-sample-0:/$ mysql -p$(cat $MYSQL_ROOT_PASSWORD_FILE) -u root mysql> CREATE DATABASE bitnami_wordpress; Query OK, 1 row affected (0.20 sec) mysql> CREATE USER 'bn_wordpress'@'%' IDENTIFIED BY 'hunter2'; Query OK, 0 rows affected (0.08 sec) mysql> GRANT ALL PRIVILEGES ON bitnami_wordpress.* TO 'bn_wordpress'@'%'; Query OK, 0 rows affected (0.03 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.10 sec)
Next, the DNS name of the TanzuMySQL service is
passed in to the chart as the value of externalDatabase.host
:
$ helm repo add bitnami https://charts.bitnami.com/bitnami "bitnami" has been added to your repositories
$ helm install wp bitnami/wordpress \ --set mariadb.enabled=false \ --set externalDatabase.host=tanzumysql-sample.default.svc.cluster.local \ --set externalDatabase.password=hunter2 NAME: wp LAST DEPLOYED: Tue Dec 8 14:32:08 2020 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: ** Please be patient while the chart is being deployed ** ...
Retrieve the Wordpress user password by running:
$ kubectl get secret wp-wordpress -o go-template='{{index .data "wordpress-password" | base64decode}}'