Trying to connect to my MySQL database via SSL, I have successfully established the connection from my webserver via ssh with the following command line:
mysql -h my.host.here --port=5454 -v --ssl-ca=/etc/apache2/ssl/mysql/ca-cert.pem --ssl-cert=/etc/apache2/ssl/mysql/client-cert.pem --ssl-key=/etc/apache2/ssl/mysql/client-key.pem -u user -p
However, trying to set up the same connection in symfony2 and doctrine, all I keep getting is an "SSL error"
$params = array(
'driver' => 'pdo_mysql',
'user' => 'user',
'password' => 'pass',
'host' => 'my.host.here',
'dbname' => 'media',
'port' => '5454',
);
if($this->container->hasParameter('media_ca') && $this->container->hasParameter('media_cert') && $this->container->hasParameter('media_key')) {
$params['driverOptions'] = array(
PDO::MYSQL_ATTR_SSL_CA => $this->container->hasParameter('media_ca'),
PDO::MYSQL_ATTR_SSL_CERT => $this->container->hasParameter('media_cert'),
PDO::MYSQL_ATTR_SSL_KEY => $this->container->hasParameter('media_key'),
);
}
/* Using this instead with only the ca_cert gives me the same error
if($this->container->hasParameter('media_ca')) {
$params['driverOptions'] = array(
PDO::MYSQL_ATTR_SSL_CA => $this->container->hasParameter('media_ca'),
);
}
*/
$connectionFactory = $this->container->get('doctrine.dbal.connection_factory');
$conn = $connectionFactory->createConnection($params);
return $conn;
In my log:
[2013-10-01 15:23:30] request.CRITICAL: Uncaught PHP Exception PDOException: "SQLSTATE[HY000] [2026] SSL connection error" at /var/www/mysite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php line 36 {"exception":"[object] (PDOException: SQLSTATE[HY000] [2026] SSL connection error at /var/www/mysite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:36)"} []
I have doublechecked that the webserver user (www-data) has access to the certificate files, and that the path to those cert files are correct (defined in the symfony2 parameters).
I can not think of anything else that is different between my command line connection and the one I have specified with doctrine/symfony2.
You are wrong with retrieving parameters. You need getParameter($param) method instead of hasParameter($param). These lines are correct.
PDO::MYSQL_ATTR_SSL_CA => $this->container->getParameter('media_ca'),
PDO::MYSQL_ATTR_SSL_CERT => $this->container->getParameter('media_cert'),
PDO::MYSQL_ATTR_SSL_KEY => $this->container->getParameter('media_key'),
Just to record the full example how I ended up solving the issue:
//Create a connection to another public database.
private function videoDatabase() {
//Create a connection to pub-DB.
$params = array(
'driver' => $this->container->getParameter('media_database_driver'),
'user' => $this->container->getParameter('media_database_user'),
'password' => $this->container->getParameter('media_database_password'),
'host' => $this->container->getParameter('media_database_host'),
'dbname' => $this->container->getParameter('media_database_name'),
'port' => $this->container->getParameter('media_database_port')
);
if($this->container->hasParameter('media_ca') && $this->container->hasParameter('media_cert') && $this->container->hasParameter('media_key')) {
$params['driverOptions'] = array(
PDO::MYSQL_ATTR_SSL_CA => $this->container->getParameter('media_ca'),
PDO::MYSQL_ATTR_SSL_CERT => $this->container->getParameter('media_cert'),
PDO::MYSQL_ATTR_SSL_KEY => $this->container->getParameter('media_key'),
);
}
$connectionFactory = $this->container->get('doctrine.dbal.connection_factory');
$conn = $connectionFactory->createConnection($params);
return $conn;
}
Related
on a module I have add a component named db where i put, like the main Yii component, the data for database connection, I need in my module use everytime the db specified in his configuration for all models and not the main database connection, how I can do this?
You have several way eg. using a separated configuration in app/config/main.php
eg adding a specific dbMyMod to component config
return [
// ...
'components' => [
// ...
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=example',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'dbMyMod ' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=hostForMudle;dbname=module_db_name',
'username' => 'user_module_name',
'password' => 'password',
'charset' => 'utf8',
],
],
or one way that not require a static configuration in app/confing
could be based on a module function that return a proper db connection
public function myModuleDbCon()
{
$myDbCon = new yii\db\Connection([
'dsn' => 'mysql:host=localhost;dbname=example',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
]);
return myDbConn;
}
then in you module you can retrive the module db connection
aDbConn = Yii::$app->getModule('my_module_name')->myModuleClass->myModuleDbCon();
.
$command = $aDbConn->createCommand('SELECT * FROM myTable');
$result= $command->queryAll();
I have a Model for DB tables, basically is one table but in different databases,
How can I set a connection to DB in Model?
protected $connection = 'ls';
Above code is not that I am looking for, I need to pass host, port, username and password. because conection are stored in DB not in config file.
I was thinking for function __construct() and call like Model($data)::where()..etc
Or I am thinking wrong way, can somebody give me an better idea.?
You can connect from Eloquent model by maintaining following aspects
First you can define multiple connection in database.php file
<?php
return array(
'default' => 'mysql',
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
You have your default connection still set to mysql . This means that, unless we specify otherwise, the application will use that mysql connection.
Now in your model you can specify which connection to use
<?php
class SomeModel extends Eloquent {
protected $connection = 'mysql2';
}
You can also define the connection at runtime via the setConnection method.
<?php
class SomeController extends BaseController {
public function someMethod()
{
$someModel = new SomeModel;
$someModel->setConnection('mysql2');
$something = $someModel->find(1);
return $something;
}
}
I am using ZF v2.2. I am having a problem during database connectivity with "Mysqli: The ext/mysqli driver". Actually i am unable to figure out the process of "connecting to database & then using $sql->select(), $sql->update() etc".
Any idea how to configure this?
File: autoload/db.local.php
return array(
'db' => array(
'driver' => 'Mysqli',
'username' => 'root',
'password' => 'redhat',
'database' => 'mydb',
'host' => 'localhost'
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),
);
public function indexAction() {
$db=$this->getServiceLocator()->get('db'); ` `echo $db->getDriver()->getConnection()->isConnected();
//**returns false**.
}
I have a MySQL table that needs to be synchronized with a SQL Server table. So the data from MySQL moves to SQL Server. It is done by simple SELECT * and INSERT INTO queries. However, I ran into problems with migrating some BLOB data to a varbinary field.
So I have this code:
$db_mysql = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => '<user>',
'password' => '<password>',
'dbname' => '<db>',
'charset' => 'utf8'
));
$db_mssql = new Zend_Db_Adapter_Pdo_Mssql(array(
'pdoType' => 'sqlsrv',
'host' => '<host>',
'username' => '<user>',
'password' => '<password>',
'dbname' => '<db>'
));
$rows = $db_mysql->fetchAll("SELECT Id, Picture FROM Rosters");
foreach ($rows as $row) {
$update_sql = "UPDATE Rosters SET Picture = :picture WHERE Id = :id";
$stmt = new Zend_Db_Statement_Pdo($db_mssql, $update_sql);
$stmt->bindValue(":picture", bin2hex($row['Picture']), Zend_Db::PARAM_LOB);
$stmt->bindValue(":id", $row['Id'], Zend_Db::PARAM_INT);
try {
$stmt->execute();
} catch (Exception $e) {
echo $e->getMessage();
var_dump($e);
die();
}
}
This gives me the incredible not-so-useful error message: PDOException: SQLSTATE[HY000]: General error: 257 General SQL Server error: Check messages from the SQL Server [257] (severity 16) [(null)]
The MySQL Blob field is defined as BLOB with the attribute BINARY, the SQL Server field is defined as (varbinary(max), null). I think it's just a simple mistake, but I can't figure it out. Can anyone help me out with this?
Ok. I found it out on my own, this is the working code:
$db_mysql = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => '<user>',
'password' => '<pass>',
'dbname' => '<db>',
'charset' => 'utf8'
));
$db_mssql = new Zend_Db_Adapter_Pdo_Mssql(array(
'pdoType' => 'odbc',
'host' => '<host>',
'username' => '<user>',
'password' => '<pwd>',
'dbname' => 'Roosters'
));
$rows = $db_mysql->fetchAll("SELECT Id, Picture FROM Rosters");
foreach ($rows as $row) {
$update_sql = "UPDATE Rosters SET Picture = CONVERT(varbinary(max), :picture, 2) WHERE Id = :id";
$stmt = new Zend_Db_Statement_Pdo($db_mssql, $update_sql);
$stmt->bindValue(":picture", bin2hex($row['Picture']));
$stmt->bindValue(":id", $row['Id'], Zend_Db::PARAM_INT);
try {
$stmt->execute();
} catch (Exception $e) {
echo $e->getMessage();
var_dump($e);
die();
}
}
?>
I'm having trouble creating a page, which requires two different databases..
The controller is automaticly set to 'DB2', which is also specified in the database config file.
When i add a var $uses = array ('groups') to the controller, which is from the other DB (DB1), i get the data from only DB2 and all requests to DB1 become a invalid query..
u guys know a solution?
Thanks in advance!
Regards,
Swen
If have multiple datasources defined in your config/database.php file, you should be able to tell your Group model to use the second (non-default) config:
public $useDbConfig = 'db2';
Your config/database.php file should looks something like this:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_host',
'login' => 'your_login_1',
'password' => 'your_password_1',
'database' => 'DB1',
'prefix' => ''
);
var $db2 = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_host',
'login' => 'your_login_2',
'password' => 'your_password_2',
'database' => 'DB2',
'prefix' => ''
);
}