You are on page 1of 7

14/2/2014

MySQL: An Introduction for Oracle DBAs | Patrick Hurley

MySQL: An Introduction for Oracle DBAs


Y ou are an Oracle Database Administrator. Y ou enjoy looking after Oracle databases and you are really good at it. In the corner of the machine room, on the edge of your peripheral vision, is a server running a database called MySQL. Y ou have been told that this holds the data for your companys website and your corporate blog, all running cool, free, open-source software. A trendy, young contractor installed this stuff a while back. He told your boss that, as MySQL is open-source, light and free, it does not require the overhead of database administration, unlike big, complex Oracle. Y ou were both impressed by and sceptical about this claim. The contractor has long gone. The company website and the corporate blog are a big success. On the edge of your peripheral vision, that server keeps worrying you. One day your manager calls you into his office, Can you just have a quick look at that MySQL database server, the one with the website and the blog on it? Y ou know, make sure its secure and stuff. Shouldnt be too hard for an Oracle DBA like yourself. Y ou know absolutely nothing about MySQL. Where do you start?

How do I connect to MySQL?


First logon to the server hosting MySQL, typically via ssh (lets assume someone has at least given you access to the server, otherwise we might as well give up now). The server in this story is running linux; however most of the commands we will cover are operating system independent. Type the following:
m y s q lur o o t

If you do not get an error message and this command seems to work, then you can already tell things are not as they should be. This means the MySQL root user (which is nothing to do with the operating system root user; it is the equivalent of Oracles sysdba) is not password-protected. This is the default situation and Ill show you how to fix it in a minute. However if there is a password for the MySQL root user and, assuming you have been told it, type the command below, followed by the password when prompted:
m y s q lur o o tp

Y ou are now logged in to the mysql client (the equivalent of sqlplus) and will see a prompt, typically ' m y s q l > ' .

http://patrickhurley.wordpress.com/mysql-for-oracle-dba/

1/7

14/2/2014

MySQL: An Introduction for Oracle DBAs | Patrick Hurley

Whats in this database?


Within the mysql client, invoked above, type:
s h o wd a t a b a s e s ;

This will list the databases contained within this MySQL server. Beware databases are not equivalent to databases in Oracle; they are equivalent to Oracle schemas. One MySQL server may contain many databases. In the mysql client you can use the word schema interchangeably with the word database. In the list of databases you will see some or all of the following standard databases: information_schema, mysql, performance_schema and test. Anything else will be a database supporting one of your own applications. To enter one of these databases:
u s ed a t a b a s e _ n a m e ;

To see which tables are in this database:


s h o wt a b l e s ;

and to see more detail:


s h o wt a b l es t a t u s ;

The second column in the output of the above command is Engine. An unusual feature of MySQL (for those familiar with Oracle) is that it has many ways of storing data, called storage engines. The two most common storage engines are MyISAM and InnoDB. MyISAM (the default prior to MySQL 5.5) is a very basic storage engine (e.g. it has table-level locking and reads are blocked by updates). InnoDB (the default from MySQL 5.5 onwards) is more similar to Oracle, providing advanced features such as transactions, multi-version concurrency, referential integrity and row-level locking. Y ou can use SQL against the tables in a database as you would expect, optionally qualifying a table with its database name, i.e.
s e l e c t*f r o mt a b l e _ n a m e ;

or:
s e l e c t*f r o md a t a b a s e _ n a m e . t a b l e _ n a m e ;

Bear in mind that each command must end with a ; or \G; the latter displaying the output vertically, which is handy for tables with many columns. Y ou will also notice, by pressing the up arrow, that the mysql client has a command history, like the bash shell.
http://patrickhurley.wordpress.com/mysql-for-oracle-dba/ 2/7

14/2/2014

MySQL: An Introduction for Oracle DBAs | Patrick Hurley

To see the indexes on a table:


s h o wi n d e xf r o mt a b l e _ n a m e ;

To see the full definition of a table:


s h o wc r e a t et a b l et a b l e _ n a m e ;

Where is my data stored?


Exit out of the mysql client, back to the servers operating system, and have a look around your MySQL base data directory, typically /var/lib/mysql:
l sl/ v a r / l i b / m y s q l

