Friday, July 11, 2014

MAMP. Access MySQL with PHP via CRON or Command line.


Again problems with MAMP.

I have a project in PHP which should access via CRON to MySQL running on MAMP.  With the default MAMP's configuration that is not possible. What to do?

OPTION A. Setting PATHs


1) Edit th .profile file (vi ~/.profile) and change the line

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

to

export PATH=/Applications/MAMP/Library/bin/:/Applications/MAMP/bin/php5/bin/:/opt/local/bin:/opt/local/sbin:$PATH

2) Reload the .profile file

$ . ~/.profile

3) (Optional) If you have problems connecting to the database tray to create a symbolic link to /tmp/mysql.sock

sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

Or more professional, add the next lines to your /etc/my.cnf file:

[mysqld]
socket=/Applications/MAMP/tmp/mysql/mysql.sock

[client]
socket=/Applications/MAMP/tmp/mysql/mysql.sock

Note: You can use also /etc/profile file if you want to do it for the rest of users.

OPTION B. Configuring a php.ini


1)  Check if your php exec has a defined config file

$ php --ini
Configuration File (php.ini) Path: /opt/local/etc/php5
Loaded Configuration File:         (none)
Scan for additional .ini files in: /opt/local/var/db/php5
Additional .ini files parsed:      /opt/local/var/db/php5/gd.ini,
/opt/local/var/db/php5/mbstring.ini,
/opt/local/var/db/php5/mcrypt.ini,
/opt/local/var/db/php5/mysql.ini,
/opt/local/var/db/php5/openssl.ini,
/opt/local/var/db/php5/zip.ini



2) Create/copy a default php.ini file in the config folder of your current PHP version. For example in my case: /opt/local/etc/php5/php.ini

Hope help!