Computer Bookshelf

Revised installation instructions for PHP, Apache & MySQL

Setting the root password on MySQL (Mac OS X)

Many apologies to Mac users. When I wrote the Mac instructions in the Appendix, I (wrongly) assumed that Allan would show Windows users how to set up the root password with MySQL Monitor. This is how you do it:

  1. First of all, make sure you have added MySQL to your PATH, as described on pages 498-500.
  2. Open Terminal, and type the following command (followed by Return):
    mysql -u root
  3. You should see a welcome message like this:MySQL welcome message
  4. The most common problem is to instead get an error message like the following one:MySQL not running
    It means that MySQL is not running. If you installed the MySQL Startup Item, the command to restart the MySQL server is
    sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
    Enter your Mac password when prompted, and go back to step 2.
  5. Assuming you have logged in successfully as described in step 2, type the following command at the mysql> prompt (follow this and all subsequent commands with Return):
    use mysql
  6. You should see the message "Database changed", which means MySQL is ready for you to work on the files controlling administrative privileges. Now enter the command to set a password for the root user. Substitute myPassword with the actual password you want to use. Also make sure you use quotes where indicated and finish the command with a semicolon:
    UPDATE user SET password = PASSWORD('myPassword') WHERE user = 'root';
  7. Next, remove anonymous access to MySQL:
    DELETE FROM user WHERE user = '';
  8. Tell MySQL to update the privileges table:
    FLUSH PRIVILEGES;

    The sequence of commands should produce a series of results like this:Setting the root password on MySQL
  9. To exit MySQL Monitor, type exit.
  10. To get back into MySQL Monitor in future, type the following command:
    mysql -u root -p
    and enter your password when prompted.
  11. Congratulations, you have a secure installation of MySQL.