Running PHP and MySQL with phpMyAdmin on OS X 10.5

on

Apache already comes pre-installed on OS X but PHP is not enabled by default. To enable PHP we need to edit our Apache config file by opening the Terminal and typing sudo nano /private/etc/apache2/httpd.conf and hitting enter (put in your admin password when it asks.) Type Control+W to bring up the search option and type php5_module and hit enter. Remove the # from the beginning of that line and then type Control+X to exit, Y to save when it asks, and Enter to overwrite the same filename.

Next, we want to create our PHP config file since it isn’t there by default. In the Terminal type cd /private/etc and hit enter, then type sudo cp php.ini.default php.ini and hit enter. This makes a copy of the default configuration we can use to get started with. We’ll use that later. Now you can start up Apache by going to System Preferences, Sharing and checking the Web Sharing box. If it was already checked, uncheck it and then recheck it again to restart Apache to accept our changes.

Now you need to install MySQL. Luckily, there’s already an install package available at MySQL.com so head on over there and download the latest stable version of 5.X. Now extract the zip, open the dmg and run the .pkg installer. I would also recommend double-clicking the .prefPane file for quick access to startup and shutdown commands without needing the Terminal. I don’t necessarily recommend installing the MySQLStartupItem package unless you really need it. In fact, you really shouldn’t leave MySQL or your web server running at all when you aren’t using it.

Once MySQL is installed you need to start it up. If you installed the preference pane (the .prefPane file) you can just go into System Preferences and then click MySQL. From there just click the button to get it started. Now we are going to want to set a password for the root user. To do that just open up the Terminal and type mysql . If it can’t find the mysql command we can add it to your path by editing your bash profile. Just type sudo nano ~/.bash_profile in the Terminal to edit the file, entering your admin password when it asks. Then add a line that looks like this: export PATH=/usr/local/mysql/bin:$PATH and type Control+X, Y, Enter to quit and save your changes. Then you’ll need to reload your bash profile by typing source ~/.bash_profile

Now you should be able to type mysql in the Terminal from any directory to connect to mysql. From the mysql> prompt type the following, substituting yourpassword with your desired password and then hit enter: set password for ‘root’@'localhost’ = password(‘yourpassword’); To make sure it was effective, log out by typing exit; and then enter and log back in again but this time we want to supply our credentials. So type the following to show that we want to log in using the root user and a password: mysql -u root -p

To make sure PHP can connect we need to edit our php configuration to know where the MySQL socket is located. For this type sudo nano /private/etc/php.ini from the Terminal. When the file opens type Control+W to bring up the search box and type in mysql.default_socket and hit enter. After the equals sign on that line enter /tmp/mysql.sock and and type Control+X, Y, Enter to quit and save your changes. Now we need to restart Apache for the change to take effect by going to System Preferences, Sharing and unchecking then rechecking the Web Sharing box. (Or via command-line if you wish.)

Now to install phpMyAdmin just download the latest and extract it into your Sites folder in your user account (/Users/username/Sites/) or the Apache root (/Library/WebServer/Documents/), whichever you prefer. Browse to that folder in the Terminal and type mkdir config to make the config folder and then chmod o+rw config to make it writable. Then browse to the setup folder in your browser, which would be http://localhost/~user/phpmyadmin/setup/ if you installed it in your user’s Sites folder or just http://localhost/phpmyadmin/setup/ if you put it in the Apache root. From there click the New Server button and then Save to take the defaults (unless you know other options you need) and then Save again in the Configuration File settings of the Overview page. Now you should be able to log into your server by going to either http://localhost/~user/phpmyadmin/ or http://localhost/phpmyadmin/ and entering your root username and password. It’s highly recommended that you create another user with fewer privledges to do most of your work, though.

If you also want to be able to use a .htaccess file you’ll need to go to the Terminal and type sudo nano /private/etc/apache2/httpd.conf to edit your Apache config file. Then type Control+W and search for AllowOverride. Change that option from None to All so that it reads AllowOverride All and then type Control+X, Y, Enter to quit and save your changes. Now we need to do the same for each user account. Do this by typing sudo nano /private/etc/apache2/users/username.conf and making the same change. If you aren’t sure what users there are you can type ls /private/etc/apache2/users/ . After you make the changes you’ll need to restart Apache again.

Some of this I did today and other parts of it I did a while ago so if anyone wants to give it a run through and let me know how it turns out that would be great. I know it could use some screenshots but those will have to come later since I was already done with this when I decided to write the instructions out.

My final word on this is to be mindful of security and make sure no one who shouldn’t can access any applications that are running. I would recommend shutting down MySQL and Apache when you aren’t developing to keep risks to a minimum. I’m not going to go into security since I assume that you know what you’re doing but it was worth mentioning again that any servers running on your machine are a potential security risk if you don’t know how to keep it safe.

References:

http://foundationphp.com/tutorials/php_leopard.php

http://dev.mysql.com/doc/refman/5.1/en/tutorial.html

http://wiki.phpmyadmin.net/pma/Setup

