Login / Status
developer.Resource
Home . Documentation . Document Library . Extension Manuals
Sponsors
hosted by punkt.deTYPO3 and Open Source Magazine

1.8. MySQL

About

Now, it's time for the MySQL relational database system. It will contain all the tables needed by Typo3. MySQL isn't shipped with Mac OS X. Therefore, OS X already provides a user which will execute the MySQL daemon. The official distribution also comes as prebuilt binaries, so we won't have to compile anything.

Installation

1. Getting the binaries

To download MySQL, go to http://dev.mysql.com/downloads/mysql/4.0.html, scroll to the OS X section, and choose the download highlighted in the screenshot below:

When you're done with the download, uncompress the file, open a terminal window, and log as root.

2. Installing

First, copy the folder you just decompressed to /usr/local/. The command will be something like that, depending of where you have decompressed the file (remember, you must have root privileges!):

cp -prf Desktop/mysql-4.0.x-apple-darwin6.6-powerpc /usr/local/mysql

Now, always logged as root, cd to /usr/local/mysql/ and run the installation script:

./scripts/mysql_install_db

We also need to change to owner of the mysql directory:

chown -R mysql /usr/local/mysql/*

That's it. We can start the daemon:

./bin/safe_mysqld --user=mysql &

The last step is to secure the installation, by providing a password for the mysql root user. To do that, type:

/usr/local/mysql/bin/mysqladmin -u root password your_password

Configuration

1. Startup item

We don't want to have to start the MySQL daemon by hand, like we've just do, each time the computer starts. So we are going to build a startup file to automatically launch MySQL when the Mac is booting.

You first need to create a directory called MySQL in /Library/StartupItems/. Then, we are going to add two files in that directory. The first one is the startup script. It must be named MySQL and contain the following lines:

#!/bin/sh
. /etc/rc.common
if [ "${MYSQL:=-YES-}" = "-YES-" ]; then
ConsoleMessage "Starting MySQL"
cd /usr/local/mysql/
if [ -e ./bin/safe_mysqld ]
then
./bin/safe_mysqld --user=mysql &
elif [ -e ./bin/mysqld_safe ]
then
./bin/mysqld_safe --user=mysql &
else
ConsoleMessage "MySQL not found"
fi
fi

The second file contains informations for OS X. It must be named StartupParameters.plist, and must contain those lines:

{
Description   = "MySQL";
Provides      = ("MySQL");
Requires      = ("Resolver");
Preference    = "Late";
Messages =
{
start = "Starting MySQL";
stop  = "Stopping MySQL";
};
}

The last thing to do is to chown and chmod the files, to allow their executions:

sudo chown -R root:admin /Library/StartupItems/MySQL/
sudo chmod 755 /Library/StartupItems/MySQL/MySQL

2. Environment variables

Maybe you noticed that, in the installation section, we called the mysqladmin program with an absolute path. That's because the system does not know that the MySQL utilities are in the /usr/local/mysql/ directory. We are going to let him know, so we can run those binaries like the other Unix commands. You need to create a file called .profile in the home directory of the root user. In the terminal, as root, type:

cd /var/root/
pico .profile

Then enter the following, save and close the file:

export PATH=$PATH:/usr/local/bin:/usr/local/mysql/bin

Then, copy that file to your home directory:

cp .profile /Users/your_username/

We are over with the configuration part. Now we can use the MySQL utilities described next section.

MySQL commands

Here you will find some of the basic MySQL shell commands. It's not exhaustive. Refer to the MySQL manual for more details about commands.

1. mysql

That's the MySQL database system itself, where you can execute queries. When you call it from the terminal, you must use sudo, because it needs root access. You also need to add the p option to tell MySQL you are going to use a password, since we've defined it previously. For example:

sudo mysql -p

Then you're in. I'm not going to teach you how to make MySQL requests. You can check the documentation section below to see where you can find informations about this.

The mysql programs also allow you to import .sql files in a specified database. For example:

sudo mysql -p databasename < mysqlfile.sql

2. mysqladmin

That's the administration program of MySQL, which allow, as an example, to create or delete databases. You must call it like mysql, with sudo and with the p option. The default syntax is:

sudo mysqladmin -p command

Command:

Description:

create databasename

Create a database.

drop databasename

Delete a database and all its tables.

extended-status

Give an extended status report from the server.

ping

Check if the MySQL daemon is running.

processlist

Show all the running processes.

reload

Reload grant tables.

refresh

Flush all tables and close log files.

shutdown

Take the server down.

status

Give a short status from the server.

version

Show the MySQL version.

3. mysqldump

That program is used to export .sql files from your database system. You must call it like mysql, with sudo and with the p option. The default syntax is:

sudo mysqldump -p [optional params] databasename > mysqlfile.sql

Option:

Name:

Description:

-A
All DB

Create a dump file containing all the databases.

-a
All

Include all the MySQL specific create options.

-B
Databases

Create a dump for several MySQL databases, passed as arguments.

-c
Complete

Create complete INSERT instructions.

-d
Data

No row data information. Dump only the structure.

-e
Extended

Create extended INSERT instructions.

-l
Lock

Lock all tables for read.

-q
Quick

Don't buffer the query.

-t
Tab

Also create a tab delimited file with the standard .sql file.

--opt

Adds the ADD DROP TABLE, ADD LOCKS instructions, and includes -a, -e, -l and -q

Database creation

We are now ready to create the database needed to run Typo3. In a terminal window, type:

sudo mysqladmin -p create typo3

In this example, we use typo3 as the name of the new database. You can of course choose another one.

User creation

Now we are going to create a user, and associate him to our new database. Never use the MySQL root user for a web application. Always create a new one. This is done like this, in mysql (sudo mysql -p):

GRANT ALL ON typo3.* TO username@localhost IDENTIFIED BY 'userpassword';

The GRANT instruction add privileges to a user for a specific database. Please refer to the MySQL manual for more detailed explanations about this instruction.

Administration

We can use the terminal for everything related to MySQL, but there are some easier ways. It's now time to install PHPMyAdmin. This is a PHP tool for the administration of a MySQL database system. It provides a user-friendly graphical interface to manage your databases and tables. First, go to the PHPMyAdmin website (http://www.phpmyadmin.net/) and download the latest stable release.

1. Configuring PHPMyAdmin

You need to decompress the file, and put the resulting folder on your server's document root. It should be /Library/Servers/Server-1/Documents/, if you followed this tutorial. Rename that folder «phpmyadmin». It will be easier to access. Now, in that folder, there's a file called config.inc.php. That's the configuration file of PHPMyAdmin. Open it in your favorite text editor, and change the lines as in the example below:

[...]
$cfg['PmaAbsoluteUri'] = 'http://127.0.0.1/phpmyadmin/';
[...]
$cfg['Servers'][$i]['auth_type']     = 'http';

That's the minimal configuration. You can of course continue to modify that file if you know how to do it, and if you feel so.

2. Accessing PHPMyAdmin

To access PHPMyAdmin, open a browser window, and go to http://127.0.0.1/phpmyadmin/. You'll be asked for a username and a password. You can either choose root, or the user you created before. If you log as root, you'll have full access to the program. In the other case, you'll only have access to your database(s).

3. Documentation

PHPMyAdmin is a complex program. If you're unexperienced with databases, you should take a look at the manual, which is available at http://www.phpmyadmin.net/home_page/docs.php.

Documentation

To get additionnal documentation about the MySQL database system, you can get the complete manual on the MySQL AB website (http://dev.mysql.com/doc/).