Y ou will see a directory for each database. A databases directory contains many files of the format *.frm, which describe the structure of each table within the database. For MyISAM tables, the data (*.MY D) and indexes (*.MY I) are stored within this directory also. InnoDB tables are stored in InnoDB tablespaces, each of which consists of one or more files, which are very similar to Oracle tablespaces. By default all InnoDB data and indexes for all databases on a MySQL server are held in one tablespace, consisting of one file: /var/lib/mysql/ibdata1. InnoDB has log files, which are the equivalent of Oracle redo logs, allowing automatic crash recovery. By default there are two log files: /var/lib/mysql/ib_logfile0 and /var/lib/mysql/ib_logfile1. Undo data is held within the tablespace file.

Is there a Data Dictionary?


Since MySQL 5.0 the information_schema database has been available, containing data dictionary information. To see all the tables (actually views) contained in the data dictionary, within the mysql client:
u s ei n f o r m a t i o n _ s c h e m a ; s h o wt a b l e s ;

And then query any of these tables, for example:


s e l e c t*f r o mi n f o r m a t i o n _ s c h e m a . t a b l e sw h e r et a b l e _ s c h e m a = ' d a t a b a s e _ n a m e ' ; s e l e c t*f r o mi n f o r m a t i o n _ s c h e m a . v i e w sw h e r et a b l e _ s c h e m a = ' d a t a b a s e _ n a m e ' ;

Information_schema is defined in the SQL 2003 standard and is implemented by other major databases, e.g. SQL Server, PostgreSQL.

Who is connected to my MySQL server?


To see sessions currently connected to MySQL:
http://patrickhurley.wordpress.com/mysql-for-oracle-dba/ 3/7

14/2/2014

MySQL: An Introduction for Oracle DBAs | Patrick Hurley

s h o wp r o c e s s l i s t ;

This will tell you: which users are connected; from which host; to which database; which commands they are currently executing.

Which users are defined in my MySQL server and what can they do?
This information is held in another standard MySQL database, called mysql. To see the users defined:
s e l e c t*f r o mm y s q l . u s e r ;

This shows all the users defined in the MySQL server, together with their global privileges. By default root users do not have a password (as we saw in the first part of this blog post) and there is also an anonymous user with no password. To make your MySQL server more secure (create a password for root, remove the anonymous user and clear up a few other default security holes) it is recommended that you run the script below (on the operating system, not within the mysql client):
m y s q l _ s e c u r e _ i n s t a l l a t i o n

An important difference between MySQL and Oracle is that, in MySQL, it is the combination of user name and host that uniquely identify a user. So the user billy@server1 is a different user to billy@server2; they can have different passwords and different privileges. Also MySQL only allows a user to connect from a host defined for it in the mysql.user table. This means, for example, assuming the two users above are the only users defined with the user name billy, if someone attempts to connect to this MySQL server from server3 as the user billy, then MySQL will deny access. To create a user:
c r e a t eu s e rw e b a p p @ s e r v e r 1i d e n t i f i e db y' a p p l e ' ;

Typically, as with Oracle databases these days, database users are pooled connections from an application (e.g. a content management or web server) and are given all privileges on the database which holds their data:
g r a n ta l lo nd a t a b a s e _ n a m e . *t ow e b a p p @ s e r v e r 1 ;

http://patrickhurley.wordpress.com/mysql-for-oracle-dba/

4/7

14/2/2014

MySQL: An Introduction for Oracle DBAs | Patrick Hurley

Ideally privileges can and should be granted at a lower granularity. To see the database-level privileges for all users:
s e l e c t*f r o mm y s q l . d b ;

It is possible to create the user and grant privileges in one statement:


g r a n ta l lo nd a t a b a s e _ n a m e . *t ow e b a p p @ s e r v e r 1i d e n t i f i e db y' a p p l e ' ;

It is also possible to put a wildcard (%) into the host to allow a given user to connect from anywhere (quotes are needed around the wildcard):
c r e a t eu s e rb i l l y @ ' % 'i d e n t i f i e db y' b a n a n a ' ;

How do I start and stop MySQL?


On linux and unix MySQL is typically controlled through init.d scripts, as you might expect:
/ e t c / i n i t . d / m y s q ls t a r t / e t c / i n i t . d / m y s q ls t o p / e t c / i n i t . d / m y s q lr e s t a r t

If MySQL fails to start, check the error log, usually /var/lib/mysql/server_name.err If you examine the running MySQL server processes (ps -ef|grep mysqld) you will see two: mysqld and mysqld_safe. The former is the MySQL server process and the latter is a process that monitors the MySQL server process and will restart it if it has failed.

Where is the MySQL Server Configuration data?


The MySQL configuration file (the equivalent of an Oracle pfile or spfile) is usually /etc/my.cnf (or my.ini on Windows). The file is divided into different sections, each of which provides configuration data for different programs. The section labelled [mysqld] controls the behaviour of the mysqld server. The section labelled [mysql] controls the behaviour of the mysql client. Often very little is needed in this file as the default values are fine for typical installations.

How do I Backup MySQL?


The easiest way to backup MySQL is to use the program mysqldump. This creates a logical database backup, which is a text file containing the SQL commands necessary to recreate the database objects and populate the data. Y ou need to specify the user name, the database to be backed up and a file in which to store the backup:
m y s q l d u m pur o o tpd a t a b a s e _ n a m e>b a c k u p _ f i l e _ n a m e
http://patrickhurley.wordpress.com/mysql-for-oracle-dba/ 5/7

14/2/2014

MySQL: An Introduction for Oracle DBAs | Patrick Hurley

By default mysqldump will make all tables read-only, preventing updates during this logical backup. If all your tables are stored in the InnoDB storage engine then it is better to apply the single-transaction option to mysqldump, which will create a consistent backup while leaving tables open for update. There are many other more powerful and flexible ways to backup MySQL which are beyond the scope of this introduction (e.g. file system snapshots, MySQL Enterprise Backup, Percona XtraBackup).

How do I restore MySQL?


It is very simple to restore a backup taken with mysqldump. As the file created by mysqldump contains plain SQL, simply invoke the mysql client, providing the mysqldump backup file as input:
m y s q lur o o tpd a t a b a s e _ n a m e<b a c k u p _ f i l e _ n a m e

Bear in mind that, as this is a logical backup, restore will involve applying many SQL statements and may therefore take a long time for large databases.

How do I Install MySQL?


The easiest way to install MySQL is from your operating system providers standard repositories. To install the server on Oracle Enterprise Linux, Red Hat or Centos:
y u mi n s t a l lm y s q l s e r v e r

and then the client:


y u mi n s t a l lm y s q l

However your distribution is unlikely to be providing the latest MySQL version. To obtain the latest version go tohttp://dev.mysql.com/downloads and obtain the software for your operating system (e.g. Linux rpm package files or Windows Installer installation files). For example to install version 5.5.19 of the server and client on 32-bit Oracle Enterprise Linux, Red Hat or Centos :
r p mi v hM y S Q L s e r v e r 5 . 5 . 1 9 1 . r h e l 5 . i 3 8 6 . r p m r p mi v hM y S Q L c l i e n t 5 . 5 . 1 9 1 . r h e l 5 . i 3 8 6 . r p m

Y ou can also obtain versions for other operating systems here, e.g. Windows Installer installation files. A good way to get started is to install one of the sample databases provided by MySQL. These can be found, confusingly, on the MySQL documentation page: http://dev.mysql.com/doc/indexother.html. I have found the Sakila database, which models a DVD rental shop, very useful for getting to know MySQL.
http://patrickhurley.wordpress.com/mysql-for-oracle-dba/ 6/7

14/2/2014

MySQL: An Introduction for Oracle DBAs | Patrick Hurley

Is MySQL free?
The community edition of MySQL can be downloaded (http://dev.mysql.com/downloads) and used for free, even for public-facing live systems. If you want support and extra tools (e.g. the MySQL Enterprise Backup hot backup tool) then you can purchase this. Details are athttp://www.mysql.com/products. Additionally if you embed MySQL in a product which you distribute you will need to purchase licences. Details are athttp://www.mysql.com/oem.

Summary
As an Oracle DBA I have been very impressed with MySQLs features, reliability and ease-of-use. I hope I have wetted your appetite and given you the confidence to jump in there and have a look at those MySQL servers that are sitting in the corner of your server rooms, or to download and install the free version and have a go with it. And now that MySQL is owned by Oracle you are not even being unfaithful.

http://patrickhurley.wordpress.com/mysql-for-oracle-dba/

7/7