What is the proper (or better) way to set connection timeout with PHP code, when using Zend_Db::factory with PDO_MYSQL.
I have:
$params = array (
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'mydb',
'charset' => 'UTF8',
);
$db = Zend_Db::factory('PDO_MYSQL', $params);
a.)
$db->getConnection()->setAttribute(PDO::ATTR_TIMEOUT, 600);
b.)
$sql = "SET SESSION wait_timeout = 600";
$db->getConnection()->query($sql);
Reduce the connection timeout to 3 seconds:
$params = array (
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'mydb',
'charset' => 'UTF8',
'driver_options' => [
\PDO::ATTR_TIMEOUT => 3
]
);
$db = Zend_Db::factory('PDO_MYSQL', $params);
Related
I am having some trouble with disconnecting my created conn.
$dbName = session("key")["db_name"]["0"]->main_db;
$oldEmail = $this->connection()->table("tbl_user")->where("id",$UserID)->get();
DB::disconnect($dbName);
$User = User::where("email",$email)->get();
return $User;
I think the connection bypass my model and search for the table name of "User" in my created connection, I don't get the error, though it has the same name in my migration.
This is my codes in generating another connection:
public function connection() {
// SESSION
$dbName = session("key")["db_name"]["0"]->main_db;
// MAKE CONNECTION
Config::set("database.connections.".$dbName, array(
"driver" => config("app.DB_CONNECTION"),
"host" => config("app.DB_HOST"),
"database" => $dbName,
"username" => config("app.DB_USERNAME"),
"password" => config("app.DB_PASSWORD"),
));
DB::setDefaultConnection($dbName);
$conn = DB::connection($dbName);
return $conn;
}
app/config/database.php
return array(
'default' => 'mysql',
'connections' => array(
# Primary/Default database connection
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database1',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database2',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
Useful Links
https://laracasts.com/discuss/channels/eloquent/laravel-5-multiple-database-connection
How to connect mysql database using ZF3 db factory method.
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => MASTER_HOST,
'username' => MASTER_USER,
'password' => MASTER_PASSWORD,
'dbname' => DB_ADMIN,
'port' => MASTER_HOST_PORT,
);
In ZF3 (or ZF2), you must use Zend\Db\Adapter\Adapter
$config = array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=db;host=localhost',
'username' => 'root',
'password' => 'root',
);
// or
$config = $serviceLocator->get('Config')['db'];
$db = new \Zend\Db\Adapter\Adapter($config);
Documentation:
https://framework.zend.com/manual/2.4/en/modules/zend.db.adapter.html
https://framework.zend.com/manual/2.4/en/tutorials/tutorial.dbadapter.html
I am writing a PHP application using the Silex framework. I'm using the Doctrine Service Provider, and I can open a connection normally as this:
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'dbs.options' => array (
'localhost' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'test',
'user' => 'root',
'password' => 'root',
'charset' => 'utf8',
)
),
));
That works perfectly. What I want now is to add another database connection afterwards in my code. I know I can do it adding another element to dbs.options, but I want to do it afterwards, in the controllers (as different controllers will use different database connections).
Is that possible? I guess I could use something like DriverManager::getConnection($options, $config, $manager); but there's probably a better way to do it.
Thanks!
$conn = DriverManager::getConnection($params, $config);
this is original code to generate new connection, so what you wrote is ok
Link: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html
You can configure multiple db connections using the DoctrineServiceProvider bundled with Silex.
Replace the db.options with an array of configurations where keys are connection names and values configuration options.
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'dbs.options' => array (
'mysql_read' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_read.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8',
),
'mysql_write' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_write.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8',
),
),
));
Access multiple connections in your controllers:
$app->get('/blog/{id}', function ($id) use ($app) {
$sql = "SELECT * FROM posts WHERE id = ?";
$post = $app['dbs']['mysql_read']->fetchAssoc($sql, array((int) $id));
$sql = "UPDATE posts SET value = ? WHERE id = ?";
$app['dbs']['mysql_write']->executeUpdate($sql, array('newValue', (int) $id));
return "<h1>{$post['title']}</h1>".
"<p>{$post['body']}</p>";
});
Source: http://silex.sensiolabs.org/doc/providers/doctrine.html
I am developing a project using laravel 4 framework. In my database.php file I get the following error:
Undefined index: driver
And my connection is as following:
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'mysql2' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
);
I am also using environments in order to set different mysql connections. What is wrong with the code?
In my case it was because I deleted
'default' => 'mysql',
by mistake from app/config/database.php.
Moving the 'driver' key up a level should fix the issue.
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'driver' => 'mysql'
),
Most of the other params that are shared can me moved as well
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
),
'write' => array(
'host' => 'localhost',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
This happens to me because I deleted
'default' =>env('DB_CONNECTION', 'mysql'),
From app/config/database.php. It's necesary have a default connection
Go to root directory .env
This values are taken first.
If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Character extends Model {
protected $connection = 'my_db_connection_name_here';
}
This would be on the of the connections defined in config/database.php.
I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.
I am trying to read an utf8 (cyrillic) data from database with Doctrine2 ORM, but it always returns unreadable symbols.
My database, table and text field in utf8-general-ci.
The connection looks like this:
$em = EntityManager::create(array(
'driver' => 'pdo_mysql',
'unix_socket' => '/var/lib/mysql/mysql.sock',
'charset' => 'utf8',
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASS,
'dbname' => DB_NAME
), $config);
But still without success. How can I solve it?
Thank you
Here is the solution I've found and it works:
$em = EntityManager::create(array(
'driver' => 'pdo_mysql',
'unix_socket' => '/var/lib/mysql/mysql.sock',
'charset' => 'utf8',
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASS,
'dbname' => DB_NAME
), $config);
$em->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8', 'utf8_unicode_ci'));