Connecting to an MySQL® cluster from applications
This section provides settings for connection to Managed Service for MySQL® cluster hosts with the help of command line tools, graphical IDEs, Yandex WebSQL, or a Docker container. To learn how to connect from your application code, see Code examples.
Command line tools
To see code examples with the host FQDN filled in, open the cluster page in the management console
Linux (Bash)/macOS (Zsh)
Before connecting, install the mysql utility:
sudo apt update && sudo apt install --yes mysql-client
mysql --host=<MySQL®_host_name>.mdb.yandexcloud.net \
--port=3306 \
--ssl-ca=~/.mysql/root.crt \
--ssl-mode=VERIFY_IDENTITY \
--user=<username> \
--password \
<DB_name>
mysql --host=<FQDN_of_any_MySQL®_host> \
--port=3306 \
--ssl-mode=DISABLED \
--user=<username> \
--password \
<DB_name>
To learn how to get a host FQDN, see this guide.
When running any command, enter the database user password.
Once connected to the DBMS, run the SELECT version(); command.
If your cluster connection and test query are successful, you will see the MySQL® version.
Windows (PowerShell)
Before connecting, downloadMySQL Shell.
mysqlsh --host=<FQDN_of_any_MySQL®_host> `
--port=3306 `
--ssl-ca=<absolute_path_to_certificate_file> `
--ssl-mode=VERIFY_IDENTITY `
--user=<username> `
--password `
--database=<DB_name> `
--sql
mysqlsh --host=<FQDN_of_any_MySQL®_host> `
--port=3306 `
--ssl-mode=DISABLED `
--user=<username> `
--password `
--database=<DB_name>
To learn how to get a host FQDN, see this guide.
When running any command, enter the database user password.
Once connected to the DBMS, run the SELECT version(); command.
If your cluster connection and test query are successful, you will see the MySQL® version.
Connecting with IAM authentication
You can connect to a Managed Service for MySQL® database via the Yandex Cloud CLI using IAM authentication. This method is available to Yandex accounts, federated accounts, and local users. When connecting with IAM authentication, you do not need to obtain an SSL certificate or specify the cluster hosts’ FQDNs.
Before connecting, install the MySQL® client:
sudo apt update && sudo apt install --yes mysql-client
Set up your Managed Service for MySQL® cluster for connection:
- Navigate to the Managed Service for MySQL service.
- Click the name of your cluster.
- Enable public access for the cluster hosts:
- Select the Hosts tab.
- Click
in the first host's row and select Edit. - Enable Public access.
- Repeat the same for the remaining hosts in the cluster.
- Assign the following role to the service account of the user that will connect to the database:
- Select the Access bindings tab and click Assign roles.
- Enter the user's email the service account is associated with.
- Click
Add role and select themanaged-mysql.clusters.connectorrole. - Click Save.
- Create a user named MySQL®:
- Select the Users tab.
- Click Create user.
- Select IAM as the authentication method.
- Select the account with the
managed-mysql.clusters.connectorrole. - Click Add database and select the database from the drop-down list.
- Click
in the Roles column and select the privilege from the drop-down list. - Click Create.
To connect to the Managed Service for MySQL® database, run this command:
yc managed-mysql connect <cluster_name_or_ID> --db <DB_name>
Connecting from graphical IDEs
Connections were tested in the following environment:
- Ubuntu 20.04, DBeaver:
22.2.4 - MacOS Monterey 12.7:
- JetBrains DataGrip:
2023.3.4 - DBeaver Community:
24.0.0
- JetBrains DataGrip:
From graphical IDEs, you can only connect to public cluster hosts using an SSL certificate.
To avoid connection errors, save the certificate
- Create a data source:
- Select File → New → Data Source → MySQL®.
- On the General tab:
- Configure the connection as follows:
- Host: FQDN of any MySQL® host or a special FQDN.
- Port:
3306. - User, Password: Database user name and password.
- Database: Name of the database to connect to.
- Click Download to download the connection driver.
- Configure the connection as follows:
- On the SSH/SSL tab:
- Enable Use SSL.
- In the CA file field, specify the path to the SSL certificate file for your connection.
- Click Test Connection. If the connection is successful, you will see the connection status, DBMS information, and driver details.
- Click OK to save the data source.
- Create a new DB connection:
- In the Database menu, select New connection.
- Select MySQL® from the DB list.
- Click Next.
- Specify the connection settings on the Main tab:
- Server host: FQDN of any MySQL® host or a special FQDN.
- Port:
3306. - Database: Target database name.
- Username, Password: Database user name and password.
- On the SSL tab:
- Enable Use SSL.
- In the CA certificate field, specify the path to the SSL certificate file you previously saved.
- Under Advanced:
- Enable Require SSL.
- Enable Verify server certificate.
- Click Test Connection .... If the connection is successful, you will see the connection status and information about the DBMS and driver.
- Click Done to save the database connection settings.
Connecting from Yandex WebSQL
You can use Yandex WebSQL to send SQL queries to Managed Service for MySQL® cluster databases.
WebSQL is a Yandex Cloud service that enables you to connect to managed database clusters, work with databases, tables, and schemas, and run queries. It is a web-based tool that requires no additional authorization and simplifies working with SQL commands by prompting the user.
To connect from WebSQL, activate the WebSQL access option in the cluster settings. You can enable it when creating or updating a cluster.
In the Managed Service for MySQL® cluster, a Connection Manager connection is automatically created for each database user, which you can use to connect to the database from WebSQL. If required, you can also create a new connection.
To connect to the database from WebSQL:
-
In the management console
, select the folder with the cluster you need. -
Go to Managed Service for MySQL.
-
Click the cluster name and go to the WebSQL tab.
-
Select the required connection.
The connection will open in WebSQL. To go to the query editor, select the required database.
Check this MySQL®
reference for supported queries.
For more information on how to work with WebSQL, see these guides.
Before you connect from a Docker container
To connect to a Managed Service for MySQL® cluster from a Docker container, add the following lines to the Dockerfile:
RUN apt-get update && \
apt-get install mysql-client --yes
RUN apt-get update && \
apt-get install wget mysql-client --yes && \
mkdir --parents ~/.mysql && \
wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \
--output-document ~/.mysql/root.crt && \
chmod 0600 ~/.mysql/root.crt