This is an article for those who are new to MySQL bu want to use MySQL as database for Play Framework on Mac OS X. Actually, I have to admit that what I am suggesting to do in this article may not be suggested by some people. Because I am not to write single line of MySQL command but just use a MySQL GUI tool for Mac called Sequel Pro, which may, to some extend, make us lazy and less familiar with actual MySQL command.
But anyway I am to start this tutorial, which will allow you to install and setup MySQL server on your computer and use it for the Play Framework.
Install MySQL
If you have not install Homebrew, you should install it by pasting and run the following command in the terminal.
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"If you would like to know, Homebrew is a package manager for OS X. It allows
you to install software like what you do in Linux with apt-get or yum.
If you have already install Homebrew before, you are recommended to update
it with command brew update.
Then you should install the MySQL with brew.
brew install mysqlIn the end of the installation, brew will tell you what you should do if you want to make MySQL server run when your computer starts. The commands should be similar with ones below
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plistHowever, if you don’t want to do that, you can simply start your server when you need that by:
mysql.server startNote: It is always recommended to secure your MySQL server by the following
command. It will ask you to setup your root account password for MySQL
and do some other staffs.
mysql_secure_installationCreating Database
By typing mysql in the terminal, you could enter the MySQL monitor, and
you can create users and databases with SQL language.
If you do not want to do so, you could use some GUI tools to help you.
PHPMyAdmin is one of the most famous tools. But this would require you to install and setup apache and PHP on your Mac. For those who are even lazier like me, I would recommend Sequel Pro. You can download it from [here][Sequel Pro].
To connect your local MySQL server, in the connection window, you can
type anything for the Name, 127.0.0.1 for Host, root for the
Username if you never change or delete that, and your password you set
when you ran mysql_secure_installation. Please leave Database as
blank and Port as default.
After connecting to the server, you could use the top left switch input to create a new database, and use the top left ‘Users’ button to create a new user and grant it all privileges for the new database.
It is not recommended to use the root account for all your application.
(Although that’s what I used to do.)
Setup Play Framework
To setup MySQL in different versions of Play, the files to modified may be different.
In Play 2.2.x, all you need to do is …
Add some lines in build.sbt file in the root of project folder.
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
"mysql" % "mysql-connector-java" % "5.1.18"
)And then change your application.conf in conf folder according to
your database detail.
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://127.0.0.1:3306/dbname"
db.default.user="root"
db.default.password=""Remarks
This is a really basic tutorial. Hope it helps.