Documente Academic
Documente Profesional
Documente Cultură
Concepts
Client/Server
mysqlcheck
for
checking the integrity
of the database files.
Client Programs
These are programs that are used
for communicating with the
server
to
manipulate
the
databases. MySQL AB provides
following client programs:
MySQL Query Browser
and
MySQL
Administrator: MySQL
Workbench provides
DBAs and developers
an integrated tools
environment for:
Database Design
&Modeling
SQL Development
Database
Administration
Database
Migration
mysql:mysql is one of
the
MySQL
client
programs. It is a
command-line program
that acts as a textbased front end for the
MySQL Server. It's used
for issuing queries and
viewing the results
interactively from a
terminal window.
To determine the options
supported by a MySQL
following
statement
command-line
importing
files
data
mysqldump for
making backups
mysql admin for
server
administration,
and
mysqlcheck for
checking
the
integrity of the
database files.
Unit-2
MySQL
Program
Client
-p
-u
-h
-u
-h
SOURCE
client
host
where
youre
running mysql. The filename
must either be an absolute
pathname listing the full name of
the file For example, if you
started mysql on a Windows
machine in the C:\mysql directory
and
your
script
file
is
my_commands.sql
in
the C:\scripts directory, both of
the following SOURCE commands
tell mysql to execute the SQL
statements in the file:
mysql>
SOURCE
C:\scripts\my_commands.sq
mysql>
SOURCE
..\scripts\my_commands.sq
2- What
are
statement
terminators in MySQL? Explain
various options for statement
terminators in MySQL with
suitable examples.
A terminator is necessary after
each
statement
because mysql allows several
queries to be entered on a single
input line. There are several
terminators to end a statement.
Two
terminators
are
the
semicolon character (;) and
the
\g
sequence.
Theyre
equivalent and may be used
interchangeably:
mysql>
SELECT
VERSION(), DATABASE();
+----------------+------------+
| VERSION()
DATABASE() |
+----------------+------------+
|
5.0.10-beta-log
world
|
+----------------+------------+
mysql>
SELECT
VERSION(), DATABASE()\g
+----------------+------------+
| VERSION()
DATABASE() |
+----------------+------------+
|
5.0.10-beta-log
world
|
+----------------+------------+
The \G sequence also terminates
queries, but causes mysql to
display query results in a vertical
style that shows each output row
with each column value on a
separate line:
mysql>
SELECT
VERSION(), DATABASE()\G
*************************
1.
row
*************************
VERSION():
log
5.0.10-beta-
DATABASE(): world
The \G terminator is especially
useful if a query produces very
wide output lines because
vertical format can make the
result much easier to read.
If you are using mysql to define a
stored routine or a trigger that
uses compound statement syntax
and
consists
of
multiple
statements, the definition will
contain semicolons internally. In
this case, it is necessary to
redefine the ; terminator to
3- What
are
MySQL(Client)
Commands? What are long
form and short form for them?.
Answer- When you issue an SQL
statement while running mysql,
the program sends the statement
to the MySQL server to be
executed. SELECT, INSERT, UPDATE,
and DELETE are examples of this
type of input. mysql also
understands a number of its own
commands that arent SQL
statements.
The
QUIT
and SOURCE commands that
STATUS;
of a backslash followed by a
single character (such as \., \s,
or \h). The long forms may be
given in any lettercase. The short
forms are case sensitive.
4- What are the different types of
Output Formats in MySQL?
How will you change your
output from default format to
another format?
AnswerBy
default, mysql produces output
in one of two formats, depending
on whether you use it in
interactive or batch mode:
1. When
invoked
interactively, mysql displ
query output in a
tabular format that
uses bars and dashes
to display values lined
up in boxed columns.
2. When
you
invoke mysql with a
file as its input source
on
the
command
line, mysql runs in
batch mode with query
output displayed using
tab
characters
between data values.
To override the default output
format, use these options:
a. --batch or -B
options:
c. --html or -H
Produce output in HTML
format.
d. --xml or -X
Produce output
format.
in
XML
Unit-3
Architecture
MySQL
is an example of invoking
"mysqld" with the "--console"
option:
>cd \mysql\bin
>mysqld --console
mysqladmin
mysqladmin is a client tool
program for database server
administrators to manage a
MySQL server remotely. The
syntax to run mysqladmin is:
\mysql\bin\mysqladmin
[options]
command
[command-arg]
It support a number of commonly
used commands like:
"mysqladmin shutdown"
Shuts down the server.
"mysqladmin
ping"
Checks if the server
alive or not.
is
"mysqladmin
status"
Displays
several
important server status
values.
"mysqladmin
Displays
information
server.
version"
version
of
the
"mysqladmin
create
databaseName" - Creates a
new database.
"mysqladmin
drop
databaseName" - Drops an
existing database.
Mysqldump
Mysqldump is part of the mysql
relational database package
which allows you to "dump" a
database, or a collection of
databases,
for
backup
or
transferral to another SQL server.
The server that imports the
databases does not have to be
mysql.
The typical way to
mysqldump is one of
following commands:
use
the
mysqldump
[options]
db_name [tables]
mysqldump
[options]
--
databases
DB3...]
DB1
mysqldump
[options]
all-databases
[DB2
--
mysqlimport
"mysqlimport" - A command-line
interface for administrators or
end users to load data files into
tables program tool to load data
into tables. Here is a sample
commands
supported
by
"mysqlimport":
"mysqlimport
databaseName
fileName" - Imports the data
from the specified file to the
specified database. The data will
Protocol Types
of Supporte
Connections Operatin
Systems
TCP/IP
Local,
remote
All
Unix
socket
Local only
Unix only
file
Named
pipe
Local only
Windows
only
Windows
only
supported only
Windows.
Shared-memory
connections
supported
by
Windows servers.
on
are
all
information?
Explain
the
various types of buffers
(caches) used by MySQL to
hold information in memory.
Answer- MySQL Server memory
use includes data structures that
the server sets up to manage
communication with clients and
to process the contents of
databases. The server allocates
memory for many kinds of
information as it runs:
Thread handlers. The server is
multi-threaded, and a thread is
like a small process running
inside the server. For each client
that
connects,
the
server
allocates a thread to it to handle
current transactions in
a
memory
buffer.
When a transaction
commits, the log buffer
is
flushed
to
the InnoDB log files,
providing a record on
disk that can be used
to
recommit
the
transaction if it is lost
due to a crash. If the
transaction rolls back
instead, the flush to
disk need not be done
at all
Unit-5 Locking
1- What is locking? Explain its
importance in MySQL with
suitable examples.
Answer-Locking is a mechanism
that prevents problems from
occurring with simultaneous data
access by multiple clients. Locks
are managed by the server: It
places a lock on data on behalf of
one client to restrict access by
other clients to the data until the
lock has been released. The lock
allows access to data by the
client that holds the lock, but
places limitations on what
operations can be done by other
must
not
be
interrupted by other
clients. For example,
an application might
select a value from one
table and then use it
to determine which
records to update in a
set of other tables.
2- What are the levels at which
data locking occurs in MySQL?
Explain their characteritics.
Answer-Data locking in MySQL
occurs at different levels. Explicit
locks
acquired
with
LOCK
TABLES are table locks. For
implicit locks, the lock level that
MySQL uses depends on the
storage engine:
MyISAM,
MEMORY,
and MERGE tables are
locked at the table
level.
BDB tables are locked
at the page level.
InnoDB tables are
locked at the row level.
The different levels of locking
granularity
have
different
concurrency characteristics:
Table locking is not as
desirable as page or
row
locking
for
concurrency in a mixed
read/write
environment. A table
Implicit Locking:
For a client that does nothing
special to acquire locks, the
server implicitly acquires locks as
necessary to process the client's
statement safely. For example,
the server acquires a read lock
when the client issues a SELECT
statement, and a write lock when
the client issues an INSERT
statement.
Implicit locks are acquired only
for the duration of a single
statement.
Explicit Locking:
If implicit locking is not sufficient
for a client's purposes, it can
to it.
READ LOCAL
Locks a table for reading, but
allows concurrent inserts. A
concurrent insert is an exception
to the readers block writers
principle.
It
applies
only
to
MyISAM
tables.
If
a MyISAM table has no holes in
the middle resulting from deleted
or updated records, inserts
always take place at the end of
the table. In that case, a client
that is reading from a table can
lock it with a READ LOCAL lock to
allow other clients to insert into
the table while the client holding
with examples?
AnswerAdvisory Locking
An advisory lock is a cooperative
lock. That is, an advisory lock has
no power to prevent data access
by other clients, but instead is
based on the concept that all
clients will use an agreed-upon
convention to cooperate for use
of a resource.
Advisory locks are implemented
using a set of function calls. To
acquire
a
lock,
use
the GET_LOCK()function:
mysql>
SELECT
GET_LOCK('my lock', 5);
+----------------------
-- +
| GET_LOCK('my lock',
5) |
+----------------------- +
|
1
|
+----------------------- +
The first argument is a string that
specifies the name to be locked,
and the second argument is a
timeout value in seconds that
indicates how long to wait for the
lock if it cannot be acquired
immediately. GET_LOCK() returns
1 for success, 0 if a timeout
occurs and the lock cannot be
updates.
To specify a storage engine
explicitly
in
a
CREATE
TABLE
statement,
use
an ENGINE option. The following
statement
creates
t
as
an InnoDB table:
CREATE TABLE t (i INT) ENGINE =
InnoDB;
To determine which storage
engine is used for a given table;
you can use the SHOW CREATE
TABLE or the SHOW TABLE
STATUS statement:
mysql> SHOW CREATE TABLE
City\G
The
INFORMATION_SCHEMA
file
length)
are
supported
on
filesystems
and
operating systems that
support large files.
Dynamic-sized rows are
much less fragmented
when mixing deletes
with
updates and
inserts. This is done by
automatically
combining
adjacent
deleted blocks and by
extending blocks if the
next block is deleted.
The maximum number
of indexes per table is
64 (32 before MySQL
4.1.2)..
The maximum key
length is 1000 bytes
(500 before MySQL
4.1.2).
BLOB
and
TEXT
columns
can
be
indexed.
NULL
values
are
allowed in indexed
columns. This takes 0-1
bytes per key.
All numeric key values
are stored with the
high byte first to allow
better
index
compression.
Index files are usually
InnoDB:
MyISAM tables are stored
in separate files in
compressed
mode,
whereas InnoDB tables
are stored in table space.
More options for further
optimization in MyISAM,
whereas there is no
further
optimization
possible with InnoDB.
Data except for TEXT and
BLOB can occupy at most
8000 bytes in InnoDB.
Full
text
indexing
available in MyISAM,
whereas no full text
indexing is available for
InnoDB.
Count
(*)
functions
execution is slower in
InnoDB because of table
space complexity.
4- When is InnoDB engine
preferred
over
MyISAM?
Explain the transaction model
of InnoDB engine.
5- Differentiate MyISAM and
InnoDB storage engines? How
they differ in their locking
mechanism?
6- What is Merge table? When
are they used? What are their
disadvantages?
7- Write short notes oni) Heap
(Memory)
Engine
ii) Federated Engine
iii) Cluster Engine
Unit-8
Backup
Recovery
and
Preserves
the
databases in exactly
the same format in
which MySQL itself
stores them on disk.
To restore copy the
files back to their
original locations.
Techniques
copy commands
(such as cp or
tar)
mysqlhotcopy
InnoDB
Hot
Backup
Its faster to make a
binary backup.
For
transferring
databases to another
machine that uses a
different architecture,
the files must be
binary portable.
With binary backup
methods, its necessary
to make sure that the
server does not modify
the files while the
backup is in progress.
Binary
backups
depends on which
storage engine created
the tables
Text Backup
A text backup is a
dump of database
contents into text files.
Restoration
involves
loading
the
file
contents back into
databases
by
processing
them
through the server.
Techniques
SELECT ... INTO
OUTFILE
SQL
statement
Mysqldump
MySQL
Administrator.
Its slower to make a
text backup because
the server must read
tables
Text
backups
are
portable
and
independent
of
machine architecture.
With
text
backup
methods, the server
must
be
running
because it must read
the files that are to be
backed up.
Text
backup
procedures are more
general and can be
used for tables created
by any storage engine.
3- What are the steps involved in
making
Binary
MyISAM
Backups?
AnswerMaking Binary MyISAM Backups
To make a binary
backup of a MyISAM
table, copy the .frm,
.MYD, and .MYI files.
While copying the
table must not be in
use so:
Either stop the server
while copying the table
Use an appropriate
locking protocol to
prevent server access
to the table.
For example:
mysql>
USE
world;
mysql>
LOCK
TABLES
Country READ;
mysql>
FLUSH
TABLES
Country;
To recover a MyISAM
table from a binary
backup,
stop
the
server,
copy
the
backup table files into
the
appropriate
database
directory,
and restart the server.
in an option file:
[mysqld]
lower_case_table_
5- How MySQL Administrator
helps in Making Text Backups?
Explain the procedure.
AnswerMaking Text Backups with MySQL
Administrator
GUI
program
to
provide backup and
restore capabilities.
It generates backup
files containing SQL
statements that can be
reloaded into your
MySQL server to re-
i) Mysqlhtocopy
Answermysqlhotcopy
A Perl script,
requires the
DBI module to
be installed.
It runs on Unix
and NetWare.
Works
for
MyISAM
tables but not
InnoDB tables.
Operation of
mysqlhotcopy
is fast
Steps
involved:
mysqlhotc
connects
to
the
local
MySQL
server
locks the
tables so
that the
server
will not
change
them,
flushes
the
tables to
make
sure that
any
pending
changes
are
written
to disk
Copies
the table
files.
When it
has
finished
the copy
operation
it
unlocks
the
tables.
Many options,
invoke it with
the
--help
option.
Some
examples:
Back up the
world
database to a
directory
named world
in
the
/var/archive
directory:
shell>
mysqlhot
world
/var/arc
Back up only
the tables in
the
world
database
whose name
contains
Country:
shell>
mysqlhot
world./C
/var/arc
available from
Innobase Oy.
It can back up
InnoDB tables
while
the
server
is
running
without
disturbing
normal
database
activity.
Its available
for Unix and
Windows.
iii) Mysqldump
AnswerMysqldump
It can dump
all databases,
specific
databases, or
specific tables.
mysqldump
can back up
local
or
remote
servers,
although the
destination
for the dump
files depends
on how you
invoke it.
It works for
tables created
by any storage
engine.
Output
files
are written in
text
format
and
are
portable.
Three general
modes
of
operation:
Default:
shell>
mysqldum
world
>
world.sq
shell>
mysqldum
world
City
Country
>
city_cou
--databases
option
shell>
mysqldum
-database
world
test>
world_an
--all-databases
option
shell>
mysqldum
--alldatabase
>
alldb.sq
after
the
backup
was
made.
Option
files
used by the
server (my.cnf
and
my.ini
files).
These
files contain
configuration
information
that must be
restored after
a crash.
Replication
slave servers
create
a
master.info
file
that
contains
information
needed
for
connecting to
the
master
server, and a
relay-log.info
file
that
indicates the
current
progress
in
processing the
relay logs.
Replication
slaves create
data files for
processing
LOAD
DATA
INFILE
statements.
These files are
located in the
directory
named by the
slave_load_tmp
system
variable,
which can be
set by starting
the
server
with the -slave-loadtmpdir option.
If
slave_load_tmp
files can be
backed
up
with
no
special
precautions.
Dynamic files
such as logs
that the server
changes as it
runs are best
backed
up
with
the
server
stopped.