MySQL SSL connection

60 views
Skip to first unread message

Razvan Anastasescu

unread,
Aug 1, 2020, 9:54:02 AM8/1/20
to redbeanphp
Hello everyone,

I cannot find a proper way / example on how to connect to MySQL using SSL (certificates)

I've opened an issue on GitHub as well about this.

There is a Facade::useMySqlSSL() method which accepts paths to the certificates for a specific database toolbox, but I cannot figure it out how to properly make use of it.

If I do this:

R::setup($dsn, $user, $pass); // here the connection is not done yet
R::useMysqlSSL(null, null, $caCertPath); // the connection will be made before applying SSL options

I get a connection error because the useMysqlSSL() method it obtaining a handle for the PDO which first needs to connect, but the connection requires the SSL options upfront.

So I really miss the PDO options parameter !

Then I have tried another approach, as I see we have a Driver::setPDO() as follows.

R::setup(); // blank sqlite file

$pdo = new \PDO($dsn, $user, $pass, [
  \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8;SET time_zone = \'+00:00\'',
  \PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca/cert/file',
]);

$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);

R::getDatabaseAdapter()->getDatabase()->setPDO($pdo);

Here the problem is that when I try to do a query it first fires the Driver::connect() method which is replacing the pdo.

Then I have modified the Driver::setPDO() method in order to tell RedBean that it is connected:

public function setPDO(\PDO $pdo)
{
   $this->isConnected = true; // I have added this
   $this->pdo = $pdo;
}

But now I have an error like bellow which I think is due to multiple DB connections ($pdo & R).

PS: I have also tried to enable the use of buffered query, with the same effect.

Fatal error: Uncaught [HY000] - SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

As the last resort, I will definitely try to modify RedBean in order to accept PDO options parameter, otherwise I will get crazy for such a simple thing like securely connecting to a DataBase server (required by Azure)

Razvan Anastasescu

unread,
Aug 1, 2020, 2:38:47 PM8/1/20
to redbeanphp
Problem solved!

- useMysqlSSL() method was dropped and now there's possible to configure PDO connection options - see discussion and commits
- setPDO() was updated in order to allow full control - see discussion and commits
Reply all
Reply to author
Forward
0 new messages