Mysql Database Administrator
Total Page:16
File Type:pdf, Size:1020Kb
MySQL Database Administrator Author: Kacper Wysocki Contact: [email protected] Date: December 2010 License: Creative Commons: CC BY-SA Oslo, December 2010, CC BY-SA Contents Introduction 5 Introductions everybody 5 About this course 5 Course outline 6 Course schedule 6 How to do excersies 6 MySQL: history and future 6 MySQL: the present 7 MySQL: the future 7 MySQL compared to other DBs 7 MySQL language support 8 Embedding MySQL 8 Getting help with MySQL 8 MySQL architecture 9 Modular architecture 9 The MySQL modules 9 Client/server architecture 10 Installing MySQL 10 Installation process 10 Distribution packages 11 MySQL official binaries 11 Deploying sandboxes 12 Installing from source 13 Server Startup and Shutdown 14 MySQL relevant files 15 Excersises: Installation 15 Upgrading MySQL 16 Clients: the mysql* suite 16 Client: mysql 16 Excersise: Client mysql 16 Excersise: mysql CLI 17 Further CLI fun 17 Digression: some SQL 18 Client: mysqladmin 18 Excersises: Client: mysql 18 Clients: applications and libraries 18 Oslo, December 2010, CC BY-SA migration 19 Importing data: timezones 19 Importing data 19 Excersises: importing data 20 Excersises: time zones 20 Exporting data 20 Excersises: Exporting data 21 Configuration 21 More configuration 21 Run-time Variables 22 MySQL Architecture 23 Storage Engines 23 Storage Engines 23 Storage Engines types 23 MyISAM 24 MYISAM_MRG 24 InnoDB 24 Excersises: InnoDB 24 FEDERATED 25 CSV 25 ARCHIVE 25 MEMORY 25 BLACKHOLE 25 So... which engine? 26 Engine Excersises 26 Implementing Security 26 Security consideration 26 MySQL access levels 26 User wildcards 27 MySQL Privileges 27 User management 28 The grant tables 28 mysqladmin user access 29 Resource limits 29 Exercises: User Management 29 Data Types 29 Which data type? 29 Oslo, December 2010, CC BY-SA Data Types: Numeric 30 Data Types: Floating point 30 Data Types: Fixed 30 Data Types: Bit 30 Data Types and SQL modes 30 Excersise: Numeric Data 31 Data Types: Strings 31 Excersise: Strings 31 Data Types: Binary 32 Data Types: Time and Date 32 Data Types: null 32 Indexes 32 SQL Locking 33 Transactions and locking 33 Transactions: ACID 33 Transaction isolation levels 33 Transaction operations 33 Excersises: Transactions 33 Metadata 34 Metadata: show 34 Metadata: INFORMATION_SCHEMA 34 Maintenance 34 Table Maintenance 35 Maintenance: CHECK TABLE 35 check table statement 35 checksum table 35 Maintenance: REPAIR TABLE 36 repair table 36 analyze table 36 Maintenance: OPTIMIZE TABLE 36 optimize table 36 Maintenance: ALTER TABLE 36 mysqlcheck and myisamchk 37 Excersises: Table Maintenance 37 MySQL backup options 37 Backup options: raw backups 37 Backup options: mysqldump 37 Oslo, December 2010, CC BY-SA Backup options: xtrabackup 37 Backup options: backup slave 38 Backup options: snapshots 38 Incremental backups 38 Backup recovery 38 Recovery: Binary Log 38 MySQL concepts 38 Stored routines 39 Triggers 39 Events 39 Event schedule 40 Views 40 Monitoring 40 Closing tips 40 Scaling MySQL 41 Clustering for HA or HP 41 Wrapup & Questions? 41 Introduction • Hello everybody • About this course • Course Outline • What's covered today • Getting up and running with MySQL Introductions everybody • Hi, my name is $name • I do $foo and am interested in $bar • After this course I will be $something MySQL • Practical stuff: coffee, breaks, food, the john. • Practical stuff: start/end times About this course • Course outline • Schedule • How to do excersises Oslo, December 2010, CC BY-SA • The Book(s) • Topics covered today Course outline In this course you will learn: • The story, installation, clients of MySQL • Import and export of data • Configuration and user management • Data types, transactions, meta data • Maintenance, backup, recovery • Storage engine fundamentals • Some best practices for performance Course schedule Time Day 1 Day 2 Day 3 09:30-10:00 Configuration Metadata 10:00-11:00 Overview, story Storage Engines Maintenance 11:15-12:00 Server install Storage Engines Recovery 12:00-12:30 Lunch Lunch Lunch 12:30-13:30 Connect clients User Management Building blocks 13:45-14:45 Migrating:import Data Types 15:00-15:45 Migrating:export Transactions Tuning and HA How to do excersies • After class? • While we are talking... • After each topic! • Stop me! Ask! MySQL: history and future History • 1994: Widenius & Axmark begat MySQL AB • 1995-2008: V.3.23, 4.0, 4.1, 5.0. Popularity! • 2008: Sun aquisition & V.5.1 • 2009: V 6.0: Falcon Maria • 2010: Oracle eclipses the Sun Oslo, December 2010, CC BY-SA MySQL: the present MySQL used and developed by major players! • Goolge • Flickr • Facebook • Tripwire • Zimbra • Banking • (almost all) web developers MySQL: the future "Prediction is very difficult, especially about the future." -- Niels Bohr 2011: The year of MySQL. • V 5.5 • MariaDB • XtraDB • OurDelta • Drizzle The recent year has been a controversial one for MySQL due to the uncertanity of the MySQL license status under Oracle, and there have sprung up several projects that fork the source code, expanding in its functionality with an emphasis on community maintenance under the GNU Public License. Here are presented some of the most promising forks: XtraDB, a backwards-compatible fork which includes many performance improvements, among them a fork of the InnoDB storage engine with better scalability on modern hardware, better monitoring and other features useful in high performance environments. It includes a non-blocking, online backup program called XtraBackup and it is developed and supported by Percona Inc. http://www.percona.com MariaDB is fork lead by Monty Widenius, the founder of MySQL, and aims to maintain a high level of API and binary compatibility with MySQL. MariaDB includes XtraDB as a replacement for InnoDB. http://mariadb.org OurDelta is a MySQL binary distribution aiming at providing enhanced, patched binaries of MySQL packaged for common production platforms. http://ourdelta.org Drizzle is a complete restructuring and simplification of the MySQL code after the KISS principle, which does away with authentication mechanisms and redundant data types in favour of plugin delegation and simplicity. Contributors include members from Canonical Ltd., Google, Six Apart, Sun Microsystems and Rackspace. Some of these projects may be worth looking into if your production environment has special operational or performance requirements, or if you have concerns about the license status of the Oracle MySQL distribution. For general use however the binary packages provided by your production environment are easiest to deploy and maintain, and will suffice. MySQL compared to other DBs Oslo, December 2010, CC BY-SA • Easy setup • Lots of documentation • Fully featured + plugins! • Clients abound • From low end to high end • Unique: Storage Engines! MySQL plugins allows extending MySQL functionality in a number of different areas, includig storage engines, information schema, parser plugins and data types. MySQL language support Most languages wrap the libmysql C library. • Perl (DBD::MySQL + objects) • PHP • C++ • C# • Java • Ruby, Lua, Lisp, Go • Many more! PHP extensions: mysql, mysqli, pdo_mysqlnd(native PHP) Python: MySQLdb. JDBC driver for Java: a java native implementation of the MySQL protocol. Connector/NET: ADO.NET managed provider for the .NET framework version 1.1 and up. C++ connector mimics a subset of the JDBC 4.0 API. Many other third-party connectors are available. Embedding MySQL Embed server into a program: • libmysqld shared library • MySQL MXJ JAR wrapper for J2EE. Getting help with MySQL • MySQL reference web: http://dev.mysql.com/ • Mailing list and documentation. • Training for Development, Clustering. • Commercial support from OSS vendors (Redpill Linpro, Percona etc.) A useful feature of the MySQL web site is the built-in URL search. If you ever need to look up any particular MySQL command or feature, you may enter http://dev.mysql.com/<searchterm> and you will be Oslo, December 2010, CC BY-SA automatically forwarded to the closest matches in the MySQL documentation. MySQL architecture • Client/Server communications • Unix sockets • TCP/IP • Shared Memory/Named Pipes (windows) • Non-client programs Modular architecture • All clients use client library • Code is segmented into modules • Modules are subsystems shared across the code • The modules have no interdependencies The MySQL modules The core modules are • Shared base functions • Resources: Process and thread • Memory: Caches and buffers • Comms: Networking • Logs: Logging and file rotation • Access: Access tables and grants Oslo, December 2010, CC BY-SA Client/server architecture Installing MySQL • Recommended deployment • Installation alternatives • Packages • Sandbox • Compilation • Upgrading MySQL Installation process • Install package Oslo, December 2010, CC BY-SA • set MySQL root password • mysql_secure_installation: • remove anonymous users • set root pw • remove test database Distribution packages Recommended for most uses # Red Hat, CentOS etc.. yum install mysql mysql-server mysql-devel # Debian and Ubuntu apt-get install mysql-server The best reason to install from distribution packages is that it saves time and effort, and maintenance overhead is kept to a minimum. Setting up the required mysql user and group accounts as well as the mysql directories is done automatically in the distribution scripts. Upgrading mysql packages happens automatically while upgrading the rest of the distribution. This type of production environment is the easiest to reproduce and