Вы находитесь на странице: 1из 10

The Basics an Oracle DBA Should Know About MySQL

Jože Senegačnik, Oracle ACE Director, Member of OakTable


DbProf d.o.o.
Ljubljana, Slovenia

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)

Things that I have learned at the very beginning were:


• Databases in MySQL are like schemas in Oracle database
• Installing it on my workstation under Windows and Linux
• SQL language is the same as in Oracle, however the functions that can be used in SQL are
available but not the same as in Oracle
• How to write you functions in database

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.

Figure 1: MySQL Database Architecture

MySQL User Interface

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.

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| atlasptic |
| cdcol |
| mysql |
| performance_schema |
| phpmyadmin |
| test |
| webauth |
+--------------------+
8 rows in set (0.01 sec)

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;
….

Create Table Command


If we want to generate the table creation SQL statement we can run the following command:

mysql> show create table atlas95;

CREATE TABLE `atlas95` (


`id` int(5) NOT NULL DEFAULT '0',
`X` int(6) DEFAULT NULL,
`Y` int(6) DEFAULT NULL,
`ID_10kmGK` varchar(9) COLLATE utf8_slovenian_ci DEFAULT NULL,
`TETRADA` varchar(9) COLLATE utf8_slovenian_ci NOT NULL,
`koda` varchar(6) COLLATE utf8_slovenian_ci DEFAULT NULL,
`id_vrste` int(6) NOT NULL,
`koda_G` varchar(7) COLLATE utf8_slovenian_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `TETRADA` (`TETRADA`),
KEY `id_vrste` (`id_vrste`),
KEY `vrsta_tetrada` (`id_vrste`,`TETRADA`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_slovenian_ci

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.

Figure 2: Explan (Plan) Command

User Defined Functions in MySQL


Needed function like LISTAGG in Oracle, unfortunately nothing like this exists in MySQL. Therefore
I had to develop my own user defined function.

In Oracle I would write:


SELECT department_id "Dept.",
LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire_date) "Employees"
FROM employees
GROUP BY department_id
ORDER BY department_id;

In MySQL my user defined function has the following code:

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

By default INFORMATION_SCHEMA is not exported.


MySQL Plugins

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.

Figure 3: Information Schema in phpMyAdmin

Process List

In order to see all users connected to MySQL one can run command: show processlist
Figure 4: SHOW PROCESSLIST in phpMyAdmin

Creating Database User

mysql> CREATE USER 'joc'@'localhost' IDENTIFIED BY 'mypass';


mysql> show grants for joc@localhost;
Grants for joc@localhost:
GRANT USAGE ON *.* TO 'joc'@'localhost' IDENTIFIED BY
PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'

Information About Database Users

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.

Figure 5: Database User List in phpMyAdmin


MySQL Access Privileges

To get a list of grants for particular user one can run:


SHOW GRANTS [FOR user@host]

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

Physical structure - Location of Data Files

Each database is stored on a separate directory.

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.

Figure 6: Data Files on Linux

Initialization File My.ini

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„
....

Backup And Restore


Logical backup is one of possible solutions and is done like export in Oracle
In logical backup we have dumped data structure definitions together with data in the form of SQL
script file. Backup is actually a text file which can be opened with editor.

To make a backup one has to run the following command:

E:\mysql\bin>mysqldump -u root -p myDatabase > myDatabase.bck


Enter password: **********

Restoring from backup means that we actually run the logical backup as script

mysql -u root -p myDatabase < myDatabase.bck


Enter password: **********

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.

Writing SQL Statements


SQL92 Syntax is preferred for joins. Unfortunately functions in SQL languages are different than in
Oracle.
According to my experience large IN-lists are working very slow: “where var IN (select .....)”
On contrary EXISTS works very fast: “where exists (select ....)”

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

Вам также может понравиться