MySQL

LPIC, Oracle, MySQL, Ruby: Certifications

I decided to study for LPIC-1. I already have basic skills of Unix, but not so much about administrating. Also, as my background is unusual, I would like my employer and collegues to make sure what skills I already have. LPIC sounds reasonable because the knowledge is applicable to any Linux systems.

After that, I may study for Oracle Bronze, which is the lowest level in Oracle Certificates. As the lowest level, the exam doesn’t require much knowledge about Oracle-specific skills. Most of the contents are usable in MySQL. Or I may simply take MySQL exams.

In terms of programming, there’s a Ruby certificate. While this sounds promissing, I’m not really sure if it’s worth in everyday programming. The exam just seems to be asking memorization. Look at the sample questions someone else made.. Also, there’s no established book or resources available at this time. I can’t throw $150 for that sort of exam yet.

I’m not a big fan of certificates, but they will help me in some way or other.

P.S.: CompTIA seems less known here in Japan. Also there are some certificates that an organization supported by the govement offers, but they are only taken twice a year (spring and fall), I will avoid them for now.

MySQL Error in MacPorts

Someone who followed my installation tutorial got stuck. Please report any similar issues at MacPorts Trac.

Installing PHP and MySQL on Leopard with MacPorts (pre-alpha)

  1. If you are brave, you can try out the entropy version of PHP package. It’s beta. Try with care.

  2. If you encounter an error when installing sqlite3, run sudo port clean sqlite3, and reinstall it. See the bug report.


Installing PHP and MySQL on Leopard

(Important: This documentatoin is still pre-alpha. I still have a MySQL launching problem. If you find any error on this tutorial, please email me.)

Why Install?

As the built-in PHP is missing some key features, it’s not very usable as a web development environment. This tutorial aims to instruct the way to install PHP 5, Apache2 and MySQL 5 for Mac OS X 10.5 (Leopard) running under Intel-based Macs.

Prerequisite

If you are not comfortable with editing files with command line tools such vi, pico and emacs, I would highly suggest that you use a GUI editor with command-line tool support. This tutorial uses TextMate. You can install TextMate’s command line tool, mate from “Help” → “Terminal Usage…”.

(TODO: screenshot for a dialog for the mate command install might be added.)

You should have a basic understanding of Mac OS X and Unix. Familiarity with basic commands like cd, ls, which, man, sudo and chmod is a prerequisite. Unix FAQ at The macosxhints Forums is a great resource if you get into trouble. After running a command that start with sudo, you are asked to enter your password. Try add sudo if you see permission errors.

We are using MacPorts as a tool to install them. MacPorts is (from the official website):

A system for compiling, installing, and managing free and open source software comprised of an infrastructure called MacPorts base and a collection of ports. MacPorts current port collection defines the software may be installed.

With MacPorts, you don’t have to download source files, configure and compile them on your own.

Installing MacPorts

MacPorts has an excellent documentation for users. Follow the guide. The easiest way is to follow the package install as written in section 2.3.1. Don’t forget to install Xcode Tools before opening the package. In addition, after installing MacPorts with the package, be sure to set the path as written in section 2.4. Your system doesn’t recognize MacPorts unless you set the right path in .profile.

Read MacPorts guide carefully. I would rather not explain each step for MacPorts installation. The guide is easy-to-read and well-written.

You can check make sure that MacPorts is ready to use by runnning the following command in Terminal:

port help

MacPorts uses port as the command. The command above shows help page of the port command. That’s a compact description of various usage of the command. It should start with somethig like this:

port
    [-bcdfiknopqRstuvx] [-D portdir] [-F cmdfile] action [privopts] [actionflags]
    [[portname|pseudo-portname|port-url] [@version] [+-variant]... [option=value]...]...

If you see an error at this point, double-check the path in .profile. After editing .prifile, you need to open a new Terminal window. Read the section carefully.

Installing MacPorts doesn’t guarantee that everything works. We need to (a) check if there’s any update of MacPorts itself, and we need to (b) update the list of available installs.

We use selfupdate for (a) and sync for (b). Both require admin access, which means you prepend sudo to the command itself. After runnin the commands, you will be asked to type your password.

(a) selfupdate

sudo port selfupdate

(b) sync

sudo port sync

