I'm attempting to switch my database connection on drupal 7. Based on http://drupal.org/node/18429 I have written the following module code : // code : $dba = array ( 'driver' => 'mysql', 'database' => 'dolphy_books', 'host' => 'localhost', 'username' => 'tim', 'password' => '******', ); // Test the current connnection $result = Database::getConnectionInfo('default'); drupal_set_message("<b>Connection Info</b>: <pre>" . print_r($result,1) . '</pre>'); // Verify that table 'actions' exists $result = drupal_get_schema('actions'); drupal_set_message("<b>schema</b>: <pre>" . print_r($result,1) . '</pre>'); // change the connection to the one defined by $dba Database::addConnectionInfo('db1','default',$dba); db_set_active('db1'); // Test what should now be the current connection. And the info for // $dba is returned $result = Database::getConnectionInfo('db1'); drupal_set_message("<b>Now : Connection Info</b>: <pre>" . print_r($result,1) . '</pre>'); // There is no 'actions' table in the 'dolphy_books' database. The // following code should generate and error $result = drupal_get_schema('actions'); drupal_set_message("<b>schema</b>: <pre>" . print_r($result,1) . '</pre>'); // YIKES! We are still seeing the 'actions' table. DB switch failed. // end code
The diagnostics that I have put in indicate (to the best that I can see) that the appropriate connection info has been provided. **BUT** the connection has not been changed! The second call to `drupal_get_schema' returns the same schema info as before the calls to addConnectionInfo and db_set_active. In fact, there is no table named 'actions' in the 'dolphy_books' database.
I could really use some help here. This has got me tearing my hair out and believe me, I've got little to spare!
thanks
The schema doesn't come from the database but from the modules that implement a schema. The schema will exist regardless of which connection you're connected to. http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_ge...
Earnie
On Tue, Jan 1, 2013 at 9:37 PM, Tim Johnson tim@akwebsoft.com wrote:
I'm attempting to switch my database connection on drupal 7. Based on http://drupal.org/node/18429 I have written the following module code : // code : $dba = array ( 'driver' => 'mysql', 'database' => 'dolphy_books', 'host' => 'localhost', 'username' => 'tim', 'password' => '******', ); // Test the current connnection $result = Database::getConnectionInfo('default'); drupal_set_message("<b>Connection Info</b>: <pre>" . print_r($result,1) . '</pre>'); // Verify that table 'actions' exists $result = drupal_get_schema('actions'); drupal_set_message("<b>schema</b>: <pre>" . print_r($result,1) . '</pre>'); // change the connection to the one defined by $dba Database::addConnectionInfo('db1','default',$dba); db_set_active('db1'); // Test what should now be the current connection. And the info for // $dba is returned $result = Database::getConnectionInfo('db1'); drupal_set_message("<b>Now : Connection Info</b>: <pre>" . print_r($result,1) . '</pre>'); // There is no 'actions' table in the 'dolphy_books' database. The // following code should generate and error $result = drupal_get_schema('actions'); drupal_set_message("<b>schema</b>: <pre>" . print_r($result,1) . '</pre>'); // YIKES! We are still seeing the 'actions' table. DB switch failed. // end code
The diagnostics that I have put in indicate (to the best that I can see) that the appropriate connection info has been provided. **BUT** the connection has not been changed! The second call to `drupal_get_schema' returns the same schema info as before the calls to addConnectionInfo and db_set_active. In fact, there is no table named 'actions' in the 'dolphy_books' database.
I could really use some help here. This has got me tearing my hair out and believe me, I've got little to spare!
thanks
Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com -- [ Drupal support list | http://lists.drupal.org/ ]
* Earnie Boyd earnie@users.sourceforge.net [130101 18:38]:
The schema doesn't come from the database but from the modules that implement a schema. The schema will exist regardless of which connection you're connected to. http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_ge...
Understood. Thank you for the reply. I intended the schema call as sort of a diagnostic. The real goal is to insert data into the 'external' database. So here is the code I have now: // code begins $dba = array ( 'driver' => 'mysql', 'database' => 'dolphy_books', 'host' => 'localhost', 'username' => 'tim', 'password' => '******', 'port' => '3306', ); Database::addConnectionInfo('db1','default',$dba); db_set_active('db1'); $result = Database::getConnectionInfo('db1'); drupal_set_message("<b>Now : Connection Info</b>: <pre>" . print_r($result,1) . '</pre>'); $inserter = db_insert('providers'); // code ends When this code is executed, I get an error: as follows """ Additional uncaught exception thrown while handling exception.
Original
PDOException: SQLSTATE[HY000] [2002] No such file or directory in db_insert() (line 2398 of /Users/http/sites/daily/includes/database/database.inc).
Additional
PDOException: SQLSTATE[HY000] [2002] No such file or directory in db_select() (line 2491 of /Users/http/sites/daily/includes/database/database.inc).
Uncaught exception thrown in session handler.
PDOException: SQLSTATE[HY000] [2002] No such file or directory in db_merge() (line 2416 of /Users/http/sites/daily/includes/database/database.inc).
""" So what am I doing wrong? Thanks again for the help.
* Tim Johnson tim@akwebsoft.com [130102 10:15]:
When this code is executed, I get an error: as follows
<....>
PDOException: SQLSTATE[HY000] [2002] No such file or directory in db_merge() (line 2416 of /Users/http/sites/daily/includes/database/database.inc).
So what am I doing wrong?
I believe that I have solved the process of switching databases and inserting data:
I am on mac osx 10.7, and there is a socket issue for drupal 7 on macs - see : http://drupal.org/node/752856 title : """ Document OS X PDO socket installation issues """ Two steps are needed: 1) Define connection information as an entry in settings.php to the $databases array as follows:
'dba' => array ( 'default' => array ( 'driver' => 'mysql', 'database' => 'aisbooks', 'host' => 'localhost', 'username' => 'tim', 'password' => 'marion', // explicitly define the socket file for mysql 'unix_socket' => '/opt/local/var/run/mysql5/mysqld.sock', ), ),
The default setting in php.ini for pdo_mysql.default_socket is /tmp/mysql.sock - which works for non-drupal PHP, but I had to create a soft link for the 'unix_socket' value above, as is explained in the document above.
2) The following code : // Implementation of hook_form_submit in module : // form_example.module function form_example_form_submit($form,&$form_state) { $result = Database::getConnection('default','dba'); db_set_active('dba'); $fe_id = db_insert('providers') ->fields(array( 'status' => $form_state["values"]["mynumber"], 'name' => $form_state["values"]["mytextfield"], ))->execute(); db_set_active(); drupal_set_message('Your form entry has been added.'); } I hope this add something to the drupal knowledgebase. Kudos to Ernie to pointing out the 'red herring'. cheers
* Tim Johnson tim@akwebsoft.com [130102 15:33]:
- Tim Johnson tim@akwebsoft.com [130102 10:15]:
Two steps are needed:
- Define connection information as an entry in settings.php to the
$databases array as follows:
'dba' => array ( 'default' => array ( 'driver' => 'mysql', 'database' => 'aisbooks', 'host' => 'localhost', 'username' => 'tim', 'password' => 'marion', // explicitly define the socket file for mysql 'unix_socket' => '/opt/local/var/run/mysql5/mysqld.sock', ), ),
The default setting in php.ini for pdo_mysql.default_socket is /tmp/mysql.sock - which works for non-drupal PHP, but I had to create a soft link for the 'unix_socket' value above, as is explained in the document above.
I should add here, that when I went to install drupal 'by hand', rather than using the bitnami stack installer, I ran into a problem installing the 'by hand' version of drupal with mysql.
I got error messages saying that drupal could not resolve or read /tmp/mysql.sock.
Setting up the softlink at /opt/local/var/run/mysql5/mysqld.sock solves this problem.
That value has to be changed in php.ini code : pdo_mysql.default_socket=/opt/local/var/run/mysql5/mysqld.sock and restart apache.
Changing the pdo_mysql.default_socket value does not seem to affect the operation of non-drupal PHP, phpinfo and phpmyadmin et. al.
To clarify: installing drupal 'by hand' is to copy the contents of the downloaded and untarred drupal package to some path recognized by apache (beneath whatever is set as apache's doc root) and then pointing the browser to it...
The problem you're talking about isn't related to only Drupal. It's a problem with MAMP. Just Google MAMP pdo and you'll see tons of results that stem from some of the weird things MAMP does with reading PHP's INI file.
Actual this is something that should probably be documented on D.O. about MAMP. Honestly the best thing to do is comment out skip-networking and add bind-address=127.0.0.1 to your my.cnf file and that way PDO can connect straight through localhost. IIRC you can actually do this from the MAMP control panel.
As far as PHPInfo and PhpMyAdmin, there's a simple reason why it doesn't affect those. First, phpinfo(() is a function and just gives you back information about PHP. It doesn't connect to the database.
PHPMyAdmin doesn't use PDO. Instead they use the old mysql_*/mysqli_* functions. You can set the socket location for PHPMyAdmin via the $cfg['Servers'][$i]['socket'] = ''; setting. I believe that is done automatically in the MAMP install though.
Jamie Holly http://www.intoxination.net http://www.hollyit.net
On 1/3/2013 4:07 PM, Tim Johnson wrote:
- Tim Johnson tim@akwebsoft.com [130102 15:33]:
- Tim Johnson tim@akwebsoft.com [130102 10:15]:
Two steps are needed:
- Define connection information as an entry in settings.php to the
$databases array as follows:
'dba' => array ( 'default' => array ( 'driver' => 'mysql', 'database' => 'aisbooks', 'host' => 'localhost', 'username' => 'tim', 'password' => 'marion', // explicitly define the socket file for mysql 'unix_socket' => '/opt/local/var/run/mysql5/mysqld.sock', ), ),
The default setting in php.ini for pdo_mysql.default_socket is /tmp/mysql.sock - which works for non-drupal PHP, but I had to create a soft link for the 'unix_socket' value above, as is explained in the document above.
I should add here, that when I went to install drupal 'by hand', rather than using the bitnami stack installer, I ran into a problem installing the 'by hand' version of drupal with mysql.
I got error messages saying that drupal could not resolve or read /tmp/mysql.sock.
Setting up the softlink at /opt/local/var/run/mysql5/mysqld.sock solves this problem.
That value has to be changed in php.ini code : pdo_mysql.default_socket=/opt/local/var/run/mysql5/mysqld.sock and restart apache.
Changing the pdo_mysql.default_socket value does not seem to affect the operation of non-drupal PHP, phpinfo and phpmyadmin et. al.
To clarify: installing drupal 'by hand' is to copy the contents of the downloaded and untarred drupal package to some path recognized by apache (beneath whatever is set as apache's doc root) and then pointing the browser to it...
* Jamie Holly hovercrafter@earthlink.net [130103 12:42]:
The problem you're talking about isn't related to only Drupal. It's a
j problem with MAMP. Just Google MAMP pdo and you'll see tons of results
that stem from some of the weird things MAMP does with reading PHP's INI file.
<...> Thanks for clarifying that, Jamie. (I miss linux sometimes).