home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeMySQL and mSQLSearch this book

5.5. Database Administration

Now that your server is running 24 hours a day, 7 days a week, your next task is to get it to serve your database needs. mSQL provides a set of utilities that support easy server administration.

5.5.1. The msqladmin Utility

The msqladmin utility is your primary database administration tool. It supports creating, deleting, copying, renaming, and examining your mSQL databases. If you look back to our discussion of what a database is in Chapter 1, "Introduction to Relational Databases", you will remember that mSQL itself is not a database. Your databases are the collection of files under each directory in the msqldb directory. mSQL is the database engine. One engine can serve many databases simultaneously. The msqladmin utility lets you administer the databases under a particular server. Server status

If you have been playing with MySQL, you will notice that the monitoring of server status is one area in which mSQL is decidedly lacking. The msqladmin utility is your interface into monitoring your servers. The msqladmin stats command under mSQL 2 will produce the following output:

Server Statistics

Mini SQL Version - Forge Alpha Build #9
Copyright (c) 1993-94 David J. Hughes
Copyright (c) 1995-98 Hughes Technologies Pty Ltd.
All rights reserved.

Config file      : c:\usr\local\hughes\msql.conf
Max connections  : 61
Cur connections  : 1
Running as user  : UID 500

Connection table :
  Sock  Username    Hostname    Database    Connect   Idle   Queries
 +-----+----------+---------- +------------+---------+------+--------+
 |   5 | randy    | Unix sock | No DB      |  0H  0M |    0 |      1 |
 |  13 | bob      | client.com| mydata     |  0H  5M |    2 |      4 |
 +-----+----------+------ ----+------------+---------+------+--------+

This output likely needs a little explanation:

Max connections

The maximum number of simultaneous connections that the server can handle.

Cur connections

The current number of connections to the database server.


The Internet socket number used by mSQL to identify each connection.


The username of the person connected to the server.


The hostname of the machine connected to the server. "Unix sock" is shown if the client is connecting from the local host via a Unix socket.


The name of the database to which the user is currently connected. "No DB" means that the client has not chosen a database.


The total time the client has been connected to the server in hours and minutes.


The amount of time in minutes since the client's last query.


The total number of queries the client has sent using this connection.

In addition to the msqladmin stats command, you can monitor other, more static, server information via the msqladmin version command. Your output might look something like this:

Version Details :-

    msqladmin version - Forge Alpha Build #9
    mSQL server version - Forge Alpha Build #9
    mSQL protocol version   23
    mSQL connection via TCP/IP
    Target platform         CYGWIN32_NT-4.0-i586

Configuration Details :-

    Default config file     c:\usr\local\hughes/msql.conf
    TCP socket              1114
    Unix socket             c:\usr\local\Hughes\msql2.sock
    mSQL user               msql
    Admin user              root
    Install directory       c:\usr\local\Hughes
    PID file location       c:\usr\local\Hughes\msql2d.pid
    Memory Sync Timer       30
    Hostname Lookup         False

Each of the values in the output of msqladmin version can be set via the mSQL 2 configuration file.

If mSQL has been compiled with debugging enabled, mSQL will place information about the running server process into the debug file chosen at compile time. mSQL provides no other logging facilities.

5.5.2. Backups

Good backups are a vital part of any administration scheme. Database corruption can happen and, if severe enough, can cripple all applications that depend on the database. As the saying goes, data is only as good as the most recent backup.

There are a couple of backup methods available when using mSQL. Like most of mSQL, they do not provide all of the bells and whistles but they do get the job done. The msqldump command is the most commonly used method for backing up mSQL databases. This command produces a full standard SQL dump of an entire database. You must issue a separate msqldump command for each database on the system. For example:

msqldump database1 > /usr/backups/database1.sql.daily
msqldump database2 > /usr/backups/database2.sql.daily
msqldump database3 > /usr/backups/database3.sql.daily

This example creates a dump of three different databases into the same directory.

