Easiest way to activate PHP and MySQL on Mac OS 10.6 (Snow Leopard), 10.7 (Lion), 10.8 (Mountain Lion)?

155,928

Solution 1

Open a good text editor (I'd recommend TextMate, but the free TextWrangler or vi or nano will do too), and open:

/etc/apache2/httpd.conf

Find the line:

"#LoadModule php5_module        libexec/apache2/libphp5.so"

And uncomment it (remove the #).

Download and install the latest MySQL version from mysql.com. Choose the x86_64 version for Intel (unless your Intel Mac is the original Macbook Pro or Macbook, which are not 64 bit chips. In those cases, use the 32 bit x86 version).

Install all the MySQL components. Using the pref pane, start MySQL.

In the Sharing System Pref, turn on (or if it was already on, turn off/on) Web Sharing.

You should now have Apache/PHP/MySQL running.

In 10.4 and 10.5 it was necessary to modify the php.ini file to point to the correct location of mysql.sock. There are reports that this is fixed in 10.6, but that doesn't appear to be the case for all of us, given some of the comments below.

Solution 2

To complete your setup or MySQL:

sudo vim /etc/profile
  1. Add alias

    alias mysql=/usr/local/mysql/bin/mysql
    alias mysqladmin=/usr/local/mysql/bin/mysqladmin
    
  2. Then set your root password

    mysqladmin -u root password 'yourPassword'
    
  3. Then you can login with

    mysql -u root -p
    

Solution 3

It's an invisible folder. Just hit Command + Shift + G (takes you to the Go to Folder menu item) and type /etc/.

Then it will take you to inside that folder.

Solution 4

I strongly prefer HomeBrew over MacPorts for installing software from source.

HomeBrew sequesters everything in /usr/local/Cellar so it doesn't spew files all over the place. (Yes, MacPorts keeps everything in /opt/local, but it requires sudo access, and I don't trust MacPorts with root.)

Installing MySQL is as simple as:

brew install mysql
mysql_install_db

To start mysql, in Terminal type:

mysqld&

There's a way to start it upon boot, but I like to start it manually.

Solution 5

This help me a lot:

http://maestric.com/doc/mac/apache_php_mysql_snow_leopard

It also works for Mac OS X Lion :D

.:EDIT:. On my case the prefepane only allows to start and stop mysql, but after some issues i've uninstalled him. If you need a application to run queries and create DB, you could use: Sequel Pro (it's free) or Navicat

If you need start and stop mysql in ~/.bash_profile you can add these lines:

#For MySQL
alias mysql_start="/Library/StartupItems/MySQLCOM/MySQLCOM start"
alias mysql_stop="/Library/StartupItems/MySQLCOM/MySQLCOM stop"

After reloaded the console just call:

$mysql_start 

or

$mysql_stop 

agreding the desired action. Hope helped you.

Share:
155,928

Related videos on Youtube

Matt Sephton
Author by

Matt Sephton

Former Apple Technology Evangelist I type stuff into computers to make them do cool things.

Updated on July 28, 2021