(Note: You don’t have to use port sync because port selfupdate includes port sync.) As is often the case with Unix commands, if you see no message, the command run successfuly.

Installing PHP, MySQL and Apache with MacPorts

What we want are:

  • PHP, MySQL and Apache installation
  • Files needed for automatic startup

We install PHP, MySQL and Apache all at once. We need all of them working together. We also want to start the web server (Apache) and database (MySQL) automatically when we log into Mac OS X. Starting the Apache and MySQL manually everytime you restart your Mac is a pain in the ass.

The automatic startup process is done by launchd. We want to download the files needed for launchd, not just files needed to make the server up and running. (d in launchd stands for daemon.)

The command we use is port install. We want to install PHP 5 that comes with Apache and MySQL. To get other necessary installs such as Apache and MySQL, we use port variants. Variants are (from the offical documentation):

Variants are a way for port authors to provide options for a port that may be chosen during the port install.

Variants are added when used with plus sign (+).

As of this writing, installing Apache with MacPorts on Mac OS X (Leopard) gives an error. We use a workaround to avoid the issues. The workaround is to install gawk and nawk before anything else. (See the ticket for details.)

Let’s install the two tools above. You will be asked for your password.

sudo port install gawk

Wait until everything finishes. Then, install the other one. This will take some time.

sudo port install nawk

Okay. Let’s dive into the most important part. We are going to install PHP 5, MySQL 5 and Apache 2 at the same time. Run the following command. Wait a while. Time for coffee.

sudo port install php5 +apache2 +mysql5 +server

(Note: +server is not listed as a variant for php5. +server is a variant for mysql5. Without +server option, you don’t get a file needed for launchd.)

MacPorts output a log on your Terminal screen while installation. The information is useful when you encounter a problem.

Automatic startup with launchd

We are going to set up our installs to make them work together. First we will run launchd that is necessary for the automatical startup. You may get some errors after running the commands because you haven’t configured Apache or MySQL yet. Just run the commands now. (See the man page to know more about launchd)

sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

and

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

To lean more about launchctl, visit the man page As you may have notices, the two files we loaded are located in /Library/LaunchDaemon. But with some exceptions, MacPorts installs everything at /opt.

Runnin Apache and PHP

Let’s set up Apache and PHP. Run the following command.

cd /opt/local/apache2/modules

and

sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

We then need to make two configuration files. One for Apache and the other for PHP. We don’t write configuration files from scrach. We simply make a copy of a default setting and rename with a new name.

PHP’s configuration file (php.ini) can be made by copying the sample file.

sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini

Apache’s configuration file (httpd.conf) can be made by copying the sample file.

sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf

To lean more about cp command, visit the man page .

Editing Apache Configuration files

In order for Apache to work with PHP, you need to edit the configuration file. You can use your favorite text editor. TextMate, BBEdit, SubEthaEdit, vi, emacs or pico will do for you. I use mate command of TextMate as an example.

mate /opt/local/apache2/conf/httpd.conf

If you get an error, the command tool, mate, is not installed.

Workaround of ssl_module

There is a bug in a module. In the httpd.conf, find “LoadModule ssl_module modules/mod_ssl.so” and comment out the line. # sign is used for comments in Apache files. The resultant line now should look like this.

(TODO: More explanation for why this happens will be good.)

#LoadModule ssl_module modules/mod_ssl.so

Activating PHP via Apache files

Apache file needs to be configuired so that it can work with PHP.

Add the following the httpd.conf file and save it.

LoadModule php5_module modules/libphp5.so
AddHandler php5-script .php
AddType text/html .php
DirectoryIndex index.php

Check if Apache and PHP work correctly. Before running the web server, make sure you turn off “Web Sharing” at Sharing in System Preferences. You will get an error if you try to do the command below with Web Sharing on.

Now let’s run Apache server by running:

sudo /opt/local/apache2/bin/apachectl restart

(Note: you have more than one apachectl installed because Mac OS X comes with it. Therefore you need to specify the path to the command. Just to remember “ctl” means “control”.)

Where your web files are located

The files you access from your browser is located at /opt/local/apache2/htdocs. The directory already has several folders including index.html. Visit localhost. If you see the webpage say “It works.”, your web server is running without a problem. Congraturations. Apache is working.

(TODO: screenshot will be added.)

