Top Banner
The Proper Care and Feeding of a The Proper Care and Feeding of a MySQL Database for Linux MySQL Database for Linux Administrators Administrators Dave Stokes MySQL Community Manager [email protected]
82

Congratsyourthedbatoo

Jan 14, 2015

Download

Technology

Dave Stokes

Congratulations -- You're the new Linux Admin and the DBA: a guide for running MySQL database instances for Linux Admins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Congratsyourthedbatoo

The Proper Care and Feeding of a The Proper Care and Feeding of a MySQL Database for Linux MySQL Database for Linux AdministratorsAdministrators

Dave StokesMySQL Community [email protected]

Page 2: Congratsyourthedbatoo

Congratulations! You're the new Linux Congratulations! You're the new Linux Admin AND the MySQL DBA too!Admin AND the MySQL DBA too!

Dave StokesMySQL Community [email protected] @stoker

Page 3: Congratsyourthedbatoo

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions.

The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 4: Congratsyourthedbatoo

Simple IntroductionThis is a general introduction to running a MySQL database server(s) for Linux Administrator

Page 5: Congratsyourthedbatoo

Simple IntroductionThis is a general introduction to running a MySQL database server(s) for Linux Administrator

Database servers have needs different than SMTP, HTTP, or other servers

Page 6: Congratsyourthedbatoo

Simple IntroductionThis is a general introduction to running a MySQL database server(s) for Linux Administrator

Database servers have needs different than SMTP, HTTP, or other servers

Hardware choices are critical! (do not go cheap)

Page 7: Congratsyourthedbatoo

Simple IntroductionThis is a general introduction to running a MySQL database server(s) for Linux Administrator

Database servers have needs different than SMTP, HTTP, or other servers

Hardware choices are critical! (do not go cheap)

Tuning to 80% efficiency is relatively easy

Page 8: Congratsyourthedbatoo

Simple IntroductionThis is a general introduction to running a MySQL database server(s) for Linux Administrator

Database servers have needs different than SMTP, HTTP, or other servers

Hardware choices are critical! (do not go cheap)

Tuning to 80% efficiency is relatively easy (last 20% is tricky)

Page 9: Congratsyourthedbatoo

Session Overview

1. Basics of a database server

2. Hardware

3. MySQL Configuration

4. Monitoring Operations

5. Backups

6. Replication

7. Indexes

8. Tuning

9. Q/A

Page 10: Congratsyourthedbatoo

How does a Database server work

Client

SELECT phone

FROM friends

WHERE name = ‘Joe’;

Server

Page 11: Congratsyourthedbatoo

How does a Database server work

Client

SELECT phone

FROM friends

WHERE name = ‘Joe’;

Server

PARSE

find Joe in friends table in memory

return phone

Page 12: Congratsyourthedbatoo

How does a Database server work

Client

SELECT phone

FROM friends

WHERE name = ‘Joe’;

. . .

Process phone data

Server

PARSE

find Joe in friends table in memory

return phone

Page 13: Congratsyourthedbatoo

How does a Database server work

Client

SELECT phone

FROM friends

WHERE name = ‘Joe’;

. . .

Process phone data

Server

PARSE

find Joe in friends table in memory

return phone

What was that about memory???

Page 14: Congratsyourthedbatoo

Rule #1

• Databases love data in memory

Page 15: Congratsyourthedbatoo

Rule #1

• Databases love data in memory

Corollary #1 – getting data in/out of memory will cause you nightmares!

Page 16: Congratsyourthedbatoo

What if it is not in memory?

MySQL

Please give me the data from the city table

OS

Page 17: Congratsyourthedbatoo

What if it is not in memory?

MySQL

Please give me the data from the city table

OS

Get inode

Page 18: Congratsyourthedbatoo

What if it is not in memory?

MySQL

Please give me the data from the city table

OS

Get inode

Ask disk for data

Page 19: Congratsyourthedbatoo

What if it is not in memory?

MySQL

Please give me the data from the city table

OS

Get inode

Ask disk for data

Get data into buffer

Page 20: Congratsyourthedbatoo

What if it is not in memory?

MySQL

Please give me the data from the city table

Load data into memory

OS

Get inode

Ask disk for data

Get data into buffer

Hand buffer off

Page 21: Congratsyourthedbatoo

What if it is not in memory?

MySQL

Please give me the data from the city table

Load data into memory

OS

Get inode

Ask disk for data

Get data into buffer

Hand buffer off

Much longer than just reading from

memory

Page 22: Congratsyourthedbatoo

Rule #2

Databases have to do unpredictable queries, random I/O, and sequential scans so slow I/O kills performance

Page 23: Congratsyourthedbatoo

Buffers

What happens when you read a file into memory

DISKDiskController

PageCache

UserBuffer

Page 24: Congratsyourthedbatoo

Buffers

Memory Lookup – 100 nanoseconds, 12GB/sec

DISKDiskController

PageCache

UserBuffer

Page 25: Congratsyourthedbatoo

Buffers

Memory Lookup – 100 nanoseconds, 12GB/secDISK seek – 10 milliseconds, 760MB/sec

DISKDiskController

PageCache

UserBuffer

Page 26: Congratsyourthedbatoo

Disk Reads

A disk read is 100,000 slower than reading memory● For comparison

– 100,000 minutes is about 69.5 days– 100,000 feet is about 19 miles– 100,000 kilometers is 15.7 times around

Earth's equator

Page 27: Congratsyourthedbatoo

Cache

Writeback and writethrough caches

DiskController

DISK

Writethrough on disk Writethrough oncontroller

Page 28: Congratsyourthedbatoo

Cache

Recommended setup

DiskController

DISK

Writethrough on disk Writeback oncontroller

Page 29: Congratsyourthedbatoo

SSD

Use standard RAID controller● SSD as writeback cache● Battery backed● $2.5/GB verses .075/GB● Very fast seek time● Density

Page 30: Congratsyourthedbatoo

Hardware recommendations1. Memory – lots of it, ecc

Page 31: Congratsyourthedbatoo

Hardware recommendations1. Memory – lots of it, ecc

2. DISKs – more spindles, high speed, fast controllers, RAID 10, write back cache, and XFS/ZFS/ext4 not ext2/3

Page 32: Congratsyourthedbatoo

Hardware recommendations1. Memory – lots of it, ecc

2. DISKs – more spindles, high speed, fast controllers, RAID 10, write back cache, and XFS/ZFS/ext4 not ext2/3

3. Write-through caches with battery backup units for disks must be monitored, and have life span much longer than planned outages

Page 33: Congratsyourthedbatoo

Hardware recommendations1. Memory – lots of it, ecc

2. DISKs – more spindles, high speed, fast controllers, RAID 10, write back cache, and XFS/ZFS/ext4 not ext2/3

3. Write-through caches with battery backup units for disks must be monitored, and have life span much longer than planned outages

4. CPUs, Core less important (spend money on Memory and IO)

Page 34: Congratsyourthedbatoo

Quick Security Warning!

MySQL login security is primitive.● Database mysql has users table● 'jsmith'@'co.com' or 'fred'@'10.10.%'

– Matches host, then user, then password fields

● Be explicit● Proxy and Pluggable logins on the way

MySQL privilege security● GRANT functions or access ● Can get Byzantine quickly● Use a GUI

Page 35: Congratsyourthedbatoo

When root is the root of your root problem OR stingy is your best friend

Linux server has root● Highly privileged● Dangerous

MySQL daemon has root

● Highly privileged● Dangerous● Understand

MySQL priv system and be stingy

● Proxy and plug-in adapters soon

● Really limit access, not just play at it

Linux root is not the same as MySQL rootbut both can be dangerous to you!!!

Page 36: Congratsyourthedbatoo

Installation

1. Use pre built MySQL packages

Page 37: Congratsyourthedbatoo

Installation

1. Use pre built MySQL packages

2. Don’t double up with other services

Page 38: Congratsyourthedbatoo

Installation

1. Use prebuilt MySQL packages

2. Don’t double up with other services

3. Supplied configuration files are OLD!

Page 39: Congratsyourthedbatoo

Installation

1. Use prebuilt MySQL packages

2. Don’t double up with other services

3. Supplied configuration files are OLD!

4. Move logs to different disk than data

Page 40: Congratsyourthedbatoo

Installation

1. Use prebuilt packages

2. Don’t double up with other services

3. Supplied configuration files are OLD!

4. Move logs to different disk than data

5. Spread data over different drives

Page 41: Congratsyourthedbatoo

Installation

1. Use prebuilt packages

2. Don’t double up with other services

3. Supplied configuration files are OLD!

4. Move logs to different disk than data

5. Spread data over different drives

6. Backups are necessary – and practice recovery!

Page 42: Congratsyourthedbatoo

Monitoring Operations

1. Slow query log -- not all long running queries are bad

Page 43: Congratsyourthedbatoo

Monitoring Operations

1. Slow query log -- not all long running queries are bad

2. Log queries not using indexes

Page 44: Congratsyourthedbatoo

Monitoring Operations

1. Slow query log -- not all long running queries are bad

2. Log queries not using indexes

3. Use monitoring software – MEM, Innotop, Cacti, Munin, Nagios, etc – and pay attention to it

Page 45: Congratsyourthedbatoo

Monitor!!!

Page 46: Congratsyourthedbatoo

Monitoring Operations

1. Slow query log -- not all long running queries are bad

2. Log queries not using indexes

3. Use monitoring software – MEM, Innotop, Cacti, Munin, Nagios, etc – and pay attention to it

4. More in tuning ….

Page 47: Congratsyourthedbatoo

Backups

Backups are usually some sort of disk snap shot or serializing data to a file

Page 48: Congratsyourthedbatoo

Backups

Backups are usually some sort of disk snap shot or serializing data to a file

The more the better but you need to know steps to recover dropped table, lost databases, or mangled data

Page 49: Congratsyourthedbatoo

Backups

Backups are usually some sort of disk snap shot or serializing data to a file

The more the better but you need to know steps to recover dropped table, lost databases, or mangled data.

Use data replication to a slave and then backup slave

Page 50: Congratsyourthedbatoo

Backups

Backups are usually some sort of disk snap shot or serializing data to a file

The more the better but you need to know steps to recover dropped table, lost databases, or mangled data.

Use data replication to a slave and then backup slave

Be paranoid!!!!!

Page 51: Congratsyourthedbatoo

Replication

Replication for MySQL is the binary log for the master being copied to a slave. The slave then updates its copy of the data

Page 52: Congratsyourthedbatoo

Replication

Replication for MySQL is the binary log for the master being copied to a slave. The slave then updates its copy of the data

Two types:1. Asynchronous – server does not check changes sent to

slave before proceeding

Page 53: Congratsyourthedbatoo

Replication

Replication for MySQL is the binary log for the master being copied to a slave. The slave then updates its copy of the data

Two types:1. Asynchronous – server does not check changes sent to

slave before proceeding

2. Semi Synchronous – server checks that slave received changes before proceeding

Page 54: Congratsyourthedbatoo

Replication -- threads

Currently single threaded – 5.6 will fix that

Page 55: Congratsyourthedbatoo

Replication -- network

Network latency will affect MySQL replication. So plan network topology to minimize bandwidth competition with other systems/services.

Page 56: Congratsyourthedbatoo

Replication -- network

Network latency will affect MySQL replication. So plan network topology to minimize bandwidth competition with other systems/services.

Slaves do not need to be as fast as the master but try to keep things reasonably close

Page 57: Congratsyourthedbatoo

Replication -- network

Network latency will affect MySQL replication. So plan network topology to minimize bandwidth competition with other systems/services.

Slaves do not need to be as fast as the master but try to keep things reasonably close

Do not have to replicate all tables/databases to all slaves. Cut down on traffic by replicating what is needed!

Page 58: Congratsyourthedbatoo

Writes & Reads Reads Reads

• Write to one master• Read from many slaves, easily add more as needed• Perfect for read/write intensive apps

Application

MySQL Replication

Load Balancer

MySQL DatabaseReplication Enables Scalability

Page 59: Congratsyourthedbatoo

Indexes are good

Without Index

DB needs to scan entire table or table scan

With Index

DB can go right to record

Page 60: Congratsyourthedbatoo

Indexes, the bad

• Overhead -- space, speed, maintenance

Page 61: Congratsyourthedbatoo

Indexes, the bad

• Overhead -- space, speed, maintenance• Not a panacea – does not cure all problems

Page 62: Congratsyourthedbatoo

Indexes, the bad

• Overhead -- space, speed, maintenance• Not a panacea – does not cure all problems• Will not help if you need to perform a table

scan

Page 63: Congratsyourthedbatoo

Indexes, the bad

• Overhead -- space, speed, maintenance• Not a panacea – does not cure all problems• Will not help if you need to perform a table

scan• Composite indexes can be tricky – YearMonthDay

usually better than DayMonthYear

Page 64: Congratsyourthedbatoo

Tuning to 80%

• Use InnoDB

Page 65: Congratsyourthedbatoo

Tuning to 80%

• Use InnoDB• Set innodb_buffer_pool_size to 70-

80% of memory

Page 66: Congratsyourthedbatoo

Tuning to 80%

• Use InnoDB• Set innodb_buffer_pool_size to 70-

80% of memory• Use XFS/ZFS/ext4

Page 67: Congratsyourthedbatoo

Tuning to 80%

• Use InnoDB• Set innodb_buffer_pool_size to 70-

80% of memory• Use XFS/ZFS/ext4• Partition data -- divide and conquer

Page 68: Congratsyourthedbatoo

Tuning to 80%

• Use InnoDB• Set innodb_buffer_pool_size to 70-

80% of memory• Use XFS/ZFS/ext4• Partition data -- divide and conquer• Architect your data

Page 69: Congratsyourthedbatoo

Tuning to 80%

• Use InnoDB• Set innodb_buffer_pool_size to 70-

80% of memory• Use XFS/ZFS/ext4• Partition data -- divide and conquer• Architect your data• Review your SQL statements

Page 70: Congratsyourthedbatoo

Don't hurt yourself

Some RAID controllers have a battery learning cycle when BBU goes write-back. Schedule during off-time!

Minimize time for most frequent queries

Keep on top of upgrades

- 5.5 20% faster than 5.1

Page 71: Congratsyourthedbatoo

Tuning Past 80%

Will depend on your data

Page 72: Congratsyourthedbatoo

Tuning Past 80%

Will depend on your data

Lots of Tuning Help Available

Page 73: Congratsyourthedbatoo

Tuning Past 80%

Will depend on your data

Lots of Tuning Help Available● High Performance MySQL – Schwartz et al● MySQL Administrator's Bible – Cabral

Page 74: Congratsyourthedbatoo

Tuning Past 80%

Will depend on your data

Lots of Tuning Help Available● High Performance MySQL – Schwartz et al● MySQL Administrator's Bible – Cabral● Effective MySQL series – Bradford● OurSQL podcast

Page 75: Congratsyourthedbatoo

Tuning Past 80%

Will depend on your data

Lots of Tuning Help Available● High Performance MySQL – Schwartz et al● MySQL Administrator's Bible – Cabral● Effective MySQL series – Bradford● OurSQL podcast● Performance forum on Forums.MySQL.Com● Planet.MySQL.com & MySQL.Com

Page 76: Congratsyourthedbatoo

Tuning Past 80%

Will depend on your data

Lots of Tuning Help Available● High Performance MySQL – Schwartz et al● MySQL Administrator's Bible – Cabral– Effective MySQL series – Bradford● OurSQL podcast● Performance forum on Forums.MySQL.Org● Planet.MySQL.com & MySQL.com

Skilled DBA

Page 77: Congratsyourthedbatoo

Tuning Past 80%

Will depend on your data

Lots of Tuning Help Available● High Performance MySQL – Schwartz et al● MySQL Administrator's Bible – Cabral● OurSQL podcast● Performance forum on Forums.MySQL.Org● Planet.MySQL.COM & MySQL.Com

Skilled DBA● Defined as obsessive professional looking to save

nanoseconds off queries, possess current backups, helps developers rewrite queries, plans for future, and watches buffer hits rates compulsively.

Page 78: Congratsyourthedbatoo

Big hint

Seek to optimize the system as a whole. Often the database is blamed for systemic slowness when other components are at fault.

Page 79: Congratsyourthedbatoo

General Last hints

SQL● Fetch needed data

only, no SELECT *● Use EXPLAIN● Think in data sets, not

nested loops● Set benchmark times● Use smallest data

type possible● Rewrite subqueries to

joins

Mysqld● Pay attention to

new technologies, updates

● Know which buffers are per-session, global

● Do not ignore log files

Page 80: Congratsyourthedbatoo

MySQL Connect ConferenceMySQL Connect Conference

Sept 21st - 23rd in San Francisco

The Call for Proposals (CFP) for MySQL Connect 2013 is now open. Conference attendees want to hear the best ideas from MySQL experts from around the globe: developers, visionaries, customers, partners. Proposals must be submitted by April 12, so don’t wait to develop the content that only you know—and that MySQL attendees want to hear.

Page 81: Congratsyourthedbatoo

MySQL Marinate!

Virtual self-study of MySQL through the Boston MySQL Users Group (

http://www.meetup.com/mysqlbos/)

http://www.meetup.com/Virtual-Tech-Self-Study/events/84103332/

Page 82: Congratsyourthedbatoo

Q&AQ&[email protected] - slideshare.net/davestokes