Installing MySQL Server on a Mac with Homebrew

djangohomebrewMySQLpython

As a total newbie I am trying to get through this tutorial: http://net.tutsplus.com/tutorials/python-tutorials/python-from-scratch-creating-a-dynamic-website/

I'm at the point where I've used brew install MySQL and have this version installed: mysql-5.6.10. I am running python 2.7

When I try to run mysqld in terminal to start the server I get this error:

Davids-MacBook-Pro:~ David$ mysqld
2013-03-17 18:20:28 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2013-03-17 18:20:28 3818 [Warning] Can't create test file /usr/local/var/mysql/Davids-MacBook-Pro.lower-test
2013-03-17 18:20:28 3818 [Warning] Can't create test file /usr/local/var/mysql/Davids-MacBook-Pro.lower-test
mysqld: Can't change dir to '/usr/local/var/mysql/' (Errcode: 2 - No such file or directory)
2013-03-17 18:20:28 3818 [ERROR] Aborting

2013-03-17 18:20:28 3818 [Note] Binlog end
2013-03-17 18:20:28 3818 [Note] mysqld: Shutdown complete

Best Answer

I've just installed MySQL (same version as you've listed) using Homebrew and encountered problems similar to yours.

I will note that my copy of Homebrew is probably not healthy; brew doctor continues to give me warnings, especially about some alleged installation of MacPorts or fink, and I plan to re-image this old box from scratch at some point.

That said I hacked my way through the problem using the following:

mkdir /local/var/mysql

# From the `brew info mysql` command's output:
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

(That's only three commands; ignore the # comment if you paste these into your terminal).

From there I ran mysqld_safe --skip-grant-tables in one terminal and, from another, connected to that server using mysql -u root. From there I ran:

UPDATE mysql.user SET Password=PASSWORD('**MY_NEW_PASSWORD**') WHERE User='root';
FLUSH PRIVILEGES;

Exit out of the server session by clicking Ctrl-C and then I killed mysqld_safe using mysqladmin -u root -p shutdown and restarted MySQL with Homebrew's link: mysql.server start

That seems to have done the job. I don't know whether it will automatically start MySQL when I restart my Mac; but I don't want that anyway. If that works for you it should be enough to get you into the tutorial itself. You might have to open a terminal and run mysql.server start every time you want to work with your databases, of course ... and I'll let you search on adding it to launchctl on your own if that's what you want).

Note the part about running mysqld_safe --skip-grant-tables is a way of "breaking in" to your MySQL databases, enabling you to locally connect as the 'root' (MySQL super-user or admin) without knowing the old password). Other posts here on SO suggest that Homebrew should have set up the directory and set the initial password to "new-password" for you.

The UPDATE command I've shown here is part of the conventional process by which any MySQL admin would force a password recovery for any MySQL system where the root password had been lost. The database named "mysql" stores MySQL's own management tables, and the "user" table within that has columns for "Password" and "User." The PASSWORD() function takes plain text (the password as you intend to type it) and hashes it ... which is to say it performs a complex set of mathematical operations on a numerical encoding of the string/text which is similar to a "checksum." MySQL stores such hashes in the Password column of mysql.user rather than the plain text of the password itself.

Password hashing is a feature of almost all operating systems, database systems, and most other system and applications where passwords are used. If you eventually write web applications then it's very likely that you'll use some sort of hashing function for storing and checking passwords.

As I said, I use the command brew info mysql to get some suggestions about how to "fix" the installation of MySQL; you can use that command for any other stuff that you install through Homebrew if it doesn't seem to be working as you expect.

Heres' the other StackOverflow article which I used for further help: https://stackoverflow.com/questions/4359131/brew-install-mysql-on-mac-os

I have intentionally over-explained some of these details because you've expressed that you are very novice at all of this.

(One of these days some really experience SO participant is going to tell me the preferred way to cross-link to other SO questions, answers, and comments).

Related Question