MySQL Router Tutorial - Deploy MySQL Router

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

:tipping_hand_man: Tip: To exit the screen with juju status --watch 1s, enter Ctrl+c. If you want to further inspect juju logs, can watch for logs with juju 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)

:tipping_hand_man: Tip: if at any point you’d like to leave the MySQL client, enter Ctrl+d or type exit.

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.

1 Like

hi, after deploying Charmed MySQL + MySQL Router cannot connect to the database, even from the localhost.

the strange is that data-integrator has file endpoint only, no ip (even 127.0.0.1)

from the server application cannot connect too

ubuntu@db-test-2:~$ mysql -h 127.0.0.1 -P6446 -urelation-18-19 -peCF9K2WcNpJ9VbcKM1QMRyuj test123

mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2003 (HY000): Can’t connect to remote MySQL server

ubuntu@db-test-2:~$ mysql -h localhost -P6446 -urelation-18-19 -peCF9K2WcNpJ9VbcKM1QMRyuj -S /var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock test123

mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2003 (HY000): Can’t connect to remote MySQL server

Dear @macchese , thank you for the question!

The current behavior you noticing is an expected design behavior. :slight_smile:

The VM charm logic for Juju applications: use UNIX socket for local mysql-router access. The UNIX socket has list of benefits comparing to TCP socket. This is why mysql-router is not listening TCP there. The mysql-router is a subordinated VM charm and principal charms should use UNIX socket.

It obliviously doesn’t work for non-Juju applications (remote TCP connection from outside). Recently we have expanded this for pgbouncer (HA for PostgreSQL) and pre-planned for MySQL Router as well. The data-integrator requests TCP using the flag external-node-connectivity=True.

Please stay tuned for MySQL Router VM charm!

thank you Alex, maybe I’m wrong but I’m trying to connect by unix socket: mysql -urelation-18-19 -peCF9K2WcNpJ9VbcKM1QMRyuj -S /var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock test123

mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2003 (HY000): Can’t connect to remote MySQL server

where: ls -al /var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock srwxrwxrwx 1 snap_daemon snap_daemon 0 Feb 28 20:27 /var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock

@macchese , I cannot reproduce your issue on my side. TL;DR: I am constantly able to connect using UNIX socket.

Full trace:

ubuntu@juju340:~$ juju status
Model  Controller  Cloud/Region         Version  SLA          Timestamp
lxd3   lxd         localhost/localhost  3.4.0    unsupported  00:15:57+01:00

Model "admin/lxd3" is empty.
ubuntu@juju340:~$ juju deploy mysql --config profile=testing
Deployed "mysql" from charm-hub charm "mysql", revision 196 in channel 8.0/stable on ubuntu@22.04/stable
ubuntu@juju340:~$ juju deploy data-integrator
Deployed "data-integrator" from charm-hub charm "data-integrator", revision 19 in channel stable on ubuntu@22.04/stable
ubuntu@juju340:~$ juju deploy mysql-router --channel dpe/edge
Deployed "mysql-router" from charm-hub charm "mysql-router", revision 138 in channel dpe/edge on ubuntu@22.04/stable
ubuntu@juju340:~$ juju relate mysql-router mysql
ubuntu@juju340:~$ juju relate mysql-router data-integrator
ubuntu@juju340:~$ juju config data-integrator database-name=test123
ubuntu@juju340:~$ juju wait-for application data-integrator
ubuntu@juju340:~$ juju run data-integrator/0 get-credentials
Running operation 1 with 1 task
  - task 2 on unit-data-integrator-0

Waiting for task 2...
mysql:
  data: '{"database": "test123", "external-node-connectivity": "true", "requested-secrets":
    "[\"username\", \"password\", \"tls\", \"tls-ca\", \"uris\"]"}'
  database: test123
  endpoints: file:///var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock
  password: YHgYJTAYozrx1d2PRHAZgjg6
  read-only-endpoints: file:///var/snap/charmed-mysql/common/run/mysqlrouter/mysqlro.sock
  username: relation-4-5
ok: "True"

ubuntu@juju340:~$ juju ssh data-integrator/0 bash
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@juju-d06d02-1:~$ charmed-mysql.mysql -S /var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock -u relation-4-5 -pYHgYJTAYozrx1d2PRHAZgjg6 test123 -e "select now()"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------+
| now()               |
+---------------------+
| 2024-02-28 23:19:39 |
+---------------------+
ubuntu@juju-d06d02-1:~$ 
exit
Connection to 10.184.219.6 closed.