Comments

  • Matt Sephton
    Matt Sephton almost 3 years

    What is the easiest way to activate PHP and MySQL on Mac OS 10.6 (Snow Leopard), 10.7 (Lion), 10.8 (Mountain Lion)?

    I'd prefer to use any bundled versions before resorting to downloading from the PHP or MySQL websites. That also rules out MAMP and similar.

    I'm comfortable at the command line in Terminal.

  • Matt Sephton
    Matt Sephton over 14 years
    Out of interest - Snow Leopard is 64-bit capable, so why choose the 32-bit of MySQL?
  • machuga
    machuga over 14 years
    Leopard, and even Tiger for most things I believe, are 64-bit capable. 64-bit MySQL has some issues with some other packages, not directly related to PHP necessarily, but lets say he tried Python down the line - 64bit MySQL causes some serious grief with the MySQLDB package. It's completely usable, just not all things have caught up yet, sadly.
  • Matt Sephton
    Matt Sephton over 14 years
    That is exactly the information I was looking for. Thanks Mr/Mrs Unknown!
  • Matt Sephton
    Matt Sephton over 14 years
    there's a typo /etc/apache2/httpd.conf
  • mdskinner
    mdskinner about 14 years
    beautiful!.. who would do it any other way!.
  • Dave DeLong
    Dave DeLong about 14 years
    +1 this is the way to go. I've also found the instructions here to be amazingly helpful (even though it says 10.5, it works for 10.6): red91.com/2007/12/14/installing-mysql-on-leopard
  • cregox
    cregox about 14 years
    if anyone else is wondering where's the php.ini, there isn't any! but you can add one to /etc by copying /etc/php.ini.default to /etc/php.ini
  • cregox
    cregox about 14 years
    and the "It works" page is under /Library/WebServer/Documents/index.html.en
  • apaderno
    apaderno almost 14 years
    I followed the steps reported here, but still PHP is not working; using a URL like example.local/dr61/install.php, the browser shows the code source. I guess it is necessary to change something in the php.ini file.
  • apaderno
    apaderno almost 14 years
    At the end, I had to copy the file php.ini.default in php.ini, and PHP files started to be executed as they should be.
  • apaderno
    apaderno almost 14 years
    To use MySQL, I had also to change the default socket, which in my case was /tmp/mysql.sock (I installed MySQL 5.5).
  • aaaidan
    aaaidan over 13 years
    Also, after saving the .conf, you can just use the command sudo apachectl restart instead of turning web sharing off-on.
  • Duke
    Duke over 13 years
    "brew link mysql" will create a number of symlinks in /usr/local/bin etc.
  • Ethan
    Ethan over 13 years
    I'm not sure I understand the 'messily spew files' comment. MacPorts sequesters everything in /opt/local, so it seems equivalent. I suppose occasionally I've seen something placed in /Applications/MacPorts (e.g. AquaTerm for gnuplot), but this is the very rare exception and usually for a good cause (e.g. AquaTerm is a .app bundle), and even then it's keeping it organized under a single directory...
  • Ethan
    Ethan over 13 years
    Oh, and I find it easier to simply add /opt/local/bin to my $PATH, and -I/opt/local/include to CPPFLAGS, -L/opt/local/lib to LDFLAGS, and then not have to remember to set up links for every single package that gets installed...
  • Duke
    Duke over 13 years
    My use of the symlink was based partially on ignorance; it's completely unnecessary. I just don't trust large installation scripts with root. And if you don't think MacPorts spreads files over many directories, just look at the uninstallation instructions: guide.macports.org/chunked/…
  • brutuscat
    brutuscat over 13 years
    I like this! Instead of messing with the PATH add an alias +1
  • Eric Bloch
    Eric Bloch almost 13 years
    To test the setup, you can put the line <?php phpinfo(); ?> inside ~/Sites/foo.php and browse to host/~user/foo.php
  • Matt Sephton
    Matt Sephton over 12 years
    Does the MySQL prefpane work for you in Lion? That is, can it switch MySQL on/off?
  • workdreamer
    workdreamer over 12 years
    i don't need the prefpane, but this solve your question? : stackoverflow.com/questions/3027479/…
  • gravyface
    gravyface over 12 years
    Also, remember to close/re-open your Terminal session for your aliases to take effect.
  • workdreamer
    workdreamer over 12 years
    But the prefpane only allows you to start and stop the mysql. I re-edited my post because here its not formated.
  • Behrang
    Behrang over 12 years
    @Cawas what's the difference between /etc/php.ini.default and /private/etc/php.ini.default?
  • cregox
    cregox over 12 years
    @BehrangSaeedzadeh /etc is just a symlink to /private/etc
  • Matt Sephton
    Matt Sephton about 11 years
    I prefer the standard bundled versions, please see the question.
  • pojda
    pojda over 8 years
    Worked like a charm on El Captain. Btw this link helped me a lot too coolestguidesontheplanet.com/…