[support] Connecting to a non-system-wide drupal database

Tim Johnson tim at akwebsoft.com
Fri Nov 9 20:45:45 UTC 2012


I'm a total drupal noob, but a long-time CGI/Web/database
programmer. When I installed drupal, the first things I wanted to do
was to understand drupal in terms of the file structure and also to
access the drupal MySQL database independent of drupal itself -
I.E. - from either bash via mysql client or via an API. I use
python.MySQLdb in this example.

The installation of drupal by acquia and the bitnami stack on Mac
OSX puts the data storage at a different location than the default
mysql installation.

So to use the MySQL client one has to use the -S option to pass the
drupal socket file and the -P option to pass the port number. 

mysql -hlocalhost -uroot -p****** -P3307
-S/Applications/drupal-7.15-0/mysql/tmp/mysql.sock
## NOTE: /Applications/drupal-7.15-0/mysql/tmp/mysql.sock is the
socket file and the drupal instance of the mysql client listens at
port 3307.

Two different API connections (using the mysql shared object
approach) and the python MySQLdb API
first, load the MySQLdb API source
import MySQLdb
## 1)Using root as user
conn =
MySQLdb.connect(db='bitnami_drupal7',host='localhost',user='root',
passwd='rootpassword',read_default_file="/Applications/drupal-7.15-0/mysql/my.cnf")

## 2)Using the bitnami user created in the installation and from the
## $databases array in sites/default/settings.php
conn=MySQLdb.connect(db='bitnami_drupal7',host = 'localhost',
user='bitnami',passwd='bitnamipassword',
read_default_file="/Applications/drupal-7.15-0/mysql/my.cnf")

Note that in both cases we pass the 'read_default_file' keyword with
the value of the drupal mysql option file :
/Applications/drupal-7.15-0/mysql/my.cnf

But the `passwd' value will be different for the different users.
:) And use user 'root' at yer own peril.

Getting this together was some work for me and perhaps this as
archived might save someone else some time.

Any comments or corrections by the wise heads on this list are
invited.

cheers
-- 
Tim 
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com


More information about the support mailing list