http://www.clagnut.com/blog/350/

19 Comments

  1. Michael

    Thanks for this man I was having some trouble getting php to see mysql, just needed to edit the sock setting. Keep it up.

  2. Georgio

    I’m having trouble with this:
    set password for ‘root’@'localhost’ = password(‘yourpassword’)

    (I change the quotes to straight ones and substituted my password)
    When i execute that in mysql I get a prompt like this:
    ->
    but it doesn’t seem to respond to any commands.

    Any advice will be welcome.

  3. Jonathan Dean

    Georgio, did you include the semicolon (;) at the end of the line? When entering sql commands via the command line, each statement must end with a semicolon. Otherwise it thinks you aren’t finished and waits for the rest of the statement on the next line. You can also put more than one statement on the same line as long as both are terminated by the semicolon. In PHP you don’t include the semicolon because you only execute one statement at a time as a single string and PHP sends the termination for you.

  4. Georgio

    Jonathan, thanks. The semicolon solved that problem. Obvious when you know.

    Now I’ve got another problem, with phpmyadmin. I’ve installed and set it up as you describe, but when I go to the login page I get:
    ‘Cannot load mcrypt extension. Please check your PHP configuration.’
    below the login box and i can’t login with newly set root login and password.

  5. Jonathan Dean

    Georgio, I also get the mcrypt extension error but I am able to log in. I believe to fix this error you would need to recompile PHP with the –with-mcrypt[=DIR] parameter (see http://php.net/mcrypt). Do you happen to be running a 64-bit machine? The documentation at http://www.phpmyadmin.net/documentation/ states: “When using the ‘cookie’ authentication method, the mcrypt extension is strongly suggested for most users and is required for 64–bit machines.”

    You may also be able to choose a different authentication mode if you think mcrypt is your problem. It may also be worth changing the password again just to make sure you typing everything correctly. I make typing mistakes quite a bit ;)

  6. Georgio

    Ok, I’ve got myphpadmin working now. As you suggested, the mcrypt module wasn’t the problem, it was my failure to set mysql.default_socket.

    Thanks for your great support Jonathan!

  7. ca

    hi,
    mysql> set password for ‘root’@'localhost’=password(‘my password here’);
    ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘mysql’
    mysql>

  8. ca

    hi again,
    i did this command from outside mysql and that did it.
    mysqladmin -u root password mypasswordhere
    thanks for the useful and current article!

  9. DJ

    Awesome awesome instructions. Been having a heck of time getting installed (using php docs). I’m a newbie and your awesome instructions saved the day! Cheers.

  10. Andrrew

    Thanks Jonathan, best set of instructions I’ve found to get to the phpMyAdmin welcome screen, but:
    I also have the message Georgio had with ‘Cannot load mcrypt…’ And I cannot login – ‘#1045 Access denied’. I was able to access mysql in Terminal with the password I set.
    I loaded phpMyAdmin in Sites/
    /private/etc/php.ini :
    line 624 ;extension=php_mcrypt.dll
    line 761 mysql.default_socket = /tmp/mysql.sock

    Anything I can do next?

  11. Andrrew

    Well, silly me, I thought I could log in with my user name, which is a system administrator. I did not know about root. Now I’m in. Thanks.

  12. Jonathan Dean

    Thanks, everyone. Glad this has been useful to some other people as well :)

  13. ben

    Thank you so much for the awesome instructions. You have helped me get closer than anyone else! i am now able to login to mysql through terminal and was able to set the password, but now I am unable to login to phpmyadmin. I am getting this error: #2002 – The server is not responding (or the local MySQL server’s socket is not correctly configured)

    per your instructions, i edited line 1060 of my php.ini file to read as follows: pdo_mysql.default_socket=/tmp/mysql.sock

    so i think the mysql socket is correctly configured. still not sure what’s wrong, any help would be greatly appreciated :)

  14. ben

    nvm got it working! YOU ROCK! thanks again for the fantastically easy and accurate guide!

  15. Pingback: Running PHP and MySQL with phpMyAdmin on OS X 10.5 | Jonathan Dean | astounDOnline

  16. tom

    JD, you’re a star! I finally got things working after closely following your instructions. One slight modification from me was to replace all occurrences in the php.ini file with /tmp/mysql.sock and restart apache. That seemed to do the trick.

  17. jenvill

    Hi,

    Thank you for the post. I now know the solution of my problem in php. Nice work! Keep it up!

  18. arumdev

    I’m having issues setting this up on my macbook pro. I have apache up and running (has been for years) and must have already installed mysql before as it was there, but I have downloaded and installed the latest version (64 bit). I get the crypt error but also can’t log in, have tried many settings but am not that familiar with a lot of this.

    #1045 Cannot log in to the MySQL server

    when trying to log in.

    I’m going to restart everything now and see if that helps. Any suggestions would be really welcome.

  19. dan

    thanks man this was very useful for me, I did everything as described and it works.

    it’s only that i could not figure out my mysql root pw (i installed that earlier) and i dunno how to reset it, but i keep on mining :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>