Welcome Guest, Not a member yet? Register   Sign In
Database Class : connecting to an SSL MySQL server
#1

[eluser]Matthieu Fauveau[/eluser]
Hi guys,

I had to make a remote connection to an SSL enabled MySQL server with CodeIgniter. Unfortunately, the database class doesn't include that functionnality yet. Here is the modification I have done to get this to work.

In the file /ci/database/drivers/mysqli/mysqli_driver.php, at line 63, replace:

Code:
return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);

by the following:

Code:
$init = mysqli_init();
mysqli_options($init, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
mysqli_ssl_set($init, '/etc/ssl/mysql/client-key.pem', '/etc/ssl/mysql/client-cert.pem', '/etc/ssl/mysql/ca-cert.pem', NULL, NULL);
@mysqli_real_connect($init, $this->hostname, $this->username, $this->password, $this->database, $this->port);
if(mysqli_connect_errno())
{
     return FALSE;
}
else
{    
     return $init;
}

Of course /etc/ssl/mysql/client-key.pem, client-cert.pem and ca-cert.pem needs to be replaced to match the location of your files.

Unfortunately this is not very handy because every time you will update CodeIgniter you will need to remember that you need to edit that file. It would be nice to have that possibility included in the next release of CI... don't you think ?

There is not much use of mysqli whithout SSL support otherwise...

Regards,
Matt.
#2

[eluser]Christophe Sautot[/eluser]
I was also looking to see if CodeIgniter supports connecting to a MySQL database with SSL, and at the moment it seems that it doesn't.

Thank you for posting your code example for the mysqli driver class.

It would be nice if they also modified the database/drivers/mysql/mysql_driver.php class around line 66, to allow for client_flags values such as to be passed in.

For use of client flags with the mysql_connect() PHP function:

http://us2.php.net/manual/en/function.mysql-connect.php

http://www.w3schools.com/PHP/func_mysql_connect.asp

Update:

From what I am reading it seems that SSL support with PHP's mysql_connect() function is really weak. If you can, you are better off using mysqli with either mysqli->ssl_set() or previous post's mysqli_ssl_set().
#3

[eluser]Matthieu Fauveau[/eluser]
Finally someone that miss this feature too !

Can't remember if I did make this into a feature request. I'll have to check.
#4

[eluser]Jamie Rumbelow[/eluser]
This is great Matthieu, thanks for the contribution! Moving to the Ignited Code forum so people can find it more easily.

Jamie
#5

[eluser]Matthieu Fauveau[/eluser]
By the way Jamie, congrats on being the new community chieftain.
#6

[eluser]Unknown[/eluser]
I made this changes and it works:

// return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
$mysqli = mysqli_init();

$mysqli->ssl_set('/etc/ssl/mysql/client-key.pem', '/etc/ssl/mysql/client-cert.pem', '/etc/ssl/mysql/ca-cert.pem', NULL, NULL);
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);

@$mysqli->real_connect($$this->hostname, $this->username, $this->password, $this->database);

return $mysqli;

Environment:
Codeigniter 2.1.3
PHP 5.3.10

database.php:
$db['default']['dbdriver'] = 'mysqli';




Theme © iAndrew 2016 - Forum software by © MyBB