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.
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
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:
create an entry in the db table for the database
set the default permissions for the database
create an entry in the user table for any users who should be allowed to access the database
set default permissions for each user
All these are achieved by performing simple INSERT or UPDATE queries on the tables in question.
Table 3-2. Available permissions include ...
Select | May perform SELECT queries |
Insert | May perform INSERT queries |
Update | May perform UPDATE queries |
Delete | May perform DELETE queries |
Create | May create new tables |
Drop | May drop (delete) tables |
Reload | May reload the database |
Shutdown | May shut down the database |
Process | Has access to processes on the OS |
File | Has access to files on the OS's file system |
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) );