- MySQL
- What Is MySQL?
- Installation
- Configuration for Use with IMP
- Database System Concepts
- The IMP Database Structure
- SQL Overview
- MySQL's License
- Onward!
Configuration for Use with IMP
You can configure MySQL by hand, or you can use the script that the IMP software package includes. I will show you how to configure MySQL for IMP by hand, and then I will show you how to do it using the script. To get to the MySQL script, you must unpack the IMP tarball.
As you go through this exercise, if you make a mistake, you can use the SQL DROP command to drop the table or database you created. You can then start over. Please don't DROP any table in the mysql database. Don't DROP the mysql database! If you do, you will have to reinstall MySQL!
First, let's look at the disk storage issue. I have decided I want to store the MySQL database files in the /home partition because I have plenty of free space on that partition. Because I won't have many users who can log in through telnet or secure shell on this machine, it is safe to do this.
Before you move things around, you need to stop MySQL. When you installed the rpm, an init script was stored in the /etc/rc.d/init.d directory. It is called mysql. It is a convention for all init scripts to obey the commands start and stop. Most also obey the restart command. So, to stop the MySQL server, run the following:
[root@winbook imp-2.0.11]# /etc/rc.d/init.d/mysql stop Killing mysqld with pid 622
The script will always respond with the process ID of the mysqld server. It also places a message in an error log file in the data directory. The logfile name is made up of the local machine's hostname, obtained by running the hostname command, with .err tacked on the end. If you run cat on the .err file in the /var/lib/mysql directory, you will see the startup and shutdown of the MySQL server:
[root@winbook mysql]# ls /var/lib/mysql
Output
mysql test winbook.err [root@winbook mysql]# cat /var/lib/mysql/winbook.err mysqld started on Sat Mar 18 08:31:42 CST 2000 /usr/sbin/mysqld: ready for connections 000318 8:31:57 /usr/sbin/mysqld: Normal shutdown 000318 8:31:57 /usr/sbin/mysqld: Shutdown Complete mysqld ended on Sat Mar 18 08:31:57 CST 2000
Now that MySQL is stopped, you will move the database directory to where you want it. Linux will not allow you to move directories across file systems. Therefore, you must copy the directory. If you use the -a command-line option for cp, it will preserve permissions and directory structure. Follow these commands, and notice the results in Listing 3.3.
Listing 3.3 Moving MySQL's Database To Another Location
[root@winbook lib]# cd /var/lib [root@winbook lib]# cp -a mysql /home/mysql [root@winbook lib]# ls /home Office51 httpd lost+found maxfield mysql root [root@winbook lib]# ls /home/mysql mysql test winbook.err [root@winbook lib]# ls -l /home total 36 drwxr-xr-x 26 root root 4096 Feb 18 16:26 Office51 drwxr-xr-x 5 root root 4096 Feb 28 18:11 httpd drwxr-xr-x 2 root root 16384 Feb 18 12:38 lost+found drwx------ 12 maxfield maxfield 4096 Mar 7 19:23 maxfield drwxr-xr-x 4 mysql root 4096 Mar 18 08:31 mysql drwxrwx--- 26 root root 4096 Mar 19 2000 root [root@winbook lib]# pwd /var/lib [root@winbook lib]# mv mysql mysql.old [root@winbook lib]# ln -s /home/mysql mysql ls[root@winbook lib]# ls cddb dosemu games mysql mysql.old nfs rpm slocate svgalib texmf xkb [root@winbook lib]# ls -l mysql lrwxrwxrwx 1 root root 11 Mar 18 10:14 mysql -> /home/mysql [root@winbook lib]# cd mysql [root@winbook mysql]# ls -l total 12 drwx--x--x 2 mysql root 4096 Mar 18 11:14 mysql drwxr-xr-x 2 mysql root 4096 Mar 18 11:14 test -rw-r--r-- 1 root root 960 Mar 18 08:31 winbook.err [root@winbook mysql]# pwd /var/lib/mysql [root@winbook mysql]# df . Filesystem 1k-blocks Used Available Use% Mounted on /dev/hda7 7178940 1101984 5712284 16% /home [root@winbook mysql]#
What happened? First, I copied the mysql data directory, which is under /var/lib/ to the /home directory, using the -a option. Then I made sure it was there, with the proper permissions. After that was verified, I made sure I was still in the /var/lib directory, and moved the mysql directory to mysql.old. If I make a mistake, I still have the original directory. I then created a soft link to the new mysql data directory, and did a cd to that directory. I verified that directory was indeed on the /home partition by using the df command.
If you have plenty of room on your root partition, you don't have to do this. If you have a partition mounted under a directory other than /home that you would like to put the data directory on, you can use it instead.
Finally, if you want to create an /etc/my.cnf file or modify the safe_mysql startup script to place the data directory elsewhere, you can. However, this can require modifying MySQL every time you upgrade it, and at the least requires additional study. It is a matter of personal taste. I believe in the motto "If it ain't broke, don't fix it." I recommend you become familiar with MySQL's default behavior before you take any such action.
To start MySQL after you have moved the database, enter the following:
[root@winbook /root]# /etc/rc.d/init.d/mysql start [root@winbook /root]# Starting mysqld daemon with databases from /var/lib/mysql [root@winbook /root]# ps ax | grep my
Output
1145 pts/1 S 0:00 sh /usr/bin/safe_mysqld --user=mysql \pid-file=/var/ 1162 pts/1 SN 0:00 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql 1164 pts/1 SN 0:00 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql 1165 pts/1 SN 0:00 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql [root@winbook /root]#
You will have to press Enter after the "Starting ..." message is displayed. Running the ps command gives you total assurance that MySQL has started. You can check the error file in the /var/lib/mysql directory for a final cross check.
Now that MySQL is started, run the mysql command-line utility and start creating the IMP database.
[root@winbook /root]# mysql -pmypass mysql
Output
mysql> INSERT INTO user ( host, user, password ) -> VALUES ( -> 'localhost', -> 'impmgr', -> password( 'impmgr' ) -> ); Query OK, 1 row affected (0.45 sec)
This SQL statement created a row in the user database that allows the impmgr user access. The password for impmgr is impmgr.
mysql> INSERT INTO db ( -> host, db, user, -> Select_priv, Insert_priv, Update_priv, Delete_priv, -> Create_priv, Drop_priv ) -> VALUES ( -> 'localhost', -> 'imp', -> 'impmgr', -> 'Y', 'Y', 'Y', 'Y', -> 'Y', 'Y' -> ); Query OK, 1 row affected (0.00 sec)
The INSERT INTO db statement sets the privileges for the impmgr user. That user is allowed to run the following SQL commands on the imp database: SELECT, INSERT, UPDATE, DELETE, CREATE, and DROP.
mysql> CREATE DATABASE imp; Query OK, 1 row affected (0.00 sec)
This statement actually creates the database. If you bring up an xterm and do an ls of the /var/lib/mysql directory, you will see a new directory named imp. This directory contains all of imp's database files.
mysql> use imp; Database changed
The use imp command tells the mysql utility to apply all the following SQL statements to the imp database, unless specifically overridden.
mysql> CREATE TABLE imp_addr ( -> user text, -> address text, -> nickname text, -> fullname text -> ); Query OK, 0 rows affected (0.46 sec) mysql> CREATE TABLE imp_pref ( -> user text, -> fullname text, -> replyto text, -> lang varchar(30), -> sig text -> ); Query OK, 0 rows affected (0.00 sec)
The CREATE commands created the imp tables. If you run the 'show tables;' command under mysql, it will list the two tables just created. The command 'show columns from imp_addr;' will show the columns for that table. Give it a try.
mysql> quit Bye [root@winbook /root]# mysqladmin -pmypass reload
The mysqladmin program is used to reload the grant tables so that you can use the new database privileges without taking the server down.
The easy way to accomplish this is to run the mysql_create.sql script, which is placed in the imp directory. The easiest way to run this script is to cd into the imp-2.0.11/config/scripts/ directory and run the following command:
[root@winbook /root]#mysql -pmypass <mysql_create.sql
This will interpret the statements in that text file as if they had been typed from the command line. This is a very powerful feature of MySQL. It enables you to build useful queries and keep them for future use.
Tip
Web-Based MySQL Administration
At http://phpwizard.net/projects/phpMyAdmin/index.html you will find a PHP Web application that can help you manage MySQL. It enables you to manage a MySQL database using a browser and makes administrative work much simpler.
After you have installed PHP and MySQL, you are ready to install phpMyAdmin. The work done while preparing for IMP has also prepared the system for phpMyAdmin. You do have to edit one of the .php3 files in phpMyAdmin to enter the MySQL database name and password.
With phpMyAdmin, you can create and drop databases and tables. You can also add and remove fields and perform other administrative chores.
I have found phpMyAdmin to be very useful when trying to create or delete tables. After you have filled out a form that describes the table, it generates the SQL for you. You can use this information to create script files for later use.
For now, phpMyAdmin has some bugs. I have had it crash on me several times. However, its usefulness is good enough for me to ignore the occasional crash.
From a security point of view, phpMyAdmin is terrible. After it is installed, everyone who uses that Web page has unrestricted access to the MySQL database. I strongly recommend that you do not put your server on the Internet with phpMyAdmin active. If you do, you must configure your Web server to serve only the phpMyAdmin Web pages to your local network. You should also protect the Web pages with a password. Be careful!