I have a Zend Framework 1.12 application that uses the Zend DB Adapter class to connect with MySql. I need to deploy it soon and am investigating Google App Engine.
My current problem is that I can't connect the application to Google Cloud SQL. This Google page, https://developers.google.com/appengine/docs/php/cloud-sql/, gives an example using PHP and PDO. It uses a DSN as a parameter.
The Zend DB Adapter class does not take a DSN as a parameter, but instead constructs it from the other parameters it receives. I have tried to engineer the Zend DB Adapter params to give the correct DSN, but still not luck. Here's my attempt:
$config = new Zend_Config(
array(
'database' => array(
'adapter' => 'Pdo_Mysql',
'params' => array(
'host' => 'test-instance',
'dbname' => 'my_db_name',
'username' => 'root',
'password' => 'rootpassword',
'charset' => 'utf8',
'driver_options' => [
'unix_socket' => '/cloudsql/test-project'
]
)
)
)
);
try {
$this->_db = Zend_Db::factory($config->database);
$this->_db->getConnection();
} catch (Exception $e){
Zend_Registry::get('logger')->debug('Cannot create the connection...');
Zend_Registry::get('logger')->debug(print_r($e, true));
}
The exception in the message is
The mysql driver is not currently installed
Any ideas?
At this stage I am just trying it from the GAE SDK on my desktop. I have not uploaded the application to the cloud. I doubt this is the issue though. (My workstation IP is authorised to use the Cloud Sql instance).
I have not used the Zend Framework with AppEngine myself, but in my experience (not that I have found it documented anywhere), if you are connecting to Cloud SQL from an AppEngine app using the "root" user, you should not provide the password, or it fails to connect.
Also as shown in the documentation (here: https://developers.google.com/appengine/docs/php/cloud-sql/#PHP_Connecting_to_your_Cloud_SQL_instance), the unix_socket option should be in the format /cloudsql/project-name:instance-name
Therefore I would try rewriting your $config as follows:
$config = new Zend_Config(
array(
'database' => array(
'adapter' => 'Pdo_Mysql',
'params' => array(
'host' => '',
'dbname' => 'my_db_name',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'driver_options' => [
'unix_socket' => '/cloudsql/test-project:test-instance'
]
)
)
)
);
Related
I am trying to connect the doctrine, but I do not quite understand how I can create entities from the console, so for example, to make it easier to create Many to Many
I have already connected the Doctrine itself:
//public/bootstrap/bootstrap.php
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(// Подробнее настройка DBAL тут: http://silex.sensiolabs.org/doc/providers/doctrine.html
'driver' => 'pdo_mysql',
'dbname' => 'test',
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'password',
'charset' => 'utf8'
)
));
there is nothing in the official documentation on how to create entities - https://silex.symfony.com/doc/2.0/providers/doctrine.html
I could not find it on the Internet either, maybe someone knows how to cope with this?
I'm working on our database and I use for that a former project I did.
I used to work with a phpmyadmin version where I didn't need to specify the server choice.
Today I work on the version 4.1.14.8 and I have to specify the server number.
What I like to know is how can I specify that in my connection string by using doctrine.
Here's what I have for the moment :
<?php
// Doctrine (db)
$app['db.options'] = array(
'driver' => 'pdo_mysql',
'charset' => 'utf8',
'host' => 'localhost',
'port' => '3306',
'dbname' => 'dbname',
'user' => 'usr',
'password' => 'pwd',
);
Thanks
The array value:
'host' => 'localhost',
Is what you use to specify the server to use. In the above it is set to 'localhost'. You could just enter in the IP address if your MySQL database is on another host.
I have been using Laravel for a while but mostly on a single database server.
Now, my client has got atleast 5 database from multiple database servers in the same network.
I am able to confirm the communication in between servers using ping through ssh and also using the
mysqli_connect to check the database connections.
However, when I use laravel through its database.php (mysql configs) like :
// This setup is working fine (using localhost)
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
// Other DB (which is failing to connect) but i can confrim the connection using a file outside
// the laravel app through mysqli
'other_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.241',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'another_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.211',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and using the new PDO
// This connectiion functions with no problem
$conn1 = new PDO('mysql:host=localhost;dbname=dbname;port=3306','username','password');
if($conn1) {
echo 'connected';
}
// But this returns an error
$conn = new PDO('mysql:host=192.168.33.241;dbname=dname;port=3306','username','password');
if($conn) {
echo 'connected';
}
// Also, i have no problems using the DB:: or even the model as long as the host is the localhost
it fails to connect, both the PDO and the laravel default DB feature only works if the host is the localhost.
For some reason it throws back an error like this if i use the other servers ip, which is where i would like to communicate with:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.241' (13)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.211' (13)
When i try using the server ip or the main domain name to connect.
Am I missing something? Why is it that I can connect only on the local db server which is localhost
and not to any other database servers? But I can connect to them with no problem if I use say an index.php file from outside the laravel application and use the mysqli_connect to make queries and make connections?
Thanks for all your help.
You can access all the connections you have specified within database.php with:
$user = DB::connection('other_db_connection')
You don't need to setup new PDO connections or similar, will work like above for you
I am creating one database wrapper class and want to have a feature to connect with multiple database using PDO. I have connected multiple database using pdo as below.
$config = array(
'database1' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'users',
'port' => '',
'dbtype' => 'mysql'
),
'database2' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'directory',
'port' => '',
'dbtype' => 'mysql'
),
);
foreach ($config as $key => $value)
{
$this->conn[$value['database']] = new PDO("mysql:host=".$value['hostname'].";dbname=".$value['database'], $value['username'],$value['password']);
}
Is it good practice to connect multiple databases as above using any types of database (may using Oracle or Mysql or may be both same). Because my above code takes more time in loading.
I want to have a best practice as Yii or Symphony framework uses to connect db.
Can anybody have solution.
Thanks
I need to add sql logging to work in native WebProfileBundle.
When I do one default connection in application config, I see the sql
queries in my log. But my application uses many connections to many db
servers, so I can't add all the possible connections to config file.
I create runtime connections, i.e.:
$config = array(
'user' => 'user1',
'password' => 'pass1',
'driver' => 'pdo_mysql',
'port' => 3306,
);
$conn = DriverManager::getConnection($config);
then I think, should be command something like this
$conn->getConfiguration()->getSQLLogger($someLoggerObject);
I've tried to solve this problem with DependencyInjection, took
DoctrineBundle as example. But have no luck.
Any help with live code or link to proper documentation would be great
I have this in my conf:
$config = new Configuration;
/** configuration stuff */
$config->setSQLLogger(MyLogger::getInstance());
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => DB_USER,
'password' => DB_PASS,
'host' => DB_HOST,
'dbname' => DB_NAME,
'charset' => 'utf8',
'driverOptions' => array(
'charset' => 'utf8'
)
);
$em = EntityManager::create($connectionOptions, $config);
I use Zend + Doctrine2 and that is in my bootstrap, my logger is a singleton (thats why getInstance). I've never user DriverManager, but i hope this helps.