Difference between revisions of "MySQL"

From Christoph's Personal Wiki
Jump to: navigation, search
(Added more info. on "LOAD DATA INFILE")
(Added "Installing and Configuring MySQL" sections)
Line 1: Line 1:
 
'''MySQL''' is a multithreaded, multi-user, SQL (Structured Query Language) Database Management System (DBMS).
 
'''MySQL''' is a multithreaded, multi-user, SQL (Structured Query Language) Database Management System (DBMS).
 +
 +
== Installing and Configuring MySQL ==
 +
 +
In this section, I will show how to install and configure MySQL for a [[LAMP]]-based system. This is not only the system I recommend (it is [[OSS]]) and I am highly biased towards, it is the ''only'' system I will discuss. If you use any other system, consulte the Web or the many books available for purchase.
 +
 +
=== Installing ===
 +
 +
The official MySQL website is http://www.mysql.com. They have many excellent resources to offer and this article is not intended to replace any of these. The goal of this article is to offer a simple step-by-step process of getting MySQL up-and-running on a LAMP-based system.
 +
 +
An RPM installation is the simplest and officially recommended way of installing (and upgrading) MySQL. I like to use the binary installation process because it offers a bit more control on a platform-specific optimized distribution. This article will only discuss the binary installation process.
 +
 +
STEP#1: Choose the closest (geographical) mirror to you from the following URL:
 +
* http://www.mysql.com/downloads/mirrors.html
 +
 +
I use '''Linux (x86, glibc-2.2, "standard" is static, gcc) - Standard''' and, as of writing, it is around 30 MB. The "Standard" version should suit the needs of a standard user.
 +
 +
STEP#2: Terminal commands
 +
 +
Execute each of the following commands as the superuser ('root') of your system:
 +
<pre>
 +
shell> groupadd mysql
 +
shell> useradd -g mysql mysql
 +
shell> cd /usr/local
 +
shell> gunzip < /PATH/TO/MYSQL-VERSION-OS.tar.gz | tar xvf -
 +
shell> ln -s FULL-PATH-TO-MYSQL-VERSION-OS mysql
 +
shell> cd mysql
 +
shell> scripts/mysql_install_db --user=mysql
 +
shell> chown -R root .
 +
shell> chown -R mysql data
 +
shell> chgrp -R mysql .
 +
shell> bin/mysqld_safe --user=mysql &
 +
</pre>
 +
 +
Installation is complete.
 +
 +
=== Configuring ===
 +
 +
==== Set the MySQL Administrator Password ====
 +
 +
<pre>
 +
shell> mysql -u root mysql
 +
shell> SET PASSWORD FOR root@localhost=PASSWORD('secret');
 +
shell> FLUSH PRIVILEGES;
 +
</pre>
 +
 +
==== Datadir ====
 +
 +
I like to keep all of my user-generated data on a separate disk partition (usually <tt>/home</tt>). (This assumption will be made for the remainder of this article.) I like to treat mysql as any other user and give it a space under <tt>/home/mysql</tt>. This will be true for my database files.
 +
 +
<pre>
 +
shell> mkdir /home/mysql/data
 +
shell> mv /usr/local/mysql/data/* /home/mysql/data
 +
</pre>
 +
 +
Then MySQL will have to be passed the location of the database files ''every'' time you start MySQL:
 +
<pre>
 +
shell> ./bin/mysqld_safe --datadir=/home/mysql/data --user=mysql &
 +
</pre>
 +
 +
==== Starting and Stopping MySQL ====
 +
 +
* Starting
 +
<pre>
 +
shell> cd INSTALL-DIR
 +
shell> ./bin/mysqld_safe --datadir=/home/mysql/data --user=mysql &
 +
</pre>
 +
 +
* Stopping
 +
<pre>
 +
shell> cd INSTALL-DIR
 +
shell> ./bin/mysqladmin -u root -p shutdown
 +
</pre>
  
 
== External data input ==
 
== External data input ==

Revision as of 16:18, 1 May 2006

MySQL is a multithreaded, multi-user, SQL (Structured Query Language) Database Management System (DBMS).

Installing and Configuring MySQL

In this section, I will show how to install and configure MySQL for a LAMP-based system. This is not only the system I recommend (it is OSS) and I am highly biased towards, it is the only system I will discuss. If you use any other system, consulte the Web or the many books available for purchase.

Installing

The official MySQL website is http://www.mysql.com. They have many excellent resources to offer and this article is not intended to replace any of these. The goal of this article is to offer a simple step-by-step process of getting MySQL up-and-running on a LAMP-based system.

An RPM installation is the simplest and officially recommended way of installing (and upgrading) MySQL. I like to use the binary installation process because it offers a bit more control on a platform-specific optimized distribution. This article will only discuss the binary installation process.

STEP#1: Choose the closest (geographical) mirror to you from the following URL:

I use Linux (x86, glibc-2.2, "standard" is static, gcc) - Standard and, as of writing, it is around 30 MB. The "Standard" version should suit the needs of a standard user.

STEP#2: Terminal commands

Execute each of the following commands as the superuser ('root') of your system:

shell> groupadd mysql
shell> useradd -g mysql mysql
shell> cd /usr/local
shell> gunzip < /PATH/TO/MYSQL-VERSION-OS.tar.gz | tar xvf -
shell> ln -s FULL-PATH-TO-MYSQL-VERSION-OS mysql
shell> cd mysql
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> chgrp -R mysql .
shell> bin/mysqld_safe --user=mysql &

Installation is complete.

Configuring

Set the MySQL Administrator Password

shell> mysql -u root mysql
shell> SET PASSWORD FOR root@localhost=PASSWORD('secret');
shell> FLUSH PRIVILEGES;

Datadir

I like to keep all of my user-generated data on a separate disk partition (usually /home). (This assumption will be made for the remainder of this article.) I like to treat mysql as any other user and give it a space under /home/mysql. This will be true for my database files.

shell> mkdir /home/mysql/data
shell> mv /usr/local/mysql/data/* /home/mysql/data

Then MySQL will have to be passed the location of the database files every time you start MySQL:

shell> ./bin/mysqld_safe --datadir=/home/mysql/data --user=mysql &

Starting and Stopping MySQL

  • Starting
shell> cd INSTALL-DIR
shell> ./bin/mysqld_safe --datadir=/home/mysql/data --user=mysql &
  • Stopping
shell> cd INSTALL-DIR
shell> ./bin/mysqladmin -u root -p shutdown

External data input

Example:

LOAD [LOCAL] DATA INFILE 'foo.txt'
INTO TABLE company
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

where [LOCAL] is optional and the following must be met:

  • If LOCAL is specified, the file is read by the client program on the client host and sent to the server. The file can be given as a full pathname to specify its exact location. If given as a relative pathname, the name is interpreted relative to the directory in which the client program was started.
  • If LOCAL is not specified, the file must be located on the server host and is read directly by the server.

External links