DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OSXFAQ Mac OS X UNIX Tip-of-the-Day  back to index

MySQL - Create the Database

By Adrian Mayo - Editor - OSXFAQ

Monday's tip showed you how to grab and install MySQL. Today we'll create the MySQL database directory and test the installation.

The database directory, by default, is created under the actual installation in /usr/local/mysql/data. It's better to create it elsewhere, perhaps under /var where most server generated data, such as incoming mail and log files, is stored.

To become the root user (give your admin password) type:

$ sudo -s
Password:
#

Create the database directory /var/mysql-data (you can place it at another location if you wish). Type:

# mkdir /var/mysql-data
# chown mysql:mysql /var/mysql-data
# chmod 770 /var/mysql-data

Create the MySQL configuration file, /etc/my.cnf, which tells mysql where the database it held. This is what it should contain:

# cat /etc/my.cnf
[mysqld]
datadir=/var/mysql-data

Next, create the necessary administrative databases by typing:

# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/var/mysql-data

This should create all the necessary files with owners mysql:mysql.

Test the installation - start the server by typing:

# mysqld_safe --user=mysql &
[1] 19718
# Starting mysqld daemon with databases from /var/mysql-data

(If mysqld_safe cannot be found, see Monday's tip to add the MySQL executable directory to your PATH. Once you have done so, start a new terminal session. Alternatively, type the full path name to this and other MySQL commands - eg /usr/local/mysql/bin/mysqld_safe.)

Check that the three default databases are there

# mysqlshow                                       
+--------------------+
|     Databases      |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+

MOST IMPORTANT: enable the root password by typing:

# mysqladmin -u root password my-password
# mysqladmin -u root -h hostname.domain.com password my-password

Replace 'hostname.domain.com' with your host's name (eg 'mymac.local'). You might get away with typing just the first line. Obviously, replace my-password with your chosen MySQL root password. (Don't confuse the Unix root user with the MySQL root user - they are entirely separate.)

NOTE: From this point on, it's necessary to give the option -p to most MySQL commands and type root's password at the prompt.

As a further test of successful installation, stop the server by typing:

# mysqladmin -u root -p shutdown
Enter password:
STOPPING server from pid file /var/mysql-data/...
060111 00:51:57  mysqld ended

[1]+ Done mysqld_safe --user=mysql #

Next, we must create a Mac OS X startup item so MySQL will be launched when your Mac starts. Create a directory by typing:

# mkdir /Library/StartupItems/MySQL

Create two files in the directory, called MySQL and StartupParameters.plist. They are included in the Mac-style Package installer, but not in the Unix style Tar package.

Change to the appropriate directory:

# cd /Library/StartupItems/MySQL

Create the files with the following contents:

-- MySQL --
#!/bin/sh
#

# Startup Item for MySQL
# ######################

. /etc/rc.common

mysqlctl="/usr/local/mysql/support-files/mysql.server"

StartService ()
{
  if [ "${MYSQLCOM:=-NO-}" = "-YES-" ] ; then
    echo "Starting MySQL server"
    $mysqlctl start
  fi
}

StopService ()
{
  echo "Stopping MySQL server"
  $mysqlctl stop
}

RestartService ()
{
  echo "Restarting MySQL server"
  $mysqlctl restart
}

if [ -x $mysqlctl ]; then
  RunService "$1"
else
  echo "Cannot execute $mysqlctl"
fi


-- StartupParameters.plist -- 
{
  Description     = "MySQL Startup";
  Provides        = ("MySQL");
  Requires        = ("DirectoryServices", "NetworkExtensions");
  OrderPreference = "Last";
}

Make sure the script is executable:

# chmod +x MySQL

When you reboot, MySQL should automatically start up.

NOTE: If you copy and paste this code from the OSXFAQ forums, you might have problems because PHP-BB adds odd invisible characters to the code sections.

Wednesday's tip will show how to add a new database, and how to add a user to the database.


Visit the Site of the Book of the Unix Tips:


Discuss this trick in the OSXFAQ Learning Center forum

E-mail your comments or suggestions to webmaster@osxfaq.com


Copyright © 2000-2010 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.