Next, we want to know if PHP is working. Make a php file that contains a PHP script. I will do so by creating a file called “test.php”.

mate /opt/local/apache2/htdocs/test.php

Write a short script shown below in the file and save it.

<?php phpinfo(); ?>

phpinfo function shows your PHP configuration. Visit localhost/test.php. It will output a long page that contains all the configuration information starting with your PHP version number.

(TODO: screenshot will be added)

Activating MySQL

You can’t still use MySQL yet. Let’s initialize the database. Run the command.

sudo -u mysql mysql_install_db5

Make a configuration file, my.cnf by copying from /opt/local/share/mysql5/mysql/my-small.cnf.

sudo /opt/local/share/mysql5/mysql/my-small.cnf /opt/local/etc/mysql5/my.cnf

Run the MySQL daemon. By this command, MySQL will keep on running in the background.

sudo /opt/local/lib/mysql5/bin/mysqld_safe

(Note: We haven’t set a password for root. It is recommended that you set a password for root.)

Let’s use MySQL. Open another Terminal window or tab, and run the command:

mysql5 -u root

Did you see an error like this?

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)

If you see the error, you may not have a socket file. Create a file with touch command. touch command simply creates an empty file.

sudo touch /opt/local/var/run/mysql5/mysqld.sock

(FIXME: I have no idea why I need to restart the sytem.)

And restart the system and try once again. (Note: I don’t know why MySQL gets stuck for this problem. Need your help!)

If successful, you see the welcome message like this:

Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.0.51 Source distribution

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql>

Type quit to exit from MySQL. Now we know MySQL is working.

Setting up a password for MySQL

We haven’t set a password for the root user. As the root user has a permission to do anything, this is a security risk. We use the following command to set up a root password. Replace the ‘secret-password’ with your original password.

mysqladmin5 -u password 'secret-password'

Then, let’s connect to MySQL with your password.

mysql5 -u root -p

You will be asked to enter your password. Type your password press return.

See the welcome message? Then the password you just set works. Exit from MySQL by running quit.

Connecting MySQL via PHP

We not modify the test.php so that we test if your PHP script can connect to MySQL.

mate /opt/local/apache2/htdocs/test.php

Type the following script in test.php and save it. Don’t forget to change the “secret_password” with the password you just for your root password. When you save it, you will be asked to enter your password.

<?php

$connection = mysql_connect("localhost", "root", "secret_password");

if ($connection) {
    echo "MySQL connection is successful.";
} else {
    echo "MySQL connection failed.";
}

?>

Now visit http://localhost/test.php. If you see “MySQL connection is successful.”, your connection is successful. If you get an error: Access denied for user 'root'@'localhost' (using password: YES), then your passwod is in correct. Make sure your password in test.php is correct.

Important File Locatoins

You at least should remember the important file locations. During your adventure with web development, you will find many other tutorials that ask you to change configuration. You basically need the following information.

  • Apache configuration file is /opt/local/apache2/conf/httpd.conf.
  • PHP configuration file is /opt/local/etc/php.ini.
  • Web root directory is /opt/local/apache2/htdocs/.
  • MySQL configuration file is /opt/local/etc/mysql5/my.cnf.

References

Progress Report on the Setup

Update: I removed the content. Check newer posts for updated information. I will break this writing into a few parts. Visit Projects for updated articles.

  1. Installing PHP and MySQL with MacPorts on Leopard
  2. Installing PEAR and Symfony from Terminal on Leopard
  3. Set up a web server for Easy Ajax Tutorial on Symfony

I will update more this weekend and early next week.

Preliminary Report on the Setup

I finally got PHP 5 and MySQL 5 working together on my Mac. I installed them with MacPorts.

I already have PEAR installed as written in a previous blog entry.

Which means… I’m getting close.

You may see a nice write-up on the installation here. Stay tuned. Visit Projects for any progress.

Failing to Compile MySQL

I was installing my system from scratch today. This is a clean install, so I need to install all Unix command-line tools I need. Luckily, though, Leopard comes with updated and new tools such as ruby, rails and svn. But, it doesn’t come with MySQL.

I’m not a user of MacPosts or Fink, so I referred to Dan Benjamin’s tutorial on compiling MySQL. I followed every single step, but I failed and failed. After looking into the problem, I noticed what was wrong.

