Keywords
MySQL, DBA, basic knowledge
Introduction
In past years I was thrown in a project where I had to use MySQL database together with PHP
language. Until that time I was “faithful” to Oracle database. You have. At the beginning of the project
I had no knowledge about MySQL database. As an experienced Oracle user for last 27 years my first
impression was quite positive although everything was completely new for me. At the very beginning
I found phpMyAdmin application for MySQL administration and running ad-hoc queries against the
database.
I was immediately impressed by the speed, simplicity and small amount of resources used. My friends
asked me why I am not using Oracle XE instead. My answer was simple: MySQL is fast, uses much
less resources and perfectly matches all project requirements.
Last year at the Oracle ACE Director briefing at Oracle headquarters we were also briefed about
MySQL database and at that time we heard astonishing facts about MySQL. It is a well known fact
that MySQL is powering big social media like Facebook and Twitter. In Facebook MySQL is running
on +50k servers. Can you imagine how one can manage such high number of servers. These facts
drove me to conclusions that probably the picture about MySQL as a free open source database are far
from being close to reality and it .looks like a mature technology perfectly suitable for web
applications.
I am still a “beginner” in the world of MySQL. However, I decided to share with you some interesting
facts about MySQL which would help me to familiarize with MySQL faster if I would know them
from the very beginning.
First Impressions
Interface like sqlplus exists but is quite different. I was urgently looking for an interface to work with
the database like SqlDeveloper or something similar. I found phpMyAdmin, written in PHP language
which I was using as well in my project. This interface satisfied almost all of my needs. It is:
• Nice tool for interactive work with MySQL database
o creating databases, creating tables, indexes, referential integrity (if available in the
used database engine)
Figure 1. depicts the MySQL architecture in the latest versions. Oracle DBA can easily spot many
similarities with Oracle Database although there are also many differences. One of most important
ones is the fact that in MySQL one can choose among several storage engines (which are actually
pluggable storage engines) while that is not a case in Oracle database. Some of these storage engines
are quite simple, some more complex, so one has to carefully study his requirements and what kind of
the storage engine would be the best choice for his case. I will not discuss the capabilities of particular
storage engine. For my project I decided to use InnoDB storage engine as it supports also referential
integrity.
The main command line interface is mysql executable. Running the program looks like this:
E:\xampp\mysql\bin>mysql -u root -p
Enter password: **********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights
reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input
statement.
If we want to see the tables in certain database we must “connect” to that database or in other words
use that database.
mysql> use atlasptic;
mysql> show tables;
+------------------------------+
| Tables_in_atlasptic |
+------------------------------+
| atlas95 |
| besedila |
| geo_tetrade |
| geometry_columns |
....
mysql> show table status;
….
The “CREATE TABLE” command has some MySQL specifics like the specification of storage
engine.
Create view command is also slightly different than in Oracle and looks like the following:
CREATE/ALTER ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY
DEFINER VIEW `izvoz_podatkov_all` AS select `v`.`koda` AS
`koda`,`v`.`vrsta_slo` AS `vrsta_slo`,`ob`.`datum` AS
`datum`,`op`.`stevilo` AS `stevilo`,`op`.`id_enote_stetja` AS
`id_enote_stetja`,`op`.`id_statusa_opazovanja` AS
`id_statusa_opazovanja`,`op`.`belezka` AS `belezka`,`ob`.`id_projekta` AS
`id_projekta`,`ob`.`id_tipa_popisa` AS `id_tipa_popisa`,`ob`.`id_vira` AS
`id_vira`,`ob`.`natancnost_vnosa` AS `natancnost_vnosa`,`ob`.`tetrada` AS
`tetrada`,(case when (isnull(`ob`.`FI`) or (`ob`.`FI` = 0.0)) then
`t`.`phi_centr` else `ob`.`FI` end) AS `FI`,(case when
(isnull(`ob`.`LAMBDA`) or (`ob`.`LAMBDA` = 0.0)) then `t`.`lambda_centr`
else `ob`.`LAMBDA` end) AS `LAMBDA`,(case when (isnull(`ob`.`X`) or
(`ob`.`X` = 0.0)) then `t`.`X_centr` else `ob`.`X` end) AS `X`,(case when
(isnull(`ob`.`Y`) or (`ob`.`Y` = 0.0)) then `t`.`Y_centr` else `ob`.`Y`
end) AS `Y`,`ob`.`opombe` AS `opombe`,`ob`.`ponovitev` AS
`ponovitev`,`p`.`popisovalci` AS `popisovalci`,`ob`.`id_obiska` AS
`id_obiska`,`op`.`id_opazovanja` AS `id_opazovanja` from
((((`obiski_terena` `ob` join `opazovanja` `op`) join `x_vrste` `v`) join
`x_tetrade` `t`) join `obiski_terena_popisovalci_tmp` `p`) where
((`ob`.`id_obiska` = `op`.`id_obiska`) and (`v`.`id_vrste` =
`op`.`id_vrste`) and (`p`.`id_obiska` = `ob`.`id_obiska`) and
(`t`.`TETRADA` = `ob`.`tetrada`) and (`op`.`potrjen` = 1))
In the text of the SQL statement one can see some MySQL specific SQL functions. isnull function is
MySQL replacement for Oracle nvl function.
Explain Plan
MySQL has kind of a explain plan command which has unfortunately completely different output as
the optimizer is also quite different in MySQL.
Not so different from PL/SQL language, but different enough that one has to study this for a while.
Actually this function could be coded better but for the purpose I needed it was just performing well
and solving my problem.
Data Dictionary
Since MySQL version5.0 the data dictionary is stored in the “information_schema” database.
It contains metadata information about:
• Tables
• Indexes
• Data Dictionary Views
• Other
Since MySQL 5.1 Plugin API supports creation of server components. The API for plugin creation is
generic.
The components supported are typically:
– storage engines,
– full-text parser plugins
– server extensions.
InnoDB (one of available storage engines) uses INFORMATION_SCHEMA plugins to provide tables
that contain information about current transactions and locks.
Plugins can be loaded at server startup, or loaded and unloaded dynamically at runtime.
Process List
In order to see all users connected to MySQL one can run command: show processlist
Figure 4: SHOW PROCESSLIST in phpMyAdmin
Information about database user is stored in mysql internal tables. In order to get the information we
must us dot notation to get info from mysql.user table. In order to run this query one has to be a
privileged user.
Information about grants is stored in internal tables (must be a privileged user to query this
information)
– mysql.db
– mysql.tables_priv
– mysql.columns_priv
– mysql.procs_priv
– mysql.proxies_priv
The *.frm files describe the structure of each table within the database.
When using MyISAM tables, the data files are named (*.MYD) and indexes (*.MYI)
When using InnoDB storage engine they are stored in InnoDB tablespaces, each of which can have
one or more “data” files (similar to Oracle).
By default all data stored in InnoDB for all databases on a MySQL server are held in one tablespace,
consisting of one file: /var/lib/mysql/ibdata1 (on Linux).
InnoDB has two transactional log files (like Oracle log files) to support automatic crash recovery.
They are : /var/lib/mysql/ib_logfile0 and /var/lib/mysql/ib_logfile1 (on Linux).
Undo data is stored within the tablespace file.
My.ini is like init.ora in Oracle database. It is a text file and the contents looks like:
....
[client]
port = 3306
socket = "E:/xampp/mysql/mysql.sock"
# The MySQL server
[mysqld]
port= 3306
#log = "jozes.log„
log_error = "mysql_error.log"
socket = "E:/xampp/mysql/mysql.sock"
basedir = "E:/xampp/mysql"
tmpdir = "E:/xampp/tmp"
datadir = "E:/xampp/mysql/data"
pid_file = "mysql.pid„
....
Restoring from backup means that we actually run the logical backup as script
Oracle offers MySQL Enterprise Backup solution which is not for free, but runs in different way
(like RMAN) and is much faster. So for bigger databases one has to consider if logical backup is a
viable solution.
Most of the SQL statements are running extremely fast, especially updates, inserts and deletes are
astonishing fast.
During my project I never used EXPAIN command as I figured out quickly that some of the
statements should be written in the proper way (for instance IN lists). However, when a SQL statement
is slow one can use EXPLAIN to get information about the execution plan. Unfortunately the
execution plans are completely different that in Oracle.
Conclusion
MySQL is simple & fast & reliable – after 2 years in production so far no outages, no problems, in one
word – perfect for the web application. So far I had no problems with data corruption, practically no
management required.
The default setup is more or less sufficient, however if you will use MySQL on a large project you
will probably find what should be changed quite soon during the development and initial testing.
Good news is that MySQL uses significantly less memory than Oracle Database.
Contact:
Jože Senegačnik
DbProf d.o.o.
Smrjene 153
SI-1291 Škofljica
Slovenia
Telefon: +386 41 72 44 61
E-Mail joze.senegacnik@dbprof.com
Internet: www.dbprof.com