Connect to Aiven for MySQL® from the command line
=================================================

Here, you can find how to connect to your MySQL via the command line with a choice of tools:

* :ref:`mysqlsh shell <connect-mysqlsh>`
* :ref:`mysql client <connect-mysql>`

.. _connect-mysqlsh:

Using ``mysqlsh``
-----------------

Variables
~~~~~~~~~

These are the placeholders you will need to replace in the code sample:

.. list-table::
  :header-rows: 1
  :widths: 15 60
  :align: left

  * - Variable
    - Description
  * - ``SERVICE_URI``
    - URL for the MySQL connection, from `Aiven Console <https://console.aiven.io/>`__ > the **Overview** page of your service

Prerequisites
~~~~~~~~~~~~~

For this example you need the ``mysqlsh`` client installed. You can install this by following the `MySQL shell installation documentation <https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-install.html>`_.

Code
~~~~

Execute the following from a terminal window to connect to the MySQL database:

.. code::

    mysqlsh --sql SERVICE_URI

You can execute this query to test:

.. code::

   MySQL ssl defaultdb SQL> select 1 + 2 as three;
   +-------+
   | three |
   +-------+
   |     3 |
   +-------+
   1 row in set (0.0539 sec)

Using ``mysql``
---------------

Variables
~~~~~~~~~

These are the placeholders you need to replace in the code sample:

.. list-table::
  :header-rows: 1
  :widths: 15 60
  :align: left

  * - Variable
    - Description
  * - ``USER_HOST``
    - Hostname for MySQL connection
  * - ``USER_PORT``
    - Port for MySQL connection
  * - ``USER_PASSWORD``
    - Password of your Aiven for MySQL connection
  * - ``DB_NAME``
    - Database Name of your Aiven for MySQL connection

.. _connect-mysql:

Prerequisites
~~~~~~~~~~~~~

For this example you need the ``mysql`` client installed. You can install it by following the `MySQL client installation documentation <https://dev.mysql.com/doc/refman/8.0/en/mysql.html>`_.

Code
~~~~

This step requires to manually specify individual parameters. You can find those parameters in `Aiven Console <https://console.aiven.io>`_ > the **Overview** page of your service.

Once you have these parameters, execute the following from a terminal window to connect to the MySQL database:

.. code::

   mysql --user avnadmin --password=USER_PASSWORD --host USER_HOST --port USER_PORT DB_NAME

.. warning::
   If you are providing the password via the command line, you must pass it as shown; putting a space between the parameter name and value will cause the password to be parsed incorrectly.