Get a MySQL Router up and running
This is part of the MySQL Router Tutorial. Please refer to this page for more information and the overview of the content. The following document will deploy “MySQL Router” together with “MySQL server” (coming from the separate charm “Charmed MySQL”).
Deploy Charmed MySQL + MySQL Router
To deploy Charmed MySQL + MySQL Router, all you need to do is run the following commands:
juju deploy mysql --channel 8.0
juju deploy mysql-router --channel dpe/edge
Juju will now fetch charms from Charmhub and begin deploying it to the LXD VMs. This process can take several minutes depending on how provisioned (RAM, CPU, etc) your machine is. You can track the progress by running:
juju status --watch 1s
This command is useful for checking the status of Juju applications and gathering information about the machines hosting them. Some of the helpful information it displays include IP addresses, ports, state, etc. The command updates the status of charms every second and as the application starts you can watch the status and messages of their change. Wait until the application is ready - when it is ready, juju status
will show:
TODO
Tip: To exit the screen with
juju status --watch 1s
, enterCtrl+c
. If you want to further inspect juju logs, can watch for logs withjuju debug-log
. More info on logging at juju logs.
At this stage MySQL Router will stay in blocked state due to missing relation/integration with MySQL DB, let’s integrate them:
juju integrate mysql mysql-router
Shortly the juju status
will report new blocking reason Missing relation: database
as it waits for a client to consume DB service, let’s deploy data-integrator and request access to database test123
:
juju deploy data-integrator --config database-name=test123
juju relate data-integrator mysql-router
In couple of seconds, the status will be happy for entire model:
TODO
Access database
The first action most users take after installing MySQL is accessing MySQL. The easiest way to do this is via the MySQL Command-Line Client mysql
. Connecting to the database requires that you know the values for host
, username
and password
. To retrieve the necessary fields please run data-integrator action get-credentials
:
juju run data-integrator/leader get-credentials
Running the command should output:
TODO
The host’s IP address can be found with juju status
(the application hosting the MySQL Router application):
...
TODO
...
To access the MySQL database via MySQL Router choose read-write (port 6446) or read-only (port 6447) endpoints:
mysql -h 10.152.183.52 -P6446 -urelation-4-6 -pNu7wK85QU7dpVX66X56lozji test123
Inside MySQL list DBs available on the host show databases
:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| performance_schema |
| test123 |
+--------------------+
3 rows in set (0.00 sec)
Tip: if at any point you’d like to leave the MySQL client, enter
Ctrl+d
or typeexit
.
You can now interact with MySQL directly using any MySQL Queries. For example entering SELECT VERSION(), CURRENT_DATE;
should output something like:
mysql> SELECT VERSION(), CURRENT_DATE;
+-------------------------+--------------+
| VERSION() | CURRENT_DATE |
+-------------------------+--------------+
| 8.0.34-0ubuntu0.22.04.1 | 2023-10-17 |
+-------------------------+--------------+
1 row in set (0.00 sec)
Feel free to test out any other MySQL queries. When you’re ready to leave the MySQL shell you can just type exit
. Now you will be in your original shell where you first started the tutorial; here you can interact with Juju and LXD.
Remove the user
To remove the user, remove the relation. Removing the relation automatically removes the user that was created when the relation was created. Enter the following to remove the relation:
juju remove-relation mysql-router data-integrator
Now try again to connect to the same MySQL Router you just used above:
mysql -h 10.152.183.52 -P6446 -urelation-4-6 -pNu7wK85QU7dpVX66X56lozji test123
This will output an error message:
ERROR 1045 (28000): Access denied for user 'relation-4-6'@'mysql-router-1.mysql-router-endpoints.tutorial.svc.clust' (using password: YES)
As this user no longer exists. This is expected as juju remove-relation mysql-router data-integrator
also removes the user.
Note: data stay remain on the server at this stage!
Relate the the two applications again if you wanted to recreate the user:
juju relate data-integrator mysql-router
Re-relating generates a new user and password:
juju run data-integrator/leader get-credentials
You can connect to the database with this new credentials. From here you will see all of your data is still present in the database.