---
title: "Connect to a MySQL sync cluster"
sidebar_label: "Connect to a cluster"
sidebar_position: 8
description: "How to connect to a MySQL sync cluster with or without an SSL certificate in the console and from various programming languages"
---

import Formbricks from '@theme/MDXComponents/Formbricks'
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import {TabItemLabel} from '@selectel/docux/components'
import ConnectionAddresses from '@site/i18n/en/docusaurus-plugin-content-docs/current/_partials/managed-databases/common/connection-addresses.mdx'

# Connect to a MySQL sync cluster

You can connect to a MySQL sync cluster:

* via the mysql console client;
* graphical database management tools: [phpMyAdmin](#connect-via-phpmyadmin), MySQL Workbench, Sequel Pro, and others;
* program code.

For all methods, you can connect [with SSL](#connect-with-ssl) or [without SSL](#connect-without-SSL).

When connecting, specify the [port](#connection-ports) and [address](#connection-addresses).

[The number of connections](#number-of-connections) for a cluster is limited by the amount of RAM.

## Connection ports \{#connection-ports}

Use port 6033 to connect to ProxySQL.

ProxySQL automatically distributes requests between cluster nodes.

## Connection addresses \{#connection-addresses}

<ConnectionAddresses DatabaseName="MySQL sync" slug="mysql-sync" />

## Number of connections \{#number-of-connections}

The number of connections for a MySQL sync cluster is determined by the amount of RAM for the database cluster nodes. 50 connections are available per 1 GB of RAM. For example, for a cluster with 4 GB of RAM, 200 connections are available, and for 6 GB of RAM, 300 connections.

To increase the number of connections, [scale the cluster](/managed-databases/mysql-sync/resize-cluster.mdx) to the required amount of RAM.

## Connect via SSL \{#connect-with-ssl}

Connecting using TLS (SSL) encryption ensures a secure connection between your server and the database cluster.

<Tabs queryString="connect-with-ssl">
  <TabItem value="bash" default>
    <TabItemLabel>
      Bash
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.mysql/` folder:

       ```bash
       mkdir -p ~/.mysql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.mysql/root.crt
       chmod 0600 ~/.mysql/root.crt
       ```

    2. Connect to the cluster:

       ```bash
       mysql --host=<host> \
             --port=<port> \
             --user=<database_user_name> \
             --password \
             --database=<database_name> \
             --ssl-ca=~/.mysql/root.crt \
             --ssl-mode=verify_ca
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<database_user_name>` — database user name;
       * `<database_name>` — database name.
  </TabItem>

  <TabItem value="powershell">
    <TabItemLabel>
      PowerShell
    </TabItemLabel>

    1. In the [Control panel](https://my.selectel.ru/vpc/default/dbaas/), click **Download certificate** to download the root certificate, and place it in the `%APPDATA%\mysql\` folder.
    2. Connect to the cluster:

       ```bash
       mysql --host=<host> `
             --port=<port> `
             --user=<database_user_name> `
             --password `
             --database=<database_name> `
             --ssl-ca=%APPDATA%\mysql\CA.pem\ `
             --ssl-mode=verify_ca
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<database_user_name>` — database user name;
       * `<database_name>` — database name.
  </TabItem>

  <TabItem value="python">
    <TabItemLabel>
      Python
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.mysql/` folder:

       ```bash
       mkdir -p ~/.mysql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.mysql/root.crt
       chmod 0600 ~/.mysql/root.crt
       ```
    2. Install the PyMySQL library:

       ```bash
       pip3 install PyMySQL
       ```
    3. Use the following connection example:

       ```python
       import pymysql.cursors

       connection = pymysql.connect(
           host='<host>',
           user='<database_user_name>',
           password='<password>',
           database='<database_name>',
           ssl_ca='<path>',
           ssl_verify_cert=True,
           port=<port>,
           cursorclass=pymysql.cursors.DictCursor
         )

       with connection:
           with connection.cursor() as cursor:
               cursor.execute("SELECT 40 + 2 AS sum")
               result = cursor.fetchone()
               print(result)
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<database_name>` — database name;
       * `<path>` — full path to the root certificate;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="php">
    <TabItemLabel>
      PHP
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.mysql/` folder:

       ```bash
       mkdir -p ~/.mysql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.mysql/root.crt
       chmod 0600 ~/.mysql/root.crt
       ```

    2. Install the mysqli library:

       ```bash
       apt install php-mysqli
       ```

    3. Use the following connection example:

       ```php
       <?php
       $mysqli = mysqli_init();
       if (!$mysqli) {
           die("mysqli_init failed");
       }
       $mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
       $mysqli->ssl_set(NULL, NULL, "<path>", NULL, NULL);

       if (
           !$mysqli->real_connect(
               "master.77c6916d-8f64-45d0-81fb-a5343bb2ff1c.c.dbaas.selcloud.ru",
               "<database_user_name>",
               "<password>",
               "<database_name>",
               <port>
           )
       ) {
           die("Connect Error: " . mysqli_connect_error());
       }

       $result = $mysqli->query("SELECT 40 + 2");

       $row = $result->fetch_row();

       echo "Result: $row[0]";

       $result->close();
       $mysqli->close();

       ?>
       ```

       Specify:

       * `<path>` — full path to the root certificate;
       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<database_name>` — database name;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="go">
    <TabItemLabel>
      Go
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.mysql/` folder:

       ```bash
       mkdir -p ~/.mysql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.mysql/root.crt
       chmod 0600 ~/.mysql/root.crt
       ```

    2. Use the following connection example:

       ```go
       package main

       import (
           "crypto/tls"
           "crypto/x509"
           "database/sql"
           "fmt"
           "github.com/go-sql-driver/mysql"
           "io/ioutil"
       )

       func main() {
           rootCertPool := x509.NewCertPool()
           pem, err := ioutil.ReadFile("<path>")
           if err != nil {
               panic(err)
           }
           if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
               panic("Failed to append PEM.")
           }
           mysql.RegisterTLSConfig("custom", &tls.Config{
               RootCAs: rootCertPool,
           })

           connectionString := fmt.Sprintf("%s:%s@tcp(%s:<port>)/%s?tls=custom",
               "<database_user_name>",
               "<password>",
               "<host>",
               "<database_name>",
           )

           db, err := sql.Open("mysql", connectionString)
           if err != nil {
               panic(err)
           }
           defer db.Close()

           var sum int64
           err = db.QueryRow("SELECT 40+2").Scan(&sum)
           if err != nil {
               panic(err)
           }
           fmt.Println(sum)

       }
       ```

       Specify:

       * `<path>` — full path to the root certificate;
       * `<port>` — [connection port](#connection-ports);
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<database_name>` — database name.
  </TabItem>

  <TabItem value="nodejs">
    <TabItemLabel>
      Node.js
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.mysql/` folder:

       ```bash
       mkdir -p ~/.mysql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.mysql/root.crt
       chmod 0600 ~/.mysql/root.crt
       ```

    2. Install the mysql2 library:

       ```bash
       npm install mysql2
       ```

    3. Use the following connection example:

       ```js
       const fs = require('fs');
       const mysql = require('mysql2');

       const config = {
         host: '<host>',
         port: <port>,
         database: '<database_name>',
         user: '<database_user_name>',
         password: '<password>',
         ssl: {
           rejectUnauthorized: true,
           ca: fs.readFileSync('<path>').toString(),
         },
       };

       const connection = mysql.createConnection(config);

       connection.query('SELECT 40 + 2 AS sum', (error, res) => {
         if (error) throw error;
         console.log(res);
         connection.end();
       });
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<path>` — full path to the root certificate.
  </TabItem>
</Tabs>

## Connect without SSL \{#connect-without-SSL}

<Tabs queryString="connect-without-ssl">
  <TabItem value="bash" default>
    <TabItemLabel>
      Bash
    </TabItemLabel>

    1. Open the CLI.
    2. Connect to the cluster:

       ```bash
       mysql --host=<host> \
             --port=<port> \
             --user=<database_user_name> \
             --password \
             --database=<database_name> \
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<database_user_name>` — database user name;
       * `<database_name>` — database name.
  </TabItem>

  <TabItem value="powershell">
    <TabItemLabel>
      PowerShell
    </TabItemLabel>

    1. Open the CLI.
    2. Connect to the cluster:

       ```bash
       mysql --host=<host> `
             --port=<port> `
             --user=<database_user_name> `
             --password `
             --database=<database_name>
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<database_user_name>` — database user name;
       * `<database_name>` — database name.
  </TabItem>

  <TabItem value="python">
    <TabItemLabel>
      Python
    </TabItemLabel>

    1. Install the PyMySQL library:

       ```bash
       pip3 install PyMySQL
       ```

    2. Use the following connection example:

       ```python
       import pymysql.cursors

       connection = pymysql.connect(
           host='<host>',
           user='<database_user_name>',
           password='<password>',
           database='<database_name>',
           port=<port>,
           cursorclass=pymysql.cursors.DictCursor
         )

       with connection:
           with connection.cursor() as cursor:
               cursor.execute("SELECT 40 + 2 AS sum")
               result = cursor.fetchone()
               print(result)
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<database_name>` — database name;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="php">
    <TabItemLabel>
      PHP
    </TabItemLabel>

    1. Install the mysqli library:

       ```bash
       apt install php-mysqli
       ```

    2. Use the following connection example:

       ```php
       <?php
       $conn = new mysqli(
           "<host>:<port>",
           "<database_user_name>",
           "<password>",
           "<database_name>"
       );

       if ($conn->connect_error) {
           die("ERROR: Unable to connect: " . $conn->connect_error);
       }

       $result = $conn->query("SELECT 40 + 2");

       $row = $result->fetch_row();

       echo "Result: $row[0]";

       $result->close();
       $conn->close();
       ?>
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<database_name>` — database name.
  </TabItem>

  <TabItem value="go">
    <TabItemLabel>
      Go
    </TabItemLabel>

    Use the following connection example:

    ```go
    package main

    import (
        "database/sql"
        "fmt"
        "github.com/go-sql-driver/mysql"
    )

    func main() {
        connectionString := fmt.Sprintf("%s:%s@tcp(%s:<port>)/%s",
            "<database_user_name>",
            "<password>",
            "<host>",
            "<database_name>",
        )

        db, err := sql.Open("mysql", connectionString)
        if err != nil {
            panic(err.Error())
        }
        defer db.Close()

        var sum int64
        err = db.QueryRow("SELECT 40+2").Scan(&sum)
        if err != nil {
            panic(err.Error())
        }
        fmt.Println(sum)

    }
    ```

    Specify:

    * `<port>` — [connection port](#connection-ports);
    * `<database_user_name>` — database user name;
    * `<password>` — user password;
    * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
    * `<database_name>` — database name.
  </TabItem>

  <TabItem value="nodejs">
    <TabItemLabel>
      Node.js
    </TabItemLabel>

    1. Install the mysql2 library:

       ```bash
       npm install mysql2
       ```

    2. Use the following connection example:

       ```js
       const mysql = require('mysql2');

       const config = {
         host: '<host>',
         port: <port>,
         database: '<database_name>',
         user: '<database_user_name>',
         password: '<password>',
       };

       const connection = mysql.createConnection(config);

       connection.query('SELECT 40 + 2 AS sum', (error, res) => {
         if (error) throw error;
         console.log(res);
         connection.end();
       });
       ```

       Specify:

       * `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
       * `<port>` — [connection port](#connection-ports);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name;
       * `<password>` — user password.
  </TabItem>
</Tabs>

## Connect via phpMyAdmin \{#connect-via-phpmyadmin}

Add the following lines to the `/etc/phpmyadmin/config.inc.php` configuration file:

```php
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['host'] = '<host>';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['port'] = '<port>';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = false;
```

Specify:

* `<host>` — the DNS address or [public IP address](/managed-databases/mysql-sync/public-ip.mdx) (Floating IP) of the node;
* `<port>` — [connection port](#connection-ports).

<Formbricks />