The problem is that my download of the souce file “mysql-5.0.45.tar.gz” was not complete. I don’t know how curl command works, but here are steps I took instead.

  1. Make a folder named “src” at my home folder
  2. Download the source file and put the file to the src folder. (It says “Compressed GNU TAR archive (tar.gz)” As of this writing, the latest version is 5.0.51.)
  3. Double click the source file to extact files. Here, a folder will show up full with files. (This stem is equivalent to tar command in the tutorial)
  4. Using Terminal, type in cd ~/src/[folder_made_in_step3]. (The name of the folder depends on the name of the source file. The tutorial says “mysql-5.0.45”, but it’s totally up to what version to the source file you downloaded.)

Then, you can start to configure MySQL following the tutorial.

PHP, MySQL and me

At work, I’m an entry-level PHP-MySQL developer. Currently, my skills are fairly limited. In terms of databasing, I will keep on learning MySQL from performansing to system maintainance. PostgreSQL has become less and less popular, and Oracle sounds, to me, something very exceptional and isn’t open source.

However, other than database, I’m not clear about where I go. At least, for now, I want to learn every bit of PHP 5, which is the version I am using at work. The language choice dosn’t solve my concern. What I need to do during my exploration of PHP is that I need to learn object orientation and model-view-control frameworks. My current plan is to go with PEAR and Smarty.

As I use classes/objects in PEAR, I will learn how to use PEAR. At this moment, my use of PEAR is very passive. I’m not able to create classes with inheritance. PEAR is like a library of methods rather than classes to me. My use of PEAR is mostly copy-and-paste. But, I hope that using PEAR deepens my understanding of object-oritented programming. (Also, object-orientation is built-in to languages like Objective-C and Ruby, both of which I expand my knowledge about in the near future).

Well, object-orientation only doesn’t bring us to make better web apps. Next up, yes, frameworks. Separation of logic and view is essential to create apps faster and better. Recently, we saw two major frameworks getting more and more popular on a daily basis. CakePHP and Symfony. I’m not familiar with the difference between them, but both provide solid foundations for model-view-controller architecture. Surely, Ruby on Rails may be more sophisticated and well grounded. For the time being, there is no plan adopt Rails as a web framework in my workplace. (hint, hint, hire me if you’re interested.) Even though popularity of PHP is going downwards, it’s the tool I use in order to bring home the bacon. Selecting which framework now might not be my priority. Both CakePHP and Symfony are likely to be fairly challenging. I want to start with somehting more readily usable by someone like me. So now, Smarty. Yes, Smarty is not exactly based on MVC architecture, but it’s still, I believe, a good start.

Getting down to OOP with PEAR and MVC with Smarty, I hope I will be more prepared to consider my future skill and career. I, as a developer, would like to establish my skills with at least Objective-C/Cocoa and one well-formed MVC framework like CakePHP, Symfony, Rails, Django and Catalyst.

Any comments are appreciated. I would like your opinions.

Learning PHP & MySQL, 2nd Edition

O’Reilly - Safari Books Online:

PHP and MySQL are quickly becoming the de facto standard for rapid development of dynamic, database-driven web sites. This book is perfect for newcomers to programming as well as hobbyists who are intimidated by harder-to-follow books. With concepts explained in plain English, the new edition starts with the basics of the PHP language, and explains how to work with MySQL, the popular open source database. You then learn how to put the two together to generate dynamic content. If you come from a web design or graphics design background and know your way around HTML, Learning PHP & MySQL is the book you’ve been looking for.

This is the second edition of “Learning” level of PHP & MySQL. The book introduces XHTML and its importantance as well as basics of PHP, MySQL, security and HTML forms.

Great for beginners like me. When I start to learn web programming and designing, most of the resource didn’t encourage use of frames and other depricated features in HTML. Even now I feel the need to learn web designing only with HTML 3 and Adobe GoLive.

XHTML is not the future, but the present.

O’reilly titles that start with “Learning” have “入門”(nyumon) in titles for Japanese-translated counterparts. Use the information to look for translated books. The second edition is not available in Japanese, though.

A Visual Explanation of SQL Joins

I will send a link to this page everytime I get asked about SQL joins.

(Via Coding Horror: A Visual Explanation of SQL Joins.)

Syndicate content