VK Cloud logo
Updated at October 31, 2023   06:09 AM

Connecting to a database

You can connect to the database of instances deployed in VK Cloud using the SDK.

PostgreSQL

  1. Make sure that PHP and composer are installed.

  2. Install php-pgsql library.

  3. Connect to the database using the function pg_connect():

    $conn = pg_connect("host=<IP-ADDRESS> dbname=<DATABASE> user=<USERNAME> password=<PASSWORD>");

    Here:

    • <IP-ADDRESS> — external IP address of the DB instance;
    • <DATABASE> — database name;
    • <USERNAME> — DB user name;
    • <PASSWORD> — DB user password.

Read more about connecting to PostgreSQL in PHP in documentation.

MySQL

  1. Make sure that PHP and composer are installed.

  2. Install php-mysql library.

  3. Connect to the database using the function mysqli_real_connect():

    $conn = mysqli_init();mysqli_real_connect($conn, "<IP-ADDRESS>", "<USERNAME>", "<PASSWORD>", "<DATABASE>", <PORT>);

    Here:

    • <IP-ADDRESS> — external IP address of the DB instance;
    • <DATABASE> — database name;
    • <PORT> — connection port, standard — 3306;
    • <USERNAME> — DB user name;
    • <PASSWORD> — DB user password.

Read more about connecting to MySQL in PHP in documentation.

Tarantool

  1. Make sure that PHP and composer are installed.

  2. Install tarantool/client library.

  3. Connect to the database using the method Client::fromDsn:

    require_once __DIR__ . '/vendor/autoload.php';use Tarantool\Client\Client;$client = Client::fromDsn('tcp://<USERNAME>:PASSWORD>@<IP-ADDRESS>');

    Here:

    • <IP-ADDRESS> — external IP address of the DB instance;
    • <USERNAME> — DB user name;
    • <PASSWORD> — DB user password.

Read more about connecting to Tarantool in PHP in documentation.

ClickHouse

  1. Make sure that PHP and composer are installed.

  2. Install php-curl.

  3. Install smi2/phpclickhouse module.

  4. Connect to the database:

    require_once __DIR__ . '/vendor/autoload.php';$config = [    'host' => '<IP-ADDRESS>',    'port' => '<PORT>',    'username' => '<USERNAME>',    'password' => '<PASSWORD>'];$db = new ClickHouseDB\Client($config);$db->database('<DATABASE>');

    Here:

    • <IP-ADDRESS> — external IP address of the DB instance;
    • <DATABASE> — database name;
    • <PORT> — connection port, standard — 8123;
    • <USERNAME> — DB user name;
    • <PASSWORD> — DB user password.

MongoDB

  1. Make sure that PHP and composer are installed.

  2. Install PHP library for MongoDB.

  3. Connect to the database:

    $manager = new MongoDB\Driver\Manager("mongodb://<IP-ADDRESS>/<DATABASE>", array("username" => <USERNAME>, "password" => <PASSWORD>));

    Here:

    • <IP-ADDRESS> — external IP address of the DB instance;
    • <DATABASE> — database name;
    • <USERNAME> — DB user name;
    • <PASSWORD> — DB user password.

Redis

  1. Make sure that PHP and composer are installed.

  2. Install predis.

  3. Connect to the database:

    require_once __DIR__ . '/vendor/autoload.php';Predis\Autoloader::register();$client = new Predis\Client('tcp://<IP-ADDRESS>');

    Here:

    • <IP-ADDRESS> — external IP address of the DB instance;
    • <DATABASE> — database name;
    • <USERNAME> — DB user name;
    • <PASSWORD> — DB user password.