@kalnin_yuri

Как подключится к базе данных в битриксе через ssl?

В яндекс облаке пишут такой способ подключения к БД битрикса

$conn = mysqli_init();
$conn->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$conn->ssl_set(NULL, NULL, '~/.mysql/root.crt', NULL, NULL);
$conn->real_connect('rc1a-cz4mil27ne5v5y6k.mdb.yandexcloud.net', '0000', '<0000>', '0000', 3306, NULL, MYSQLI_CLIENT_SSL);

в файлах .settings.php и dbconn.php таких настроек нет. что делать?
  • Вопрос задан
  • 558 просмотров
Решения вопроса 1
@kalnin_yuri Автор вопроса
Решил, дописав файлы в ядре. Странно то что в битриксе коннект к базе происходит в двух местах. По мимо класса указанного в .setting.php

в bitrix/modules/main/lib/db/mysqliconnection.php
mysql_connect заменил на

$connection = \mysqli_init();
$connection->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$connection->ssl_set(NULL, NULL, '/var/www/www-root/data/.mysql/root.crt', NULL, NULL);


и в bitrix/modules/main/classes/mysql/database_mysqli.php
$this->db_Conn = mysqli_connect($persistentPrefix.$dbHost, $this->DBLogin, $this->DBPassword, $this->DBName, $dbPort);


поменял на
$this->db_Conn = \mysqli_init();
$this->db_Conn->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$this->db_Conn->ssl_set(NULL, NULL, '/var/www/www-root/data/.mysql/root.crt', NULL, NULL);
$this->db_Conn->real_connect($persistentPrefix.$dbHost, $this->DBLogin, $this->DBPassword, $this->DBName, $dbPort);
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
AlexanderKs3
@AlexanderKs3
Похоже, такая возможность в Bitrix не заложена. По крайней мере, если посмотреть исходники, /bitrix/modules/main/lib/db/mysqliconnection.php, то в методе connectInternal() между mysqli_init() и real_connect() не предусмотрена установка options() и ssl_set(), только назначение нестандартного порта и MYSQLI_INIT_COMMAND, а в конце - подключение файла after_connect_d7.php, в которм указываются доп. инструкции после успешного коннекта к базе:
код function connectInternal()

protected function connectInternal()
{
	if ($this->isConnected)
		return;

	$host = $this->host;
	$port = 0;
	if (($pos = strpos($host, ":")) !== false)
	{
		$port = intval(substr($host, $pos + 1));
		$host = substr($host, 0, $pos);
	}
	if (($this->options & self::PERSISTENT) != 0)
		$host = "p:".$host;

	/** @var $connection \mysqli */
	$connection = \mysqli_init();
	if (!$connection)
		throw new ConnectionException('Mysql init failed');

	if (!empty($this->initCommand))
	{
		if (!$connection->options(MYSQLI_INIT_COMMAND, $this->initCommand))
			throw new ConnectionException('Setting mysql init command failed');
	}

	if ($port > 0)
		$r = $connection->real_connect($host, $this->login, $this->password, $this->database, $port);
	else
		$r = $connection->real_connect($host, $this->login, $this->password, $this->database);

	if (!$r)
	{
		throw new ConnectionException(
			'Mysql connect error ['.$this->host.']',
			sprintf('(%s) %s', $connection->connect_errno, $connection->connect_error)
		);
	}

	$this->resource = $connection;
	$this->isConnected = true;

	// nosql memcached driver
	if (isset($this->configuration['memcache']))
	{
		$memcached = \Bitrix\Main\Application::getInstance()->getConnectionPool()->getConnection($this->configuration['memcache']);
		mysqlnd_memcache_set($this->resource, $memcached->getResource());
	}

	$this->afterConnected();
}


В файле .settings.php определяется класс для подключения (className), возможно это пригодится для подключения своего, модиф. класса.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы