ExploringMySQL BestPractices for
DBA`s
About ME
Baruchosoveskiy
• Senior Consultant @brillix
• Linux/UNIXSYSADMIN(from 1999)
• DBA onoracleand MySQL.
• Working withMySQLfrom 2000.
• blogger in ildba.co.il
History of MySQL
• 1979Founded anddevelopedbyDavid Axmark,Allan Larsson,
and Michael“Monty” Widenius
• Named afterMonty'sdaughter,My
• Sun acquiredMySQLABin Jan2008for$1billiondollars
• OracleacquiredSun in 2010for $5.6billiondollars
• 4December2012 DavidAxmark,AllanLarsson,and Michael
“Monty” Widenius announcedMariaDB
MySQLVersion History
• 3.11.1First public release
• 3.23 IntegratedBerkeleyDB supportstransactions,
abstractStorageEngine
• 4.0IntegrationInnoDB
• 4.1 significantimprovements,subquery,Unicode,c /s
communicationprotocol
MySQL Version History
• 5.0storedprocedure,view,triggers,queryoptimizer
• 5.1 NDB,record replicationInnoDB plugin defaultinstall
• 5.5 OracleFirstVersionMulti processsupport,
~300% performanceimprovement
• 5.6and7.4clusterCurrentGAVersion
• 5.7CurrentDEV Version
** Productionrecommendationis 5.5 and 5.6.
Who uses MySQL?
US Navy carrier flight
operations
MySQL Server
Community:
Freelydownloadableversionopensourcedatabase.It is
availableunder the GPL license and issupportedbya huge
and activecommunityof open sourcedevelopers.
Enterprise: Free for 30 day evaluation
Paidsubscriptionincludessupportand the following:
• MySQLEnterpriseBackup (LIVE BACKUPtool)
• MySQLEnterpriseMonitor(“GRID CONTROL” like tool)
• MySQLQueryAnalyzer
• MySQLWorkbench(Free)
MySQLArchitecture vs ORACLE Architecture
is it user or schema ?
DataBase
Instance(pmon,smon…) ServerInstance(mysqld)
User User
TableSpace TableSpace
DataFile OSFile-.ibdMYI.MYD.Frm
DataBaseSchema
User
Storage Engines
why so many ?
Storage Engines
why so many ?
• handles,and retrievesinformationfrom a table
• EachStorage Enginehaveits Advantageanddisadvantage
• There is noperfectStorageEngine
• The recommended/default StorageEngineis InnoDB
Storage Engines
why so many ?
Attribute MyISAM HEAP
(Memory)
InnoDB/*XtraDB ARCHIVE
(Compressed stor
age)
Transaction NO NO YES No
LockGranularity Table Table Row row
Storage Filepre
table
In memory TableSpace / fileper
table
Files
Isolationlevel None None All None
Referential
Integrity(FK)
NO NO Yes No
Cached Data NO YES YES No
Advantageand disadvantage
* mariadb donot have INNODB
Storage Engines
why so many ?
Attribute MyISAM HEAP
(Memory)
InnoDB/*XtraDB ARCHIVE
(Compressed stor
age)
Transaction NO NO YES No
LockGranularity Table Table Row row
Storage Filepre
table
In memory TableSpace / fileper
table
Files
Isolationlevel None None All None
Referential
Integrity(FK)
NO NO Yes No
Cached Data NO YES YES No
Advantageand disadvantage
* mariadb donot have INNODB
Storage Engines
why so many ?
Attribute MyISAM HEAP
(Memory)
InnoDB/*XtraDB ARCHIVE
(Compressed stor
age)
Transaction NO NO YES No
LockGranularity Table Table Row row
Storage Filepre
table
In memory TableSpace / fileper
table
Files
Isolationlevel None None All None
Referential
Integrity(FK)
NO NO Yes No
Cached Data NO YES YES No
Advantageand disadvantage
* mariadb donot have INNODB
Storage Engines
why so many ?
Attribute MyISAM HEAP
(Memory)
InnoDB/*XtraDB ARCHIVE
(Compressed stor
age)
Transaction NO NO YES No
LockGranularity Table Table Row row
Storage Filepre
table
In memory TableSpace / fileper
table
Files
Isolationlevel None None All None
Referential
Integrity(FK)
NO NO Yes No
Cached Data NO YES YES No
Advantageand disadvantage
* mariadb donot have INNODB
Storage Engines
why so many ?
And more
• Percona XtraDB enhancedversionof the InnoDB storage
engine.
• RocksDB (LevelDB) is an persistentkey-valuestore for
faststorage.
• OQGRAPH (for MariaDB) Open QueryGRAPHengine
MySQL configuration-my.cnf,my.ini
where is my spfile?
MySQL configuration - my.cnf,my.ini
where is my spfile?
• Configurationfile name my.cnf
• ConfigurationfileLocation:
“/etc/my.cnf”, “/etc/mysql/my.cnf “ in linux,
“C:ProgramDataMySQLMySQL Server 5.6my.ini” inwindows
• mysqld_safe --defaults-file=/etc/my.cnf &
• Can run Multiple MySQLInstancesononemachine
needdifferent port
Storage and log types
what is this file? can I delete it ?
• ErrorLog:log-error (Oracle alertlog)
• BinaryLog:log-bin (Oracle Archivelogs)
• SlowQueryLog:log-slow-queries,slow-query-time,log-queries-
not-using-indexes
• GeneralLog: MySQLsessiondebug log
• .frm: MySQLrepresentseach tablebyan .frm tableformat
file, storestabledefinitionin the databasedirectory.
• .MYD: This is theextensionof theDatafiles for MyISAM
tables.
• .MYI:Thisis theextensionof the Indexfiles for MyISAM
tables.
Storage and log types
what is this file? can I delete it ?
• TABLE_NAME.ibd:If youspecify innodb_file_per_tableoption
to my.cnf,InnoDBstoreseachtable in itsown .ibd
• ibdata1: Sharedtablespace/ data file for InnoDB tables
when innodb_file_per_tableoption isnotspecifiedin my.cnf.
• ib_logfile0, ib_logfile1: InnoDBlogfiles. importantfor
performanceand recovery.
Storage and log types
what is this file? can I delete it ?
The Data Dictionary
where is v$ and DBA_ ?
The Data Dictionary
where is v$ and DBA_
• INFORMATION_SCHEMA– INFORMATIONaboutthe instance,table,
columns,views,privileges
• PERFORMANCE_SCHEMA– INFORMATIONaboutinstance
performance,topsql,top tableIO
• Showcommand– get systemvariablesinformation,oracle
:show parameters
show process list select * from v$sessions ;
show tables select table_name from user_tables;
show variables like ‘%size%’ show parameters size
SELECT TABLE_SCHEMA, SUM((DATA_LENGTH + INDEX_LENGTH) /
(1024 * 1024)) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES
GROUP BY TABLE_SCHEMA ORDER BY SIZE_MB DESC
SELECT ROUTINE_TYPE, ROUTINE_NAME FROM
INFORMATION_SCHEMA.ROUTINES WHERE
ROUTINE_SCHEMA='dbname';
SELECT EVENT_ID, EVENT_NAME, TIMER_WAIT
FROM PERFORMANCE_SCHEMA.events_waits_history
WHERE THREAD_ID = 13
ORDER BY EVENT_ID;
The Data Dictionary
where is v$ and DBA_
MySQL User account
• MySQLuser accountis basedon username and hostname
For example:
select user,host from mysql.user;
+--------+---------------------+
| user | host |
+--------+---------------------+
| baruch| % |
| root | 127.0.0.1 |
| root | 192.168.10.40 |
| root | ::1 |
| root | localhost |
+--------+--------------------+
The Userroot@127.0.0.1 cannotconnect from 192.168.10.40
And theuserbaruch@% asconnectfrom all clients
Tips for
Developer
Tips for Developer
what the F***, no Sequences ?
• NoSequences - useAUTO_INCREMENT
id INT NOT NULL AUTO_INCREMENT
- Must be tied toa [table].[column]
- Only one per table
- No systemwide capability
- LAST_INSERT_ID()
- No getnextcapability
Tips for Developer
Character Set/Collation
• Every character column hasa column characterset and a
column collation
CREATE TABLE t1
(
col1 CHAR(10) CHARACTER SET utf8,
COLLATE utf8_unicode_ci,
col2 TEXT
) CHARACTER SET latin1 COLLATE
latin1_bin;
Tips for Developer
• EscapedReservedWords areallowed
CREATE TABLE `workers` (...);
CREATE TABLE “workers” (...); (depend on sql_mode)
• Spaceandother specialcharactersallowed
CREATE TABLE `My Table Name` (...);
(depend on OS for file creation )
Tips for Developer
can I use PL/SQL ?
• NoPL/SQL - StoredProcedureswith limited Functionality
NO - Types,Overloading,namedparameters,pinning,
packagesBuilt-in Packages
• MySQLProvide proceduresand functions
Tips for Developer
inserts and dual
• noneedof dual
• Multi valueinsert
INSERT INTO example
VALUES (100, 'Name 1', 'Value 1', 'Other 1'),
(101, 'Name 2', 'Value 2', 'Other 2');
• INSERTON DUPLICATEKEY
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
• StoreIP Addressas UNSIGNEDINT
SELECT INET_ATON('10.0.5.9');
167773449
select 1 select 1 from dual
Tips for Developer
SQL_MODE
• EmulateOracleBehavior
SET SQL_MODE=TRADITIONAL,ORACLE
Example :
Use concatin mysql :
SELECT CONCAT('A','B');
SET sql_mode='PIPES_AS_CONCAT';
SELECT 'A'||'B';
http://dev.mysql.com/doc/refman/5.6/en/server-sql-mode.html
MySQL Cluster
MySQLReplication – Master Slave(n)
MySQL and NoSQL
NoSQL API
Q & A
Thanks for attending!
Baruch osoveskiy
baruch@brillix.co.il

iloug2015.Mysql.for.oracle.dba.V2

  • 2.
  • 3.
    About ME Baruchosoveskiy • SeniorConsultant @brillix • Linux/UNIXSYSADMIN(from 1999) • DBA onoracleand MySQL. • Working withMySQLfrom 2000. • blogger in ildba.co.il
  • 4.
    History of MySQL •1979Founded anddevelopedbyDavid Axmark,Allan Larsson, and Michael“Monty” Widenius • Named afterMonty'sdaughter,My • Sun acquiredMySQLABin Jan2008for$1billiondollars • OracleacquiredSun in 2010for $5.6billiondollars • 4December2012 DavidAxmark,AllanLarsson,and Michael “Monty” Widenius announcedMariaDB
  • 5.
    MySQLVersion History • 3.11.1Firstpublic release • 3.23 IntegratedBerkeleyDB supportstransactions, abstractStorageEngine • 4.0IntegrationInnoDB • 4.1 significantimprovements,subquery,Unicode,c /s communicationprotocol
  • 6.
    MySQL Version History •5.0storedprocedure,view,triggers,queryoptimizer • 5.1 NDB,record replicationInnoDB plugin defaultinstall • 5.5 OracleFirstVersionMulti processsupport, ~300% performanceimprovement • 5.6and7.4clusterCurrentGAVersion • 5.7CurrentDEV Version ** Productionrecommendationis 5.5 and 5.6.
  • 7.
  • 8.
    US Navy carrierflight operations
  • 9.
    MySQL Server Community: Freelydownloadableversionopensourcedatabase.It is availableunderthe GPL license and issupportedbya huge and activecommunityof open sourcedevelopers. Enterprise: Free for 30 day evaluation Paidsubscriptionincludessupportand the following: • MySQLEnterpriseBackup (LIVE BACKUPtool) • MySQLEnterpriseMonitor(“GRID CONTROL” like tool) • MySQLQueryAnalyzer • MySQLWorkbench(Free)
  • 10.
    MySQLArchitecture vs ORACLEArchitecture is it user or schema ? DataBase Instance(pmon,smon…) ServerInstance(mysqld) User User TableSpace TableSpace DataFile OSFile-.ibdMYI.MYD.Frm DataBaseSchema User
  • 11.
  • 12.
    Storage Engines why somany ? • handles,and retrievesinformationfrom a table • EachStorage Enginehaveits Advantageanddisadvantage • There is noperfectStorageEngine • The recommended/default StorageEngineis InnoDB
  • 13.
    Storage Engines why somany ? Attribute MyISAM HEAP (Memory) InnoDB/*XtraDB ARCHIVE (Compressed stor age) Transaction NO NO YES No LockGranularity Table Table Row row Storage Filepre table In memory TableSpace / fileper table Files Isolationlevel None None All None Referential Integrity(FK) NO NO Yes No Cached Data NO YES YES No Advantageand disadvantage * mariadb donot have INNODB
  • 14.
    Storage Engines why somany ? Attribute MyISAM HEAP (Memory) InnoDB/*XtraDB ARCHIVE (Compressed stor age) Transaction NO NO YES No LockGranularity Table Table Row row Storage Filepre table In memory TableSpace / fileper table Files Isolationlevel None None All None Referential Integrity(FK) NO NO Yes No Cached Data NO YES YES No Advantageand disadvantage * mariadb donot have INNODB
  • 15.
    Storage Engines why somany ? Attribute MyISAM HEAP (Memory) InnoDB/*XtraDB ARCHIVE (Compressed stor age) Transaction NO NO YES No LockGranularity Table Table Row row Storage Filepre table In memory TableSpace / fileper table Files Isolationlevel None None All None Referential Integrity(FK) NO NO Yes No Cached Data NO YES YES No Advantageand disadvantage * mariadb donot have INNODB
  • 16.
    Storage Engines why somany ? Attribute MyISAM HEAP (Memory) InnoDB/*XtraDB ARCHIVE (Compressed stor age) Transaction NO NO YES No LockGranularity Table Table Row row Storage Filepre table In memory TableSpace / fileper table Files Isolationlevel None None All None Referential Integrity(FK) NO NO Yes No Cached Data NO YES YES No Advantageand disadvantage * mariadb donot have INNODB
  • 17.
    Storage Engines why somany ? And more • Percona XtraDB enhancedversionof the InnoDB storage engine. • RocksDB (LevelDB) is an persistentkey-valuestore for faststorage. • OQGRAPH (for MariaDB) Open QueryGRAPHengine
  • 19.
  • 20.
    MySQL configuration -my.cnf,my.ini where is my spfile? • Configurationfile name my.cnf • ConfigurationfileLocation: “/etc/my.cnf”, “/etc/mysql/my.cnf “ in linux, “C:ProgramDataMySQLMySQL Server 5.6my.ini” inwindows • mysqld_safe --defaults-file=/etc/my.cnf & • Can run Multiple MySQLInstancesononemachine needdifferent port
  • 21.
    Storage and logtypes what is this file? can I delete it ? • ErrorLog:log-error (Oracle alertlog) • BinaryLog:log-bin (Oracle Archivelogs) • SlowQueryLog:log-slow-queries,slow-query-time,log-queries- not-using-indexes • GeneralLog: MySQLsessiondebug log
  • 22.
    • .frm: MySQLrepresentseachtablebyan .frm tableformat file, storestabledefinitionin the databasedirectory. • .MYD: This is theextensionof theDatafiles for MyISAM tables. • .MYI:Thisis theextensionof the Indexfiles for MyISAM tables. Storage and log types what is this file? can I delete it ?
  • 23.
    • TABLE_NAME.ibd:If youspecifyinnodb_file_per_tableoption to my.cnf,InnoDBstoreseachtable in itsown .ibd • ibdata1: Sharedtablespace/ data file for InnoDB tables when innodb_file_per_tableoption isnotspecifiedin my.cnf. • ib_logfile0, ib_logfile1: InnoDBlogfiles. importantfor performanceand recovery. Storage and log types what is this file? can I delete it ?
  • 24.
    The Data Dictionary whereis v$ and DBA_ ?
  • 25.
    The Data Dictionary whereis v$ and DBA_ • INFORMATION_SCHEMA– INFORMATIONaboutthe instance,table, columns,views,privileges • PERFORMANCE_SCHEMA– INFORMATIONaboutinstance performance,topsql,top tableIO • Showcommand– get systemvariablesinformation,oracle :show parameters show process list select * from v$sessions ; show tables select table_name from user_tables; show variables like ‘%size%’ show parameters size
  • 26.
    SELECT TABLE_SCHEMA, SUM((DATA_LENGTH+ INDEX_LENGTH) / (1024 * 1024)) AS SIZE_MB FROM INFORMATION_SCHEMA.TABLES GROUP BY TABLE_SCHEMA ORDER BY SIZE_MB DESC SELECT ROUTINE_TYPE, ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA='dbname'; SELECT EVENT_ID, EVENT_NAME, TIMER_WAIT FROM PERFORMANCE_SCHEMA.events_waits_history WHERE THREAD_ID = 13 ORDER BY EVENT_ID; The Data Dictionary where is v$ and DBA_
  • 27.
    MySQL User account •MySQLuser accountis basedon username and hostname For example: select user,host from mysql.user; +--------+---------------------+ | user | host | +--------+---------------------+ | baruch| % | | root | 127.0.0.1 | | root | 192.168.10.40 | | root | ::1 | | root | localhost | +--------+--------------------+ The Userroot@127.0.0.1 cannotconnect from 192.168.10.40 And theuserbaruch@% asconnectfrom all clients
  • 28.
  • 29.
    Tips for Developer whatthe F***, no Sequences ? • NoSequences - useAUTO_INCREMENT id INT NOT NULL AUTO_INCREMENT - Must be tied toa [table].[column] - Only one per table - No systemwide capability - LAST_INSERT_ID() - No getnextcapability
  • 30.
    Tips for Developer CharacterSet/Collation • Every character column hasa column characterset and a column collation CREATE TABLE t1 ( col1 CHAR(10) CHARACTER SET utf8, COLLATE utf8_unicode_ci, col2 TEXT ) CHARACTER SET latin1 COLLATE latin1_bin;
  • 31.
    Tips for Developer •EscapedReservedWords areallowed CREATE TABLE `workers` (...); CREATE TABLE “workers” (...); (depend on sql_mode) • Spaceandother specialcharactersallowed CREATE TABLE `My Table Name` (...); (depend on OS for file creation )
  • 32.
    Tips for Developer canI use PL/SQL ? • NoPL/SQL - StoredProcedureswith limited Functionality NO - Types,Overloading,namedparameters,pinning, packagesBuilt-in Packages • MySQLProvide proceduresand functions
  • 33.
    Tips for Developer insertsand dual • noneedof dual • Multi valueinsert INSERT INTO example VALUES (100, 'Name 1', 'Value 1', 'Other 1'), (101, 'Name 2', 'Value 2', 'Other 2'); • INSERTON DUPLICATEKEY INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; • StoreIP Addressas UNSIGNEDINT SELECT INET_ATON('10.0.5.9'); 167773449 select 1 select 1 from dual
  • 34.
    Tips for Developer SQL_MODE •EmulateOracleBehavior SET SQL_MODE=TRADITIONAL,ORACLE Example : Use concatin mysql : SELECT CONCAT('A','B'); SET sql_mode='PIPES_AS_CONCAT'; SELECT 'A'||'B'; http://dev.mysql.com/doc/refman/5.6/en/server-sql-mode.html
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
    Thanks for attending! Baruchosoveskiy baruch@brillix.co.il