P.S. juju status:

ubuntu@juju340:~$ juju status
Model  Controller  Cloud/Region         Version  SLA          Timestamp
lxd3   lxd         localhost/localhost  3.4.0    unsupported  00:21:07+01:00

App              Version          Status  Scale  Charm            Channel     Rev  Exposed  Message
data-integrator                   active      1  data-integrator  stable       19  no       
mysql            8.0.34-0ubun...  active      1  mysql            8.0/stable  196  no       
mysql-router     8.0.35-0ubun...  active      1  mysql-router     dpe/edge    138  no       

Unit                Workload  Agent  Machine  Public address  Ports           Message
data-integrator/0*  active    idle   1        10.184.219.6                    
  mysql-router/0*   active    idle            10.184.219.6                    
mysql/0*            active    idle   0        10.184.219.231  3306,33060/tcp  Primary

Machine  State    Address         Inst id        Base          AZ  Message
0        started  10.184.219.231  juju-d06d02-0  ubuntu@22.04      Running
1        started  10.184.219.6    juju-d06d02-1  ubuntu@22.04      Running
ubuntu@juju340:~$

The new data-integrator has been released today (rev 13 vs 19) but it should have no effect here.

I have also installed mysql client from apt… still works well:

ubuntu@juju-d06d02-1:~$ sudo apt install mysql-client-core-8.0
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  mysql-client-core-8.0
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 2692 kB of archives.
After this operation, 62.0 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 mysql-client-core-8.0 amd64 8.0.36-0ubuntu0.22.04.1 [2692 kB]
Fetched 2692 kB in 1s (2206 kB/s)             
Selecting previously unselected package mysql-client-core-8.0.
(Reading database ... 34019 files and directories currently installed.)
Preparing to unpack .../mysql-client-core-8.0_8.0.36-0ubuntu0.22.04.1_amd64.deb ...
Unpacking mysql-client-core-8.0 (8.0.36-0ubuntu0.22.04.1) ...
Setting up mysql-client-core-8.0 (8.0.36-0ubuntu0.22.04.1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...                                                                                                                                                                                                                                  
Scanning candidates...                                                                                                                                                                                                                                 

Restarting services...
Service restarts being deferred:
 systemctl restart packagekit.service
 systemctl restart ssh.service
 systemctl restart systemd-journald.service
 systemctl restart systemd-logind.service
 /etc/needrestart/restart.d/systemd-manager
 systemctl restart systemd-networkd.service
 systemctl restart systemd-resolved.service
 systemctl restart systemd-udevd.service
 systemctl restart user@1000.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
ubuntu@juju-d06d02-1:~$ 


ubuntu@juju-d06d02-1:~$ which mysql
/usr/bin/mysql
ubuntu@juju-d06d02-1:~$ which charmed-mysql.mysql
/snap/bin/charmed-mysql.mysql


ubuntu@juju-d06d02-1:~$ charmed-mysql.mysql -S /var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock -u relation-4-5 -pYHgYJTAYozrx1d2PRHAZgjg6 test123 -e "select now()"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------+
| now()               |
+---------------------+
| 2024-02-28 23:25:52 |
+---------------------+

ubuntu@juju-d06d02-1:~$ mysql -S /var/snap/charmed-mysql/common/run/mysqlrouter/mysql.sock -u relation-4-5 -pYHgYJTAYozrx1d2PRHAZgjg6 test123 -e "select now()"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------+
| now()               |
+---------------------+
| 2024-02-28 23:25:56 |
+---------------------+
ubuntu@juju-d06d02-1:~$ 

Ideas?

thank alex, maybe I found something interesting after looking into mysql router logs, It seems to be a dns resolver problem. Mysql router tryies to boostrap to innodb cluster members using hostname and not (public) ip, so it fails because I’m not using lxd

maybe if boostrap used the public ip address it would work, or not?

I agree with your finding. On the screenshot router is trying to connect mysql-1 hostname which is unresolvable. Report it as an official issue to https://github.com/canonical/mysql-operator with steps to reproduce and the cloud in use (what is op1?). I suspect it is Server operator issue (as Router received hostnames in relation from Server).

BTW, you can try to relate data-integrator to mysql server directly to re-check if it works well OR unsolvable hostnames received as well in relation.

Update: moved to https://github.com/canonical/mysql-operator/issues/404

I set up a VM manual cloud with 3 mysql, 3 controllers and an application VM where I deployed data-integrator and mysql-router