MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

25
MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012

Transcript of MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Page 1: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

MySQL Gubbins

Richard Sinclair

HepSysMan – 11/05/2012

Page 2: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Agenda

• Installation• Users• Server Parameters• Security• Backup and Recovery (the important

bit)• Coffee (the more important bit)

Page 3: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Installation

• $ yum install mysql-server mysql• Edit my.cnf to specify data directory:

[mysqld]

datadir=/var/data/mysql• $ /usr/bin/mysql_install_db• $ /usr/bin/mysql_secure_installation

Page 4: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Installation 2• Red Hat ships MySQL 5.1• 5.5 Introduced:

– Better multi-core performance– Better Partitioning (Truncate Partition)– Innodb as default storage engine– Performance schema

• Install as RPM from MySQL website– Same Post Install Steps

Page 5: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Storage Engines• MyISAM – default in 5.1:

– Very fast read/writes– No row level locking (only table locks)– Separate datafiles for each table

• Innodb – default in 5.5:– Row level locking– Foreign key support– Storage for all Innodb tables in single file (by

default)

Page 6: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Upgrading from 5.1 to 5.5

• Shutdown MySQL• Install new version from RPM• Run the mysql_upgrade script• If you get the “Table 'mysql.proxies_priv'

doesn't exist” error then run:– $ mysqld --skip-grant-tables & – $ mysql_upgrade

Page 7: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Starting/Stopping MySQL

• Recommended to use mysqld_safe rather than mysqld start:– Will restart the MySQL server after a crash– Will read the [mysqld_safe] section of the

my.cnf file as well as the [mysqld] section

Page 8: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

User Administration(The Bad Way)

• CREATE USER 'monty'@‘%' IDENTIFIED BY 'some_pass';

• GRANT ALL PRIVILEGES ON *.* TO 'monty'@' %' WITH GRANT OPTION;

Page 9: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

User Administration(The Good Way)

• CREATE USER 'monty'@' montysmachine.ac.uk' IDENTIFIED BY 'some_pass';

• GRANT ALL PRIVILEGES ON montydb.* TO 'monty'@‘montysmachine.ac.uk';

Page 10: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Server Parameters

• MySQL is pretty good straight ‘out of the box’

• There are a few parameters you can change to improve performance, improve logging and ensure disaster recovery is possible.

• All set in /etc/my.cnf file

Page 11: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Basic Settings

datadir=/var/lib/mysql/data

socket=/var/lib/mysql/mysql.sock

user=mysql

Page 12: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Log Files

• log-error=/var/log/mysqld.log• slow_query_log_file=/var/log/slow.log• long_query_time = 60• log-output=TABLE,FILE• mysql> SHOW CREATE TABLE

mysql.slow_log;

Page 13: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Performance Tweaks

• MySQL is pretty quick but it can be made A LOT faster very easily.

• These are the 2 parameters that have made the most difference for us….

Page 14: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

key_buffer_size

• Allocates memory to cache indexes• Defaults to 8MB• Too low meaning that indexes are

mostly read from disk needlessly• Increasing to 256MB on a 4GB system

improved throughput by 300% (on MyISAM tables)

Page 15: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

innodb_buffer_pool_size

• Only relevant when using InnoDB• Defines the maximum memory InnoDB

can use to buffer data• Defaults to 128MB• Raising to 512MB on a 4GB system

greatly increased throughput

Page 16: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Disaster (Avoidance)• Enable Binary Logging (in my.cnf):

– log-bin = /mysql-backup/binlog/bin.log– expire_logs_days = 10– max_binlog_size = 256M

• Ensure Binary Logs are on separate disk to Data Directory

• Can be used for Point-in-Time recovery• More later….

Page 17: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Security• Don’t use port 3306• Only allow super user access from localhost (fixed by

mysql_secure_installation script)• Don’t run MySQL as root (set user=mysql in my.cnf

file)• Change name of root user:

– mysql> UPDATE mysql.user SET user = ‘superman' WHERE user = 'root';

Page 18: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

SQL Injection Attacks

• If your database is public facing via a web server:– Change root user name– Test, test and test web pages (using

common injection strings – plenty on Google)

– Do not grant FILE privilege to users

Page 19: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Backups

• Daily backups + binary logs should be all you need to return to any point in time.

• Which tool to use for backups…….

Page 20: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Backup Utilities

• Mysqldump• Mysqlhotcopy• Mysql-zrm• Xtrabackup• About a hundred more….

Page 21: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

What We Do

• Xtrabackup with a homebrewed wrapper which deals with alerts, cataloguing of backups, compression and more.

• Very fast for innodb tables, table locks on MyISAM (no way to avoid this)

Page 22: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Key Advantages

• Catalogue (separate MySQL server) records Binary Log position so you know at what point in time the backup was taken.

• Cleans out old backups based on user defined retention policy.

• Easy to restore – drag and drop.

Page 23: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Point In Time Recovery• Restore the databases from most recent

backup• Get the binlog position from catalogue or text

file in backup directory• Use mysqlbinlog tool to parse binlogs from

position of backups onwards into .sql script• Run the .sql script from MySQL• Get a cuppa (beverage size is dependent on

amount of changes in binlogs)

Page 24: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Links

[email protected][email protected]• http://www.percona.com/software/

percona-xtrabackup

Page 25: MySQL Gubbins Richard Sinclair HepSysMan – 11/05/2012.

Questions? (and hopefully answers)