How to connect Mysql database over SSL in php? - php

I have a YII 1 application. And I try to connect with the Azure database.
I can connect with the azure database if I disable the option: Enforce SSL connection
But of course I want to enable this option.
But if I enable this option. I get this error:
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [9002] SSL connection is required. Please specify SSL options and retry.
CWebUser.php(146)
134
135 /**
136 * PHP magic method.
137 * This method is overridden so that persistent states can be accessed like properties.
138 * #param string $name property name
139 * #return mixed property value
140 */
141 public function __get($name)
142 {
143 if($this->hasState($name))
144 return $this->getState($name);
145 else
146 return parent::__get($name);
147 }
148
149 /**
150 * PHP magic method.
151 * This method is overridden so that persistent states can be set like properties.
152 * #param string $name property name
153 * #param mixed $value property value
154 * #throws CException
155 */
156 public function __set($name,$value)
157 {
158 if($this->hasState($name))
And my main.php in looks like this:
'db'=> [
'pdoClass' => 'NestedPDO',
'connectionString' => 'mysql:host=host_name;dbname=db_name',
'emulatePrepare' => true,
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'enableProfiling'=>true,
],
'cache'=>[
'class'=>'system.caching.CDbCache',
],
So my question is: what is missing in this part of configuration?
Thank you
if I try it like this:
'db'=> [
'pdoClass' => 'NestedPDO',
'connectionString' => 'mysql:host=host_name;dbname=db_name',
'emulatePrepare' => true,
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'enableProfiling'=>true,
'attributes' =>[
PDO::MYSQL_ATTR_SSL_CA => 'path ssl'
]
I get this error:
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] (trying to connect via (null))
and In mysql workbrench if I test the ssl certificat I get a successful response.

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] (trying to connect via (null))
As suggested by toffler, adding comment as a community wiki answer to help community members who might face a similar issue.
The error got resolved after adding PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false as per How to use SSL in PHP Data Objects (PDO) mysql

Related

php undefined property of an object

I'm sure this is more simple than I'm making it.
The error that I get...
Notice (8): Undefined property: Cake\Database\Connection::$config [... line 73]
The code on line 73...
Line 72: debug($conn);
Line 73: debug($conn->config);
Here's what $conn equals
$conn = object(Cake\Database\Connection) {
'config' => [
'password' => '*****',
'username' => '*****',
'host' => '*****',
'database' => '*****',
'driver' => 'Cake\Database\Driver\Postgres',
'persistent' => false,
'encoding' => 'utf8',
],
}
Anyone know why $conn->config isn't the right syntax?
There is no public property named $config only a protected $_config in the database connection class in Cake (http://api.cakephp.org/3.2/source-class-Cake.Database.Connection.html).
With the the database connection in Cake you must use the public function config() to get the configuration array.
$config = $conn->config();
I don't know what debug() does, but it's not returning a detailed representation of the properties of the object as would var_dump or as you could get from reflection. Don't rely on it to learn the interface of an object/class.

Strato Server PHP Zend PDOException Socket

I'm trying to connect to a database using a PDO Connection via Zend:
<?php
class Feuerwehr_Db extends Zend_Db {
public static function factoryEinsaetze(){
$config = array(
'LOCAL' => array(
'servername' => "localhost",
'username' => "root",
'password' => "admin",
'dbname' => "einsaetze"
),
'LIVE' => array(
'servername' => "rdbms.strato.de",
'username' => "U2340080",
'password' => "*****",
'dbname' => "DB2340080"
)
);
return parent::factory('Pdo_Mysql', $config[Feuerwehr_Environment::getStage()]);
}
}
The Connection selects all login data correctly and creates a DB connection. Everything works fine locally but on my Strato Server the Connection fails with the following exception:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)' in /mnt/web024/d1/57/5707957/htdocs/Beta/library/Zend/Db/Adapter/Pdo/Abstract.php:129 Stack trace: #0 /mnt/web024/d1/57/5707957/htdocs/Beta/library/Zend/Db/Adapter/Pdo/Abstract.php(129): PDO->__construct('mysql:servernam...', 'U2340080', '*****', Array) #1.....
The PHP Version on the server is 5.6 and a standard mysql_connect works perfectly.
Zend expected the key "host" and not "servername". Default param is set as localhost which explains the error exception.

