 |
» |
|
|
 |
|  |  |
Starting and Stopping the MySQL Database |  |
To start the MySQL database and verify the daemon is started,
enter the following commands as the MySQL root user: # /etc/init.d/mysql
start # ps –ef
| grep mysqld To stop the MySQL database and verify the MySQL daemon has disappeared,
enter the following commands as the MySQL root user:  |  |  |  |  | NOTE: If you have a large amount of data, shutting down the database
might take several minutes. |  |  |  |  |
# /etc/init.d/mysql
stop # ps –ef
| grep mysqld Creating and Deleting the MySQL Database |  |
In this section, you create a database, delete a database, and
add a table. Use the mysqladmin command to create
a database. For this example, create a database named osmsdb by entering the following command: # mysqladmin –u root –p create osmsdb At the prompt, enter the MySQL root password. Start the MySQL client by entering the following command: # mysql –u
root –p osmsdb Create a sample table by entering the following: mysql> create table sample (
id int(10) auto_increment not null primary key,
name char(20),
index(id)
); |
This creates a new table named sample with two columns, one called id of type integer and the other called name of type string. The table is indexed by the id column, which is also the table's primary key. If the table is created without any errors, the following
is displayed: Query OK, 0 rows affected
(0.04 sec) For a detailed explanation of the SQL commands available in
MySQL, see “SQL Statement Syntax” in the MySQL
5.0 Reference Manual, which can be found at: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax.html To delete the database, use the mysqladmin command as follows: # mysqladmin –u root –p drop osmsdb  |  |  |  |  | CAUTION: This command permanently removes all the tables and data contained
in the specified database. Use the mysqladmin drop command with caution. |  |  |  |  |
Enter the database root password, at the prompt. Enter y to confirm deletion of
the database. The following should be displayed: # mysqladmin -u root -p drop osmsdb
Enter password:
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'osmsdb' database [y/N] y
Database "osmsdb" dropped |
Administering User Accounts |  |
This section describes how to administer user accounts on the
MySQL server, including how to set up new accounts, remove existing
accounts, and grant access rights to a user account.  |  |  |  |  | NOTE: In general, should set up a new account for any user or application
that accesses the MySQL server. This allows different users to have
access to only the specific databases and tables they need. |  |  |  |  |
Setting Up a New AccountStart the MySQL command-line client by entering the following
command: # mysql –u root –p At the prompt, enter the MySQL root password. To create a database user, use the following command: mysql> create user username ; For example, enter the following: mysql> create user
osmsusr ;  |  |  |  |  | CAUTION: This command creates a user named osmsusr, which can connect to the MySQL database from any system. |  |  |  |  |
To verify creation of the database user account, the following
command is used: mysql> show grants for username ; For example, enter the following: mysql> show grants
for osmsusr ;
Removing a User AccountStart the MySQL command-line client by entering the following
command: # mysql –u root –p At the prompt, enter the MySQL root password. To delete a database user, use the following command: mysql> drop user username ; For example, enter the following: mysql> drop user
osmsusr ;
 |  |  |  |  | NOTE: The drop user command removes privilege rows
for the user account from all grant tables, but it does not automatically
close any open user sessions. Therefore, if a user with an open session
is dropped, the command does not take effect until that user session
is closed. After the session is closed, the user is dropped. Thereafter,
any attempts by the user to log in fail. |  |  |  |  |
Granting Access RightsUse the grant command to manage the access
rights for a database user. You can grant each MySQL user specific
read and write permissions for every table and database stored in
the MySQL system. Additionally, you must run the flush command in order for the grant command to take
effect. Start the MySQL command-line client by entering the following
command: # mysql –u root –p At the prompt, enter the MySQL root password. To grant privileges to a database user account, use the
following command: mysql> grant all privileges on database.* to username@'hostname' \ identified by 'password'
; For example, enter the following: mysql> grant all
privileges on osmsdb.* to osmsuser@'%' \ identified by 'osmspass' ; For more information about the grant command,
see “MySQL Access System” in the MySQL 5.0
Reference Manual located at: http://dev.mysql.com/doc/refman/5.0/en/privilege-system.html Immediately after granting a database user permissions,
activate the permissions by entering the following: mysql> flush privileges;
Verifying Database Operation |  |
System administrators can query the MySQL server to obtain a
number of current status values, which indicate basic health and performance,
by running the following mysqladmin command: # mysqladmin –u
root –p status At the prompt, enter the database password. Figure 2 shows the
results. Alternatively, enter the following mysqladmin command to view a more detailed status report: # mysqladmin –u
root –p extended-status Figure 3 shows
the results. Creating the Sample Database for MySQL |  |
To perform the tests in this blueprint, you need a sample database
that uses multiple storage engines and is already populated with sample
data. Use the following procedure to create the sample database: Using your favorite text editor, create the following
SQL script to generate the sample database:  |
drop database if exists osmsdb;
create database osmsdb ;
use osmsdb ;
create table customer
(
customerID int(10) auto_increment not null primary key,
name char(20),
purchased datetime not null,
product varchar(30),
productID int,
INDEX (customerID)
)
TYPE = InnoDB;
create table employee
(
id int(5) auto_increment not null primary key,
depno int(5),
name varchar(20) not null,
cardnumber bigint(15) not null
);
insert into customer(name,purchased,product,productID) \
values( 'Jenny','2006-07-07 13:00:33','HP Laptop nc6220',5001), \
('Kate','2006-07-09 09:38:12', 'HP LaserJet 5M',9076);
insert into employee(depno,name,cardnumber) values( 10,'Mike',100081),\
(10,'Tom',100082),(10,'Jack',100083), (20,'Mary',100084),(20,'Cherry',100085),\
(20,'Jones',100086);
select * from customer ;
select * from employee ;
grant all on osmsdb.* to osmsusr@'localhost' identified by 'osmspass';
grant all on osmsdb.* to osmsusr@'%' identified by 'osmspass';
grant select on osmsdb.* to osmsusr2@'localhost' identified by 'osmspass2' ;
grant select on osmsdb.* to osmsusr2@'%' identified by 'osmspass2' ;
flush privileges; |
 |
Save the script as osmsample.sql. Import the script by entering the following command: # mysql –u
root –p < osmsample.sql At the prompt, enter the database password. The following results are displayed: # mysql -u root -p < osmsample.sql
Enter password:
customerID name purchased product productID
1 Jenny 2006-07-07 13:00:33 HP Laptop nc6220 5001
2 Kate 2006-07-09 09:38:12 HP LaserJet 5M 9076
id depno name cardnumber
1 10 Mike 100081
2 10 Tom 100082
3 10 Jack 100083
4 20 Mary 100084
5 20 Cherry 100085
6 20 Jones 100086
# |
If this error occurs, remove the pre-existing database using
the instructions in “Creating and Deleting the MySQL Database ”, and rerun the script to create the
database.  |  |  |  |  | NOTE: For security, after testing is complete, you must destroy all
sample objects, such as the sample database, user accounts, and tables. |  |  |  |  |
|