3.5. Setting up MySQL databases

A tool called mysqladmin is distributed with MySQL. This tool allows the database administrator (DBA) to create, remove, or otherwise manage databases.

Table 3-1. Mysqladmin commands:

create databasename Create a new database
drop databasename Delete a database and all its tables
flush-hosts Flush all cached hosts
flush-logs Flush all logs
flush-tables Flush all tables
kill id,id,... Kill mysql threads
password new-password Change old password to new-password
processlist Show list of active threads in server
reload Reload grant tables
refresh Flush all tables and close and open logfiles
shutdown Take server down
status Gives a short status message from the server
variables Prints variables available
version Get version info from server

More help for this command is available by typing mysqladmin --help from the command line or by reading the MySQL reference manual.

3.5.1. Creating the Acme inventory database

To create a database called inventory, we would perform the following steps as the user who has permission to run mysqladmin (eg root):

% mysqladmin create inventory
% mysqladmin reload

3.5.2. Setting up permissions

To set up security permissions for the inventory database, we would need to create appropriate records in the mysql database (that's right, it's a database which has the same name as the database server). This is the central repository for access control information for all databases served by your MySQL server.

Typically, you will want to:

All these are achieved by performing simple INSERT or UPDATE queries on the tables in question.

Table 3-2. Available permissions include ...

SelectMay perform SELECT queries
InsertMay perform INSERT queries
UpdateMay perform UPDATE queries
DeleteMay perform DELETE queries
CreateMay create new tables
DropMay drop (delete) tables
ReloadMay reload the database
ShutdownMay shut down the database
ProcessHas access to processes on the OS
FileHas access to files on the OS's file system

3.5.3. Creating tables

The SQL statements used to create tables are documented in the MySQL manual. CREATE statements are used to create each individual table by specifying the fields for each table, their data types and other options.

Below is an example --- these SQL statements create the Acme Widget Co. tables we will be working with throughout this session.

#
# Table structure for table 'customer'
#
CREATE TABLE customer (
  id int(11) DEFAULT '0' NOT NULL auto_increment,
  name text,
  address text,
  suburb text,
  state char(3),
  postcode varchar(4),
  PRIMARY KEY (id)
);

#
# Table structure for table 'sales'
#
CREATE TABLE sales (
  id int(11) DEFAULT '0' NOT NULL auto_increment,
  sale_date date,
  customer_id int(11),
  salesperson_id int(11),
  stock_item_id int(11),
  quantity int(11),
  price float(4,2),
  PRIMARY KEY (id)
);

#
# Table structure for table 'salesperson'
#
CREATE TABLE salesperson (
  id int(11) DEFAULT '0' NOT NULL auto_increment,
  name text,
  PRIMARY KEY (id)
);

#
# Table structure for table 'stock_item'
#
CREATE TABLE stock_item (
  id int(11) DEFAULT '0' NOT NULL auto_increment,
  description text,
  price float(4,2),
  quantity int(11),
  PRIMARY KEY (id)
);