You use the file extension daily to indicate that the backup files are replaced every day. How often you backup your data will depend on the importance of the data and the amount and type of backup storage you have available. Because only full dumps are available from mSQL, the size of the backups can grow large on systems that contain a great deal of data. If you have enough space, a good backup plan is to keep separate daily backups for each day of the week -- or for two weeks or even a month. At the end of the cycle, the tapes are reused as needed -- or the files are overwritten if backing up to hard disk. This way there is always one week of data available. If backing up to hard disk, you can possibly condense the individual daily backups into a single daily backup that is overwritten each day. In this case, you should also keep a separate weekly backup in order to recover accidentally deleted data that cannot be found on the most recent backup. Because of the lack of redundancy, this plan should only be used if you have a limited amount of backup space available.

The other method of backing up mSQL databases involves taking advantage of the simple nature of the mSQL data files. Unlike some database packages, mSQL keeps its data in regular files stored in the native operating system's file system. Thus is it possibly to act on these files as if they were any other type of file. Therefore a complete backup of an mSQL system can be obtained by shutting down the mSQL server and then creating a tar archive of the mSQL data directory. You must shut down the server first or the archive data files could be incomplete or corrupt.

Restoring data from mSQL data backups is as simple as creating the backups. Dumps created with msqldump are in standard SQL that can be fed to the msql monitor. These dumps contain the instructions to create the tables as well as the data, so you should either remove the existing table, if it exists, or remove the CREATE TABLE statement from the backup file. If you are restoring only specific rows of data, you can simply copy them out of the backup file and feed them into the msql monitor.

When restoring data from a tar archive of the mSQL data directory, it is only possible to do a full restore to the backed up state. You cannot restore only parts of the data, and any data that has been added to the database since the backup will be lost. To perform this restore, simply shutdown the mSQL server then enter the backup file into the mSQL data directory. When you restart the server, it will be in exactly the state it was in when the backup was performed -- except that any new databases added will still be there, untouched.

Which method you choose to use depends on your needs. Creating a binary archive is simple and complete and allows for a very fast recovery time. However, it is not possible to do a partial recovery and any data you have added since the backup will be lost. The SQL dump method on the other hand can be slow, but it allows for partial recovery, albeit with a little work. In addition, the SQL dump method can be performed at any time, while a binary archive required the server to be shut down, which can be a deciding factor in a busy installation.

One final concern to consider is portability. Unlike a binary backup, a SQL dump consists entirely of plain ASCII SQL commands. With little modification -- mainly weeding out any of the mSQL specific SQL -- a mSQL SQL dump can be imported into any standards compliant SQL server. This is a very handy way to transport your data if you ever need to switch SQL servers.

5.5.3. Security

Depending on your point of view, the mSQL security scheme is either one of its advantages or one of its disadvantages. On the one hand, mSQL's security is easier to manage than any other server-based relational database engine available. It accomplishes this ease of maintenance through simplicity. Unfortunately, this simplicity is insufficient for even moderately complex database applications.

mSQL manages security through a file called msql.acl stored in the mSQL installation directory. The .acl extension refers to "Access Control List," a very flexible form of authorization that has been in use on several operating systems and applications for some time. The format of the msql.acl file looks like this:



Each database has a stanza of options. The read and write lines indicate which users you want to give read (SELECT) or write (INSERT,UPDATE,DELETE) access to the database. The host line specifies which hosts can connect remotely to the database. If the access line contains "local," local Unix socket connections are allowed. Similarly, if the access line contains "remote," remote TCP connections are allowed.

The ACL file allows a "*" wildcard entry in the read, write, and host fields. You could therefore have the following ACL:


Under this ACL, anyone from any host at client.com or anyone from any host at any domain ending in isp.com -- for example, wisp.com and lisp.com -- can connect to the database. These users can read from the database, but only user names beginning with "msql" can modify the database.

By default, everything is excluded. Thus, if you leave out a write entry, nobody can modify the database. Specific users and hosts may be excluded by prefixing their entries with a "-". Consider the following:


This ACL enables all machines from the isp.com domain to connect, except for junk.isp.com. In addition, everyone except "bob" can read from the database. Only "jane" can write to the database. Because rejection is mSQL's default, having specific rejection entries like "bob," is meaningless unless the line also contains a wildcard entry.

mSQL acts on the first match it encounters. If, for example, the wildcard in the read entry came before "-bob," the wildcard would have matched "bob" and "bob" would have read access.

The msqladmin reload command, as noted earlier in the chapter, reloads an ACL after you have made any changes. If you fail to issue the msqladmin reload command, your changes will not be seen until the server shuts down and starts back up.

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.