Renaming `database.php.default` to `database.php`in `\app\Config\` in CAKEPHP gives Error

Renaming database.php.default to database.phpin \app\Config\ in CAKEPHP gives Error
I have installed a fresh copy of CAKEPHP 2.1.3 on XAMPP with Windows 7 and i have got an error
Warning (2): include_once(D:\xampp\htdocs\cakephp\example\app\Config\database.php) [function.include-once]: failed to open stream: No such file or directory [CORE\Cake\Model\ConnectionManager.php, line 68]
Warning (2): include_once() [function.include]: Failed opening 'D:\xampp\htdocs\cakephp\example\app\Config\database.php' for inclusion (include_path='D:\xampp\htdocs\cakephp\example\lib;.;D:\xampp\php\PEAR') [CORE\Cake\Model\ConnectionManager.php, line 68]
When i look into for database.php file in \app\config\ its not present
Instead, i found database.php.default in \app\Config\ and \lib\Cake\Console\Templates\skel\Config
And when i rename the database.php.default to database.php, it still gives me an error.
How do i need to connect to database and next procedure to go ahead.
Move config file from \lib\Cake\Console\Templates\skel\Config to D:\xampp\htdocs\cakephp\example\app\Config\
Or create new one with contents:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'username',
'password' => 'password',
'database' => 'databas name',
'prefix' => '',
//'encoding' => 'utf8',
);
}

jobberBase php installation is not working

I installed jobberBase using automatic installation (softaculous) on arvixe. As this was all automatic installation chances of any errors are rare. Still I did double checked that database got created fine; also the configuration looked fined.
I saw this errors on many other threads too but couldnt find a solution anywhere ; so looks like its a common installation problem. Most probably the php are not able to connect to DB ! Any help would be highly appreciated.
Warning: mysqli::mysqli() expects parameter 5 to be long, string given in /home/muskete3/public_html/jobber/_includes/class.Db.php on line 20
Warning: mysqli::query() [mysqli.query]: Couldn't fetch Db in /home/muskete3/public_html/jobber/_includes/class.Db.php on line 72
Warning: mysqli_error() [function.mysqli-error]: Couldn't fetch Db in /home/muskete3/public_html/jobber/_includes/class.Db.php on line 73
Warning: mysqli::query() [mysqli.query]: Couldn't fetch Db in /home/muskete3/public_html/jobber/_includes/class.Db.php on line 31
Warning: mysqli_error() [function.mysqli-error]: Couldn't fetch Db in /home/muskete3/public_html/jobber/_includes/class.Db.php on line 32
Fatal error: Call to a member function fetch_assoc() on a non-object in /home/muskete3/public_html/jobber/_includes/class.JobberSettings.php on line 23
My configuration file is :
<?php
/**
* Define environments
*
*/
// local (http://localhost/jobberbase/public)
$__instances['local'] = array(
// should be a unique part of the url (or the entire url if you wish)
'prefix' => 'www.jobs41.net',
// mysql credentials
'db_host' => 'localhost',
'db_port' => 3306,
'db_user' => '***',
'db_password' => '***',
'db_name' => '***',
'db_prefix' => '',
// your site's full url
'app_url' => 'http://www.jobs41.net/jobber/',
// error reporting
'ini_error_reporting' => E_ALL,
'ini_display_errors' => 'On',
// environment setting 1 (use 'local' for localhost/testing OR 'online' for live, production environment)
'location' => 'local',
// environment setting 2 (use 'dev' together with 'local' in the previous setting OR 'prod' with 'online')
'environment' => 'dev',
//'apache_mod_rewrite', 'iis_url_rewrite' -microsoft URL Rewrite module, 'iis_isapi_rewrite'
'rewrite_mode' => 'apache_mod_rewrite'
);
// live (http://www.yourjobberbasedomain.com)
$__instances['live'] = array(
'prefix' => 'www.jobs41.net',
'db_host' => 'localhost',
'db_port' => 3306,
'db_user' => 'muskete3_jobb563',
'db_password' => '6Sn82hxv7P',
'db_name' => 'muskete3_jobb563',
'db_prefix' => '',
'app_url' => 'http://www.jobs41.net/jobber/',
'ini_error_reporting' => E_ALL,
'ini_display_errors' => 'Off',
'location' => 'online',
'environment' => 'prod',
'rewrite_mode' => 'apache_mod_rewrite'
);
Previously I had installed it not as root directory; hence it was installed inside public_html/job.. Now when i installed it again in root directory it worked fine.

Error using Cake PHP SQL Server Driver

Upgraded a (dev) Cake PHP site and had to change database drivers from ADO to SQL Server's own driver as ADO is no longer supported in Cake 1.3, I . The new SQL Server driver is installed in PHP, shows in PHPinfo() and should work, but when I try to load a page using the database, I get this error:
Warning (2): sqlsrv_query() expects parameter 1 to be resource, boolean given
[APP\plugins\datasources\models\datasources\dbo\dbo_sqlsrv.php, line 184]
Warning (512): SQL Error: An invalid parameter was passed to sqlsrv_query.
[CORE\cake\libs\model\datasources\dbo_source.php, line 684]
Query: SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
What appears to be happening is my connection isn't established in the dbo_sqlsrv.php driver; a "$this->connection" variable is supposed to be the connection resource then passed to sqlsrv_query() and it's apparently a bool which is wrong (I tried to Echo the variable and it displays nothing).
This is where the connection SHOULD be set:
sqlsrv_connect($this->config['host'] . $port, $params)
Printing the variables that were passed in gives me this:
SRV, 1433 Array ( [Database] => DB [CharacterSet] => char
[MultipleActiveResultSets] => [UID] => sa [PWD] => password )
Each of those parameters is correct, is there a specific way I should format or change my database configuration array for this driver or is there something I am missing?
This error was due to outdated SQL Server drivers. Installing the latest from below allows a SQL Server connection:
http://www.microsoft.com/download/en/details.aspx?id=20098
I think there is some issue with your connection. PHP can't establish connection with sql Server.
var $default = array(
'driver' => 'sqlsrv',
'persistent' => false,
'host' => 'Myhome-PC\SQLEXPRESS', // or ip-address(192.168.1.13)
'login' => 'username',
'password' => 'password',
'database' => 'db',
'prefix' => 'tbl',
'port' => NULL,
'returnDatesAsStrings' => True
);
I think the Connection should be this
Here is the sqlsrv_dbo which I used.
And your $param should be something like this
$connectionInfo = array("UID" => $config['login'],
"PWD" => $config['password'],
"Database" => $config['database'],
'MultipleActiveResultSets' => 1,
'ConnectionPooling' => 0
);
if(isset($config['ReturnDatesAsStrings'])){
$connectionInfo['ReturnDatesAsStrings'] = $config['ReturnDatesAsStrings'];
}
$this->connection = sqlsrv_connect($config['host'] . $port, $connectionInfo);

Categories