SlideShare a Scribd company logo
Eduardo Legatti - DBA
Agenda
• History
• Download / Install
• Configuration
• Users / Privileges
• Instance Structures / Engines
• InnoDB vs. MyISAM
• Transaction Isolation Level
• Locks
• Binary Log
• CSV Files
• Exporting / Importing (Logical backup)
• COLD / HOT Backup (Physical backup)
• Events (Jobs)
• Performance Tuning
• MySQL in the CLOUD
History
O MySQL foi criado na Suécia por suecos e um finlandês: David Axmark, Allan Larsson e Michael "Monty" Widenius, que
têm trabalhado juntos desde a década de 1980. Hoje seu desenvolvimento e manutenção empregam aproximadamente
400 profissionais no mundo inteiro, e mais de mil contribuem testando o software, integrando-o a outros produtos, e
escrevendo a respeito dele.
No dia 16 de Janeiro de 2008, a MySQL AB, desenvolvedora do MySQL foi adquirida pela Sun Microsystems, por US$ 1
bilhão, um preço jamais visto no setor de licenças livres. No dia 20 de Abril de 2009 foi anunciado que a Oracle compraria a
Sun Microsystems e todos os seus produtos, incluindo o MySQL. Isso aconteceu após as negociações da Sun com a IBM
terem fracassado. A Oracle não perdeu tempo e ofereceu US$ 7,4 bilhões pela Sun Microsystems que aceitou a proposta. O
Conselho de Administração da Sun Microsystems aprovou a transação por unanimidade.
MySQL - History
MySQL - History
MySQL - History
mysql> select @@version;
+------------+
| @@version |
+------------+
| 5.0.88 |
+------------+
mysql> create table customer (name varchar(5));
Query OK, 0 rows affected (0,11 sec)
mysql> insert into customer (name) values ('123456789');
1 row(s) affected, 1 warning(s)
Warning Code : 1265
Data truncated for column 'name' at row 1
mysql> select * from customer;
+-------+
| name |
+-------+
| 12345 |
+-------+
mysql> select @@version;
+------------+
| @@version |
+------------+
| 5.7.18-log |
+------------+
mysql> create table customer (name varchar(5));
Query OK, 0 rows affected (0,11 sec)
mysql> insert into customer (name) values ('123456789');
ERROR 1406 (22001): Data too long for column 'name' at row 1
Download / Install
MySQL - Download
MySQL - Download
MySQL – Download (64 bits)
[root@linux1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.18-1.el7.x86_64.rpm --no-check-certificate
Conectando-se a cdn.mysql.com|104.105.143.228|:443... conectado.
A requisição HTTP foi enviada, aguardando resposta... 200 OK
Tamanho: 335516 (328K) [application/x-redhat-package-manager]
Salvando em: “mysql-community-common-5.7.18-1.el7.x86_64.rpm”
100%[==============================================================================================>] 335.516 1,21M/s em 0,3s
[root@linux1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.18-1.el7.x86_64.rpm --no-check-certificate
Conectando-se a cdn.mysql.com|104.105.143.228|:443... conectado.
A requisição HTTP foi enviada, aguardando resposta... 200 OK
Tamanho: 2103888 (2,0M) [application/x-redhat-package-manager]
Salvando em: “mysql-community-libs-5.7.18-1.el7.x86_64.rpm”
100%[===============================================================================================>] 2.103.888 3,40M/s em 0,6s
[root@linux1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.18-1.el7.x86_64.rpm --no-check-certificate
Conectando-se a cdn.mysql.com|104.105.143.228|:443... conectado.
A requisição HTTP foi enviada, aguardando resposta... 200 OK
Tamanho: 23039984 (22M) [application/x-redhat-package-manager]
Salvando em: “mysql-community-client-5.7.18-1.el7.x86_64.rpm”
100%[===============================================================================================>] 23.039.984 2,86M/s em 7,3s
[root@linux1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.18-1.el7.x86_64.rpm --no-check-certificate
Conectando-se a cdn.mysql.com|104.105.143.228|:443... conectado.
A requisição HTTP foi enviada, aguardando resposta... 200 OK
Tamanho: 156322552 (149M) [application/x-redhat-package-manager]
Salvando em: “mysql-community-server-5.7.18-1.el7.x86_64.rpm”
100%[===============================================================================================>] 156.322.552 967K/s em 3m 4s
MySQL – Install (64 bits)
[root@linux1 ~]# ls -lh *.rpm
-rw-r--r-- 1 root root 328K Mar 20 06:40 mysql-community-common-5.7.18-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 2,1M Mar 20 06:40 mysql-community-libs-5.7.18-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 22M Mar 20 06:40 mysql-community-client-5.7.18-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 150M Mar 20 06:41 mysql-community-server-5.7.18-1.el7.x86_64.rpm
[root@linux1 ~]# rpm -ivh mysql-community-common-5.7.18-1.el7.x86_64.rpm
aviso: mysql-community-common-5.7.18-1.el7.x86_64.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparando... ########################################### [100%]
1:mysql-community-common ########################################### [100%]
[root@linux1 ~]# rpm -ivh mysql-community-libs-5.7.18-1.el7.x86_64.rpm
aviso: mysql-community-libs-5.7.18-1.el7.x86_64.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparando... ########################################### [100%]
1:mysql-community-libs ########################################### [100%]
[root@linux1 ~]# rpm -ivh mysql-community-client-5.7.18-1.el7.x86_64.rpm
aviso: mysql-community-client-5.7.18-1.el7.x86_64.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparando... ########################################### [100%]
1:mysql-community-client ########################################### [100%]
[root@linux1 ~]# rpm -ivh mysql-community-server-5.7.18-1.el7.x86_64.rpm
aviso: mysql-community-server-5.7.18-1.el7.x86_64.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparando... ########################################### [100%]
1:mysql-community-server ########################################### [100%]
[root@linux1 ~]# rpm -qa | grep mysql
mysql-community-server-5.7.18-1.el7.x86_64
mysql-community-common-5.7.18-1.el7.x86_64
mysql-community-libs-5.7.18-1.el7.x86_64
mysql-community-client-5.7.18-1.el7.x86_64
[root@linux1 ~]# rpm -e mysql-community-server-5.7.18-1.el7.x86_64 --nodeps
Warning: /etc/my.cnf salvo como /etc/my.cnf.rpmsave
[root@linux1 ~]# rpm -e mysql-community-common-5.7.18-1.el7.x86_64 --nodeps
[root@linux1 ~]# rpm -e mysql-community-libs-5.7.18-1.el7.x86_64 --nodeps
[root@linux1 ~]# rpm -e mysql-community-client-5.7.18-1.el7.x86_64 –nodeps
[root@linux1 ~]# rm -rf /var/lib/mysql/
MySQL – Uninstall (64 bits)
Configuration
[root@linux1 tmp]# service mysqld start (systemctl start mysqld)
Inicializando o banco de dados MySQL: [ OK ]
Installing validate password plugin: [ OK ]
Iniciando o mysqld: [ OK ]
[root@linux1]# grep 'temporary password' /var/log/mysqld.log
2017-05-02T10:30 [Note] A temporary password is generated for root@localhost: 8V*vubKtFQp(
[root@linux1 ~]# mysql -uroot –p'8V*vubKtFQp('
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5 Server version: 5.7.18
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing
this statement.
MySQL - Initial Configuration
mysql> alter user root@localhost identified by '@#4Jhfjk43jkdjk3#@';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user root@localhost identified by 'Asdjer3jk4kjd4@#';
Query OK, 0 rows affected (0,00 sec)
mysql> show variables like '%password_policy';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| validate_password_policy | MEDIUM |
+--------------------------+--------+
1 row in set (0,07 sec)
mysql> SET GLOBAL validate_password_policy=LOW;
Query OK, 0 rows affected (0,03 sec)
mysql> alter user root@localhost identified by 'admin';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user root@localhost identified by '12345678';
Query OK, 0 rows affected (0,01 sec)
MySQL - Configuration (reset root password)
mysql> show plugins;
+----------------------------+----------+--------------------+----------------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+----------------------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL |
| partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
| validate_password | ACTIVE | VALIDATE PASSWORD | validate_password.so | GPL |
+----------------------------+----------+--------------------+----------------------+---------+
45 rows in set (0,01 sec)
mysql> UNINSTALL PLUGIN validate_password;
Query OK, 0 rows affected (0,07 sec)
mysql> alter user root@localhost identified by 'admin';
Query OK, 0 rows affected (0,00 sec)
mysql> INSTALL PLUGIN validate_password SONAME 'validate_password.so';
Query OK, 0 rows affected (0,03 sec)
MySQL - Configuration (reset root password)
[root@linux1]# whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
mysql> help
For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (?) Synonym for `help'.
clear (c) Clear the current input statement.
connect (r) Reconnect to the server. Optional arguments are db and host.
delimiter (d) Set statement delimiter.
edit (e) Edit command with $EDITOR.
ego (G) Send command to mysql server, display result vertically.
exit (q) Exit mysql. Same as quit.
go (g) Send command to mysql server.
help (h) Display this help.
nopager (n) Disable pager, print to stdout.
notee (t) Don't write into outfile.
pager (P) Set PAGER [to_pager]. Print the query results via PAGER.
print (p) Print current command.
prompt (R) Change your mysql prompt.
quit (q) Quit mysql.
rehash (#) Rebuild completion hash.
source (.) Execute an SQL script file. Takes a file name as an argument.
status (s) Get status information from the server.
system (!) Execute a system shell command.
tee (T) Set outfile [to_outfile]. Append everything into given outfile.
use (u) Use another database. Takes database name as argument.
charset (C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (W) Show warnings after every statement.
nowarning (w) Don't show warnings after every statement.
resetconnection(x) Clean session context.
For server side help, type 'help contents'
MySQL - mysql utility
mysql> help contents;
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item>
is one of the following
categories:
Account Management
Administration
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Procedures
Storage Engines
Table Maintenance
Transactions
User-Defined Functions
Utility
[root@linux1 ~]# systemctl start mysqld
[root@linux1 ~]# systemctl status mysqld
* mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2017-05-02 13:45:55 BST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3105 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3084 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3109 (mysqld)
CGroup: /system.slice/mysqld.service
??3109 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Mai 2 13:45:50 linux1 systemd[1]: Starting MySQL Server...
Mai 2 13:45:55 linux1 systemd[1]: Started MySQL Server.
[root@linux1 ~]# systemctl stop mysqld
[root@linux1 ~]# systemctl status mysqld
* mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2017-05-02 13:47:00 BST; 904ms ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3105 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3084 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3109 (code=exited, status=0/SUCCESS)
Mai 2 13:45:50 linux1 systemd[1]: Starting MySQL Server...
Mai 2 13:45:55 linux1 systemd[1]: Started MySQL Server.
Mai 2 13:46:59 linux1 systemd[1]: Stopping MySQL Server...
Mai 2 13:47:00 linux1 systemd[1]: Stopped MySQL Server.
MySQL - Status / Stop / Start
[root@linux1 ~]# service mysqld status
mysqld (pid 3489) está em execução...
[root@linux1 ~]# ps aux | grep mysql
root 3244 0.0 0.1 5152 1368 pts/0 S 15:00 0:01
/bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock
--pid-file=/var/run/mysqld/mysqld.pid--basedir=/usr --user=mysql
mysql 3489 1.6 16.7 555380 172072 pts/0 Sl 15:00 1:37 /usr/sbin/mysqld --basedir=/usr
--datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql
--log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
[root@linux1 ~]# service mysqld stop (systemctl stop mysqld)
Parando o mysqld: [ OK ]
[root@linux1 ~]# service mysqld start (systemctl start mysqld)
Iniciando o mysqld: [ OK ]
[root@linux1 ~]# service mysqld restart (systemctl restart mysqld)
Parando o mysqld: [ OK ]
Iniciando o mysqld: [ OK ]
mysql> show variables like '%bind%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| bind_address | * |
+---------------+-------+
[root@linux1 ~]# netstat -na | grep 3306
tcp 0 0 :::3306 :::* LISTEN
MySQL - Status / Stop / Start
mysql> help SHUTDOWN;
Name: 'SHUTDOWN'
This statement stops the MySQL server. It requires the SHUTDOWN
privilege.
SHUTDOWN was added in MySQL 5.7.9. It provides an SQL-level interface
to the same functionality available using the mysqladmin shutdown
command or the mysql_shutdown() C API function.
mysql> shutdown;
Query OK, 0 rows affected (0,03 sec)
mysql> select 1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)
ERROR:
Can't connect to the server
MySQL - Files or Resources
[root@linux1 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
MySQL - Configuration File
[root@linux1 ~]# tail -20 /var/log/mysqld.log
2017-05-02T19:41:01.660047Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s)
are active.
2017-05-02T19:41:01.660148Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2017-05-02T19:41:01.674840Z 0 [Note] InnoDB: Waiting for purge to start
2017-05-02T19:41:01.730361Z 0 [Note] InnoDB: 5.7.17 started; log sequence number 2537004
2017-05-02T19:41:01.749315Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2017-05-02T19:41:01.759440Z 0 [Note] Plugin 'FEDERATED' is disabled.
2017-05-02T19:41:01.854483Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170408 16:41:01
2017-05-02T19:41:02.093335Z 0 [Warning] CA certificate ca.pem is self signed.
2017-05-02T19:41:02.211370Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2017-05-02T19:41:02.218146Z 0 [Note] IPv6 is available.
2017-05-02T19:41:02.218559Z 0 [Note] - '::' resolves to '::';
2017-05-02T19:41:02.219456Z 0 [Note] Server socket created on IP: '::'.
2017-05-02T19:41:02.428972Z 0 [Note] Event Scheduler: Loaded 0 events
[root@linux1 /]# service mysqld start
MySQL Daemon failed to start.
Iniciando o mysqld: [FALHOU]
[root@linux1 ~]# cat /var/log/mysqld.log | grep ERROR
2017-05-02T20:35:39.247229Z 0 [ERROR] unknown variable 'not_exist=legatti'
2017-05-02T20:35:39.247309Z 0 [ERROR] Aborting
MySQL - Error Log File
mysql> show variables like 'log_timestamps';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| log_timestamps | UTC |
+----------------+-------+
mysql> set GLOBAL log_timestamps=SYSTEM;
Query OK, 0 rows affected (0,00 sec)
[root@linux1 ~]# cat /var/log/mysqld.log | grep ERROR
2017-05-02T20:35:39.247229Z 0 [ERROR] unknown variable 'not_exist=legatti'
2017-05-02T20:35:39.247309Z 0 [ERROR] Aborting
MySQL - Error Log File
As of MySQL 5.7.2, the log_timestamps system variable controls the timestamp time zone of
messages written to the error log (as well as to general query log and slow query log files).
Permitted values are UTC (the default) and SYSTEM (local system time zone). Before MySQL
5.7.2, messages use the local system time zone
[root@linux1 ~]# mysql –u<user> -p<password>  mysql –uroot –padmin
mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 5.7.18 |
+-----------+
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
mysql> show variables like 'bind_address';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| bind_address | * |
+---------------+-------+
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
MySQL - Connecting Locally
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@linux1 ~]# mysql -uroot -padmin -e "show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| bd01 |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@linux1 ~]# echo "show databases" | mysql -uroot -padmin
Database
information_schema
mysql
performance_schema
sys
[root@linux1 ~]# mysql –h<host> –u<user> -p<password> --port<port>
C:>mysql -h192.168.56.101 -uroot -padmin --port 3306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 24
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
MySQL - Connecting Remotely
Users / Privileges
mysql> CREATE USER 'admin01'@'192.168.56.102' IDENTIFIED BY '#a1234@';
Query OK, 0 rows affected (0,00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin02'@'%' IDENTIFIED BY '#b1234@';
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> CREATE USER 'admin03'@'192.168.%' IDENTIFIED BY '#c1234@';
Query OK, 0 rows affected (0,01 sec)
mysql> select user,host,authentication_string from mysql.user order by 1;
+-----------+----------------+-------------------------------------------+
| user | host | authentication_string |
+-----------+----------------+-------------------------------------------+
| admin01 | 192.168.56.102 | *1260D0031D9EDAC141609A3723FBD16C2260425F |
| admin02 | % | *E9190029100941FBE4D00E22146E8EDA75667142 |
| admin03 | 192.168.% | *3A9EF2D8B8D45B60FD5D978A872D28C62FD139BB |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| root | localhost | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| root | % | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
+-----------+----------------+-------------------------------------------+
6 rows in set (0,00 sec)
MySQL - Creating Users
CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'hostname' IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> show grants for scott;
+------------------------------------+
| Grants for scott@% |
+------------------------------------+
| GRANT SELECT ON *.* TO 'scott'@'%' |
+------------------------------------+
1 row in set (0.00 sec)
mysql> RENAME USER 'scott'@'%' TO 'adam'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for adam;
+-----------------------------------+
| Grants for adam@% |
+-----------------------------------+
| GRANT SELECT ON *.* TO 'adam'@'%' |
+-----------------------------------+
1 row in set (0.00 sec)
MySQL - Renaming Users
RENAME USER 'old_user' TO 'new_user';
The RENAME USER statement renames existing MySQL accounts. An error occurs for old accounts that do not exist or new
accounts that already exist. To use RENAME USER, you must have the global CREATE USER privilege, or the UPDATE privilege
for the mysql database.
mysql> create user 'scott'@'%' identified by 'tiger';
Query OK, 0 rows affected (0.00 sec)
mysql> grant select on *.* to 'scott'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> alter user scott ACCOUNT LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@linux1 ~]# mysql -uscott -ptiger
ERROR 3118 (HY000): Access denied for user 'scott'@'localhost'. Account is locked.
Mysql> alter user scott ACCOUNT UNLOCK;
MySQL - Creating Users (account lock – 5.7)
mysql> SHOW PRIVILEGES;
+-------------------------+---------------------------------------+-------------------------------------------------------+
| Privilege | Context | Comment |
+-------------------------+---------------------------------------+-------------------------------------------------------+
| Alter | Tables | To alter the table |
| Alter routine | Functions,Procedures | To alter or drop stored functions/procedures |
| Create | Databases,Tables,Indexes | To create new databases and tables |
| Create routine | Databases | To use CREATE FUNCTION/PROCEDURE |
| Create temporary tables | Databases | To use CREATE TEMPORARY TABLE |
| Create view | Tables | To create new views |
| Create user | Server Admin | To create new users |
| Delete | Tables | To delete existing rows |
| Drop | Databases,Tables | To drop databases, tables, and views |
| Event | Server Admin | To create, alter, drop and execute events |
| Execute | Functions,Procedures | To execute stored routines |
| File | File access on server | To read and write files on the server |
| Grant option | Databases,Tables,Functions,Procedures | To give to other users those privileges you possess |
| Index | Tables | To create or drop indexes |
| Insert | Tables | To insert data into tables |
| Lock tables | Databases | To use LOCK TABLES (together with SELECT privilege) |
| Process | Server Admin | To view the plain text of currently executing queries |
| Proxy | Server Admin | To make proxy user possible |
| References | Databases,Tables | To have references on tables |
| Reload | Server Admin | To reload or refresh tables, logs and privileges |
| Replication client | Server Admin | To ask where the slave or master servers are |
| Replication slave | Server Admin | To read binary log events from the master |
| Select | Tables | To retrieve rows from table |
| Show databases | Server Admin | To see all databases with SHOW DATABASES |
| Show view | Tables | To see views with SHOW CREATE VIEW |
| Shutdown | Server Admin | To shut down the server |
| Super | Server Admin | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc. |
| Trigger | Tables | To use triggers |
| Create tablespace | Server Admin | To create/alter/drop tablespaces |
| Update | Tables | To update existing rows |
| Usage | Server Admin | No privileges - allow connect only |
+-------------------------+---------------------------------------+-------------------------------------------------------+
MySQL - Privileges
GRANT ALTER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT ALTER ROUTINE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE ROUTINE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE TABLESPACE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE TEMPORARY TABLES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE USER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE VIEW ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT DELETE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT DROP ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT EVENT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT EXECUTE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT FILE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT INDEX ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT INSERT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT LOCK TABLES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT PROCESS ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT REFERENCES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT RELOAD ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT REPLICATION CLIENT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT REPLICATION SLAVE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SELECT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SHOW DATABASES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SHOW VIEW ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SHUTDOWN ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SUPER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT TRIGGER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT UPDATE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
MySQL - Creating Users (Privileges)
https://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html
mysql> SHOW CREATE USER 'admin02'@'%';
+-------------------------------------------------------------------------------------------------------------------+
| CREATE USER for admin02@% |
+-------------------------------------------------------------------------------------------------------------------+
| CREATE USER 'admin02'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*E9190029100941FBE4D00E22146E8EDA75667142' |
+-------------------------------------------------------------------------------------------------------------------+
1 row in set (0,01 sec)
mysql> SHOW GRANTS FOR 'admin02'@'%';
+----------------------------------------------+
| Grants for admin02@% |
+----------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'admin02'@'%' |
+----------------------------------------------+
1 row in set (0,00 sec)
mysql> SELECT CONCAT('SHOW GRANTS FOR ''',USER,'''@''',HOST,''';') FROM mysql.user WHERE USER <> '';
+------------------------------------------------------+
| CONCAT('SHOW GRANTS FOR ''',USER,'''@''',HOST,''';') |
+------------------------------------------------------+
| SHOW GRANTS FOR 'admin01'@'192.168.56.102'; |
| SHOW GRANTS FOR 'admin02'@'%'; |
| SHOW GRANTS FOR 'admin03'@'192.168.%'; |
| SHOW GRANTS FOR 'root'@'%'; |
| SHOW GRANTS FOR 'mysql.sys'@'localhost'; |
| SHOW GRANTS FOR 'root'@'localhost'; |
+------------------------------------------------------+
6 rows in set (0,00 sec)
MySQL - Extracting Users Information
SHOW CREATE USER 'username'@'hostname';
SHOW GRANTS FOR 'username'@'hostname';
mysql> select current_user;
+----------------+
| current_user |
+----------------+
| root@localhost |
+----------------+
1 row in set (0,00 sec)
mysql> SHOW GRANTS FOR CURRENT_USER;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0,00 sec)
mysql> show grants for root;
+-------------------------------------------+
| Grants for root@% |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' |
+-------------------------------------------+
1 row in set (0,00 sec)
mysql> show grants for root@localhost;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0,00 sec)
MySQL - Extracting Users Information
mysql> desc mysql.user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| authentication_string | text | YES | | NULL | |
|... | | | | | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
MySQL - Extracting Users Information
mysql.user: User level privileges
mysql> desc mysql.db;
+-----------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
+-----------------------+---------------+------+-----+---------+-------+
MySQL - Extracting Users Information
mysql.db: Database level privileges
mysql> desc mysql.tables_priv;
+-------------+--------------------------------------------------------------------------------------------+
| Field | Type |
+-------------+--------------------------------------------------------------------------------------------+
| Host | char(60) |
| Db | char(64) |
| User | char(32) |
| Table_name | char(64) |
| Grantor | char(93) |
| Timestamp | timestamp |
| Table_priv | set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index',etc.) |
| Column_priv | set('Select','Insert','Update','References') |
+-------------+--------------------------------------------------------------------------------------------+
MySQL - Extracting Users Information
mysql.tables_priv: Table level privileges
mysql> desc mysql.columns_priv;
+-------------+----------------------------------------------+------+-----+
| Field | Type | Null | Key |
+-------------+----------------------------------------------+------+-----+
| Host | char(60) | NO | PRI |
| Db | char(64) | NO | PRI |
| User | char(32) | NO | PRI |
| Table_name | char(64) | NO | PRI |
| Column_name | char(64) | NO | PRI |
| Timestamp | timestamp | NO | |
| Column_priv | set('Select','Insert','Update','References') | NO | |
+-------------+----------------------------------------------+------+-----+
mysql.columns_priv: Column level privileges
mysql> desc mysql.procs_priv;
+--------------+----------------------------------------+------+-----+
| Field | Type | Null | Key |
+--------------+----------------------------------------+------+-----+
| Host | char(60) | NO | PRI |
| Db | char(64) | NO | PRI |
| User | char(32) | NO | PRI |
| Routine_name | char(64) | NO | PRI |
| Routine_type | enum('FUNCTION','PROCEDURE') | NO | PRI |
| Grantor | char(93) | NO | MUL |
| Proc_priv | set('Execute','Alter Routine','Grant') | NO | |
| Timestamp | timestamp | NO | |
+--------------+----------------------------------------+------+-----+
MySQL - Extracting Users Information
mysql.procs_priv: Procedure / Function privileges
mysql> ALTER USER 'admin02'@'%' WITH MAX_USER_CONNECTIONS 5;
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> SELECT USER,HOST,MAX_USER_CONNECTIONS FROM mysql.user ORDER BY 1;
+-----------+----------------+----------------------+
| USER | HOST | MAX_USER_CONNECTIONS |
+-----------+----------------+----------------------+
| admin01 | 192.168.56.102 | 0 |
| admin02 | % | 5 |
| admin03 | 192.168.% | 0 |
| mysql.sys | localhost | 0 |
| root | localhost | 0 |
| root | % | 0 |
| teste | % | 0 |
+-----------+----------------+----------------------+
7 rows in set (0,00 sec)
[root@linux1 ~]# mysql -uadmin02 –p****
ERROR 1226 (42000): User 'admin02' has exceeded the 'max_user_connections' resource (current value: 5)
mysql> SHOW FULL PROCESSLIST;
+----+---------+----------------------+------+---------+------+----------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+---------+----------------------+------+---------+------+----------+-----------------------+
| 33 | admin02 | 192.168.56.102:55235 | NULL | Sleep | 309 | | NULL |
| 34 | admin02 | 192.168.56.102:55236 | NULL | Sleep | 309 | | NULL |
| 36 | admin02 | 192.168.56.102:55238 | NULL | Sleep | 300 | | NULL |
| 37 | admin02 | 192.168.56.102:55239 | NULL | Sleep | 300 | | NULL |
| 38 | admin02 | 192.168.56.102:55240 | NULL | Sleep | 289 | | NULL |
| 42 | root | localhost | NULL | Query | 0 | starting | SHOW FULL PROCESSLIST |
+----+---------+----------------------+------+---------+------+----------+-----------------------+
6 rows in set (0,00 sec)
MySQL - Limiting Connections
mysql> set GLOBAL max_connections = 3;
Query OK, 0 rows affected (0,00 sec)
[root@linux1 ~]# mysql –uadmin02 –p****
ERROR 1040 (HY000): Too many connections
mysql> set GLOBAL max_connections = 200;
Query OK, 0 rows affected (0,00 sec)
mysql> set GLOBAL max_user_connections = 2;
Query OK, 0 rows affected (0,00 sec)
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
ERROR 1226 (42000): User 'admin02' has exceeded the 'max_user_connections' resource (current value: 5)
[root@linux1 ~]# mysql -uadmin01 -p****
[root@linux1 ~]# mysql -uadmin01 -p****
[root@linux1 ~]# mysql -uadmin01 -p****
ERROR 1203 (42000): User admin01 already has more than 'max_user_connections' active connections
MySQL - Limiting Connections
Priority
[MAX_CONNECTIONS]
[ALTER USER...WITH MAX_USER_CONNECTIONS]
[MAX_USER_CONNECTIONS]
mysql> SHOW FULL PROCESSLIST;
+--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+
| 1 | event_scheduler | localhost | NULL | Daemon | 2128 | Waiting for next activation | NULL |
| 358213 | user_bd01 | 11.215.210.209:60440 | bd01 | Query | 1 | Sending Data | SELECT id FROM T1 |
| 358214 | user_bd01 | 11.215.210.209:60441 | bd01 | Sleep | 363 | | NULL |
| 358216 | user_bd01 | 11.215.210.209:60464 | bd01 | Query | 544 | Sorting for order | SELECT * FROM T3,T2 WHERE...|
| 358217 | user_bd01 | 11.215.210.209:60465 | bd01 | Query | 0 | Creating sort index | SELECT * FROM T3 ORDER BY 2 |
| 361836 | root | 11.215.200.30:55730 | NULL | Sleep | 28 | | NULL |
| 361837 | root | 11.215.200.30:55731 | NULL | Sleep | 1502 | | NULL |
| 361865 | root | localhost | NULL | Query | 0 | init | SHOW FULL PROCESSLIST |
| 361975 | user_bd02 | 11.215.210.209:59730 | bd02 | Query | 2 | Writing to net | SELECT COUNT(*) FROM T2 |
| 362101 | user_bd02 | 11.215.210.209:60554 | bd02 | Sleep | 12 | | NULL |
+--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+
mysql> SELECT
-> USER,
-> SUBSTRING_INDEX(HOST, ':', 1) HOST,
-> DB,
-> COUNT(*) QTD
-> FROM
-> INFORMATION_SCHEMA.PROCESSLIST
-> GROUP BY USER,
-> SUBSTRING_INDEX(HOST, ':', 1),
-> DB
-> ORDER BY QTD DESC ;
+-----------------+----------------+-------+-----+
| USER | HOST | DB | QTD |
+-----------------+----------------+-------+-----+
| user_bd01 | 11.215.210.209 | bd01 | 4 |
| user_bd02 | 11.215.210.209 | bd02 | 2 |
| root | 11.215.200.30 | NULL | 2 |
| root | localhost | NULL | 1 |
| event_scheduler | localhost | NULL | 1 |
+-----------------+----------------+-------+-----+
MySQL – Listing Connections
-- Current Thread ID
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 3435|
+-----------------+
-- Current User
mysql> select CURRENT_USER;
+----------------+
| current_user |
+----------------+
| root@localhost |
+----------------+
MySQL – Thread States
https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html
Instance Structures / Engines
MySQL – Instance Structure
• Query cache : Shared identical queries from memory. If an identical query from client found in query cache then, the mysql server retrieves the results from
the query cache rather than parsing and executing that query again. It’s a shared cache for sessions , so a result set generated by one client can be sent in
response to the same query issued by another client. If MySQL does find a match in the query cache, it must check privileges before returning the cached
query. This is possible without parsing the query, because MySQL stores table information with the cached query. If the privileges are OK, MySQL
retrieves the stored result from the query cache and sends it to the client, bypassing every other stage in query execution. The query is never parsed,
optimized, or executed.
• Parser: Check for SQL syntax by checking every character in SQL query and generate SQL_ID for each SQL query.
• Metadata cache: Cache for object metadata information and statistics.
• key cache: Cache table indexes.In MySQL keys are indexes. if index size is small then it will cache index structure and data leaf.If an index is large then it will
only cache index structure.Used by MyISAM storage engine.
• Optimizer : Created efficient query execution plan as per the storage engine.It will rewrite a query.Example: InnoDB has shared buffer so optimizer will get
pre-cached data from it. Using table statistics optimizer will generate an execution plan for a SQL query.
MySQL - SQL Execution
Query Cache
• Any query that contains a nondeterministic function, such
as NOW() or CURRENT_DATE(), will not be cached.
Similarly, functions such as CURRENT_USER() or
CONNECTION_ID() might vary when executed by different
users, thereby preventing a cache hit.
• The query cache is not supported for partitioned tables,
and is automatically disabled for queries involving
partitioned tables.
• Each client connection gets its own thread within the
server process. The connection’s queries execute within
that single thread, which in turn resides on one core or
CPU. The server caches threads, so they don’t need to be
created and destroyed for each new connection.
• Before even parsing the query, though, the server consults
the query cache, which can store only SELECT statements,
along with their result sets. If anyone issues a query that’s
identical to one already in the cache, the server doesn’t
need to parse, optimize, or execute the query at all—it
can simply pass back the stored result set.
MySQL - Create DATABASE
MySQL - Create DATABASE
mysql> show variables like 'lower_case_table_names';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_table_names | 0 |
+------------------------+-------+
1 row in set (0,06 sec)
mysql> select count(*) from teste01.table_01;
+----------+
| count(*) |
+----------+
| 335872 |
+----------+
1 row in set (0,26 sec)
mysql> select count(*) from teste01.TABLE_01;
ERROR 1146 (42S02): Table 'teste01.TABLE_01' doesn't exist
mysql> show variables like 'lower_case_table_names';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_table_names | 1 |
+------------------------+-------+
1 row in set (0,03 sec)
mysql> select count(*) from teste01.TABLE_01;
+----------+
| count(*) |
+----------+
| 335872 |
+----------+
1 row in set (0,16 sec)
MySQL - Disabling Case Sensitive
No linux, o mysql é case sensitive por padrão. No windows não.
Para desabilitar a sensibilidade do Linux, hablite o parametro "lower_case_table_names"
no MySQL
Pare o seu mysql
$ service mysqld stop
Edite o seu my.conf
Na seção [mysqld] adicione o parametro lower_case_table_names com o valor 1
[mysqld]
...
lower_case_table_names=1
Salve e feche o arquivo e reinicie o serviço do mysql
$ service mysqld start
mysql> create database teste01;
Query OK, 1 row affected (0,01 sec)
mysql> USE teste01;
Database changed
mysql> SELECT @@character_set_database, @@collation_database;
+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| latin1 | latin1_swedish_ci |
+--------------------------+----------------------+
1 row in set (0,00 sec)
mysql> SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
-> FROM INFORMATION_SCHEMA.SCHEMATA WHERE
-> SCHEMA_NAME = 'teste01';
+----------------------------+------------------------+
| DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME |
+----------------------------+------------------------+
| latin1 | latin1_swedish_ci |
+----------------------------+------------------------+
1 row in set (0,01 sec)
MySQL - Create DATABASE
mysql> USE teste01;
Database changed
mysql> CREATE TABLE IF NOT EXISTS table_01 (
-> task_id INT(11) NOT NULL AUTO_INCREMENT,
-> subject VARCHAR(45) DEFAULT NULL,
-> PRIMARY KEY (task_id)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0,02 sec)
mysql> CREATE TABLE IF NOT EXISTS table_02 (
-> task_id INT(11) NOT NULL AUTO_INCREMENT,
-> subject VARCHAR(45) DEFAULT NULL,
-> PRIMARY KEY (task_id)
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0,01 sec)
mysql> show tables;
+-------------------+
| Tables_in_teste01 |
+-------------------+
| table_01 |
| table_02 |
+-------------------+
2 rows in set (0,00 sec)
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
MySQL - Create Table
mysql> SHOW TABLE STATUSG
*************************** 1. row ***************************
Name: table_01
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 335773
Avg_row_length: 35
Data_length: 12075008
Max_data_length: 0
Index_length: 6832128
Data_free: 4194304
Auto_increment: 430046
Create_time: 2017-05-02 12:43:52
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
*************************** 2. row ***************************
Name: table_02
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 458752
Avg_row_length: 23
Data_length: 10780672
Max_data_length: 281474976710655
Index_length: 5847040
Data_free: 0
Auto_increment: 458753
Create_time: 2017-05-02 12:43:58
Update_time: 2017-05-02 15:07:01
Check_time: 2017-05-02 15:06:03
Collation: latin1_swedish_ci
MySQL - Show Table Status
Data Size in Bytes (~12MB)
No Maximum (64Tb for tablespace)
Index size in bytes (~6MB)
Unused space (~4MB)
mysql> show variables like 'innodb_page_size';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| innodb_page_size | 16384 |
+------------------+-------+
mysql> delete from table_02;
Query OK, 458752 rows affected (22,73 sec)
mysql> show table status like 'table_02'G
*************************** 1. row ***************************
Name: table_02
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 10780672
Max_data_length: 281474976710655
Index_length: 5847040
Data_free: 10780672
Auto_increment: 458753
Create_time: 2017-05-02 12:43:58
Update_time: 2017-05-02 21:03:35
Check_time: 2017-05-02 15:06:03
Collation: latin1_swedish_ci
mysql> show table status like 'table_02'G
*************************** 1. row ***************************
Name: table_02
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 0
Max_data_length: 281474976710655
Index_length: 1024
Data_free: 0
Auto_increment: 458753
Create_time: 2017-05-02 12:43:58
Update_time: 2017-05-02 21:14:43
Check_time: 2017-05-02 21:14:43
Collation: latin1_swedish_ci
MySQL - Optimize Table (MyISAM)
mysql> optimize table table_02;
+------------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------+----------+----------+----------+
| teste01.table_02 | optimize | status | OK |
+------------------+----------+----------+----------+
1 row in set (0,08 sec)
• To reclaim space to the filesystem run "OPTIMIZE TABLE"
Creates a copy of the table, drops the original, and
renames the copy
• If frequent activity on the table run "ANALYZE TABLE"
mysql> delete from table_01;
Query OK, 335872 rows affected (9,37 sec)
mysql> optimize table table_01;
+------------------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+------------------+----------+----------+-------------------------------------------------------------------+
| teste01.table_01 | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| teste01.table_01 | optimize | status | OK |
+------------------+----------+----------+-------------------------------------------------------------------+
mysql> ANALYZE TABLE table_01;
+------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------+---------+----------+----------+
| teste01.table_01 | analyze | status | OK |
+------------------+---------+----------+----------+
mysql> show table status like 'table_01'G
*************************** 1. row ***************************
Name: table_01
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 16384
Data_free: 0
Auto_increment: 430046
Create_time: 2017-05-02 21:40:16
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
MySQL - Optimize Table (InnoDB)
• To reclaim space to the tablespace run "OPTIMIZE TABLE"
• Reorganization process similar to MyISAMm "ANALYZE
TABLE" rarely needed, as InnoDB updates stats
SELECT * FROM
(
SELECT
table_schema,
COUNT(table_schema) "tables",
ROUND(SUM(data_length)/1024/1024,2) AS "data_size (MB)",
ROUND(SUM(index_length)/1024/1024,2) AS "index_size (MB)",
ROUND(SUM(data_length+index_length)/1024/1024,2) AS "total (MB)",
ENGINE
FROM information_schema.tables
GROUP BY table_schema,ENGINE
) v1
WHERE TABLE_SCHEMA NOT IN('mysql','information_schema','performance_schema');
table_schema tables data_size (MB) index_size (MB) total (MB) ENGINE
------------------- ------ -------------- --------------- ---------- --------
bd01 414 38698.09 11547.17 50245.27 InnoDB
bd01 3 0.00 0.01 0.01 MyISAM
bd02 15 5.98 4.47 10.45 InnoDB
bd02 58 404.73 150.33 555.06 InnoDB
bd03 77 32.20 1.14 33.34 InnoDB
bd03 5 0.00 0.01 0.01 MyISAM
bd04 149 1771.77 120.52 1892.28 InnoDB
bd04 69 6.28 1.78 8.06 MyISAM
bd05 24 320.13 0.11 320.23 InnoDB
bd05 187 289.27 46.27 335.54 MyISAM
bd06 6 7.59 5.94 13.53 InnoDB
MySQL - Information_Schema
MySQL - Instance Files
MySQL - Instance Files
mysql> show variables like 'innodb_buffer_pool_dump_at_shutdown';
+-------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------+-------+
| innodb_buffer_pool_dump_at_shutdown | ON |
+-------------------------------------+-------+
1 row in set (0,05 sec)
mysql> show variables like 'innodb_buffer_pool_load_at_startup';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| innodb_buffer_pool_load_at_startup | ON |
+------------------------------------+-------+
1 row in set (0,04 sec)
mysql> show variables like 'innodb_buffer_pool_filename';
+-----------------------------+----------------+
| Variable_name | Value |
+-----------------------------+----------------+
| innodb_buffer_pool_filename | ib_buffer_pool |
+-----------------------------+----------------+
1 row in set (0,05 sec)
Every time you restart your MySQL server, it starts with a clean/empty buffer pool and usually it take some time to warm-up the buffer. To
speed up this process, we can configure 2 variables that will dump and reload the pages reference stored in the buffer, this is a new
functionality added on MySQL 5.6
MySQL - Instance/Database Files
mysql> show variables like 'innodb%';
+------------------------------------------+------------------------+
| Variable_name | Value |
+------------------------------------------+------------------------+
| innodb_data_home_dir | |
| innodb_data_file_path | ibdata1:12M:autoextend |
| innodb_file_per_table | ON |
| innodb_log_group_home_dir | ./ |
| innodb_log_files_in_group | 2 |
| innodb_log_file_size | 50331648 |
| innodb_temp_data_file_path | ibtmp1:12M:autoextend |
| innodb_flush_log_at_trx_commit | 1 |
| innodb_buffer_pool_size | 134217728 |
| etc. | |
+------------------------------------------+------------------------+
• innodb_file_per_table
This option causes the server to create one tablespace file per table. When innodb_file_per_table is enabled (the default), InnoDB stores the data and
indexes for each newly created table in a separate .ibd file instead of the system tablespace. The storage for these tables is reclaimed when the tables are
dropped or truncated.
• innodb_log_group_home_dir: Specifies the location of the InnoDB log files eg: innodb_log_group_home=/var/lib/mysql
• innodb_log_files_in_group: The number of log files in the log group. InnoDB writes to the files in a circular fashion. The default (and recommended) value is 2.
• innodb_log_file_size: The size in bytes of each log file in a log group.
• innodb_temp_data_file_path: In 5.7, InnoDB added a separate tablespace for all non-compressed InnoDB temporary tables. This new tablespace is named
ibtmp1 and is located in the datadir by default.
• innodb_flush_log_at_trx_commit: sets the behaviour of syncing on commit. The default value is 1. There are several possible values:
0 - if MySQL crashes, last second of txns lost;
1 - flush to disk each update/commit;
2 - flush to OS (if OS crashes, 1 sec txns are lost)
• innodb_buffer_pool_size
The size in bytes of the memory buffer to cache data and indexes of InnoDB‘s tables. This aims to reduce disk access to provide better performance.
• innodb_data_home_dir (datadir=/var/lib/mysql)
The directory (relative to :term:` datadir`) where the database server
stores the files in a shared tablespace setup. This option does not affect the
location of innodb_file_per_table.
• innodb_data_file_path
Specifies the names, sizes and location of shared tablespace files eg:
innodb_data_file_path=ibdata1:50M;ibdata2:50M:autoextend
MySQL - Instance/Database Files
Ibdata: Default prefix for tablespace files, e.g. ibdata1 is a 10MB autoextensible file that MySQL creates for the
shared tablespace by default.
.frm: For each table, the server will create a file with the .frm extension containing the table definition (for all
storage engines).
.ibd: On a multiple tablespace setup (innodb_file_per_table enabled), MySQL will store each newly created table
on a file with a .ibd extension.
.MYD: Each MyISAM table has .MYD (MYData) file which contains the data on it.
.MYI: Each MyISAM table has .MYI (MYIndex) file which contains the table’s indexes.
.TRG: File containing the Triggers associated to a table, e.g. :file:`mytable.TRG. With the .TRN file, they represent
all the Trigger definitions.
.TRN: File containing the Triggers’ Names associated to a table, e.g. :file:`mytable.TRN. With the .TRG file, they
represent all the Trigger definitions.
.ARM: Each table with the Archive Storage Engine has .ARM file which contains the metadata of it.
.ARZ: Each table with the Archive Storage Engine has .ARZ file which contains the data of it.
.CSM: Each table with the CSV Storage Engine has .CSM file which contains the metadata of it.
.CSV: Each table with the CSV Storage engine has .CSV file which contains the data of it (which is a standard
Comma Separated Value file).
.opt: MySQL stores options of a database (like charset) in a file with a .opt extension in the database directory.
.par: Each partitioned table has .par file which contains metadata about the partitions.
MySQL – CREATE TABLESPACE
mysql> CREATE TABLESPACE TBS_DADOS ADD DATAFILE '/tmp/TBS_DADOS_DATAFILE_001.ibd';
Query OK, 0 rows affected (0,01 sec)
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES;
+-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+
| SPACE | NAME | FLAG | FILE_FORMAT | ROW_FORMAT | PAGE_SIZE | ZIP_PAGE_SIZE | SPACE_TYPE | FS_BLOCK_SIZE | FILE_SIZE | ALLOCATED_SIZE |
+-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+
| 2 | mysql/plugin | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 3 | mysql/servers | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 4 | mysql/help_topic | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 9437184 | 9453568 |
| 5 | mysql/help_category | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 114688 | 118784 |
| 6 | mysql/help_relation | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 131072 | 135168 |
| 7 | mysql/help_keyword | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 245760 | 249856 |
| 8 | mysql/time_zone_name | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 9 | mysql/time_zone | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 10 | mysql/time_zone_transition | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 11 | mysql/time_zone_transition_type | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 12 | mysql/time_zone_leap_second | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 13 | mysql/innodb_table_stats | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 14 | mysql/innodb_index_stats | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 15 | mysql/slave_relay_log_info | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 16 | mysql/slave_master_info | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 17 | mysql/slave_worker_info | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 18 | mysql/gtid_executed | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 19 | mysql/server_cost | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 20 | mysql/engine_cost | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 21 | sys/sys_config | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 |
| 46 | teste01/table_01 | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 114688 | 118784 |
| 49 | TBS_DADOS | 2048 | Any | Any | 16384 | 0 | General | 4096 | 65536 | 69632 |
+-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+
22 rows in set (0,00 sec)
mysql> USE teste01;
Database changed
mysql> CREATE TABLE customer (id int) TABLESPACE=TBS_DADOS;
Query OK, 0 rows affected (0,03 sec)
MySQL – CREATE TABLESPACE
mysql> ALTER TABLESPACE TBS_DADOS ADD DATAFILE '/tmp/TBS_DADOS_DATAFILE_002.ibd';
ERROR 1178 (42000): The storage engine for the table doesn't support ALTER TABLESPACE
mysql> SELECT FILE_NAME, FILE_TYPE, LOGFILE_GROUP_NAME, STATUS, EXTRA FROM INFORMATION_SCHEMA.FILES;
+---------------------------------------+------------+--------------------+--------+-------+
| FILE_NAME | FILE_TYPE | LOGFILE_GROUP_NAME | STATUS | EXTRA |
+---------------------------------------+------------+--------------------+--------+-------+
| ./ibdata1 | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/engine_cost.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/gtid_executed.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/help_category.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/help_keyword.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/help_relation.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/help_topic.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/innodb_index_stats.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/innodb_table_stats.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/plugin.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/server_cost.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/servers.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/slave_master_info.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/slave_relay_log_info.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/slave_worker_info.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/time_zone.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/time_zone_leap_second.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/time_zone_name.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/time_zone_transition.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./mysql/time_zone_transition_type.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./sys/sys_config.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./teste01/table_01.ibd | TABLESPACE | NULL | NORMAL | NULL |
| ./ibtmp1 | TEMPORARY | NULL | NORMAL | NULL |
| /tmp/TBS_DADOS_DATAFILE_001.ibd | TABLESPACE | NULL | NORMAL | NULL |
+---------------------------------------+------------+--------------------+--------+-------+
24 rows in set (0,01 sec)
mysql> DROP TABLE customer;
Query OK, 0 rows affected (0,01 sec)
mysql> DROP TABLESPACE TBS_DADOS;
Query OK, 0 rows affected (0,01 sec)
InnoDB vs. MyISAM
MySQL - InnoDB vs. MyISAM
Diferente da engine InnoDB que armazena em buffer de memória, tanto blocos de dados de tabelas, quanto blocos de dados de índices, a
engine MyISAM armazena em buffer de memória apenas dados de blocos de índices. Com a evolução da engine InnoDB ao longo do tempo, a
mesma vem oferecendo muitos recursos das quais a engine MyISAM não é capaz de oferecer. Em relação aos níveis de locks, a engine
MyISAM possui tratamento de lock apenas no nível de tabela.
• SELECTs travam UPDATEs, DELETEs mas não INSERTs
• INSERTs, UPDATEs, DELETEs travam todas as instruções SQL
A engine InnoDB oferece tratamento de locks no nível de linhas de tabelas, além de prover recursos como Foreign Keys que ajudam o
otimizador do banco de dados a encontrar melhores planos de execução para os SQLs que são executados. A engine InnoDB é compatível com
as propriedades ACID oferecendo recursos transacionais, além de ser mais seguro que a engine MyISAM no que se refere a corrupção de
dados em caso de falhas. Em relação ao parâmetros de configuração, somente a engine InnoDB oferece parâmetros de ajustes finos.
MySQL - Convert MyISAM to InnoDB
mysql> SELECT CONCAT('ALTER TABLE `',table_schema,'`.`',table_name,'` ENGINE=InnoDB;')
-> FROM information_schema.tables
-> WHERE table_schema='bd01'
-> AND ENGINE='MyISAM';
+--------------------------------------------------------------------------+
| CONCAT('ALTER TABLE `',table_schema,'`.`',table_name,'` ENGINE=InnoDB;') |
+--------------------------------------------------------------------------+
| ALTER TABLE `bd01`.`cerber_acl` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`cerber_blocks` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`cerber_lab` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`cerber_log` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_commentmeta` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_termmeta` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_terms` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_usermeta` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_users` ENGINE=InnoDB; |
+--------------------------------------------------------------------------+
9 rows in set (0.04 sec)
mysql> ALTER TABLE `bd01`.`cerber_log` ENGINE=InnoDB;
ALTER TABLE `wordpress`.`wp_jc_posts` ENGINE=InnoDB;
Query OK, 219 rows affected (0.10 sec)
Records: 219 Duplicates: 0 Warnings: 0
ALTER TABLE `bd01`.`wp_jc_terms` ENGINE=InnoDB;
Query OK, 642 rows affected (0.26 sec)
Records: 642 Duplicates: 0 Warnings: 0
mysql> show full processlist;
+----+------+---------------------+------+---------+------+-------------------+-------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+-------------------+-------------------------------+
| 5 | root | localhost | bd01 | Sleep | 804 | | NULL |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
| 7 | root | 192.168.56.51:49970 | bd01 | Sleep | 103 | | NULL |
| 8 | root | 192.168.56.51:49971 | NULL | Sleep | 9838 | | NULL |
| 14 | root | localhost | bd01 | Query | 15 | copy to tmp table | alter table wp engine innodb |
+----+------+---------------------+------+---------+------+-------------------+-------------------------------+
MySQL - InnoDB vs. MyISAM
MySQL - InnoDB vs. MyISAM
Transaction Isolation Level
MySQL - Autocommit
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0,03 sec)
mysql> SET AUTOCOMMIT = 0;
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
MySQL operates in AUTOCOMMIT mode by default. This means that unless you’ve explicitly begun a transaction, it
automatically executes each query in a separate transaction. You can enable or disable AUTOCOMMIT for the current
connection by setting a variable:
The values 1 and ON are equivalent, as are 0 and OFF. When you run with AUTOCOMMIT=0, you are always in a
transaction, until you issue a COMMIT or ROLLBACK. MySQL then starts a new transaction immediately. Changing the value
of AUTOCOMMIT has no effect on nontransactional tables, such as MyISAM or Memory tables, which have no option of
committing or rolling back changes.
ISOLATION LEVEL (READ COMMITTED vs. REPEATABLE READ)
ISOLATION LEVEL (READ COMMITTED vs. REPEATABLE READ)
mysql> CREATE TABLE transaction_test(
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> val VARCHAR(20) NOT NULL,
-> created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
-> PRIMARY KEY(id)
-> ) ENGINE=InnoDB DEFAULT CHARSET latin1;
Query OK, 0 rows affected (0.29 sec)
mysql> INSERT INTO transaction_test(val) VALUES ('a'),('b'),('c');
Query OK, 3 rows affected (0.08 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM transaction_test;
+----+-----+---------------------+
| id | val | created |
+----+-----+---------------------+
| 1 | a | 2017-05-02 10:09:33 |
| 2 | b | 2017-05-02 10:09:33 |
| 3 | c | 2017-05-02 10:09:33 |
+----+-----+---------------------+
3 rows in set (0.00 sec)
SESSION 1
ISOLATION LEVEL (REPEATABLE READ)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO transaction_test(val) VALUES ('x'),('y'),('z');
COMMIT;
SESSION 2
mysql> SELECT * FROM transaction_test;
+----+-----+---------------------+
| id | val | created |
+----+-----+---------------------+
| 1 | a | 2017-05-02 10:09:33 |
| 2 | b | 2017-05-02 10:09:33 |
| 3 | c | 2017-05-02 10:09:33 |
+----+-----+---------------------+
3 rows in set (0.00 sec)
SESSION 1
ISOLATION LEVEL (REPEATABLE READ)
mysql> SET SESSION tx_isolation='READ-COMMITTED';
TRUNCATE TABLE transaction_test;
INSERT INTO transaction_test(val) VALUES ('a'),('b'),('c');
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
SESSION 1
ISOLATION LEVEL (READ COMMITED)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO transaction_test(val) VALUES ('x');
Query OK, 3 rows affected (0.00 sec)
mysql> COMMIT;
SESSION 2
mysql> SELECT * FROM transaction_test;
+----+--------+---------------------+
| id | val | created |
+----+--------+---------------------+
| 1 | a | 2017-05-02 10:18:08 |
| 2 | b | 2017-05-02 10:18:08 |
| 3 | c | 2017-05-02 10:18:08 |
| 4 | x | 2017-05-02 10:19:00 |
+----+--------+---------------------+
SESSION 1
MySQL DEFAULT ISOLATION LEVEL
mysql> show variables like '%isolation%';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| tx_isolation | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0,06 sec)
Locks
mysql> START TRANSACTION;
Query OK, 0 rows affected (0,00 sec)
mysql> update t2 set id=10;
Query OK, 1 row affected (0,01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
SESSION 1
MySQL - Locks / innodb_lock_wait_timeout
mysql> update t2 set id=20;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
SESSION 2
mysql> show variables like 'innodb_lock_wait_timeout';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 50 |
+--------------------------+-------+
1 row in set (0,19 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0,00 sec)
mysql> update t2 set id=10;
Query OK, 1 row affected (0,01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
SESSION 1
MySQL - Locks / innodb_lock_wait_timeout
mysql> SET innodb_lock_wait_timeout = 50000;
Query OK, 0 rows affected (0,00 sec)
mysql> update t2 set id=20;
hang
SESSION 2
mysql> show full processlist;
+----+------+-----------+------+---------+------+----------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+----------+-----------------------+
| 4 | root | localhost | bd01 | Sleep | 818 | | NULL |
| 5 | root | localhost | bd01 | Query | 97 | updating | update t2 set id=20 |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
+----+------+-----------+------+---------+------+----------+-----------------------+
mysql> select trx_mysql_thread_id,trx_id,trx_state,
-> trx_started,trx_isolation_level,
-> trx_query FROM `information_schema`.`innodb_trx`;
+---------------------+--------+-----------+---------------------+---------------------+---------------------+
| trx_mysql_thread_id | trx_id | trx_state | trx_started | trx_isolation_level | trx_query |
+---------------------+--------+-----------+---------------------+---------------------+---------------------+
| 5 | 11792 | LOCK WAIT | 2017-04-21 11:37:06 | REPEATABLE READ | update t2 set id=20 |
| 4 | 11790 | RUNNING | 2017-04-21 11:25:05 | REPEATABLE READ | NULL |
+---------------------+--------+-----------+---------------------+---------------------+---------------------+
mysql> SELECT * FROM `information_schema`.`innodb_locks`;
+--------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+
| lock_id | lock_trx_id | lock_mode | lock_type | lock_table | lock_index | lock_space | lock_page | lock_rec | lock_data |
+--------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+
| 11792:48:3:2 | 11792 | X | RECORD | `bd01`.`t2` | GEN_CLUST_INDEX | 48 | 3 | 2 | 0x000000018300 |
| 11790:48:3:2 | 11790 | X | RECORD | `bd01`.`t2` | GEN_CLUST_INDEX | 48 | 3 | 2 | 0x000000018300 |
+--------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+
mysql> SHOW ENGINE INNODB STATUS
------------
TRANSACTIONS
------------
Trx id counter 11794
Purge done for trx's n:o < 11789 undo n:o < 0 state: running but idle
History list length 2
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 18446744072350939916, not started
0 lock struct(s), heap size 1080, 0 row lock(s)
---TRANSACTION 18446744072350939356, not started
0 lock struct(s), heap size 1080, 0 row lock(s)
---TRANSACTION 11792, ACTIVE 2846 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1080, 1 row lock(s)
MySQL thread id 5, OS thread handle 2763115376, query id 37 localhost root updating
update t2 set id=20
------- TRX HAS BEEN WAITING 2846 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 48 page no 3 n bits 72 index GEN_CLUST_INDEX of table `bd01`.`t2` trx id 11792 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
0: len 6; hex 000000018300; asc ;;
1: len 6; hex 000000002e0e; asc . ;;
2: len 7; hex 2c0000013b0c4e; asc , ; N;;
3: len 4; hex 8000000a; asc ;;
------------------
---TRANSACTION 11790, ACTIVE 3567 sec
2 lock struct(s), heap size 1080, 2 row lock(s), undo log entries 1
MySQL thread id 4, OS thread handle 2763316080, query id 28 localhost root
--------
MySQL - Locks / INNODB STATUS
mysql> SELECT p.id,
-> p.HOST,
-> p.DB,
-> p.time,
-> t.trx_state,
-> t.trx_query,
-> t.trx_started
-> FROM INFORMATION_SCHEMA.INNODB_TRX t
-> JOIN INFORMATION_SCHEMA.PROCESSLIST p
-> ON (p.ID = t.TRX_MYSQL_THREAD_ID)
-> LEFT JOIN INFORMATION_SCHEMA.INNODB_LOCK_WAITS ot
-> ON (ot.BLOCKING_TRX_ID = t.TRX_id);
+----+-----------+------+------+-----------+---------------------+---------------------+
| id | HOST | DB | time | trx_state | trx_query | trx_started |
+----+-----------+------+------+-----------+---------------------+---------------------+
| 4 | localhost | bd01 | 2858 | RUNNING | NULL | 2017-04-21 11:25:05 |
| 5 | localhost | bd01 | 2137 | LOCK WAIT | update t2 set id=20 | 2017-04-21 11:37:06 |
+----+-----------+------+------+-----------+---------------------+---------------------+
mysql> SELECT CONCAT('KILL ',id,';'),CONCAT('KILL QUERY ',id,';')
-> FROM information_schema.processlist WHERE db = 'bd01';
+------------------------+------------------------------+
| CONCAT('KILL ',id,';') | CONCAT('KILL QUERY ',id,';') |
+------------------------+------------------------------+
| KILL 4; | KILL QUERY 4; |
| KILL 5; | KILL QUERY 5; |
+------------------------+------------------------------+
2 rows in set (0,02 sec)
mysql> KILL 4;
Query OK, 0 rows affected (0,00 sec)
MySQL - Locks / Querying / Killing
SESSION 2
mysql> update t2 set id=20;
Query OK, 1 row affected (58 min 0,83 sec)
Rows matched: 1 Changed: 1 Warnings: 0
SESSION 1
mysql> select 1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 10
Current database: bd01
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0,06 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0,01 sec)
mysql> update t2 set id=30;
Query OK, 1 row affected (0,00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> alter table t2 modify column id bigint;
hang
mysql> SHOW FULL PROCESSLIST;
+----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+
| 5 | root | localhost | bd01 | Query | 580 | Waiting for table metadata lock | alter table t2 modify column id bigint |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
| 7 | root | 192.168.56.51:49970 | NULL | Sleep | 400 | | NULL |
| 8 | root | 192.168.56.51:49971 | NULL | Sleep | 6733 | | NULL |
| 10 | root | localhost | bd01 | Sleep | 682 | | NULL |
+----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+
mysql> SELECT *
-> FROM information_schema.processlist
-> WHERE state IN ('Waiting for table flush',
-> 'Locked',
-> 'System lock',
-> 'Table lock' )
-> OR state REGEXP 'Waiting for .* lock'
-> ORDER BY TIME DESC;
+----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+
| 5 | root | localhost | bd01 | Query | 606 | Waiting for table metadata lock | alter table t2 modify column id bigint |
+----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+
MySQL - Metadata Locks
SESSION 1
SESSION 2
mysql> LOCK TABLES customer READ;
Query OK, 0 rows affected (0,01 sec)
mysql> delete from customer where name=1;
hang
mysql> SHOW FULL PROCESSLIST;
+----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+
| 5 | root | localhost | bd01 | Query | 12 | Waiting for table metadata lock | delete from customer where name=1 |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
| 7 | root | 192.168.56.51:49970 | NULL | Sleep | 899 | | NULL |
| 8 | root | 192.168.56.51:49971 | NULL | Sleep | 7232 | | NULL |
| 10 | root | localhost | bd01 | Sleep | 34 | | NULL |
+----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+
mysql> SELECT *
-> FROM information_schema.processlist
-> WHERE state IN ('Waiting for table flush',
-> 'Locked',
-> 'System lock',
-> 'Table lock' )
-> OR state REGEXP 'Waiting for .* lock'
-> ORDER BY TIME DESC;
+----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+
| 5 | root | localhost | bd01 | Query | 560 | Waiting for table metadata lock | delete from customer where name=1 |
+----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0,01 sec)
MySQL - Metadata Locks
SESSION 1
SESSION 2
SESSION 1
mysql> delete from customer where name=1;
Query OK, 1 row affected (11 min 39,52 sec)
SESSION 2
mysql> CREATE TABLE emp (id int) ENGINE=MyISAM;
mysql> select count(*) from emp;
+----------+
| count(*) |
+----------+
| 2097153 |
+----------+
1 row in set (0,01 sec)
mysql> update emp set id=20000;
updating
mysql> select count(*) from emp;
hang
mysql> SHOW FULL PROCESSLIST;
+----+------+---------------------+------+---------+------+------------------------------+--------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+------------------------------+--------------------------+
| 5 | root | localhost | bd01 | Query | 4 | Waiting for table level lock | select count(*) from emp |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
| 7 | root | 192.168.56.51:49970 | NULL | Sleep | 2705 | | NULL |
| 8 | root | 192.168.56.51:49971 | NULL | Sleep | 9038 | | NULL |
| 14 | root | localhost | bd01 | Query | 14 | updating | update emp set id=20000 |
+----+------+---------------------+------+---------+------+------------------------------+--------------------------+
mysql> select count(*) from emp;
+----------+
| count(*) |
+----------+
| 2097153 |
+----------+
1 row in set (2 min 26,55 sec)
MySQL - Table Locks MyISAM
SESSION1
SESSION 2
SESSION 2
mysql> SHOW CREATE TABLE bd01.emp;
+-------+---------------------------------------+
| Table | Create Table |
+-------+----------------------------------------
| emp | CREATE TABLE `emp` ( |
| `id` int DEFAULT NULL |
| ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-------+---------------------------------------+
Binary Log
MySQL - Enabling Binary Logs
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | OFF |
+---------------+-------+
1 row in set (0,04 sec)
[root@linux1~]# cat /etc/my.cnf
server_id=1
log_bin=/var/lib/mysql/mysql-bin.log
root@linux1~]# service mysqld restart
Parando o mysqld: [ OK ]
Iniciando o mysqld: [ OK ]
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0,04 sec)
mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 154 |
+------------------+-----------+
1 row in set (0,00 sec)
mysql> SELECT @@log_bin;
+-----------+
| @@log_bin |
+-----------+
| 1 |
+-----------+
1 row in set (0,01 sec)
[root@linux1~]# ls -lh mysql-bin.*
-rw-r----- 1 mysql mysql 154 Abr 12 15:44 mysql-bin.000001
-rw-r----- 1 mysql mysql 32 Abr 12 15:44 mysql-bin.index
+---------------------------------+--------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------+
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| binlog_format | ROW |
| max_binlog_size | 1073741824 |
| expire_logs_days | 0 |
| sync_binlog | 1 |
+---------------------------------+--------------------------------+
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
MySQL - Viewing Contents of Binary Logs
[root@linux1~]# mysqlbinlog mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#170419 20:21:30 server id 1 end_log_pos 123 CRC32 0x14c02170 Start: binlog v 4,
server v 5.7.18-log created 170419 20:21:30 at startup
ROLLBACK/*!*/;
BINLOG '
+vD3WA8BAAAAdwAAAHsAAAAAAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAD68PdYEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AXAhwBQ=
'/*!*/;
# at 123
#170419 20:21:30 server id 1 end_log_pos 154 CRC32 0xb9a3dfb0 Previous-GTIDs
# [empty]
# at 154
#170419 20:36:16 server id 1 end_log_pos 219 CRC32 0x8e618eb0 Anonymous_GTID last_committed=0 sequence_number=1
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#170419 20:36:16 server id 1 end_log_pos 291 CRC32 0x1930ff1a Query thread_id=7 exec_time=0 error_code=0
SET TIMESTAMP=1492644976/*!*/;
SET @@session.pseudo_thread_id=7/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 291
#170419 20:36:16 server id 1 end_log_pos 338 CRC32 0x1381aefd Table_map: `bd01`.`t1` mapped to number 108
# at 338
#170419 20:36:16 server id 1 end_log_pos 382 CRC32 0x8c6cf04e Write_rows: table id 108 flags: STMT_END_F
MySQL - Viewing Contents of Binary Logs
mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 428 |
+------------------+-----------+
1 row in set (0,00 sec)
mysql> SHOW BINLOG EVENTS IN 'mysql-bin.000001';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.18-log, Binlog ver: 4 |
| mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 219 | Query | 1 | 294 | BEGIN |
| mysql-bin.000001 | 294 | Table_map | 1 | 351 | table_id: 219 (teste01.table_01) |
| mysql-bin.000001 | 351 | Write_rows | 1 | 397 | table_id: 219 flags: STMT_END_F |
| mysql-bin.000001 | 397 | Xid | 1 | 428 | COMMIT /* xid=31 */ |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
7 rows in set (0,00 sec)
mysql> create table table_03 (id int);
Query OK, 0 rows affected (0,04 sec)
mysql> SHOW BINLOG EVENTS IN 'mysql-bin.000001';
+------------------+-----+----------------+-----------+-------------+-----------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+-----------------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.18-log, Binlog ver: 4 |
| mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 219 | Query | 1 | 294 | BEGIN |
| mysql-bin.000001 | 294 | Table_map | 1 | 351 | table_id: 219 (teste01.table_01) |
| mysql-bin.000001 | 351 | Write_rows | 1 | 397 | table_id: 219 flags: STMT_END_F |
| mysql-bin.000001 | 397 | Xid | 1 | 428 | COMMIT /* xid=31 */ |
| mysql-bin.000001 | 428 | Anonymous_Gtid | 1 | 493 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 493 | Query | 1 | 603 | use `teste01`; create table table_03 (id int) |
+------------------+-----+----------------+-----------+-------------+-----------------------------------------------+
9 rows in set (0,01 sec)
MySQL - Rotating Binary Logs (utilities)
[root@linux1~]# rpm -ivh mysql-connector-python-2.1.6-1.el7.x86_64.rpm
aviso: mysql-connector-python-2.1.6-1.el6.i686.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparando... ########################################### [100%]
1:mysql-connector-python ########################################### [100%]
[root@linux1~]# rpm -ivh mysql-utilities-1.6.5-1.el7.noarch.rpm
aviso: mysql-utilities-1.6.5-1.el7.noarch.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparando... ########################################### [100%]
1:mysql-utilities ########################################### [100%]
[root@linux1 ~]# mysqlbinlogrotate --server=root:admin@localhost:3306 -v
# Checking user permission to rotate binary logs...
#
# Active binlog file: 'mysql-bin.000001' (size: 931 bytes)'
# The binlog file has been rotated.
# New active binlog file: 'mysql-bin.000002'
[root@linux1 ~]# mysqlbinlogrotate --server=root:admin@localhost:3306 -v
# Checking user permission to rotate binary logs...
#
# Active binlog file: 'mysql-bin.000002' (size: 1449 bytes)'
# The binlog file has been rotated.
# New active binlog file: 'mysql-bin.000003‘
[root@linux1 ~]# mysqlbinlogrotate --server=root:admin@localhost:3306 -v
# Checking user permission to rotate binary logs...
#
# Active binlog file: 'mysql-bin.000003' (size: 1345 bytes)'
# The binlog file has been rotated.
# New active binlog file: 'mysql-bin.000004'
https://dev.mysql.com/downloads/connector/python/ https://dev.mysql.com/downloads/utilities/
MySQL - Purging Binary Logs
mysql> SHOW BINARY LOGS;
+------------------+------------+
| Log_name | File_size |
+------------------+------------+
| mysql-bin.000858 | 1073769033 |
| mysql-bin.000859 | 1073772184 |
| mysql-bin.000860 | 1073769005 |
| mysql-bin.000861 | 1073809148 |
| mysql-bin.000862 | 1073765575 |
| mysql-bin.000863 | 1073797185 |
| mysql-bin.000864 | 1073757152 |
| mysql-bin.000865 | 1073763101 |
| mysql-bin.000866 | 1073771131 |
| mysql-bin.000867 | 1073770555 |
| mysql-bin.000868 | 1073744076 |
| mysql-bin.000869 | 1073818669 |
| mysql-bin.000870 | 1073754790 |
| mysql-bin.000871 | 1073770448 |
| mysql-bin.000872 | 1073755079 |
| mysql-bin.000873 | 1073744861 |
| mysql-bin.000874 | 1073807803 |
| mysql-bin.000875 | 1073777945 |
| mysql-bin.000876 | 1073741999 |
| mysql-bin.000877 | 718403419 |
+------------------+------------+
20 rows in set (0.00 sec)
mysql> PURGE BINARY LOGS TO 'mysql-bin.000878';
ERROR 1373 (HY000): Target log not found in binlog index
mysql> PURGE BINARY LOGS BEFORE '2017-04-22 13:00:00';
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND;
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000003 | 154 |
| mysql-bin.000004 | 154 |
+------------------+-----------+
mysql> reset master;
Query OK, 0 rows affected (0.02 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 154 |
+------------------+-----------+
RESET MASTER: Deletes all binary log
files listed in the index file, resets the
binary log index file to be empty, and
creates a new binary log file.
MySQL - General Log File
mysql> show variables like '%general%'; mysql> show variables like 'log_output';
+------------------+---------------------------+ +---------------+-------+
| Variable_name | Value | | Variable_name | Value |
+------------------+---------------------------+ +---------------+-------+
| general_log | OFF | | log_output | FILE |
| general_log_file | /var/lib/mysql/linux1.log | +---------------+-------+
+------------------+---------------------------+
mysql> USE teste01;
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0,43 sec)
mysql> select * from t1 order by 1;
+------+
| id |
+------+
| 10 |
+------+
mysql> delete from t1;
Query OK, 1 row affected (0,08 sec)
mysql> select * from mysql.general_log;
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
| 2017-05-02 20:15:30.546899 | root[root] @ localhost [] | 15 | 0 | Query | SELECT DATABASE() |
| 2017-05-02 20:15:30.556462 | root[root] @ localhost [] | 15 | 0 | Init DB | teste01 |
| 2017-05-02 20:15:30.572724 | root[root] @ localhost [] | 15 | 0 | Query | show databases |
| 2017-05-02 20:15:30.579491 | root[root] @ localhost [] | 15 | 0 | Query | show tables |
| 2017-05-02 20:15:40.651512 | root[root] @ localhost [] | 15 | 0 | Query | create table t1(id int) |
| 2017-05-02 20:15:52.616530 | root[root] @ localhost [] | 15 | 0 | Query | insert into t1 values (10) |
| 2017-05-02 20:16:10.976444 | root[root] @ localhost [] | 15 | 0 | Query | select * from t1 order by 1 |
| 2017-05-02 20:16:16.186997 | root[root] @ localhost [] | 15 | 0 | Query | delete from t1 |
| 2017-05-02 20:16:29.026673 | root[root] @ localhost [] | 15 | 0 | Query | select * from mysql.general_log |
+----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+
mysql> set GLOBAL general_log = 1;
mysql> set GLOBAL general_log_file = 'TABLE';
mysql> show variables like 'general_log';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| general_log | ON |
+---------------+--------+
mysql> show variables like 'log_output';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| log_output | TABLE |
+---------------+--------+
CSV Files
MySQL - Generate CSV Files
mysql> select * from customer;
+------+------+
| id | name |
+------+------+
| 1 | Joe |
| 2 | Jack |
| 3 | Zack |
+------+------+
3 rows in set (0,00 sec)
SELECT * INTO OUTFILE '/tmp/customer.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' FROM customer;
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0,01 sec)
mysql> SELECT * INTO OUTFILE '/var/lib/mysql-files/customer.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' FROM customer;
Query OK, 3 rows affected (0,01 sec)
mysql> system cat /var/lib/mysql-files/customer.csv
"1","Joe"
"2","Jack"
"3","Zack"
MySQL - Generate CSV Files (Headers)
SET @table_name = 'customer';
SET @table_schema = 'bd01';
SET SESSION group_concat_max_len = 1000000;
SET @col_names = (
SELECT GROUP_CONCAT(QUOTE(`column_name`)) AS columns
FROM information_schema.columns
WHERE table_schema = @table_schema
AND table_name = @table_name);
SET @cols = CONCAT('(SELECT ', @col_names, ')');
SET @query = CONCAT('(SELECT * FROM ', @table_schema, '.', @table_name,
' INTO OUTFILE '/var/lib/mysql-files/customer.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"')');
SET @sql = CONCAT(@cols, ' UNION ALL ', @query);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
system cat /var/lib/mysql-files/customer.csv;
mysql> SET @table_name = 'customer';
Query OK, 0 rows affected (0.00 sec)
mysql> SET @table_schema = 'bd01';
Query OK, 0 rows affected (0.00 sec)
mysql> SET SESSION group_concat_max_len = 1000000;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @col_names = (
-> SELECT GROUP_CONCAT(QUOTE(`column_name`)) AS columns
-> FROM information_schema.columns
-> WHERE table_schema = @table_schema
-> AND table_name = @table_name);
Query OK, 0 rows affected (0.00 sec)
mysql> SET @cols = CONCAT('(SELECT ', @col_names, ')');
Query OK, 0 rows affected (0.00 sec)
mysql> SET @query = CONCAT('(SELECT * FROM ', @table_schema, '.', @table_name,
-> ' INTO OUTFILE '/var/lib/mysql-files/customer.csv'
'> FIELDS TERMINATED BY ',' ENCLOSED BY '"')');
Query OK, 0 rows affected (0.00 sec)
mysql> SET @sql = CONCAT(@cols, ' UNION ALL ', @query);
Query OK, 0 rows affected (0.00 sec)
mysql> PREPARE stmt FROM @sql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE stmt;
Query OK, 4 rows affected (0.00 sec)
mysql> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)
mysql> system cat /var/lib/mysql-files/customer.csv;
"id","name"
"1","Joe"
"2","Jack"
"3","Zack"
MySQL - LOAD CSV Files
mysql> TRUNCATE TABLE customer;
Query OK, 0 rows affected (0,02 sec)
mysql> select * from customer;
Empty set (0,00 sec)
mysql> LOAD DATA LOCAL INFILE '/var/lib/mysql-files/customer.csv' INTO TABLE customer FIELDS TERMINATED BY ','
-> ENCLOSED BY '"' LINES TERMINATED BY 'n'
-> (id,name);
Query OK, 3 rows affected (0,02 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from customer;
+------+------+
| id | name |
+------+------+
| 1 | Joe |
| 2 | Jack |
| 3 | Zack |
+------+------+
3 rows in set (0,00 sec)
mysql> TRUNCATE TABLE customer;
Query OK, 0 rows affected (0,02 sec)
[root@linux1 ~]# mysqlimport --columns='id,name' --fields-terminated-by=',' --fields-enclosed-by '"'
--local -uroot -padmin
bd01
/var/lib/mysql-files/customer.csv
mysql> select * from customer;
+------+------+
| id | name |
+------+------+
| 1 | Joe |
| 2 | Jack |
| 3 | Zack |
+------+------+
3 rows in set (0,00 sec)
No caso do mysqlimport, o nome
do arquivo tem que ser igual ao
nome da tabela de destino
MySQL - Executing External Scripts
[root@linux1 ~]# cat script.sql
show databases;
select @@version;
select now();
root@linux1 ~]# mysql -uroot -padmin -e "source /tmp/script.sql"
+--------------------+
| Database |
+--------------------+
| information_schema |
| bd01 |
| mysql |
| performance_schema |
| sys |
+--------------------+
+------------+
| @@version |
+------------+
| 5.7.18-log |
+------------+
+---------------------+
| now() |
+---------------------+
| 2017-05-02 15:12:50 |
+---------------------+
MySQL - Executing External Scripts
TEE Script_Master.log
use dbname
source script1.sql
source script2.sql
NOTEE
TEE script_Master_Rollback.log
use dbname
source script2.sql
source script1.sql
NOTEE
mysql -uuser -ppass < Script_Master.sql --verbose > log.txt
Exporting / Importing
MySQL - Export (mysqldump)
[root@linux1]# mysqldump -uroot -padmin --single-transaction --opt --add-drop-database -B teste02 --result-file=/tmp/teste02.sql
[root@linux1]# cat /tmp/teste02.sql
-- MySQL dump 10.13 Distrib 5.7.18, for Linux (i686)
--
-- Host: localhost Database: teste02
-- ------------------------------------------------------
-- Server version 5.7.18-log
-- Current Database: `teste02`
/*!40000 DROP DATABASE IF EXISTS `teste02`*/;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `teste02` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `teste02`;
--
-- Table structure for table `a`
--
DROP TABLE IF EXISTS `a`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `a` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `a` WRITE;
/*!40000 ALTER TABLE `a` DISABLE KEYS */;
INSERT INTO `a` VALUES (1);
/*!40000 ALTER TABLE `a` ENABLE KEYS */;
UNLOCK TABLES;
.
.
.
--opt  Shorthand for --add-drop-table --add-
locks --create-options --disable-keys --extended-
insert --lock-tables --quick --set-charset
MySQL - Export (mysqldump)
mysql> show grants for backup@192.168.56.101;
+------------------------------------------------------------------------------------------------------------------+
| Grants for backup@192.168.56.101 |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'backup'@'192.168.56.101' IDENTIFIED BY PASSWORD '*26C3C87785FA94A58BEF806DE0774FE6D14906' |
| GRANT SELECT, SHOW VIEW, TRIGGER ON `mysql`.* TO 'backup'@'192.168.56.101' |
| GRANT SELECT, LOCK TABLES, SHOW VIEW, TRIGGER ON `bd01`.* TO 'backup'@'192.168.56.101' |
| GRANT SELECT, LOCK TABLES, SHOW VIEW, TRIGGER ON `bd02`.* TO 'backup'@'192.168.56.101' |
+------------------------------------------------------------------------------------------------------------------+
Examples
------------
mysqldump --all-databases --user=root --password=admin --single-transaction --opt --add-drop-database --routines --triggers --events > /tmp/all-databases.sql
mysqldump --all-databases --user=root --password=admin --single-transaction --opt --add-drop-database --routines --triggers --events | gzip > /tmp/all-databases.sql.gz
mysqldump -uroot -padmin --single-transaction --opt --add-drop-database --databases teste01 > /tmp/teste01.sql
mysqldump -uroot -padmin --single-transaction --opt --add-drop-database -B teste01 > /tmp/teste01.sql (-B ou --databases  create database)
mysqldump -uroot -padmin --single-transaction --opt teste01 > /tmp/teste01.sql
mysqldump -uroot -padmin --single-transaction --opt --add-drop-database --databases teste01 teste02 > /tmp/teste01_02.sql
mysqldump -uroot -padmin --single-transaction --opt teste01 customer > /tmp/customer.sql
mysqldump -uroot -padmin --single-transaction --opt teste02 customer emp > /tmp/customer_emp.sql
-- Only DDL
mysqldump -uroot -padmin --no-data --routines --triggers --events teste02 > teste02_ddl.sql
-- Only Data
mysqldump -uroot -padmin --single-transaction --opt --no-create-info teste02 > teste02_data.sql
-- Ignore table
mysqldump -uroot -padmin --single-transaction --opt --add-drop-database -B teste02 --ignore-table=teste02.customer > teste02_ignore.sql
http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
Ignora os bancos sys, information_schema,
performance_schema
MySQL - Import (mysql)
-- O comando CREATE DATABASE está no DUMP
mysql -uroot -padmin < bd01.sql
-- O comando CREATE DATABASE NÃO está no DUMP
mysql -uroot -padmin bd01 < bd01.sql
-- Import com log
mysql -uroot -padmin < /tmp/all-databases.sql --verbose > /tmp/import.log
-- Import com logs separados
mysql -uroot -padmin < /tmp/all-databases.sql --verbose > /tmp/import.log 2> /tmp/import_erro.log
-- Import de tabela customer para o banco bd01
mysql -uroot -padmin bd01 < customer.sql
-- Import remoto
mysql -hlinux1 -p3306 -uroot -padmin bd01 < bd01.sql
-- Extrair scripts de um único banco de dados do arquivo all-databases.sql para ser importado
sed -n '/^-- Current Database: `bd01`/,/^-- Current Database: `/p' /tmp/all-databases.sql > /tmp/bd01.sql
MySQL - Point-in-Time Recovery Using the Binary Log
mysql> use bd01
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> show tables;
+----------------+
| Tables_in_bd01 |
+----------------+
| t1 |
+----------------+
1 row in set (0,00 sec)
mysql> desc t1;
+-------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| data | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------+-----------+------+-----+-------------------+----------------+
2 rows in set (0,00 sec)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0,00 sec)
mysql> select * from t1;
Empty set (0,01 sec)
Mysql - Point-in-Time Recovery Using the Binary Log
[root@linux1 ~]# mysqldump -uroot -padmin --single-transaction --opt --add-drop-database -B bd01 > /tmp/bd01.sql
[root@linux1 ~]# ls -lh /tmp/bd01.sql
-rw-r--r-- 1 root root 2,0K Mai 2 20:34 /tmp/bd01.sql
-- Insert executado N vezes e depois executado o LOG ROTATE
mysql> INSERT INTO t1 VALUES (null,null);
[root@linux1 ~]# mysqlbinlogrotate --server=root:admin@localhost:3306 –v
RESULTADO
mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 978 |
| mysql-bin.000002 | 1496 |
| mysql-bin.000003 | 1755 |
| mysql-bin.000004 | 1496 |
| mysql-bin.000005 | 154 |
+------------------+-----------+
5 rows in set (0,00 sec)
mysql> select * from bd01.t1;
+----+---------------------+
| id | data |
+----+---------------------+
| 1 | 2017-05-02 20:36:16 |
| 2 | 2017-05-02 20:36:33 |
| 3 | 2017-05-02 20:36:52 |
| 4 | 2017-05-02 20:53:26 |
| 5 | 2017-05-02 20:53:27 |
| 6 | 2017-05-02 20:53:28 |
| 7 | 2017-05-02 20:53:29 |
| 8 | 2017-05-02 20:53:30 |
| 9 | 2017-05-02 20:58:19 |
| 10 | 2017-05-02 20:58:20 |
| 11 | 2017-05-02 20:58:22 |
| 12 | 2017-05-02 20:58:23 |
| 13 | 2017-05-02 20:58:24 |
| 14 | 2017-05-02 20:58:25 |
| 15 | 2017-05-02 21:10:37 |
| 16 | 2017-05-02 21:10:38 |
| 17 | 2017-05-02 21:10:39 |
| 18 | 2017-05-02 21:10:42 |
| 19 | 2017-05-02 21:10:43 |
+----+---------------------+
19 rows in set (0,02 sec)
MySQL - Point-in-Time Recovery Using the Binary Log
mysql> drop database bd01;
Query OK, 1 row affected (0,11 sec)
mysql -uroot -padmin bd01 < /tmp/bd01.sql
mysql> select * from bd01.t1;
Empty set (0,01 sec)
[root@linux1 ~]# mysqlbinlog mysql-bin.000001 | mysql -uroot -padmin
[root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1"
+----+---------------------+
| id | data |
+----+---------------------+
| 1 | 2017-05-02 20:36:16 |
| 2 | 2017-05-02 20:36:33 |
| 3 | 2017-05-02 20:36:52 |
+----+---------------------+
3 rows in set (0,00 sec)
[root@linux1 ~]# mysqlbinlog mysql-bin.000002 | mysql -uroot -padmin
[root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1"
+----+---------------------+
| id | data |
+----+---------------------+
| 1 | 2017-05-02 20:36:16 |
| 2 | 2017-05-02 20:36:33 |
| 3 | 2017-05-02 20:36:52 |
| 4 | 2017-05-02 20:53:26 |
| 5 | 2017-05-02 20:53:27 |
| 6 | 2017-05-02 20:53:28 |
| 7 | 2017-05-02 20:53:29 |
| 8 | 2017-05-02 20:53:30 |
+----+---------------------+
8 rows in set (0,02 sec)
MySQL - Point-in-Time Recovery Using the Binary Log
[root@linux1 ~]# mysqlbinlog mysql-bin.000003 mysql-bin.000004 | mysql -uroot -padmin
[root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1"
+----+---------------------+
| id | data |
+----+---------------------+
| 1 | 2017-05-02 20:36:16 |
| 2 | 2017-05-02 20:36:33 |
| 3 | 2017-05-02 20:36:52 |
| 4 | 2017-05-02 20:53:26 |
| 5 | 2017-05-02 20:53:27 |
| 6 | 2017-05-02 20:53:28 |
| 7 | 2017-05-02 20:53:29 |
| 8 | 2017-05-02 20:53:30 |
| 9 | 2017-05-02 20:58:19 |
| 10 | 2017-05-02 20:58:20 |
| 11 | 2017-05-02 20:58:22 |
| 12 | 2017-05-02 20:58:23 |
| 13 | 2017-05-02 20:58:24 |
| 14 | 2017-05-02 20:58:25 |
| 15 | 2017-05-02 21:10:37 |
| 16 | 2017-05-02 21:10:38 |
| 17 | 2017-05-02 21:10:39 |
| 18 | 2017-05-02 21:10:42 |
| 19 | 2017-05-02 21:10:43 |
+----+---------------------+
[root@linux1 ~]# mysql -uroot -padmin -e "truncate table bd01.t1"
[root@linux1 ~]# mysqlbinlog mysql-bin.000004 | mysql -uroot -padmin
[root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1"
+----+---------------------+
| id | data |
+----+---------------------+
| 15 | 2017-05-02 21:10:37 |
| 16 | 2017-05-02 21:10:38 |
| 17 | 2017-05-02 21:10:39 |
| 18 | 2017-05-02 21:10:42 |
| 19 | 2017-05-02 21:10:43 |
+----+---------------------+
[root@linux1 ~]# mysqlbinlog mysql-bin.000003 mysql-bin.000004 | mysql -uroot -padmin
ERROR 1062 (23000) at line 35: Duplicate entry '9' for key 'PRIMARY'
MySQL - Point-in-Time Recovery Using the Binary Log
[root@linux1 ~]# mysql -uroot -padmin -e "SHOW BINLOG EVENTS IN 'mysql-bin.000003'"
+------------------+------+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000003 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.17-log, Binlog ver: 4 |
| mysql-bin.000003 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000003 | 219 | Query | 1 | 291 | BEGIN |
| mysql-bin.000003 | 291 | Table_map | 1 | 338 | table_id: 108 (bd01.t1) |
| mysql-bin.000003 | 338 | Write_rows | 1 | 382 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000003 | 382 | Xid | 1 | 413 | COMMIT /* xid=162 */ |
| mysql-bin.000003 | 413 | Anonymous_Gtid | 1 | 478 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000003 | 478 | Query | 1 | 550 | BEGIN |
| mysql-bin.000003 | 550 | Table_map | 1 | 597 | table_id: 108 (bd01.t1) |
| mysql-bin.000003 | 597 | Write_rows | 1 | 641 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000003 | 641 | Xid | 1 | 672 | COMMIT /* xid=163 */ |
| mysql-bin.000003 | 737 | Query | 1 | 809 | BEGIN |
| mysql-bin.000003 | 809 | Table_map | 1 | 856 | table_id: 108 (bd01.t1) |
| mysql-bin.000003 | 856 | Write_rows | 1 | 900 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000003 | 900 | Xid | 1 | 931 | COMMIT /* xid=164 */ |
| mysql-bin.000003 | 996 | Query | 1 | 1068 | BEGIN |
| mysql-bin.000003 | 1068 | Table_map | 1 | 1115 | table_id: 108 (bd01.t1) |
| mysql-bin.000003 | 1115 | Write_rows | 1 | 1159 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000003 | 1159 | Xid | 1 | 1190 | COMMIT /* xid=165 */ |
| mysql-bin.000003 | 1708 | Rotate | 1 | 1755 | mysql-bin.000004;pos=4 |
+------------------+------+----------------+-----------+-------------+---------------------------------------+
[root@linux1 ~]# mysql -uroot -padmin -e "truncate table bd01.t1"
[root@linux1 ~]# mysqlbinlog --start-position=4 --stop-position=900 mysql-bin.000003 | mysql -uroot -padmin
[root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1"
+----+---------------------+
| id | data |
+----+---------------------+
| 9 | 2017-05-02 20:58:19 |
| 10 | 2017-05-02 20:58:20 |
+----+---------------------+
MySQL - Point-in-Time Recovery Using Event Times
mysqlbinlog --start-datetime="2017-04-19 20:36:16" --stop-datetime="2017-04-19 21:10:43" /var/lib/mysql/mysql-bin.0* | mysql -u root -padmin
[root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1"
+----+---------------------+
| id | data |
+----+---------------------+
| 1 | 2017-05-02 20:36:16 |
| 2 | 2017-05-02 20:36:33 |
| 3 | 2017-05-02 20:36:52 |
| 4 | 2017-05-02 20:53:26 |
| 5 | 2017-05-02 20:53:27 |
| 6 | 2017-05-02 20:53:28 |
| 7 | 2017-05-02 20:53:29 |
| 8 | 2017-05-02 20:53:30 |
| 9 | 2017-05-02 20:58:19 |
| 10 | 2017-05-02 20:58:20 |
| 11 | 2017-05-02 20:58:22 |
| 12 | 2017-05-02 20:58:23 |
| 13 | 2017-05-02 20:58:24 |
| 14 | 2017-05-02 20:58:25 |
| 15 | 2017-05-02 21:10:37 |
| 16 | 2017-05-02 21:10:38 |
| 17 | 2017-05-02 21:10:39 |
| 18 | 2017-05-02 21:10:42 |
| 19 | 2017-05-02 21:10:43 |
+----+---------------------+
Obs.: É possível direcionar o resultado da execução dos binary logs para um arquivo ao invés de direcionar para o MySQL
• mysqlbinlog mysql-bin.000003 > /tmp/mysql-bin.000003.sql
• mysqlbinlog --start-datetime="2017-04-19 20:36:16" --stop-datetime="2017-04-19 21:10:43" /var/lib/mysql/mysql-bin.0* > /tmp/script.sql
COLD / HOT Backups
MySQL – COLD Backup
[root@linux1 ~]# systemctl stop mysqld
[root@linux1 ~]# cp -a /var/lib/mysql /backup/
[root@linux1 ~]# ls -lh /backup/mysql/
total 109M
-rw-r-----. 1 mysql mysql 56 May 4 00:27 auto.cnf
drwxr-x---. 2 mysql mysql 4.0K May 4 00:30 bd01
-rw-------. 1 mysql mysql 1.7K May 4 00:27 ca-key.pem
-rw-r--r--. 1 mysql mysql 1.1K May 4 00:27 ca.pem
-rw-r--r--. 1 mysql mysql 1.1K May 4 00:27 client-cert.pem
-rw-------. 1 mysql mysql 1.7K May 4 00:27 client-key.pem
-rw-r-----. 1 mysql mysql 358 May 4 00:45 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12M May 4 00:45 ibdata1
-rw-r-----. 1 mysql mysql 48M May 4 00:45 ib_logfile0
-rw-r-----. 1 mysql mysql 48M May 4 00:27 ib_logfile1
drwxr-x---. 2 mysql mysql 4.0K May 4 00:27 mysql
drwxr-x---. 2 mysql mysql 4.0K May 4 00:27 performance_schema
-rw-------. 1 mysql mysql 1.7K May 4 00:27 private_key.pem
-rw-r--r--. 1 mysql mysql 451 May 4 00:27 public_key.pem
-rw-r--r--. 1 mysql mysql 1.1K May 4 00:27 server-cert.pem
-rw-------. 1 mysql mysql 1.7K May 4 00:27 server-key.pem
drwxr-x---. 2 mysql mysql 12K May 4 00:27 sys
[root@linux1 ~]# systemctl start mysqld
Obs.: Lembrar de incluir o
backup do arquivo /etc/my.cnf
MySQL – HOT Backup (XtraBackup)
https://www.percona.com/downloads/XtraBackup/LATEST/
http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
MySQL – HOT Backup (XtraBackup)
[root@linux1 ~]# yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm -y
Loaded plugins: fastestmirror, langpacks
percona-release-0.1-4.noarch.rpm | 6.4 kB 00:00:00
Examining /var/tmp/yum-root-YMmTLa/percona-release-0.1-4.noarch.rpm: percona-release-0.1-4.noarch
Marking /var/tmp/yum-root-YMmTLa/percona-release-0.1-4.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package percona-release.noarch 0:0.1-4 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================
Package Arch Version Repository Size
=======================================================================================================
Installing:
percona-release noarch 0.1-4 /percona-release-0.1-4.noarch 5.8 k
Transaction Summary
=======================================================================================================
Install 1 Package
Total size: 5.8 k
Installed size: 5.8 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : percona-release-0.1-4.noarch 1/1
Verifying : percona-release-0.1-4.noarch 1/1
Installed:
percona-release.noarch 0:0.1-4
Complete!
MySQL – HOT Backup (XtraBackup)
root@linux1 ~]# yum install percona-xtrabackup-24 -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* updates: mirror.globo.com
Resolving Dependencies
--> Running transaction check
---> Package percona-xtrabackup-24.x86_64 0:2.4.7-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===========================================================================================================
Package Arch Version Repository Size
===========================================================================================================
Installing:
percona-xtrabackup-24 x86_64 2.4.7-1.el7 percona-release-x86_64 7.3 M
Transaction Summary
===========================================================================================================
Install 1 Package
Total download size: 7.3 M
Installed size: 31 M
Downloading packages:
percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm | 7.3 MB 00:00:00
Transaction test succeeded
Running transaction
Installing : percona-xtrabackup-24-2.4.7-1.el7.x86_64 1/1
Verifying : percona-xtrabackup-24-2.4.7-1.el7.x86_64 1/1
Installed:
percona-xtrabackup-24.x86_64 0:2.4.7-1.el7
Complete!
root@linux1 ~]# whereis innobackupex
innobackupex: /usr/bin/innobackupex /usr/share/man/man1/innobackupex.1.gz
MySQL – HOT Backup (XtraBackup)
[root@linux1 ~]# innobackupex --user=root --password=admin --no-timestamp /backup/new_backup
170504 00:38:30 innobackupex: Starting the backup operation
170504 00:38:30 version_check Connecting to MySQL server
170504 00:38:30 version_check Connected to MySQL server
170504 00:38:30 Connecting to MySQL server host: localhost
Using server version 5.7.18
innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64)
xtrabackup: cd to /var/lib/mysql
xtrabackup: open files limit requested 0, set to 1024
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
170504 00:38:31 [01] Copying ./ibdata1 to /backup/new_backup/ibdata1
170504 00:38:31 [01] ...done
170504 00:38:31 [01] Copying ./bd01/t1.ibd to /backup/new_backup/bd01/t1.ibd
170504 00:38:31 [01] ...done
170504 00:38:31 >> log scanned up to (2541003)
170504 00:38:32 Starting to backup non-InnoDB tables and files
xtrabackup: Stopping log copying thread.
170504 00:38:34 Executing UNLOCK TABLES
170504 00:38:34 All tables unlocked
170504 00:38:34 [00] Copying ib_buffer_pool to /backup/new_backup/ib_buffer_pool
170504 00:38:34 [00] ...done
170504 00:38:34 Backup created in directory '/backup/new_backup/'
170504 00:38:34 [00] Writing backup-my.cnf
170504 00:38:34 [00] ...done
170504 00:38:34 [00] ...done
xtrabackup: Transaction log of lsn (2540994) to (2541003) was copied.
170504 00:38:34 completed OK!
[root@linux1 ~]# innobackupex --user=root --password=admin /backup
--no-timestamp indica que não deverá
ser gerado um diretório e que o
mesmo será informado manualmente
new_backup é o diretório onde o
backup deverá ser gerado.
Um diretório será criado automaticamente
dentro de /backup com nome no formato
YYYY-MM-DD_HH-MI-SS
MySQL – HOT Backup (XtraBackup)
[root@linux1 ~]# ls -lh /backup
total 8.0K
drwx------. 6 root root 4.0K May 4 00:38 new_backup
drwxr-x---. 6 root root 4.0K May 4 00:42 2017-05-04_00-42-01
[root@linux1 ~]# ls -lh /backup/new_backup/
total 13M
-rw-r-----. 1 root root 424 May 4 00:38 backup-my.cnf
drwxr-x---. 2 root root 4.0K May 4 00:38 bd01
-rw-r-----. 1 root root 419 May 4 00:38 ib_buffer_pool
-rw-r-----. 1 root root 12M May 4 00:38 ibdata1
drwxr-x---. 2 root root 4.0K May 4 00:38 mysql
drwxr-x---. 2 root root 4.0K May 4 00:38 performance_schema
drwxr-x---. 2 root root 12K May 4 00:38 sys
-rw-r-----. 1 root root 113 May 4 00:38 xtrabackup_checkpoints
-rw-r-----. 1 root root 442 May 4 00:38 xtrabackup_info
-rw-r-----. 1 root root 2.5K May 4 00:38 xtrabackup_logfile
MySQL – Prepare HOT Backup (XtraBackup)
[root@linux1 ~]# innobackupex --apply-log /backup/new_backup
170504 00:44:01 innobackupex: Starting the apply-log operation
innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 6f7a799)
xtrabackup: cd to /backup/new_backup/
xtrabackup: This target seems to be already prepared.
InnoDB: Number of pools: 1
xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
InnoDB: Creating shared tablespace for temporary tables
InnoDB: File './ibtmp1' size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.7.13 started; log sequence number 2541143
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2541162
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2541181
170504 00:44:04 completed OK!
MySQL – Restore HOT Backup (XtraBackup)
[root@linux1 ~]# systemctl stop mysqld
[root@linux1 ~]# mkdir /tmp/mysql
[root@linux1 ~]# mv /var/lib/mysql/* /tmp/mysql
[root@linux1 ~]# innobackupex --copy-back /backup/new_backup
170504 00:46:29 innobackupex: Starting the copy-back operation
innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 6f7a799)
170504 00:46:29 [01] Copying ib_logfile0 to /var/lib/mysql/ib_logfile0
170504 00:46:29 [01] ...done
170504 00:46:29 [01] Copying ib_logfile1 to /var/lib/mysql/ib_logfile1
170504 00:46:30 [01] ...done
170504 00:46:30 [01] Copying ibdata1 to /var/lib/mysql/ibdata1
170504 00:46:30 [01] ...done
170504 00:46:30 [01] Copying ./sys/sys_config_insert_set_user.TRN to /var/lib/mysql/sys/sys_config_insert_set_user.TRN
170504 00:46:30 [01] ...done
170504 00:46:30 [01] Copying ./sys/db.opt to /var/lib/mysql/sys/db.opt
170504 00:46:30 [01] ...done
170504 00:46:30 [01] Copying ./sys/host_summary_by_file_io_type.frm to /var/lib/mysql/sys/host_io_type.frm
170504 00:46:30 [01] ...done
170504 00:46:30 [01] Copying ./sys/sys_config.ibd to /var/lib/mysql/sys/sys_config.ibd
170504 00:46:30 [01] ...done
170504 00:46:30 [01] Copying ./sys/schema_index_statistics.frm to /var/lib/mysql/sys/schema_index_statistics.frm
170504 00:46:31 [01] ...done
170504 00:46:31 [01] Copying ./mysql/time_zone.ibd to /var/lib/mysql/mysql/time_zone.ibd
170504 00:46:31 [01] ...done
170504 00:46:31 [01] Copying ./mysql/event.frm to /var/lib/mysql/mysql/event.frm
170504 00:46:31 [01] ...done
170504 00:46:32 [01] Copying ./mysql/columns_priv.MYD to /var/lib/mysql/mysql/columns_priv.MYD
170504 00:46:32 [01] ...done
170504 00:46:33 [01] Copying ./performance_schema/event_name.frm to 170504 00:46:33 [01] ...done
170504 00:46:33 [01] Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info
170504 00:46:33 [01] ...done
170504 00:46:33 completed OK!
[root@linux1 ~]# chown -R mysql.mysql /var/lib/mysql
[root@linux1 ~]# systemctl start mysqld
MySQL – Incremental HOT Backup (XtraBackup)
BACKUP FULL / INCREMENTAL
-------------------------
1) innobackupex --user=root --password=admin /backup (BACKUP FULL)
2) innobackupex --incremental /backup --incremental-basedir=/backup/2017-05-04_05-27-23 --user=root --password=admin (BACKUP INC)
3) innobackupex --apply-log --redo-only /backup/2017-05-04_05-27-23 --user=root --password=admin (PREPARE FULL)
4) innobackupex --apply-log --redo-only /backup/2017-05-04_05-27-23 --incremental-dir=2017-05-04_05-28-34 --user=root --password=admin (PREPARE INC)
5) innobackupex --incremental /backup --incremental-basedir=/backup/2017-05-04_05-27-23 --user=root --password=admin (BACKUP INC)
6) innobackupex --apply-log --redo-only /backup/2017-05-04_05-27-23 --incremental-dir=2017-05-04_05-31-35 --user=root --password=admin (PREPARE INC)
[root@linux1 ~]# ls -lh /backup
total 12K
drwxr-x---. 6 root root 4.0K May 4 05:32 2017-05-04_05-27-23 (FULL)
drwxr-x---. 6 root root 4.0K May 4 05:30 2017-05-04_05-28-34 (INCREMENTAL)
drwxr-x---. 6 root root 4.0K May 4 05:32 2017-05-04_05-31-35 (INCREMENTAL)
RESTORE FULL
------------
[root@linux1 ~]# systemctl stop mysqld
[root@linux1 ~]# mkdir /tmp/mysql
[root@linux1 ~]# mv /var/lib/mysql/* /tmp/mysql
[root@linux1 ~]# innobackupex --copy-back /backup/2017-05-04_05-27-23 (FULL)
[root@linux1 ~]# chown -R mysql.mysql /var/lib/mysql
[root@linux1 ~]# systemctl start mysqld
MySQL – Change datadir (move from /var/lib/mysql)
# cat /etc/selinux/config
SELINUX=disabled
# systemctl stop mysqld
# mkdir -p /data/lib
# cp -a /var/lib/mysql /data/lib
# mv /var/lib/mysql /var/lib/mysql_old
# chown -R mysql.mysql /data/lib/mysql
# yum install policycoreutils-python
# semanage fcontext -a -s system_u -t mysqld_db_t "/data/lib/mysql(/.*)?"
# restorecon -Rv /data/lib/mysql
# cat /etc/my.cnf
[mysqld]
datadir=/data/lib/mysql
socket=/data/lib/mysql/mysql.sock
[client]
port=3306
socket=/data/lib/mysql/mysql.sock
# systemctl start mysqld
Events
MySQL - Events
mysql> show global variables like 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.01 sec)
mysql> SET GLOBAL event_scheduler = ON;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.02 sec)
mysql> SHOW FULL PROCESSLIST;
+----+-----------------+-----------+--------------------+---------+------+------------------------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+--------------------+---------+------+------------------------+-----------------------+
| 7 | root | localhost | performance_schema | Query | 0 | starting | SHOW FULL PROCESSLIST |
| 8 | event_scheduler | localhost | NULL | Daemon | 8 | Waiting on empty queue | NULL |
+----+-----------------+-----------+--------------------+---------+------+------------------------+-----------------------+
2 rows in set (0.00 sec)
https://dev.mysql.com/doc/refman/5.7/en/create-event.html
MySQL - Events
mysql> show create table t1;
+-------+-----------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------+
| t1 | CREATE TABLE `t1` ( |
| `id` int(11) NOT NULL AUTO_INCREMENT, |
| `data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| PRIMARY KEY (`id`) |
|) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CURRENT_USER(),SCHEMA();
+----------------+----------+
| CURRENT_USER() | SCHEMA() |
+----------------+----------+
| root@localhost | bd01 |
+----------------+----------+
1 row in set (0.00 sec)
mysql> USE bd01;
Database changed
mysql> CREATE EVENT event_insert_t1 ON SCHEDULE
-> EVERY 1 MINUTE
-> STARTS (NOW() + INTERVAL 1 MINUTE)
-> ON COMPLETION PRESERVE ENABLE
-> DO insert into t1 values (NULL,NULL);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT NAME,interval_value,STARTS,execute_at,last_executed,on_completion,STATUS FROM mysql.event;
+-----------------+----------------+---------------------+------------+---------------+---------------+---------+
| NAME | interval_value | STARTS | execute_at | last_executed | on_completion | STATUS |
+-----------------+----------------+---------------------+------------+---------------+---------------+---------+
| event_insert_t1 | 1 | 2017-05-02 13:08:38 | NULL | NULL | PRESERVE | ENABLED |
+-----------------+----------------+---------------------+------------+---------------+---------------+---------+
1 row in set (0.00 sec)
MySQL - Events
mysql> select * from t1;
+----+---------------------+
| id | data |
+----+---------------------+
| 1 | 2017-05-02 13:08:38 |
| 2 | 2017-05-02 13:09:38 |
| 3 | 2017-05-02 13:10:38 |
| 4 | 2017-05-02 13:11:38 |
+----+---------------------+
mysql> SELECT NAME,interval_value,STARTS,execute_at,last_executed,on_completion,STATUS FROM mysql.event;
+-----------------+----------------+---------------------+------------+---------------------+---------------+---------+
| NAME | interval_value | STARTS | execute_at | last_executed | on_completion | STATUS |
+-----------------+----------------+---------------------+------------+---------------------+---------------+---------+
| event_insert_t1 | 1 | 2017-05-02 13:08:38 | NULL | 2017-05-02 13:11:38 | PRESERVE | ENABLED |
+-----------------+----------------+---------------------+------------+---------------------+---------------+---------+
mysql> show eventsG
*************************** 1. row ***************************
Db: bd01
Name: event_insert_t1
Definer: root@localhost
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 1
Interval field: MINUTE
Starts: 2017-05-02 13:08:38
Ends: NULL
Status: ENABLED
Originator: 0
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
mysql> drop event event_insert_t1;
Query OK, 0 rows affected (0.00 sec)
MySQL - Events
mysql> desc INFORMATION_SCHEMA.events;
+----------------------+---------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------------------+-------+
| EVENT_CATALOG | varchar(64) | NO | | | |
| EVENT_SCHEMA | varchar(64) | NO | | | |
| EVENT_NAME | varchar(64) | NO | | | |
| DEFINER | varchar(93) | NO | | | |
| TIME_ZONE | varchar(64) | NO | | | |
| EVENT_BODY | varchar(8) | NO | | | |
| EVENT_DEFINITION | longtext | NO | | NULL | |
| EVENT_TYPE | varchar(9) | NO | | | |
| EXECUTE_AT | datetime | YES | | NULL | |
| INTERVAL_VALUE | varchar(256) | YES | | NULL | |
| INTERVAL_FIELD | varchar(18) | YES | | NULL | |
| SQL_MODE | varchar(8192) | NO | | | |
| STARTS | datetime | YES | | NULL | |
| ENDS | datetime | YES | | NULL | |
| STATUS | varchar(18) | NO | | | |
| ON_COMPLETION | varchar(12) | NO | | | |
| CREATED | datetime | NO | | 0000-00-00 00:00:00 | |
| LAST_ALTERED | datetime | NO | | 0000-00-00 00:00:00 | |
| LAST_EXECUTED | datetime | YES | | NULL | |
| EVENT_COMMENT | varchar(64) | NO | | | |
| ORIGINATOR | bigint(10) | NO | | 0 | |
| CHARACTER_SET_CLIENT | varchar(32) | NO | | | |
| COLLATION_CONNECTION | varchar(32) | NO | | | |
| DATABASE_COLLATION | varchar(32) | NO | | | |
+----------------------+---------------+------+-----+---------------------+-------+
Performance Tuning
Linux - Workload (top)
[root@linux1 ~]# free -m
total used free shared buffers cached
Mem: 30099 29953 145 0 10 22254
-/+ buffers/cache: 7688 22410
Swap: 16383 3716 12667
[root@linux1 ~]# nice top –d1
top - 12:53:25 up 209 days, 18:08, 1 user, load average: 1.11, 5.30, 15.35
Tasks: 1431 total, 1 running, 936 sleeping, 26 stopped, 468 zombie
Cpu(s): 2.7%us, 1.2%sy, 0.5%ni, 92.3%id, 3.4%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 16329264k total, 16123804k used, 205460k free, 24664k buffers
Swap: 16777208k total, 7544608k used, 9232600k free, 10381940k cached
Cpu0 : 38.9%us, 27.7%sy, 0.0%ni, 4.3%id, 91.1%wa, 0.0%hi, 18.1%si, 0.0%st
Cpu1 : 44.7%us, 10.6%sy, 0.0%ni, 41.5%id, 3.2%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 17.2%us, 9.7%sy, 0.0%ni, 68.8%id, 4.3%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 28.7%us, 10.6%sy, 0.0%ni, 56.4%id, 4.3%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu4 : 22.0%us, 2.0%sy, 0.0%ni, 75.0%id, 1.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu5 : 12.2%us, 7.1%sy, 0.0%ni, 77.6%id, 3.1%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu6 : 9.0%us, 5.0%sy, 0.0%ni, 86.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu7 : 12.1%us, 6.1%sy, 0.0%ni, 77.8%id, 4.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu8 : 4.9%us, 2.9%sy, 0.0%ni, 91.2%id, 1.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 30821572k total, 30481844k used, 339728k free, 6456k buffers
Swap: 16777208k total, 3153372k used, 13623836k free, 21027488k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10987 mysql 20 0 2898m 494m 6160 S 98.0 3.1 119:30.47 /usr/sbin/mysqld
9959 oracle 20 0 22.2g 2.4g 2.4g S 56.8 8.0 1:28.77 oracle (LOCAL=NO)
25530 oracle 20 0 22.2g 4.4g 4.4g S 43.3 14.9 6:19.42 oracle (LOCAL=NO)
9961 oracle 20 0 22.2g 3.4g 3.4g S 38.5 11.5 3:00.94 oracle (LOCAL=NO)
16268 oracle 20 0 22.2g 1.3g 1.3g S 37.5 4.4 0:05.55 oracle (LOCAL=NO)
Load Average (CPU + I/O)
A grosso modo, dizemos que um servidor está a 100% se o valor do
Load Average coincide com o número de CPUs do servidor. Por
exemplo, em um servidor com 8 CPUs:
Load Average: 2 = 25% da capacidade total
Load Average: 4 = 50% da capacidade total
Load Average: 8 = 100% da capacidade total
Load Average: 16 = 200% da capacidade total
VIRT
VIRT representa o tamanho virtual de um processo, que é a soma de
memória que ele [processo] está realmente usando (por exemplo
RAM da placa de vídeo para o servidor X), os arquivos no disco que
foram mapeados para ele [processo] (bibliotecas mais notavelmente
compartilhadas), e memória compartilhada com outros processos.
VIRT representa a quantidade de memória do programa que é capaz
de acessar no momento presente.
RES
RES representa o tamanho residente, que é uma representação
precisa da quantidade de memória física real consumida de um
processo. Também corresponde diretamente à coluna MEM%. Será
virtualmente sempre menor do que o tamanho VIRT, uma vez que a
maioria dos programas dependem da biblioteca C.
Linux - Workload (iostat)
[root@linux1 ~]# nice iostat -dm -p 5
Linux 2.6.32-431.el6.x86_64 (ip-172-31-39-64) 05/04/2017 _x86_64_ (16 CPU)
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
xvdf 0.00 0.00 0.00 0 0
xvdg 0.00 0.00 0.00 0 0
xvdt 3.00 0.00 0.01 0 0
xvdaf 0.00 0.00 0.00 0 0
xvdu 0.00 0.00 0.00 0 0
xvds 0.00 0.00 0.00 0 0
xvdr 0.20 0.00 0.00 0 0
xvdq 11.40 0.00 0.04 0 0
xvdae 0.20 0.00 0.00 0 0
xvdad 107.00 0.08 0.39 0 1
xvdw 262.20 9.29 9.83 16 18
[root@linux1 ~]# nice iostat -xdm 5
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util
xvdf 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
xvdg 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
xvdt 0.00 0.00 0.00 1.40 0.00 0.01 8.00 0.00 0.71 0.43 0.06
xvdaf 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
xvdu 0.00 0.00 0.00 3.00 0.00 0.01 8.00 0.01 2.33 1.13 0.34
xvds 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
xvdr 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
xvdq 0.00 0.00 1.40 7.40 0.01 0.03 9.27 0.01 1.43 0.77 0.68
xvdae 0.00 0.00 0.00 0.60 0.00 0.00 8.00 0.00 0.67 0.33 0.02
xvdad 0.00 0.00 75.60 49.40 0.90 0.19 17.91 0.13 1.03 0.62 7.76
xvdw 0.00 9.80 419.40 348.40 9.47 9.16 87.07 9.87 9.31 0.42 98.20
Linux - Workload (iotop)
[root@linux1 ~]# yum install iotop -y
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package iotop.noarch 0:0.6-2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================
Package Arch Version Repository Size
============================================================================================================
Installing:
iotop noarch 0.6-2.el7 base 52 k
Transaction Summary
============================================================================================================
Install 1 Package
Total download size: 52 k
Installed size: 156 k
Downloading packages:
iotop-0.6-2.el7.noarch.rpm | 52 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : iotop-0.6-2.el7.noarch 1/1
Verifying : iotop-0.6-2.el7.noarch 1/1
Installed:
iotop.noarch 0:0.6-2.el7
Complete!
Linux - Workload (iotop)
[root@linux1 ~]# nice iotop -o
Total DISK READ: 4.95 M/s | Total DISK WRITE: 1660.44 K/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
3991 be/4 mysql 999.27 K/s 1973.45 K/s 0.00 % 95.16 % /usr/sbin/mysqld --basedir=/usr --datadir=
9959 be/4 oracle 753.15 K/s 330.26 K/s 0.00 % 24.31 % oracle (LOCAL=NO)
806 be/4 oracle 1482.67 K/s 0.00 B/s 0.00 % 22.32 % oracle (LOCAL=NO)
2562 be/4 oracle 834.04 K/s 0.00 B/s 0.00 % 11.12 % oracle (LOCAL=NO)
8582 be/4 oracle 230.72 K/s 0.00 B/s 0.00 % 0.87 % oracle (LOCAL=NO)
8132 be/4 oracle 121.03 K/s 0.00 B/s 0.00 % 0.64 % oracle (LOCAL=NO)
16795 be/4 oracle 56.73 K/s 0.00 B/s 0.00 % 0.57 % oracle (LOCAL=NO)
4214 be/4 oracle 109.69 K/s 0.00 B/s 0.00 % 0.46 % oracle (LOCAL=NO)
21397 be/4 oracle 166.42 K/s 0.00 B/s 0.00 % 0.46 % oracle (LOCAL=NO)
19870 be/4 oracle 204.25 K/s 0.00 B/s 0.00 % 0.43 % oracle (LOCAL=NO)
20236 be/4 oracle 105.91 K/s 0.00 B/s 0.00 % 0.39 % oracle (LOCAL=NO)
8822 be/4 oracle 94.56 K/s 0.00 B/s 0.00 % 0.39 % oracle (LOCAL=NO)
5058 be/4 oracle 185.33 K/s 0.00 B/s 0.00 % 0.38 % oracle (LOCAL=NO)
1495 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.27 % [jbd2/xvdo1-8]
20880 be/4 oracle 105.91 K/s 0.00 B/s 0.00 % 0.24 % oracle (LOCAL=NO)
25479 be/4 oracle 30.26 K/s 0.00 B/s 0.00 % 0.22 % oracle (LOCAL=NO)
1532 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.19 % [jbd2/xvdaa1-8]
1538 be/3 root 0.00 B/s 7.56 K/s 0.00 % 0.17 % [jbd2/xvdj1-8]
7269 be/4 oracle 22.69 K/s 0.00 B/s 0.00 % 0.17 % oracle (LOCAL=NO)
1536 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.16 % [jbd2/xvdz1-8]
29391 be/4 oracle 22.69 K/s 0.00 B/s 0.00 % 0.16 % oracle (LOCAL=NO)
1499 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.16 % [jbd2/xvdq1-8]
24565 be/4 oracle 98.34 K/s 0.00 B/s 0.00 % 0.14 % oracle (LOCAL=NO)
1540 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.13 % [jbd2/xvdw1-8]
20240 be/4 oracle 15.13 K/s 0.00 B/s 0.00 % 0.11 % oracle (LOCAL=NO)
26552 be/4 oracle 15.13 K/s 0.00 B/s 0.00 % 0.10 % oracle (LOCAL=NO)
7050 be/4 oracle 37.82 K/s 0.00 B/s 0.00 % 0.10 % oracle (LOCAL=NO)
18935 be/4 oracle 30.26 K/s 0.00 B/s 0.00 % 0.10 % oracle (LOCAL=NO)
Linux - Workload (vmstat)
* High numbers in the blocked processes column (b) indicates slow disks.
* (r) should always be higher than (b); if it is not, it usually means you have a CPU bottleneck
* Whenever the value of the (r) column exceeds the number of CPUs on the server, tasks are forced to wait for execution
* Remember that we need to know the number of CPUs on our server because the vmstat (r) value must never exceed the number of CPUs. (r)
value of 13 is perfectly acceptable for a 16-CPU server, while a value of 16 would be a serious problem for a 12-CPU server.
[root@linux1 ~]# cat /proc/cpuinfo|grep processor|wc -l
16
[root@linux1 ~]# nice vmstat -SM 5
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
6 0 3846 1906 7 19812 0 0 7874 15245 61567 40740 13 8 77 2 0
4 2 3845 1763 7 19932 0 0 1429 21140 48832 30933 11 6 81 1 0
11 0 3845 1678 7 20020 0 0 1452 9647 58602 36188 11 8 80 1 0
8 1 3845 1561 7 20154 0 0 10593 7233 60044 37682 16 8 75 1 0
4 0 3844 899 7 20803 0 0 49169 90868 45795 29962 8 6 84 2 0
8 1 3844 880 8 20818 0 0 1510 1837 41939 27825 7 5 87 0 0
9 0 3843 851 8 20830 0 0 1550 9910 56914 35578 30 11 59 1 0
8 0 3842 829 8 20837 0 0 499 1131 43510 26554 25 6 69 0 0
5 0 3842 835 8 20846 0 0 803 13694 34505 22058 22 5 72 0 0
9 0 3842 809 8 20860 0 0 1626 1486 35354 22317 25 6 69 0 0
4 0 3841 783 9 20880 0 0 2308 2097 34873 21853 29 6 64 1 0
5 0 3841 711 9 20928 0 0 913 9214 34264 19545 16 5 78 0 0
7 1 3838 531 9 21114 0 0 4815 34359 44799 27590 16 6 75 2 0
5 3 3838 328 9 21220 0 0 4259 20427 39931 22669 23 7 69 1 0
5 0 3837 268 9 21371 0 0 4283 40010 31617 19840 15 5 79 1 0
6 1 3837 252 9 21389 0 0 2118 2403 31822 21441 9 4 86 1 0
7 0 3836 225 9 21414 0 0 3614 2380 41579 27585 12 5 82 1 0
11 1 3835 189 9 21453 0 0 5604 3347 49111 32252 20 6 72 1 0
7 0 3834 142 9 21490 0 0 6078 11898 60447 40898 19 8 71 2 0
Linux - mytop
[root@linux1 ~]# yum install epel-release -y
[root@linux1 ~]# yum install mytop –y
[root@linux1 ~]# whereis mytop
mytop: /usr/bin/mytop /usr/share/man/man1/mytop.1.gz
[root@linux1 ~]# mytop -hlocalhost -port3306 -uroot -padmin -dmysql -s1
MySQL on localhost (5.7.18) up 50+05:05:24 [20:47:06]
Queries: 11.1M qps: 3 Slow: 535.0 Se/In/Up/De(%): 43/14/01/01
qps now: 6 Slow qps: 0.0 Threads: 13 ( 5/ 0) 29/00/00/00
Key Efficiency: 100.0% Bps in/out: 998.3/ 6.4k Now in/out: 275.5/11.3k
Id User Host/IP DB Time Cmd Query or State
-- ---- ------- -- ---- --- ----------
909077 root 11.315.260.30 mysql 0 Query show full processlist
909000 user01 11.315.260.209 bd01 2 Sleep
909107 user02 11.315.260.246 4 Query SELECT CASE WHEN (SELECT DISTINCT 1 FROM...
909090 root 11.315.200.30 bd02 5 Sleep
909091 root 11.315.210.30 105 Sleep
908873 user02 11.315.210.209 bd02 542 Sleep
1 event_sch localhost 2071 Daemon Waiting for next activation
905245 user03 11.315.206.209 bd02 10653 Sleep
905246 user03 11.315.201.209 bd02 10659 Sleep
906711 user04 11.315.209.209 bd02 10653 Sleep
904528 user04 11.315.203.103 21162 Sleep
904526 user05 11.315.220.103 bd03 21163 Sleep
MySQL - slow_query_log
mysql> show variables like '%_query_%';
+------------------------------+--------------------------------+
| Variable_name | Value |
+------------------------------+--------------------------------+
| long_query_time | 10.000000 |
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/linux1-slow.log |
+------------------------------+--------------------------------+
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
mysql> SET GLOBAL slow_query_log=1;
Query OK, 0 rows affected (0,07 sec)
mysql> SET GLOBAL long_query_time=5;
Query OK, 0 rows affected (0,00 sec)
mysql> SET GLOBAL log_output='TABLE';
Query OK, 0 rows affected (0,00 sec)
mysql> show global variables like '%_query_%';
+------------------------------+--------------------------------+
| Variable_name | Value |
+------------------------------+--------------------------------+
| long_query_time | 5.000000 |
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/linux1-slow.log |
+------------------------------+--------------------------------+
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | TABLE |
+---------------+-------+
MySQL - slow_query_log
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 61 |
+-----------------+
mysql> select count(*) from bd01.emp;
+----------+
| count(*) |
+----------+
| 2097153 |
+----------+
1 row in set (33,67 sec)
mysql> select count(*) from bd01.emp where id<>34;
+----------+
| count(*) |
+----------+
| 2097153 |
+----------+
1 row in set (4,60 sec)
mysql> SELECT
-> start_time, user_host,
-> lock_time, query_time,
-> lock_time, rows_sent,
-> sql_text, thread_id
-> FROM mysql.slow_log;
+----------------------------+---------------------------+-----------------+-----------------+-----------+-------------------------------+-----------+
| start_time | user_host | query_time | lock_time | rows_sent | sql_text | thread_id |
+----------------------------+---------------------------+-----------------+-----------------+-----------+-------------------------------+-----------+
| 2017-04-21 15:52:19.265806 | root[root] @ localhost [] | 00:00:33.739565 | 00:00:00.000504 | 1 | select count(*) from bd01.emp | 61 |
+----------------------------+---------------------------+-----------------+-----------------+-----------+-------------------------------+-----------+
1 row in set (0,00 sec)
MySQL - slow_query_log
MyISAM Tables
MySQL - slow_query_log
MySQL - slow_query_log
MySQL – Query Performance
Em relação às consultas que utilizam o operador LIKE, é possível melhorar a performance de sua execução fazendo uso de
índices FULLTEXT que oferece suporte na engine MyISAM e na engine InnoDB da versão 5.6 do MySQL. Segue abaixo um
comparativo de uma das consultas que demoraram mais de 10 segundos para serem executadas. No lado esquerdo da
imagem se encontra a consulta original e a do lado direito a consulta alterada para fazer uso de índices FULLTEXT.
MySQL – Query Performance
Testes realizados comprovam a eficácia de
utilização de índices FULLTEXT para consultas
complexas que fazem uso do operador LIKE. A
imagem ao lado demonstra que o tempo de
resposta (eixo vertical) diminui na medida em que
o número de registros aumenta (eixo horizontal).
MySQL – Explain Plan / Profiles
mysql> EXPLAIN EXTENDED select count(*) from bd01.emp where id<>34;
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| 1 | SIMPLE | emp | NULL | ALL | NULL | NULL | NULL | NULL | 2087295 | 90.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 2 warnings (0,00 sec)
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0,01 sec)
mysql> select count(*) from bd01.emp where id<>34;
+----------+
| count(*) |
+----------+
| 2097153 |
+----------+
1 row in set (4,92 sec)
mysql> show profiles;
+----------+------------+--------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+--------------------------------------------+
| 1 | 4.93420575 | select count(*) from bd01.emp where id<>34 |
+----------+------------+--------------------------------------------+
1 row in set, 1 warning (0,01 sec)
MySQL – Explain Plan / Profiles
mysql> show profile cpu for query 1;
+----------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| starting | 0.000216 | 0.000000 | 0.000000 |
| checking permissions | 0.000186 | 0.000000 | 0.000000 |
| Opening tables | 0.000100 | 0.000000 | 0.000000 |
| init | 0.000106 | 0.000000 | 0.000000 |
| System lock | 0.001301 | 0.001000 | 0.001000 |
| optimizing | 0.000107 | 0.000000 | 0.000000 |
| statistics | 0.000020 | 0.000000 | 0.000000 |
| preparing | 0.000073 | 0.000000 | 0.000000 |
| executing | 0.000070 | 0.000000 | 0.000000 |
| Sending data | 4.917282 | 4.518313 | 0.117982 |
| end | 0.000674 | 0.000000 | 0.000999 |
| query end | 0.000146 | 0.000000 | 0.000000 |
| closing tables | 0.000156 | 0.000000 | 0.000000 |
| freeing items | 0.013413 | 0.000000 | 0.002000 |
| cleaning up | 0.000359 | 0.000000 | 0.000000 |
+----------------------+----------+----------+------------+
15 rows in set, 1 warning (0,00 sec)
* User CPU Time: Amount of
time the processor worked on
the specific programm.
* System CPU Time: Amount of
time the processor worked on
operating system's functions
connected to that specific
programm.
MySQL – Explain Plan / Profiles
mysql> SET @query_id = 1;
Query OK, 0 rows affected (0,01 sec)
mysql> SELECT state,
-> Sum(duration) AS Total_R,
-> Round(100 * Sum(duration) / (SELECT Sum(duration)
-> FROM information_schema.profiling
-> WHERE query_id = @query_id), 2) AS Pct_R,
-> Count(*) AS Calls,
-> Sum(duration) / Count(*) AS "R/Call"
-> FROM information_schema.profiling
-> WHERE query_id = @query_id
-> GROUP BY state
-> ORDER BY total_r DESC;
+----------------------+----------+-------+-------+--------------+
| state | Total_R | Pct_R | Calls | R/Call |
+----------------------+----------+-------+-------+--------------+
| Sending data | 4.917282 | 99.66 | 1 | 4.9172820000 |
| freeing items | 0.013413 | 0.27 | 1 | 0.0134130000 |
| System lock | 0.001301 | 0.03 | 1 | 0.0013010000 |
| end | 0.000674 | 0.01 | 1 | 0.0006740000 |
| cleaning up | 0.000359 | 0.01 | 1 | 0.0003590000 |
| starting | 0.000216 | 0.00 | 1 | 0.0002160000 |
| checking permissions | 0.000186 | 0.00 | 1 | 0.0001860000 |
| closing tables | 0.000156 | 0.00 | 1 | 0.0001560000 |
| query end | 0.000146 | 0.00 | 1 | 0.0001460000 |
| optimizing | 0.000107 | 0.00 | 1 | 0.0001070000 |
| init | 0.000106 | 0.00 | 1 | 0.0001060000 |
| Opening tables | 0.000100 | 0.00 | 1 | 0.0001000000 |
| preparing | 0.000073 | 0.00 | 1 | 0.0000730000 |
| executing | 0.000070 | 0.00 | 1 | 0.0000700000 |
| statistics | 0.000020 | 0.00 | 1 | 0.0000200000 |
+----------------------+----------+-------+-------+--------------+
15 rows in set, 16 warnings (0,11 sec)
MySQL - SHOW [GLOBAL] STATUS
mysql> SHOW GLOBAL STATUS;
+-----------------------------------------------+---------+
| Variable_name | Value |
+-----------------------------------------------+---------+
| Bytes_received | 2119 |
| Bytes_sent | 5351 |
| Com_select | 234 |
| Com_delete | 4 |
| Com_update | 38 |
| Com_insert | 5 |
| Com_drop_index | 3 |
| Com_drop_table | 5 |
| Com_drop_user | 0 |
| Com_kill | 3 |
| Com_show_processlist | 9 |
| Com_show_tables | 12 |
| Created_tmp_disk_tables | 10 |
| Created_tmp_tables | 77 |
| Innodb_buffer_pool_read_requests | 2293 |
| Innodb_buffer_pool_reads | 1526 |
| Innodb_rows_deleted | 0 |
| Innodb_rows_inserted | 0 |
| Innodb_rows_read | 8 |
| Innodb_rows_updated | 0 |
| Key_read_requests | 6 |
| Key_reads | 3 |
| Key_write_requests | 0 |
| Key_writes | 0 |
| Qcache_free_memory | 1040184 |
| Qcache_hits | 12 |
| Qcache_inserts | 5 |
| Qcache_lowmem_prunes | 3 |
| Connections | 565 |
| Threads_connected | 12 |
| Threads_created | 45 |
| Threads_running | 5 |
| Uptime | 48410 |
+-----------------------------------------------+---------+
• SHOW GLOBAL STATUS will give you status variables that have updated since
mysqld started for all sessions that are connected or have ever been
connected.
• SHOW STATUS will give you status variables that have updated within your
session. The command can also be expressed as SHOW SESSION STATUS.
FLUSH STATUS -> Reset SESSION values
Stop/Start MySQL -> Reset GLOBAL values
• Key_reads - Index blocks read from disk
• Key_read_requests - Index blocks read from cache
• Key_writes - Index blocks written on disk
• Innodb_buffer_pool_reads -Pages read from disk
• Innodb_buffer_pool_read_requests - Pages read from the cache
• Qcache_hits - Queries satisfied from the cache
• QCache_inserts - Query result sets added to the cache
• Qcache_lowmem_prunes - Query results flushed due to lack of memory
• Qcache_free_memory - Bytes free
• Created_tmp_tables - The number of internal temporary tables created by the server while executing
statements.
• Created_tmp_disk_tables - The number of internal on-disk temporary tables created by the server while
executing statements.
• Connection - The number of connection attempts (successful or not) to the MySQL server
• Threads_connected - The number of currently open connections
• Threads_running - The number of threads that are not sleeping
https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html
MySQL – SHOW [GLOBAL] STATUS
mysql> SELECT A.VARIABLE_NAME,A.VARIABLE_VALUE GLOBAL,B.VARIABLE_VALUE SESSION
-> FROM information_schema.global_status A INNER join information_schema.session_status B
-> USING (VARIABLE_NAME) WHERE A.VARIABLE_VALUE <> B.VARIABLE_VALUE;
ERROR 3167 (HY000): The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation for 'show_compatibility_56'
mysql> set GLOBAL show_compatibility_56=ON;
Query OK, 0 rows affected (0,00 sec)
+----------------------------+----------+-----------+
| VARIABLE_NAME | GLOBAL | SESSION |
+----------------------------+----------+-----------+
| BYTES_RECEIVED | 74334 | 456 |
| BYTES_SENT | 1384004 | 1486 |
| COM_CHANGE_DB | 2 | 0 |
| COM_GRANT | 1 | 0 |
| COM_INSERT | 2 | 0 |
| COM_INSERT_SELECT | 42 | 0 |
| COM_SELECT | 102 | 2 |
| COM_SET_OPTION | 156 | 0 |
| COM_SHOW_COLLATIONS | 1 | 0 |
| COM_SHOW_CREATE_TABLE | 562 | 0 |
| COM_SHOW_DATABASES | 5 | 0 |
| COM_SHOW_FIELDS | 2 | 0 |
| COM_SHOW_PROCESSLIST | 8 | 0 |
| COM_SHOW_VARIABLES | 1 | 0 |
| CREATED_TMP_DISK_TABLES | 77 | 0 |
| CREATED_TMP_TABLES | 719 | 4 |
| LAST_QUERY_COST | 0.000000 | 21.650822 |
| LAST_QUERY_PARTIAL_PLANS | 0 | 3 |
| OPENED_TABLE_DEFINITIONS | 221 | 0 |
| OPENED_TABLES | 393 | 0 |
| QUESTIONS | 1158 | 2 |
| SELECT_FULL_JOIN | 17 | 2 |
| SELECT_SCAN | 306 | 2 |
| SORT_ROWS | 593 | 0 |
| SORT_SCAN | 37 | 0 |
+----------------------------+----------+-----------+
MySQL – Query Cache
mysql> show variables like '%query_cache%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
mysql> set GLOBAL query_cache_type=1;
ERROR 1651 (HY000): Query cache is disabled; restart the server with query_cache_type=1 to enable it
• have_query_cache - indicates whether the query cache is available.
• query_cache_type - Whether the query cache is enabled. Possible values are OFF, ON, or DEMAND, where the latter means that only queries containing
the SQL_CACHE modifier are eligible for caching.
• query_cache_size - The total memory to allocate to the query cache, in bytes. Setting it to 0 disables the query cache, as does setting
query_cache_type=0. By default, the query cache is disabled.
• query_cache_min_res_unit - The minimum size when allocating a block.
• query_cache_limit - The largest result set that MySQL will cache. Queries whose results are larger than this setting will not be cached. If the result
exceeds the specified limit, MySQL will increment the Qcache_not_cachedstatus variable and discard the result cached so far.
• query_cache_wlock_invalidate - Whether to serve cached results that refer to tables other connections have locked. This really doesn’t matter for most
applications, so the default is generally fine.
MySQL – Query Cache
[root@linux1 ~]# cat /etc/my.cnf
query_cache_type=1
query_cache_size=250M
query_cache_limit=2M
[root@linux1 ~]# service mysqld restart
Parando o mysqld: [ OK ]
Iniciando o mysqld: [ OK ]
mysql> show variables like '%query_cache%';
+------------------------------+-----------+
| Variable_name | Value |
+------------------------------+-----------+
| have_query_cache | YES |
| query_cache_limit | 2097152 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 262144000 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+-----------+
6 rows in set (0,03 sec)
mysql> SHOW GLOBAL STATUS LIKE "qcache%";
+-------------------------+-----------+
| Variable_name | Value |
+-------------------------+-----------+
| Qcache_free_blocks | 1 | The number of free memory blocks in the query cache.
| Qcache_free_memory | 262135240 | The amount of free memory for the query cache.
| Qcache_hits | 0 | The number of query cache hits.
| Qcache_inserts | 0 | The number of queries added to the query cache.
| Qcache_lowmem_prunes | 0 | The number of queries that were deleted from the query cache because of low memory.
| Qcache_not_cached | 1 | The number of noncached queries (not cacheable).
| Qcache_queries_in_cache | 0 | The number of queries registered in the query cache.
| Qcache_total_blocks | 1 | The total number of blocks in the query cache.
+-------------------------+-----------+
8 rows in set (0,07 sec)
"Be cautious about sizing the query cache excessively large, which increases
the overhead required to maintain the cache, possibly beyond the benefit of
enabling it. Sizes in tens of megabytes are usually beneficial. Sizes in the
hundreds of megabytes might not be."
FLUSH QUERY CACHE - This command compacts the query cache by moving
all blocks "upward" and removing the free space between them, leaving a
single free block at the bottom.
RESET QUERY CACHE - Remove queries from the cache.
https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html
MySQL – Query Cache
Em relação ao parâmetro query_cache_size, o mesmo não estava
habilitado e seu valor foi ajustado de forma a armazenar resultados de
consultas SQL que poderão ser reutilizados por instruções SQL
reincidentes. Ao lado o gráfico que demonstra este buffer armazenando
resultados de cerca de 68 mil instruções SQL ao longo do dia.
Mais baixo a imagem demonstra a eficiência do cache na qual se observa
um Hit de acerto de mais de 40 mil instruções SQL com pico de mais de
100 mil instruções. Neste caso, mais de 40 mil instruções SQL que foram
executadas tiveram seus resultados provenientes do cache em memória ao
invés vez do disco. A coluna Inserts demonstra a quantidades de vezes que
novas instruções foram inseridas no cache, enquanto que a coluna
Lowmem Prunes mostra a quantidade de instruções que precisaram ser
removidas do cache para dar lugar à novas instruções SQL. Quanto menor
for o valor de Lowmem Prunes e maior o valor de Hits, melhor a
eficiência do cache.
MySQL – Other System Variables
innodb_buffer_pool_size: (128M) The size in bytes of the memory buffer innodb uses to cache data and indexes of its tables
key_buffer_size : (8M) Very important if you use MyISAM tables
Find the All MyISAM index Size: SELECT SUM(INDEX_LENGTH)/(1024*1024) 'Index Size'
FROM information_schema.TABLES
where ENGINE='MyISAM' AND TABLE_SCHEMA NOT
IN('mysql','information_schema','performance_schema');
(Index Size)/key_buffer_size(in MB) * 100
if result is (<=100 ) then your all indexes are cached into key_buffer
if result is (>100) then your all indexes are not cached into key_buffer you may gain performance
boost by increasing key_buffer_size.
join_buffer_size : (256K) Increase the value of join_buffer_size to get a faster full join when adding indexes is not
possible.
sort_buffer_size : (256K) Improves large and complex sorts. Each thread that needs to do a sort allocates a buffer of
this size. Increase this value for faster ORDER BY or GROUP BY operations.
tmp_table_size : (16M) If an in-memory temporary table exceeds the limit, MySQL automatically converts it to an on-
disk MyISAM table. Increased value can improve performance for many advanced GROUP BY queries. It’s
possible compare the number of internal on-disk temporary tables created to the total number of
internal temporary tables created by comparing the values of the Created_tmp_disk_tables and
Created_tmp_tables variables.
slow_query_log : (OFF) Whether the slow query log is enabled.
general_log : (OFF) Whether the general query log is enabled.
innodb_flush_log_at_trx_commit: (1) sets the behaviour of syncing on commit
0 - if MySQL crashes, last second of txns lost
1 - flush to disk each update/commit
2 - flush to OS (if OS crashes, 1 sec txns are lost)
MySQL - Resizing the InnoDB Buffer Pool Online
In MySQL 5.7.5, the innodb_buffer_pool_size parameter is now dynamic, allowing you to
resize the buffer pool without restarting the server. The resizing operation, which involves
moving pages to a new location in memory, is performed chunks. Chunk size is configurable
using the new innodb_buffer_pool_chunk_size configuration option. You can monitor
resizing progress using the new innodb_buffer_pool_resize_status status variable.
SHOW GLOBAL STATUS LIKE 'innodb_buffer_pool_resize_status'
Variable_name Value
-------------------------------- --------------------------------------------------
Innodb_buffer_pool_resize_status Completed resizing buffer pool at 170522 13:15:13.
MySQL – innodb_flush_log_at_trx_commit
CREATE TABLE t1 (cod INT, ascii_char CHAR(10)) ENGINE=INNODB;
DELIMITER $$
CREATE PROCEDURE insert_loop(IN p_limit INT)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i<=p_limit) DO
INSERT INTO t1 VALUES(i,CHAR(i));
SET i=i+1;
END WHILE;
END$$
DELIMITER ;
use bd01
CALL insert_loop(100000);
SHOW GLOBAL VARIABLES LIKE 'innodb_flush_log_at_trx_commit';
SET GLOBAL innodb_flush_log_at_trx_commit=?; (? = 1,0,2)
-- INSERT LOOP 100.000 registros InnoDB
1-Execution Time : 2 min 54.18 sec
0-Execution Time : 5.877 sec
2-Execution Time : 7.538 sec
-- INSERT LOOP 100.000 registros MyISAM
1-Execution Time : 4.595 sec
0-Execution Time : 4.566 sec
2-Execution Time : 4.562 sec
-- INSERT LOOP 1.000.000 registros InnoDB
1-Execution Time : 29 min 12.29 sec
0-Execution Time : 57.391 sec
2-Execution Time : 59.564 sec
-- INSERT LOOP 1.000.000 registros MyISAM
1-Execution Time : 45.089 sec
0-Execution Time : 44.437 sec
2-Execution Time : 44.608 sec
MySQL – MySQL Workbench
https://www.mysql.com/products/workbench/
Design
MySQL Workbench enables a DBA, developer, or data architect to visually design, model, generate, and manage
databases. It includes everything a data modeler needs for creating complex ER models, forward and reverse
engineering, and also delivers key features for performing difficult change management and documentation tasks
that normally require much time and effort.
Develop
MySQL Workbench delivers visual tools for creating, executing, and optimizing SQL queries. The SQL Editor provides
color syntax highlighting, auto-complete, reuse of SQL snippets, and execution history of SQL. The Database
Connections Panel enables developers to easily manage standard database connections, including MySQL Fabric.
The Object Browser provides instant access to database schema and objects.
Administer
MySQL Workbench provides a visual console to easily administer MySQL environments and gain better visibility
into databases. Developers and DBAs can use the visual tools for configuring servers, administering users,
performing backup and recovery, inspecting audit data, and viewing database health.
Visual Performance Dashboard
MySQL Workbench provides a suite of tools to improve the performance of MySQL applications. DBAs can quickly
view key performance indicators using the Performance Dashboard. Performance Reports provide easy
identification and access to IO hotspots, high cost SQL statements, and more. Plus, with 1 click, developers can see
where to optimize their query with the improved and easy to use Visual Explain Plan.
Database Migration
MySQL Workbench now provides a complete, easy to use solution for migrating Microsoft SQL Server, Microsoft
Access, Sybase ASE, PostreSQL, and other RDBMS tables, objects and data to MySQL. Developers and DBAs can
quickly and easily convert existing applications to run on MySQL both on Windows and other platforms. Migration
also supports migrating from earlier versions of MySQL to the latest releases.
MySQL – MySQL Workbench
MySQL – MySQL Workbench (Performance Schema)
mysql> show variables like 'performance_schema';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| performance_schema | OFF |
+--------------------+-------+
Parametro estático -> my.cnf
mysql> show variables like 'performance_schema';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| performance_schema | ON |
+--------------------+-------+
A partir da versão 5.7 o padrão é ON
MySQL – Performance Schema
• Top File I/O Activity Report: Show the files performing the most IOs (in bytes)
• Top I/O by File by Time: Show the highest IO usage by file and latency
• Top I/O by Event Category: Show the highest IO Data usage by event categories
• Top I/O in Time by Event Categories: Show the highest IO time consumers by event categories
• Top I/O Time by User/Thread: Show the top IO time consumers by user/thread
• Statement Analysis: Lists statements with aggregated statistics
• Statements in Highest 5 Percent by Runtime: Lists the top 5% statements with the highest runtime (in microseconds)
• Using Temp Tables: Lists all statements that use temporary tables -- accesses the highest number of disk temporary tables, then memory temp tables
• With Sorting: List all normalized statements that have done sorts, and accesses them in the following priority order -- sort_merge_passes, sort_scans,
then sort_rows
• Full Table Scans: Lists statements that performed a full table scan. Accesses query performance and the where clause(s), and if no index is used then it
• recommends adding indexes for large tables
• Errors or Warnings: Lists statements that have raised errors or warnings
• Schema Object Overview (High Overhead): Shows counts by object type for each schema
• Schema Index Statistics
• Schema Table Statistics
• Schema Table Statistics (with InnoDB buffer)
• Tables with Full Table Scans: Finds tables that are being accessed by full table scans, ordering them by the number of rows scanned (DESC)
• Unused Indexes: List of indexes that were never used since the server started or since P_S data collection started
• Waits by Time: Lists the top wait events by their total time, ignoring idle (this often contain large values)
• Waits by User by Time: Lists the top wait events by their total time, ignoring idle (this often contain large values)
• Wait Classes by Time: Lists the top wait classes by total time, ignoring idle (this often contain large values)
• Waits Classes by Average Time: Lists the top wait classes by average time, ignoring idle (this often contain large values)
• InnoDB Buffer Stats by Schema: Summarizes the output of the INFORMATION_SCHEMA.INNODB_BUFFER_PAGE table, aggregating it by schema
• InnoDB Buffer Stats by Table: Summarizes the output of the INFORMATION_SCHEMA.INNODB_BUFFER_PAGE table, aggregating it by schema and table
name
MySQL – Performance Schema (overhead)
• PERFORMANCE SCHEMA disabled (NO PS)
• PERFORMANCE SCHEMA enabled, with all consumers ON (PS on)
• PERFORMANCE SCHEMA enabled, but only global_instrumentation consumer enabled. (PS only global)
• PERFORMANCE SCHEMA enabled, but all consumers OFF (PS all off)
MySQL – SYS Schema in MySQL 5.7.7
Who is taking up all the resources on my database server?
mysql> show tables like 'user%';
+-----------------------------------+
| Tables_in_sys (user%) |
+-----------------------------------+
| user_summary |
| user_summary_by_file_io |
| user_summary_by_file_io_type |
| user_summary_by_stages |
| user_summary_by_statement_latency |
| user_summary_by_statement_type |
+-----------------------------------+
6 rows in set (0.00 sec)
mysql> select * from user_summary_by_file_io_type where user = 'user01';
+----------+------------------------------------+-------+-----------+-------------+
| user | event_name | total | latency | max_latency |
+----------+------------------------------------+-------+-----------+-------------+
| user01 | wait/io/file/sql/FRM | 36929 | 21.95 s | 239.56 ms |
| user01 | wait/io/file/sql/file_parser | 1266 | 4.44 s | 250.93 ms |
| user01 | wait/io/file/innodb/innodb_data_file | 1086 | 1.35 s | 647.00 ms |
| user01 | wait/io/file/csv/metadata | 124 | 1.15 s | 192.03 ms |
| user01 | wait/io/file/myisam/kfile | 4376 | 221.53 ms | 23.71 ms |
| user01 | wait/io/file/innodb/innodb_log_file | 274 | 68.37 ms | 14.65 ms |
| user01 | wait/io/file/myisam/dfile | 3544 | 56.23 ms | 6.66 ms |
| user01 | wait/io/file/csv/data | 56 | 9.90 ms | 1.91 ms |
| user01 | wait/io/file/sql/trigger | 2 | 3.86 ms | 3.77 ms |
| user01 | wait/io/file/archive/data | 349 | 2.58 ms | 27.89 us |
| user01 | wait/io/file/sql/dbopt | 14 | 601.64 us | 117.95 us |
| user01 | wait/io/file/sql/misc | 13 | 540.14 us | 69.84 us |
+----------+------------------------------------+-------+-----------+-------------+
12 rows in set (0.01 sec)
mysql> select * from sys.user_summary_by_file_io;
+------------+-------+------------+
| user | ios | io_latency |
+------------+-------+------------+
| root | 19514 | 2.87 s |
| background | 5916 | 1.91 s |
+------------+-------+------------+
2 rows in set (0.00 sec)
mysql> select * from sys.x$user_summary_by_file_io;
+------------+-------+---------------+
| user | ios | io_latency |
+------------+-------+---------------+
| root | 19514 | 2871847094292 |
| background | 5916 | 1905079715132 |
+------------+-------+---------------+
MySQL – SYS Schema in MySQL 5.7.7
Which hosts are hitting my database server those most?
mysql> show tables like 'host%';
+-----------------------------------+
| Tables_in_sys (host%) |
+-----------------------------------+
| host_summary |
| host_summary_by_file_io |
| host_summary_by_file_io_type |
| host_summary_by_stages |
| host_summary_by_statement_latency |
| host_summary_by_statement_type |
+-----------------------------------+
6 rows in set (0.04 sec)
MySQL – SYS Schema in MySQL 5.7.7
Which objects are accessed the most, and how?
mysql> select * from sys.schema_table_statistics limit 1G
*************************** 1. row ***************************
table_schema: bd01
table_name: customer
total_latency: 1.01 m
rows_fetched: 8171
fetch_latency: 321.97 ms
rows_inserted: 9524
insert_latency: 58.24 s
rows_updated: 8171
update_latency: 2.16 s
rows_deleted: 0
delete_latency: 0 ps
io_read_requests: 37
io_read: 57.82 KiB
io_read_latency: 86.55 ms
io_write_requests: 354
io_write: 3.99 MiB
io_write_latency: 2.40 ms
io_misc_requests: 140
io_misc_latency: 500.78 ms
MySQL – SYS Schema in MySQL 5.7.7
What statements have the highest overall latency, and what statistics did they have?
mysql> select * from statement_analysis limit 1G
*************************** 1. row ***************************
query: SELECT `t` . `THREAD_ID` AS `t ... _NUMBER_OF_BYTES_USED` ) DESC
db: NULL
full_scan: *
exec_count: 20835
err_count: 4
warn_count: 0
total_latency: 38.96 m
max_latency: 1.84 s
avg_latency: 112.21 ms
lock_latency: 33.91 s
rows_sent: 844
rows_sent_avg: 0
rows_examined: 216255376
rows_examined_avg: 10379
rows_affected: 0
rows_affected_avg: 0
tmp_tables: 83340
tmp_disk_tables: 41666
rows_sorted: 1419984
sort_merge_passes: 0
digest: 2a8440817e00ec634e3d0b88c8e85ca4
first_seen: 2017-05-02 10:23:49
last_seen: 2017-05-02 11:11:49
MySQL – SYS Schema in MySQL 5.7.7
Which statements are using temporary tables on disk?
mysql> select * from statements_with_temp_tables limit 2G;
*************************** 1. row ***************************
query: SELECT `r` . `trx_wait_started ... ode` , `b` . `trx_started` AS
db: NULL
exec_count: 478707
total_latency: 13.43 m
memory_tmp_tables: 3350949
disk_tmp_tables: 957414
avg_tmp_tables_per_query: 7
tmp_tables_to_disk_pct: 29
first_seen: 2017-05-02 09:49:18
last_seen: 2017-05-02 13:12:36
digest: 529f34e4046a3f19b7023aca37d6a394
*************************** 2. row ***************************
query: SELECT `t` . `THREAD_ID` AS `t ... _NUMBER_OF_BYTES_USED` ) DESC
db: NULL
exec_count: 20835
total_latency: 38.96 m
memory_tmp_tables: 83340
disk_tmp_tables: 41666
avg_tmp_tables_per_query: 4
tmp_tables_to_disk_pct: 50
first_seen: 2017-05-02 10:23:49
last_seen: 2017-05-02 11:11:49
digest: 2a8440817e00ec634e3d0b88c8e85ca4
MySQL – SYS Schema in MySQL 5.7.7
Where is all the memory going on my instance?
mysql> select * from memory_global_by_current_bytes limit 10;
+---------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
| event_name | current_count | current_alloc | current_avg_alloc | high_count | high_alloc | high_avg_alloc |
+---------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
| memory/innodb/buf_buf_pool | 1 | 131.06 MiB | 131.06 MiB | 1 | 131.06 MiB | 131.06 MiB |
| memory/innodb/mem0mem | 12476 | 17.17 MiB | 1.41 KiB | 13197 | 32.27 MiB | 2.50 KiB |
| memory/innodb/log0log | 9 | 16.01 MiB | 1.78 MiB | 9 | 16.01 MiB | 1.78 MiB |
| memory/innodb/lock0lock | 9279 | 14.41 MiB | 1.59 KiB | 9279 | 14.41 MiB | 1.59 KiB |
| memory/performance_schema/events_s_h_long | 1 | 13.50 MiB | 13.50 MiB | 1 | 13.50 MiB | 13.50 MiB |
| memory/performance_schema/events_s_sry_by_digest | 1 | 9.77 MiB | 9.77 MiB | 1 | 9.77 MiB | 9.77 MiB |
| memory/performance_schema/events_s_h_long.sqltext | 1 | 9.77 MiB | 9.77 MiB | 1 | 9.77 MiB | 9.77 MiB |
| memory/performance_schema/events_s_h_long.tokens | 1 | 9.77 MiB | 9.77 MiB | 1 | 9.77 MiB | 9.77 MiB |
| memory/performance_schema/table_handles | 1 | 9.00 MiB | 9.00 MiB | 1 | 9.00 MiB | 9.00 MiB |
| memory/performance_schema/events_sta_name | 1 | 8.58 MiB | 8.58 MiB | 1 | 8.58 MiB | 8.58 MiB |
+---------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
MySQL – SYS Schema in MySQL 5.7.7
Which tables take the most space in my InnoDB buffer pool?
mysql> select * from innodb_buffer_stats_by_table limit 10;
+-------------------+----------------------------------------------------+------------+------------+-------+--------------+-----------+-------------+
| object_schema | object_name | allocated | data | pages | pages_hashed | pages_old | rows_cached |
+-------------------+----------------------------------------------------+------------+------------+-------+--------------+-----------+-------------+
| mem__quan | normalized_statements_by_server_by_schema_data | 8.39 MiB | 10.87 MiB | 1074 | 1074 | 1074 | 36995 |
| InnoDB System | SYS_TABLES | 1.70 MiB | 1.43 MiB | 109 | 109 | 109 | 4656 |
| mem__advisor_text | localized_text | 1.45 MiB | 1.11 MiB | 93 | 93 | 93 | 1514 |
| mysql | innodb_index_stats | 784.00 KiB | 380.36 KiB | 49 | 49 | 49 | 3182 |
| mem__instruments | globaleventwaitsfilesummaryadvisor_fileioeventdata | 600.00 KiB | 792.25 KiB | 75 | 75 | 75 | 2265 |
| mem__quan | example_statements | 560.00 KiB | 550.59 KiB | 70 | 70 | 70 | 1414 |
| mem__bean_config | serialized_states | 480.00 KiB | 190.83 KiB | 30 | 30 | 30 | 481 |
| mem__events | events | 320.00 KiB | 216.23 KiB | 20 | 20 | 20 | 169 |
| InnoDB System | SYS_COLUMNS | 288.00 KiB | 203.33 KiB | 18 | 18 | 18 | 3055 |
| mem__quan | normalized_statements | 288.00 KiB | 355.21 KiB | 36 | 36 | 36 | 716 |
+-------------------+----------------------------------------------------+------------+------------+-------+--------------+-----------+-------------+
10 rows in set (0.22 sec)
MySQL – SYS Schema in MySQL 5.7.7
Which database files cause the most IO, and what kind of IO pattern do they have?
mysql> select * from io_global_by_file_by_latency limit 10;
+----------------------------------------------+---------+---------------+------------+--------------+-------------+---------------+
| file | total | total_latency | count_read | read_latency | count_write | write_latency |
+----------------------------------------------+---------+---------------+------------+--------------+-------------+---------------+
| @@datadir/ib_logfile0 | 70001 | 57.67 s | 4 | 12.47 us | 35507 | 3.59 s |
| @@datadir/ibdata1 | 37512 | 37.15 s | 715 | 1.14 s | 32690 | 623.45 ms |
| @@datadir/ib_logfile1 | 46994 | 24.88 s | 2 | 18.75 ms | 23495 | 399.44 ms |
| @@datadir/mysql/proc.MYD | 3072639 | 14.14 s | 2048438 | 12.02 s | 7 | 217.13 us |
| @@datadir/mem__quan/data.ibd | 3134 | 2.59 s | 7 | 31.52 ms | 2793 | 397.90 ms |
| @@datadir/mem__instruments/globaldata.ibd | 778 | 1.50 s | 10 | 10.37 ms | 629 | 272.66 ms |
| @@datadir/mem__instruments/network.ibd | 424 | 1.30 s | 15 | 1.09 ms | 337 | 351.11 ms |
| @@datadir/mem__instruments/fsstatistics.ibd | 397 | 1.21 s | 9 | 11.39 ms | 328 | 44.60 ms |
| @@datadir/mem__events/events.ibd | 1518 | 1.15 s | 15 | 6.11 ms | 1292 | 11.03 ms |
| @@datadir/mem__inventory/httpserver.ibd | 158 | 588.31 ms | 6 | 906.05 us | 98 | 650.16 us |
+----------------------------------------------+---------+---------------+------------+--------------+-------------+---------------+
MySQL – SYS Schema in MySQL 5.7.7
Where is the time being spent the most within my instance?
mysql> select * from waits_global_by_latency limit 20;
+----------------------------------------------+----------+---------------+-------------+-------------+
| events | total | total_latency | avg_latency | max_latency |
+----------------------------------------------+----------+---------------+-------------+-------------+
| wait/io/table/sql/handler | 651645 | 7.54 m | 694.20 us | 2.31 s |
| wait/io/socket/sql/client_connection | 8171190 | 3.74 m | 27.44 us | 215.29 ms |
| wait/synch/sxlock/innodb/dict_operation_lock | 270734 | 1.60 m | 354.35 us | 1.21 s |
| wait/io/file/innodb/innodb_log_file | 118289 | 1.40 m | 707.62 us | 820.19 ms |
| wait/io/file/innodb/innodb_data_file | 82271 | 1.19 m | 869.42 us | 2.98 s |
| wait/synch/mutex/innodb/log_sys_mutex | 4154064 | 37.93 s | 9.13 us | 238.31 ms |
| wait/synch/mutex/sql/LOCK_table_cache | 4239803 | 35.55 s | 8.38 us | 13.47 ms |
| wait/synch/sxlock/innodb/index_tree_rw_lock | 2447691 | 27.68 s | 11.31 us | 228.94 ms |
| wait/synch/mutex/innodb/buf_pool_mutex | 3465947 | 18.26 s | 5.27 us | 2.31 s |
| wait/synch/mutex/innodb/dict_sys_mutex | 1104503 | 15.95 s | 14.44 us | 1.18 s |
| wait/io/file/myisam/dfile | 3185357 | 14.69 s | 4.61 us | 130.09 ms |
| wait/io/file/sql/FRM | 26965 | 13.40 s | 497.05 us | 96.99 ms |
| wait/synch/mutex/innodb/trx_sys_mutex | 5830470 | 10.92 s | 1.87 us | 83.22 ms |
| wait/synch/mutex/innodb/lock_mutex | 1015659 | 6.13 s | 6.03 us | 81.22 ms |
| wait/synch/sxlock/innodb/checkpoint_lock | 2036 | 5.19 s | 2.55 ms | 355.62 ms |
| wait/synch/sxlock/innodb/fil_space_latch | 7061817 | 4.85 s | 686.52 ns | 528.87 ms |
| wait/synch/mutex/sql/THD::LOCK_thd_data | 37813109 | 4.08 s | 107.82 ns | 29.66 ms |
| wait/synch/sxlock/innodb/btr_search_latch | 451144 | 3.78 s | 8.39 us | 27.38 ms |
| wait/synch/mutex/innodb/fil_system_mutex | 12971063 | 2.79 s | 215.27 ns | 32.92 ms |
| wait/io/socket/sql/server_unix_socket | 275681 | 2.03 s | 7.35 us | 7.39 ms |
+----------------------------------------------+----------+---------------+-------------+-------------+
MySQL – SYS Schema in MySQL 5.7.7
Oracle´s V$SESSION?
mysql> SELECT conn_id,user,db,command,state,time,current_statement,program_name,pid,last_statement_latency,last_wait FROM sys.session;
+---------+-------------------+------+---------+--------------+------+----------------------+----------------+------+------------------------+-----------+
| conn_id | user | db | command | state | time | current_statement | program_name | pid | last_statement_latency | last_wait |
+---------+-------------------+------+---------+--------------+------+----------------------+----------------+------+------------------------+-----------+
| 17 | root@192.168.56.1 | NULL | Sleep | NULL | 130 | NULL | NULL | 3524 | 68.41 ms | NULL |
| 12 | root@192.168.56.1 | sys | Sleep | NULL | 121 | NULL | MySQLWorkbench | 6928 | 203.05 us | NULL |
| 13 | root@192.168.56.1 | NULL | Sleep | NULL | 121 | NULL | MySQLWorkbench | 6928 | 122.16 us | NULL |
| 16 | root@192.168.56.1 | sys | Sleep | NULL | 4 | NULL | NULL | 3524 | 2.55 ms | NULL |
| 15 | root@192.168.56.1 | NULL | Sleep | NULL | 1 | NULL | MySQLWorkbench | 6928 | 2.56 ms | NULL |
| 14 | root@192.168.56.1 | sys | Sleep | NULL | 0 | NULL | MySQLWorkbench | 6928 | 888.02 us | NULL |
| 19 | root@localhost | NULL | Query | Sending data | 0 | SELECT conn_id,USER, | mysql | 4961 | NULL | NULL |
+---------+-------------------+------+---------+--------------+------+----------------------+----------------+------+------------------------+-----------+
7 rows in set (0.10 sec)
MySQL – Jet Profiler
https://www.jetprofiler.com/download/
MySQL Enterprise Monitor (free for a 30 day trial)
MonYog (14-day evaluation)
MySQL – mysqltuner.pl
http://mysqltuner.pl/
[root@linux1 ~]# wget http://mysqltuner.pl/ -O mysqltuner.pl --no-check-certificate
Resolvendo mysqltuner.pl... 217.70.184.38
Conectando-se a mysqltuner.pl|217.70.184.38|:80... conectado.
Localização: https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl [redirecionando]
--2017-05-02 14:56:44-- https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl
Resolvendo raw.githubusercontent.com... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...
Conectando-se a raw.githubusercontent.com|151.101.0.133|:443... conectado.
A requisição HTTP foi enviada, aguardando resposta... 200 OK
Tamanho: 213310 (208K) [text/plain]
Salvando em: “mysqltuner.pl”
100%[============================================================================>] 213.310 --.-K/s em 0,1s
[root@linux1 ~]# chmod 775 mysqltuner.pl
[root@linux1 ~]# ls -lh mysqltuner.pl
-rwxrwxr-x 1 root root 209K Abr 26 14:56 mysqltuner.pl
>> MySQLTuner 1.7.1 - Major Hayden <major@mhtx.net>
>> Bug reports, feature requests, and downloads at http://mysqltuner.com/
>> Run with '--help' for additional options and output filtering
Pré-requisito: yum install perl-Data-Dumper.x86_64 -y
MySQL – mysqltuner.pl
[root@linux1 ~]# perl mysqltuner.pl –-host localhost --user root --pass admin --port 3306 --forcemem 4096
>> MySQLTuner 1.7.1 - Major Hayden <major@mhtx.net>
>> Bug reports, feature requests, and downloads at http://mysqltuner.com/
>> Run with '--help' for additional options and output filtering
[--] Skipped version check for MySQLTuner script
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password: [OK] Currently running supported MySQL version 5.7.18
[OK] Operating on 64-bit architecture
-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/log/mysqld.log(294K)
[OK] Log file /var/log/mysqld.log exists
[OK] Log file /var/log/mysqld.log is readable.
[OK] Log file /var/log/mysqld.log is not empty
[OK] Log file /var/log/mysqld.log is smaller than 32 Mb
[!!] /var/log/mysqld.log contains 235 warning(s).
[!!] /var/log/mysqld.log contains 11 error(s).
[--] 34 start(s) detected in /var/log/mysqld.log
[--] 1) 2017-05-02T17:49:31.850245Z 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 10) 2017-05-02T18:59:27.934180Z 0 [Note] /usr/sbin/mysqld: ready for connections.
[--] 33 shutdown(s) detected in /var/log/mysqld.log
[--] 1) 2017-05-02T20:35:24.502292Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 10) 2017-05-02T18:59:23.651679Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA
[--] Data in MyISAM tables: 28M (Tables: 1)
[--] Data in InnoDB tables: 32K (Tables: 2)
[OK] Total fragmented tables: 0
-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[!!] User 'root@%' hasn't specific host restriction.
[!!] There is no basic password file list!
Análise em servidor remoto:
--forcemem <size> Amount of
RAM installed in megabytes
MySQL – mysqltuner.pl
-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 25m 23s (199 q [0.131 qps], 75 conn, TX: 331K, RX: 20K)
[--] Reads / Writes: 100% / 0%
[--] Binary logging is disabled
[--] Physical Memory : 1006.1M
[--] Max MySQL memory : 578.4M
[--] Other process memory: 34.3M
[--] Total buffers: 418.0M global + 1.1M per thread (151 max threads)
[--] P_S Max memory usage: 36B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 419.1M (41.65% of installed RAM)
[OK] Maximum possible memory usage: 578.4M (57.49% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory available
[OK] Slow queries: 0% (0/199)
[OK] Highest usage of available connections: 0% (1/151)
[!!] Aborted connections: 5.33% (4/75)
[!!] Query cache may be disabled by default due to mutex contention.
[!!] Query cache efficiency: 0.0% (0 cached / 95 selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 4 sorts)
[OK] No joins without indexes
[OK] Temporary tables created on disk: 4% (82 on disk / 1K total)
[OK] Thread cache hit rate: 98% (1 created / 75 connections)
[OK] Table cache hit rate: 68% (529 open / 776 opened)
[OK] Open file limit used: 0% (49/5K)
[OK] Table locks acquired immediately: 100% (198 immediate / 198 locks)
-------- Performance schema ------------------------------------------------------------------------
[--] Memory used by P_S: 36B
[--] Sys schema is installed.
-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is disabled.
-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 11.6% (973K used / 8M cache)
[OK] Key buffer size / total MyISAM indexes: 8.0M/44.0K
[OK] Read Key buffer hit rate: 96.2% (186 cached / 7 reads)
MySQL – mysqltuner.pl
-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 128.0M/32.0K
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (75 %): 48.0M * 2/128.0M should be equal 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[OK] InnoDB Read buffer efficiency: 92.84% (3032 hits/ 3266 total)
[!!] InnoDB Write Log efficiency: 0% (2 hits/ 0 total)
[OK] InnoDB log waits: 0.00% (0 waits / 2 writes)
-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.
-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.
-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] This is a standalone server.
-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
Control warning line(s) into /var/log/mysqld.log file
Control error line(s) into /var/log/mysqld.log file
Restrict Host for user@% to user@SpecificDNSorIp
MySQL started within last 24 hours - recommendations may be inaccurate
Reduce or eliminate unclosed connections and network issues
Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
Variables to adjust:
query_cache_size (=0)
query_cache_type (=0)
query_cache_limit (> 2M, or use smaller result sets)
innodb_log_file_size * innodb_log_files_in_group should be equals to 1/4 of buffer pool size (=64M) if possible.
MySQL in the CLOUD
MySQL – Amazon RDS
MySQL – Amazon RDS – Multi-AZ Deployments
MySQL – Amazon RDS – Multi-AZ Deployments
MySQL – Amazon RDS
MySQL – Amazon RDS
MySQL – Amazon RDS
MySQL – Amazon RDS
MySQL – Amazon RDS
MySQL – Amazon RDS
MySQL – Amazon RDS
MySQL – Amazon RDS
MySQL – Amazon RDS
MySQL – The End
THE END

More Related Content

PDF
ProxySQL High Availability (Clustering)
Mydbops
 
PDF
MySQL Timeout Variables Explained
Mydbops
 
PDF
New awesome features in MySQL 5.7
Zhaoyang Wang
 
PDF
Troubleshooting Complex Oracle Performance Problems with Tanel Poder
Tanel Poder
 
PDF
Open Source 101 2022 - MySQL Indexes and Histograms
Frederic Descamps
 
PDF
MySQL High Availability -- InnoDB Clusters
Matt Lord
 
PDF
EM12c: Capacity Planning with OEM Metrics
Maaz Anjum
 
PPSX
Oracle Performance Tools of the Trade
Carlos Sierra
 
ProxySQL High Availability (Clustering)
Mydbops
 
MySQL Timeout Variables Explained
Mydbops
 
New awesome features in MySQL 5.7
Zhaoyang Wang
 
Troubleshooting Complex Oracle Performance Problems with Tanel Poder
Tanel Poder
 
Open Source 101 2022 - MySQL Indexes and Histograms
Frederic Descamps
 
MySQL High Availability -- InnoDB Clusters
Matt Lord
 
EM12c: Capacity Planning with OEM Metrics
Maaz Anjum
 
Oracle Performance Tools of the Trade
Carlos Sierra
 

What's hot (20)

PDF
mysql 8.0 architecture and enhancement
lalit choudhary
 
PDF
Tanel Poder - Scripts and Tools short
Tanel Poder
 
PDF
監査ログをもっと身近に!〜統合監査のすすめ〜
Michitoshi Yoshida
 
PPT
Sga internals
sergkosko
 
PDF
Upgrade to MySQL 8.0!
Ted Wennmark
 
PDF
MySQLとPostgreSQLの基本的なレプリケーション設定比較
Shinya Sugiyama
 
PDF
Oracle_Multitenant_19c_-_All_About_Pluggable_D.pdf
SrirakshaSrinivasan2
 
PDF
Analyzing and Interpreting AWR
pasalapudi
 
PDF
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder
 
PDF
Oracle Latch and Mutex Contention Troubleshooting
Tanel Poder
 
PDF
Oracle Performance Tuning Fundamentals
Enkitec
 
PDF
MariaDB 제품 소개
NeoClova
 
PDF
Oracle Database SQL Tuning Concept
Chien Chung Shen
 
PDF
Upgrade from MySQL 5.7 to MySQL 8.0
Olivier DASINI
 
PDF
Oracle Flex ASM - What’s New and Best Practices by Jim Williams
Markus Michalewicz
 
PDF
My First 100 days with an Exadata (PPT)
Gustavo Rene Antunez
 
PPTX
SELinuxの課題について
Atsushi Mitsu
 
PPTX
Oracle ACFS High Availability NFS Services (HANFS)
Anju Garg
 
PDF
MySQL Advanced Administrator 2021 - 네오클로바
NeoClova
 
PDF
[오픈소스컨설팅]Session Clustering
Ji-Woong Choi
 
mysql 8.0 architecture and enhancement
lalit choudhary
 
Tanel Poder - Scripts and Tools short
Tanel Poder
 
監査ログをもっと身近に!〜統合監査のすすめ〜
Michitoshi Yoshida
 
Sga internals
sergkosko
 
Upgrade to MySQL 8.0!
Ted Wennmark
 
MySQLとPostgreSQLの基本的なレプリケーション設定比較
Shinya Sugiyama
 
Oracle_Multitenant_19c_-_All_About_Pluggable_D.pdf
SrirakshaSrinivasan2
 
Analyzing and Interpreting AWR
pasalapudi
 
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder
 
Oracle Latch and Mutex Contention Troubleshooting
Tanel Poder
 
Oracle Performance Tuning Fundamentals
Enkitec
 
MariaDB 제품 소개
NeoClova
 
Oracle Database SQL Tuning Concept
Chien Chung Shen
 
Upgrade from MySQL 5.7 to MySQL 8.0
Olivier DASINI
 
Oracle Flex ASM - What’s New and Best Practices by Jim Williams
Markus Michalewicz
 
My First 100 days with an Exadata (PPT)
Gustavo Rene Antunez
 
SELinuxの課題について
Atsushi Mitsu
 
Oracle ACFS High Availability NFS Services (HANFS)
Anju Garg
 
MySQL Advanced Administrator 2021 - 네오클로바
NeoClova
 
[오픈소스컨설팅]Session Clustering
Ji-Woong Choi
 
Ad

Similar to Curso de MySQL 5.7 (20)

PPTX
MySQL DBA OCP 1Z0-883
Kwaye Kant
 
PPT
Fudcon talk.ppt
webhostingguy
 
PDF
Mysql nowwhat
sqlhjalp
 
PPT
Mysql ppt
Sanmuga Nathan
 
PDF
My sql 5.6&MySQL Cluster 7.3
Oleksii(Alexey) Porytskyi
 
PDF
MySQL update SCaLE 2012
Dave Stokes
 
PPT
MySQL Strategy&Roadmap
slidethanks
 
PPT
My sqlstrategyroadmap
slidethanks
 
DOCX
Upgrading mysql version 5.5.30 to 5.6.10
Vasudeva Rao
 
PDF
Seminar : &quot;The Future of MySQL - Roadmap to Success&quot; session MySQL ...
Software Park Thailand
 
PDF
MySQL Backup and Security Best Practices
Lenz Grimmer
 
PDF
Posscon my sql56
Dave Stokes
 
PDF
Mysql tutorial 5257
Phuong Do Anh
 
PDF
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
Aurimas Mikalauskas
 
PPT
Mysqlppt3510
Anuja Lad
 
PPT
Mysqlppt3510
Khan Rahimeen
 
PDF
Percona Live 2019 - MySQL Security
Vinicius M Grippa
 
PPT
Mysql
Mindtree
 
PDF
20111121 osi keynote
OpenSourceIndia
 
PDF
Instalar MySQL CentOS
Moisés Elías Araya
 
MySQL DBA OCP 1Z0-883
Kwaye Kant
 
Fudcon talk.ppt
webhostingguy
 
Mysql nowwhat
sqlhjalp
 
Mysql ppt
Sanmuga Nathan
 
My sql 5.6&MySQL Cluster 7.3
Oleksii(Alexey) Porytskyi
 
MySQL update SCaLE 2012
Dave Stokes
 
MySQL Strategy&Roadmap
slidethanks
 
My sqlstrategyroadmap
slidethanks
 
Upgrading mysql version 5.5.30 to 5.6.10
Vasudeva Rao
 
Seminar : &quot;The Future of MySQL - Roadmap to Success&quot; session MySQL ...
Software Park Thailand
 
MySQL Backup and Security Best Practices
Lenz Grimmer
 
Posscon my sql56
Dave Stokes
 
Mysql tutorial 5257
Phuong Do Anh
 
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
Aurimas Mikalauskas
 
Mysqlppt3510
Anuja Lad
 
Mysqlppt3510
Khan Rahimeen
 
Percona Live 2019 - MySQL Security
Vinicius M Grippa
 
Mysql
Mindtree
 
20111121 osi keynote
OpenSourceIndia
 
Instalar MySQL CentOS
Moisés Elías Araya
 
Ad

Recently uploaded (20)

PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Doc9.....................................
SofiaCollazos
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 

Curso de MySQL 5.7

  • 2. Agenda • History • Download / Install • Configuration • Users / Privileges • Instance Structures / Engines • InnoDB vs. MyISAM • Transaction Isolation Level • Locks • Binary Log • CSV Files • Exporting / Importing (Logical backup) • COLD / HOT Backup (Physical backup) • Events (Jobs) • Performance Tuning • MySQL in the CLOUD
  • 4. O MySQL foi criado na Suécia por suecos e um finlandês: David Axmark, Allan Larsson e Michael "Monty" Widenius, que têm trabalhado juntos desde a década de 1980. Hoje seu desenvolvimento e manutenção empregam aproximadamente 400 profissionais no mundo inteiro, e mais de mil contribuem testando o software, integrando-o a outros produtos, e escrevendo a respeito dele. No dia 16 de Janeiro de 2008, a MySQL AB, desenvolvedora do MySQL foi adquirida pela Sun Microsystems, por US$ 1 bilhão, um preço jamais visto no setor de licenças livres. No dia 20 de Abril de 2009 foi anunciado que a Oracle compraria a Sun Microsystems e todos os seus produtos, incluindo o MySQL. Isso aconteceu após as negociações da Sun com a IBM terem fracassado. A Oracle não perdeu tempo e ofereceu US$ 7,4 bilhões pela Sun Microsystems que aceitou a proposta. O Conselho de Administração da Sun Microsystems aprovou a transação por unanimidade. MySQL - History
  • 6. MySQL - History mysql> select @@version; +------------+ | @@version | +------------+ | 5.0.88 | +------------+ mysql> create table customer (name varchar(5)); Query OK, 0 rows affected (0,11 sec) mysql> insert into customer (name) values ('123456789'); 1 row(s) affected, 1 warning(s) Warning Code : 1265 Data truncated for column 'name' at row 1 mysql> select * from customer; +-------+ | name | +-------+ | 12345 | +-------+ mysql> select @@version; +------------+ | @@version | +------------+ | 5.7.18-log | +------------+ mysql> create table customer (name varchar(5)); Query OK, 0 rows affected (0,11 sec) mysql> insert into customer (name) values ('123456789'); ERROR 1406 (22001): Data too long for column 'name' at row 1
  • 10. MySQL – Download (64 bits) [root@linux1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.18-1.el7.x86_64.rpm --no-check-certificate Conectando-se a cdn.mysql.com|104.105.143.228|:443... conectado. A requisição HTTP foi enviada, aguardando resposta... 200 OK Tamanho: 335516 (328K) [application/x-redhat-package-manager] Salvando em: “mysql-community-common-5.7.18-1.el7.x86_64.rpm” 100%[==============================================================================================>] 335.516 1,21M/s em 0,3s [root@linux1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.18-1.el7.x86_64.rpm --no-check-certificate Conectando-se a cdn.mysql.com|104.105.143.228|:443... conectado. A requisição HTTP foi enviada, aguardando resposta... 200 OK Tamanho: 2103888 (2,0M) [application/x-redhat-package-manager] Salvando em: “mysql-community-libs-5.7.18-1.el7.x86_64.rpm” 100%[===============================================================================================>] 2.103.888 3,40M/s em 0,6s [root@linux1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.18-1.el7.x86_64.rpm --no-check-certificate Conectando-se a cdn.mysql.com|104.105.143.228|:443... conectado. A requisição HTTP foi enviada, aguardando resposta... 200 OK Tamanho: 23039984 (22M) [application/x-redhat-package-manager] Salvando em: “mysql-community-client-5.7.18-1.el7.x86_64.rpm” 100%[===============================================================================================>] 23.039.984 2,86M/s em 7,3s [root@linux1 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.18-1.el7.x86_64.rpm --no-check-certificate Conectando-se a cdn.mysql.com|104.105.143.228|:443... conectado. A requisição HTTP foi enviada, aguardando resposta... 200 OK Tamanho: 156322552 (149M) [application/x-redhat-package-manager] Salvando em: “mysql-community-server-5.7.18-1.el7.x86_64.rpm” 100%[===============================================================================================>] 156.322.552 967K/s em 3m 4s
  • 11. MySQL – Install (64 bits) [root@linux1 ~]# ls -lh *.rpm -rw-r--r-- 1 root root 328K Mar 20 06:40 mysql-community-common-5.7.18-1.el7.x86_64.rpm -rw-r--r-- 1 root root 2,1M Mar 20 06:40 mysql-community-libs-5.7.18-1.el7.x86_64.rpm -rw-r--r-- 1 root root 22M Mar 20 06:40 mysql-community-client-5.7.18-1.el7.x86_64.rpm -rw-r--r-- 1 root root 150M Mar 20 06:41 mysql-community-server-5.7.18-1.el7.x86_64.rpm [root@linux1 ~]# rpm -ivh mysql-community-common-5.7.18-1.el7.x86_64.rpm aviso: mysql-community-common-5.7.18-1.el7.x86_64.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparando... ########################################### [100%] 1:mysql-community-common ########################################### [100%] [root@linux1 ~]# rpm -ivh mysql-community-libs-5.7.18-1.el7.x86_64.rpm aviso: mysql-community-libs-5.7.18-1.el7.x86_64.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparando... ########################################### [100%] 1:mysql-community-libs ########################################### [100%] [root@linux1 ~]# rpm -ivh mysql-community-client-5.7.18-1.el7.x86_64.rpm aviso: mysql-community-client-5.7.18-1.el7.x86_64.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparando... ########################################### [100%] 1:mysql-community-client ########################################### [100%] [root@linux1 ~]# rpm -ivh mysql-community-server-5.7.18-1.el7.x86_64.rpm aviso: mysql-community-server-5.7.18-1.el7.x86_64.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparando... ########################################### [100%] 1:mysql-community-server ########################################### [100%]
  • 12. [root@linux1 ~]# rpm -qa | grep mysql mysql-community-server-5.7.18-1.el7.x86_64 mysql-community-common-5.7.18-1.el7.x86_64 mysql-community-libs-5.7.18-1.el7.x86_64 mysql-community-client-5.7.18-1.el7.x86_64 [root@linux1 ~]# rpm -e mysql-community-server-5.7.18-1.el7.x86_64 --nodeps Warning: /etc/my.cnf salvo como /etc/my.cnf.rpmsave [root@linux1 ~]# rpm -e mysql-community-common-5.7.18-1.el7.x86_64 --nodeps [root@linux1 ~]# rpm -e mysql-community-libs-5.7.18-1.el7.x86_64 --nodeps [root@linux1 ~]# rpm -e mysql-community-client-5.7.18-1.el7.x86_64 –nodeps [root@linux1 ~]# rm -rf /var/lib/mysql/ MySQL – Uninstall (64 bits)
  • 14. [root@linux1 tmp]# service mysqld start (systemctl start mysqld) Inicializando o banco de dados MySQL: [ OK ] Installing validate password plugin: [ OK ] Iniciando o mysqld: [ OK ] [root@linux1]# grep 'temporary password' /var/log/mysqld.log 2017-05-02T10:30 [Note] A temporary password is generated for root@localhost: 8V*vubKtFQp( [root@linux1 ~]# mysql -uroot –p'8V*vubKtFQp(' Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 5 Server version: 5.7.18 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. MySQL - Initial Configuration
  • 15. mysql> alter user root@localhost identified by '@#4Jhfjk43jkdjk3#@'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> alter user root@localhost identified by 'Asdjer3jk4kjd4@#'; Query OK, 0 rows affected (0,00 sec) mysql> show variables like '%password_policy'; +--------------------------+--------+ | Variable_name | Value | +--------------------------+--------+ | validate_password_policy | MEDIUM | +--------------------------+--------+ 1 row in set (0,07 sec) mysql> SET GLOBAL validate_password_policy=LOW; Query OK, 0 rows affected (0,03 sec) mysql> alter user root@localhost identified by 'admin'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> alter user root@localhost identified by '12345678'; Query OK, 0 rows affected (0,01 sec) MySQL - Configuration (reset root password)
  • 16. mysql> show plugins; +----------------------------+----------+--------------------+----------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+----------------------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL | | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL | | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL | | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL | | FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL | | partition | ACTIVE | STORAGE ENGINE | NULL | GPL | | validate_password | ACTIVE | VALIDATE PASSWORD | validate_password.so | GPL | +----------------------------+----------+--------------------+----------------------+---------+ 45 rows in set (0,01 sec) mysql> UNINSTALL PLUGIN validate_password; Query OK, 0 rows affected (0,07 sec) mysql> alter user root@localhost identified by 'admin'; Query OK, 0 rows affected (0,00 sec) mysql> INSTALL PLUGIN validate_password SONAME 'validate_password.so'; Query OK, 0 rows affected (0,03 sec) MySQL - Configuration (reset root password)
  • 17. [root@linux1]# whereis mysql mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz mysql> help For information about MySQL products and services, visit: http://www.mysql.com/ For developer information, including the MySQL Reference Manual, visit: http://dev.mysql.com/ To buy MySQL Enterprise support, training, or other products, visit: https://shop.mysql.com/ List of all MySQL commands: Note that all text commands must be first on line and end with ';' ? (?) Synonym for `help'. clear (c) Clear the current input statement. connect (r) Reconnect to the server. Optional arguments are db and host. delimiter (d) Set statement delimiter. edit (e) Edit command with $EDITOR. ego (G) Send command to mysql server, display result vertically. exit (q) Exit mysql. Same as quit. go (g) Send command to mysql server. help (h) Display this help. nopager (n) Disable pager, print to stdout. notee (t) Don't write into outfile. pager (P) Set PAGER [to_pager]. Print the query results via PAGER. print (p) Print current command. prompt (R) Change your mysql prompt. quit (q) Quit mysql. rehash (#) Rebuild completion hash. source (.) Execute an SQL script file. Takes a file name as an argument. status (s) Get status information from the server. system (!) Execute a system shell command. tee (T) Set outfile [to_outfile]. Append everything into given outfile. use (u) Use another database. Takes database name as argument. charset (C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. warnings (W) Show warnings after every statement. nowarning (w) Don't show warnings after every statement. resetconnection(x) Clean session context. For server side help, type 'help contents' MySQL - mysql utility mysql> help contents; You asked for help about help category: "Contents" For more information, type 'help <item>', where <item> is one of the following categories: Account Management Administration Compound Statements Data Definition Data Manipulation Data Types Functions Functions and Modifiers for Use with GROUP BY Geographic Features Help Metadata Language Structure Plugins Procedures Storage Engines Table Maintenance Transactions User-Defined Functions Utility
  • 18. [root@linux1 ~]# systemctl start mysqld [root@linux1 ~]# systemctl status mysqld * mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2017-05-02 13:45:55 BST; 5s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 3105 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 3084 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 3109 (mysqld) CGroup: /system.slice/mysqld.service ??3109 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Mai 2 13:45:50 linux1 systemd[1]: Starting MySQL Server... Mai 2 13:45:55 linux1 systemd[1]: Started MySQL Server. [root@linux1 ~]# systemctl stop mysqld [root@linux1 ~]# systemctl status mysqld * mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: inactive (dead) since Thu 2017-05-02 13:47:00 BST; 904ms ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 3105 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 3084 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 3109 (code=exited, status=0/SUCCESS) Mai 2 13:45:50 linux1 systemd[1]: Starting MySQL Server... Mai 2 13:45:55 linux1 systemd[1]: Started MySQL Server. Mai 2 13:46:59 linux1 systemd[1]: Stopping MySQL Server... Mai 2 13:47:00 linux1 systemd[1]: Stopped MySQL Server. MySQL - Status / Stop / Start
  • 19. [root@linux1 ~]# service mysqld status mysqld (pid 3489) está em execução... [root@linux1 ~]# ps aux | grep mysql root 3244 0.0 0.1 5152 1368 pts/0 S 15:00 0:01 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid--basedir=/usr --user=mysql mysql 3489 1.6 16.7 555380 172072 pts/0 Sl 15:00 1:37 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock [root@linux1 ~]# service mysqld stop (systemctl stop mysqld) Parando o mysqld: [ OK ] [root@linux1 ~]# service mysqld start (systemctl start mysqld) Iniciando o mysqld: [ OK ] [root@linux1 ~]# service mysqld restart (systemctl restart mysqld) Parando o mysqld: [ OK ] Iniciando o mysqld: [ OK ] mysql> show variables like '%bind%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | bind_address | * | +---------------+-------+ [root@linux1 ~]# netstat -na | grep 3306 tcp 0 0 :::3306 :::* LISTEN MySQL - Status / Stop / Start mysql> help SHUTDOWN; Name: 'SHUTDOWN' This statement stops the MySQL server. It requires the SHUTDOWN privilege. SHUTDOWN was added in MySQL 5.7.9. It provides an SQL-level interface to the same functionality available using the mysqladmin shutdown command or the mysql_shutdown() C API function. mysql> shutdown; Query OK, 0 rows affected (0,03 sec) mysql> select 1; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) ERROR: Can't connect to the server
  • 20. MySQL - Files or Resources
  • 21. [root@linux1 ~]# cat /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid MySQL - Configuration File
  • 22. [root@linux1 ~]# tail -20 /var/log/mysqld.log 2017-05-02T19:41:01.660047Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active. 2017-05-02T19:41:01.660148Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active. 2017-05-02T19:41:01.674840Z 0 [Note] InnoDB: Waiting for purge to start 2017-05-02T19:41:01.730361Z 0 [Note] InnoDB: 5.7.17 started; log sequence number 2537004 2017-05-02T19:41:01.749315Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool 2017-05-02T19:41:01.759440Z 0 [Note] Plugin 'FEDERATED' is disabled. 2017-05-02T19:41:01.854483Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170408 16:41:01 2017-05-02T19:41:02.093335Z 0 [Warning] CA certificate ca.pem is self signed. 2017-05-02T19:41:02.211370Z 0 [Note] Server hostname (bind-address): '*'; port: 3306 2017-05-02T19:41:02.218146Z 0 [Note] IPv6 is available. 2017-05-02T19:41:02.218559Z 0 [Note] - '::' resolves to '::'; 2017-05-02T19:41:02.219456Z 0 [Note] Server socket created on IP: '::'. 2017-05-02T19:41:02.428972Z 0 [Note] Event Scheduler: Loaded 0 events [root@linux1 /]# service mysqld start MySQL Daemon failed to start. Iniciando o mysqld: [FALHOU] [root@linux1 ~]# cat /var/log/mysqld.log | grep ERROR 2017-05-02T20:35:39.247229Z 0 [ERROR] unknown variable 'not_exist=legatti' 2017-05-02T20:35:39.247309Z 0 [ERROR] Aborting MySQL - Error Log File
  • 23. mysql> show variables like 'log_timestamps'; +----------------+-------+ | Variable_name | Value | +----------------+-------+ | log_timestamps | UTC | +----------------+-------+ mysql> set GLOBAL log_timestamps=SYSTEM; Query OK, 0 rows affected (0,00 sec) [root@linux1 ~]# cat /var/log/mysqld.log | grep ERROR 2017-05-02T20:35:39.247229Z 0 [ERROR] unknown variable 'not_exist=legatti' 2017-05-02T20:35:39.247309Z 0 [ERROR] Aborting MySQL - Error Log File As of MySQL 5.7.2, the log_timestamps system variable controls the timestamp time zone of messages written to the error log (as well as to general query log and slow query log files). Permitted values are UTC (the default) and SYSTEM (local system time zone). Before MySQL 5.7.2, messages use the local system time zone
  • 24. [root@linux1 ~]# mysql –u<user> -p<password>  mysql –uroot –padmin mysql> select @@version; +-----------+ | @@version | +-----------+ | 5.7.18 | +-----------+ mysql> show variables like 'port'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ mysql> show variables like 'bind_address'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | bind_address | * | +---------------+-------+ mysql> select database(); +------------+ | database() | +------------+ | NULL | +------------+ MySQL - Connecting Locally mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ [root@linux1 ~]# mysql -uroot -padmin -e "show databases" +--------------------+ | Database | +--------------------+ | information_schema | | bd01 | | mysql | | performance_schema | | sys | +--------------------+ [root@linux1 ~]# echo "show databases" | mysql -uroot -padmin Database information_schema mysql performance_schema sys
  • 25. [root@linux1 ~]# mysql –h<host> –u<user> -p<password> --port<port> C:>mysql -h192.168.56.101 -uroot -padmin --port 3306 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 24 Server version: 5.7.18-log MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) MySQL - Connecting Remotely
  • 27. mysql> CREATE USER 'admin01'@'192.168.56.102' IDENTIFIED BY '#a1234@'; Query OK, 0 rows affected (0,00 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin02'@'%' IDENTIFIED BY '#b1234@'; Query OK, 0 rows affected, 1 warning (0,00 sec) mysql> CREATE USER 'admin03'@'192.168.%' IDENTIFIED BY '#c1234@'; Query OK, 0 rows affected (0,01 sec) mysql> select user,host,authentication_string from mysql.user order by 1; +-----------+----------------+-------------------------------------------+ | user | host | authentication_string | +-----------+----------------+-------------------------------------------+ | admin01 | 192.168.56.102 | *1260D0031D9EDAC141609A3723FBD16C2260425F | | admin02 | % | *E9190029100941FBE4D00E22146E8EDA75667142 | | admin03 | 192.168.% | *3A9EF2D8B8D45B60FD5D978A872D28C62FD139BB | | mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | root | localhost | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | | root | % | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | +-----------+----------------+-------------------------------------------+ 6 rows in set (0,00 sec) MySQL - Creating Users CREATE USER 'username'@'hostname' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'username'@'hostname' IDENTIFIED BY 'password' WITH GRANT OPTION;
  • 28. mysql> show grants for scott; +------------------------------------+ | Grants for scott@% | +------------------------------------+ | GRANT SELECT ON *.* TO 'scott'@'%' | +------------------------------------+ 1 row in set (0.00 sec) mysql> RENAME USER 'scott'@'%' TO 'adam'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for adam; +-----------------------------------+ | Grants for adam@% | +-----------------------------------+ | GRANT SELECT ON *.* TO 'adam'@'%' | +-----------------------------------+ 1 row in set (0.00 sec) MySQL - Renaming Users RENAME USER 'old_user' TO 'new_user'; The RENAME USER statement renames existing MySQL accounts. An error occurs for old accounts that do not exist or new accounts that already exist. To use RENAME USER, you must have the global CREATE USER privilege, or the UPDATE privilege for the mysql database.
  • 29. mysql> create user 'scott'@'%' identified by 'tiger'; Query OK, 0 rows affected (0.00 sec) mysql> grant select on *.* to 'scott'@'%'; Query OK, 0 rows affected (0.01 sec) mysql> alter user scott ACCOUNT LOCK; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye [root@linux1 ~]# mysql -uscott -ptiger ERROR 3118 (HY000): Access denied for user 'scott'@'localhost'. Account is locked. Mysql> alter user scott ACCOUNT UNLOCK; MySQL - Creating Users (account lock – 5.7)
  • 30. mysql> SHOW PRIVILEGES; +-------------------------+---------------------------------------+-------------------------------------------------------+ | Privilege | Context | Comment | +-------------------------+---------------------------------------+-------------------------------------------------------+ | Alter | Tables | To alter the table | | Alter routine | Functions,Procedures | To alter or drop stored functions/procedures | | Create | Databases,Tables,Indexes | To create new databases and tables | | Create routine | Databases | To use CREATE FUNCTION/PROCEDURE | | Create temporary tables | Databases | To use CREATE TEMPORARY TABLE | | Create view | Tables | To create new views | | Create user | Server Admin | To create new users | | Delete | Tables | To delete existing rows | | Drop | Databases,Tables | To drop databases, tables, and views | | Event | Server Admin | To create, alter, drop and execute events | | Execute | Functions,Procedures | To execute stored routines | | File | File access on server | To read and write files on the server | | Grant option | Databases,Tables,Functions,Procedures | To give to other users those privileges you possess | | Index | Tables | To create or drop indexes | | Insert | Tables | To insert data into tables | | Lock tables | Databases | To use LOCK TABLES (together with SELECT privilege) | | Process | Server Admin | To view the plain text of currently executing queries | | Proxy | Server Admin | To make proxy user possible | | References | Databases,Tables | To have references on tables | | Reload | Server Admin | To reload or refresh tables, logs and privileges | | Replication client | Server Admin | To ask where the slave or master servers are | | Replication slave | Server Admin | To read binary log events from the master | | Select | Tables | To retrieve rows from table | | Show databases | Server Admin | To see all databases with SHOW DATABASES | | Show view | Tables | To see views with SHOW CREATE VIEW | | Shutdown | Server Admin | To shut down the server | | Super | Server Admin | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc. | | Trigger | Tables | To use triggers | | Create tablespace | Server Admin | To create/alter/drop tablespaces | | Update | Tables | To update existing rows | | Usage | Server Admin | No privileges - allow connect only | +-------------------------+---------------------------------------+-------------------------------------------------------+ MySQL - Privileges
  • 31. GRANT ALTER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT ALTER ROUTINE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT CREATE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT CREATE ROUTINE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT CREATE TABLESPACE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT CREATE TEMPORARY TABLES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT CREATE USER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT CREATE VIEW ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT DELETE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT DROP ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT EVENT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT EXECUTE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT FILE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT INDEX ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT INSERT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT LOCK TABLES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT PROCESS ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT REFERENCES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT RELOAD ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT REPLICATION CLIENT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT REPLICATION SLAVE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT SELECT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT SHOW DATABASES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT SHOW VIEW ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT SHUTDOWN ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT SUPER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT TRIGGER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION GRANT UPDATE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION MySQL - Creating Users (Privileges) https://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html
  • 32. mysql> SHOW CREATE USER 'admin02'@'%'; +-------------------------------------------------------------------------------------------------------------------+ | CREATE USER for admin02@% | +-------------------------------------------------------------------------------------------------------------------+ | CREATE USER 'admin02'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*E9190029100941FBE4D00E22146E8EDA75667142' | +-------------------------------------------------------------------------------------------------------------------+ 1 row in set (0,01 sec) mysql> SHOW GRANTS FOR 'admin02'@'%'; +----------------------------------------------+ | Grants for admin02@% | +----------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'admin02'@'%' | +----------------------------------------------+ 1 row in set (0,00 sec) mysql> SELECT CONCAT('SHOW GRANTS FOR ''',USER,'''@''',HOST,''';') FROM mysql.user WHERE USER <> ''; +------------------------------------------------------+ | CONCAT('SHOW GRANTS FOR ''',USER,'''@''',HOST,''';') | +------------------------------------------------------+ | SHOW GRANTS FOR 'admin01'@'192.168.56.102'; | | SHOW GRANTS FOR 'admin02'@'%'; | | SHOW GRANTS FOR 'admin03'@'192.168.%'; | | SHOW GRANTS FOR 'root'@'%'; | | SHOW GRANTS FOR 'mysql.sys'@'localhost'; | | SHOW GRANTS FOR 'root'@'localhost'; | +------------------------------------------------------+ 6 rows in set (0,00 sec) MySQL - Extracting Users Information SHOW CREATE USER 'username'@'hostname'; SHOW GRANTS FOR 'username'@'hostname';
  • 33. mysql> select current_user; +----------------+ | current_user | +----------------+ | root@localhost | +----------------+ 1 row in set (0,00 sec) mysql> SHOW GRANTS FOR CURRENT_USER; +---------------------------------------------------------------------+ | Grants for root@localhost | +---------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | +---------------------------------------------------------------------+ 2 rows in set (0,00 sec) mysql> show grants for root; +-------------------------------------------+ | Grants for root@% | +-------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' | +-------------------------------------------+ 1 row in set (0,00 sec) mysql> show grants for root@localhost; +---------------------------------------------------------------------+ | Grants for root@localhost | +---------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | +---------------------------------------------------------------------+ 2 rows in set (0,00 sec) MySQL - Extracting Users Information
  • 34. mysql> desc mysql.user; +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Host | char(60) | NO | PRI | | | | User | char(32) | NO | PRI | | | | Select_priv | enum('N','Y') | NO | | N | | | Insert_priv | enum('N','Y') | NO | | N | | | Update_priv | enum('N','Y') | NO | | N | | | Delete_priv | enum('N','Y') | NO | | N | | | Create_priv | enum('N','Y') | NO | | N | | | Reload_priv | enum('N','Y') | NO | | N | | | Shutdown_priv | enum('N','Y') | NO | | N | | | Process_priv | enum('N','Y') | NO | | N | | | Grant_priv | enum('N','Y') | NO | | N | | | References_priv | enum('N','Y') | NO | | N | | | Index_priv | enum('N','Y') | NO | | N | | | Alter_priv | enum('N','Y') | NO | | N | | | Super_priv | enum('N','Y') | NO | | N | | | Create_tmp_table_priv | enum('N','Y') | NO | | N | | | Execute_priv | enum('N','Y') | NO | | N | | | Create_view_priv | enum('N','Y') | NO | | N | | | Create_routine_priv | enum('N','Y') | NO | | N | | | Alter_routine_priv | enum('N','Y') | NO | | N | | | Create_user_priv | enum('N','Y') | NO | | N | | | max_connections | int(11) unsigned | NO | | 0 | | | max_user_connections | int(11) unsigned | NO | | 0 | | | authentication_string | text | YES | | NULL | | |... | | | | | | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ MySQL - Extracting Users Information mysql.user: User level privileges
  • 35. mysql> desc mysql.db; +-----------------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------------+---------------+------+-----+---------+-------+ | Host | char(60) | NO | PRI | | | | Db | char(64) | NO | PRI | | | | User | char(32) | NO | PRI | | | | Select_priv | enum('N','Y') | NO | | N | | | Insert_priv | enum('N','Y') | NO | | N | | | Update_priv | enum('N','Y') | NO | | N | | | Delete_priv | enum('N','Y') | NO | | N | | | Create_priv | enum('N','Y') | NO | | N | | | Drop_priv | enum('N','Y') | NO | | N | | | Grant_priv | enum('N','Y') | NO | | N | | | References_priv | enum('N','Y') | NO | | N | | | Index_priv | enum('N','Y') | NO | | N | | | Alter_priv | enum('N','Y') | NO | | N | | | Create_tmp_table_priv | enum('N','Y') | NO | | N | | | Lock_tables_priv | enum('N','Y') | NO | | N | | | Create_view_priv | enum('N','Y') | NO | | N | | | Show_view_priv | enum('N','Y') | NO | | N | | | Create_routine_priv | enum('N','Y') | NO | | N | | | Alter_routine_priv | enum('N','Y') | NO | | N | | | Execute_priv | enum('N','Y') | NO | | N | | | Event_priv | enum('N','Y') | NO | | N | | | Trigger_priv | enum('N','Y') | NO | | N | | +-----------------------+---------------+------+-----+---------+-------+ MySQL - Extracting Users Information mysql.db: Database level privileges
  • 36. mysql> desc mysql.tables_priv; +-------------+--------------------------------------------------------------------------------------------+ | Field | Type | +-------------+--------------------------------------------------------------------------------------------+ | Host | char(60) | | Db | char(64) | | User | char(32) | | Table_name | char(64) | | Grantor | char(93) | | Timestamp | timestamp | | Table_priv | set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index',etc.) | | Column_priv | set('Select','Insert','Update','References') | +-------------+--------------------------------------------------------------------------------------------+ MySQL - Extracting Users Information mysql.tables_priv: Table level privileges mysql> desc mysql.columns_priv; +-------------+----------------------------------------------+------+-----+ | Field | Type | Null | Key | +-------------+----------------------------------------------+------+-----+ | Host | char(60) | NO | PRI | | Db | char(64) | NO | PRI | | User | char(32) | NO | PRI | | Table_name | char(64) | NO | PRI | | Column_name | char(64) | NO | PRI | | Timestamp | timestamp | NO | | | Column_priv | set('Select','Insert','Update','References') | NO | | +-------------+----------------------------------------------+------+-----+ mysql.columns_priv: Column level privileges
  • 37. mysql> desc mysql.procs_priv; +--------------+----------------------------------------+------+-----+ | Field | Type | Null | Key | +--------------+----------------------------------------+------+-----+ | Host | char(60) | NO | PRI | | Db | char(64) | NO | PRI | | User | char(32) | NO | PRI | | Routine_name | char(64) | NO | PRI | | Routine_type | enum('FUNCTION','PROCEDURE') | NO | PRI | | Grantor | char(93) | NO | MUL | | Proc_priv | set('Execute','Alter Routine','Grant') | NO | | | Timestamp | timestamp | NO | | +--------------+----------------------------------------+------+-----+ MySQL - Extracting Users Information mysql.procs_priv: Procedure / Function privileges
  • 38. mysql> ALTER USER 'admin02'@'%' WITH MAX_USER_CONNECTIONS 5; Query OK, 0 rows affected, 1 warning (0,00 sec) mysql> SELECT USER,HOST,MAX_USER_CONNECTIONS FROM mysql.user ORDER BY 1; +-----------+----------------+----------------------+ | USER | HOST | MAX_USER_CONNECTIONS | +-----------+----------------+----------------------+ | admin01 | 192.168.56.102 | 0 | | admin02 | % | 5 | | admin03 | 192.168.% | 0 | | mysql.sys | localhost | 0 | | root | localhost | 0 | | root | % | 0 | | teste | % | 0 | +-----------+----------------+----------------------+ 7 rows in set (0,00 sec) [root@linux1 ~]# mysql -uadmin02 –p**** ERROR 1226 (42000): User 'admin02' has exceeded the 'max_user_connections' resource (current value: 5) mysql> SHOW FULL PROCESSLIST; +----+---------+----------------------+------+---------+------+----------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+---------+----------------------+------+---------+------+----------+-----------------------+ | 33 | admin02 | 192.168.56.102:55235 | NULL | Sleep | 309 | | NULL | | 34 | admin02 | 192.168.56.102:55236 | NULL | Sleep | 309 | | NULL | | 36 | admin02 | 192.168.56.102:55238 | NULL | Sleep | 300 | | NULL | | 37 | admin02 | 192.168.56.102:55239 | NULL | Sleep | 300 | | NULL | | 38 | admin02 | 192.168.56.102:55240 | NULL | Sleep | 289 | | NULL | | 42 | root | localhost | NULL | Query | 0 | starting | SHOW FULL PROCESSLIST | +----+---------+----------------------+------+---------+------+----------+-----------------------+ 6 rows in set (0,00 sec) MySQL - Limiting Connections
  • 39. mysql> set GLOBAL max_connections = 3; Query OK, 0 rows affected (0,00 sec) [root@linux1 ~]# mysql –uadmin02 –p**** ERROR 1040 (HY000): Too many connections mysql> set GLOBAL max_connections = 200; Query OK, 0 rows affected (0,00 sec) mysql> set GLOBAL max_user_connections = 2; Query OK, 0 rows affected (0,00 sec) [root@linux1 ~]# mysql -uadmin02 –p**** [root@linux1 ~]# mysql -uadmin02 –p**** [root@linux1 ~]# mysql -uadmin02 –p**** [root@linux1 ~]# mysql -uadmin02 –p**** [root@linux1 ~]# mysql -uadmin02 –p**** [root@linux1 ~]# mysql -uadmin02 –p**** ERROR 1226 (42000): User 'admin02' has exceeded the 'max_user_connections' resource (current value: 5) [root@linux1 ~]# mysql -uadmin01 -p**** [root@linux1 ~]# mysql -uadmin01 -p**** [root@linux1 ~]# mysql -uadmin01 -p**** ERROR 1203 (42000): User admin01 already has more than 'max_user_connections' active connections MySQL - Limiting Connections Priority [MAX_CONNECTIONS] [ALTER USER...WITH MAX_USER_CONNECTIONS] [MAX_USER_CONNECTIONS]
  • 40. mysql> SHOW FULL PROCESSLIST; +--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+ | Id | User | Host | db | Command | Time | State | Info | +--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+ | 1 | event_scheduler | localhost | NULL | Daemon | 2128 | Waiting for next activation | NULL | | 358213 | user_bd01 | 11.215.210.209:60440 | bd01 | Query | 1 | Sending Data | SELECT id FROM T1 | | 358214 | user_bd01 | 11.215.210.209:60441 | bd01 | Sleep | 363 | | NULL | | 358216 | user_bd01 | 11.215.210.209:60464 | bd01 | Query | 544 | Sorting for order | SELECT * FROM T3,T2 WHERE...| | 358217 | user_bd01 | 11.215.210.209:60465 | bd01 | Query | 0 | Creating sort index | SELECT * FROM T3 ORDER BY 2 | | 361836 | root | 11.215.200.30:55730 | NULL | Sleep | 28 | | NULL | | 361837 | root | 11.215.200.30:55731 | NULL | Sleep | 1502 | | NULL | | 361865 | root | localhost | NULL | Query | 0 | init | SHOW FULL PROCESSLIST | | 361975 | user_bd02 | 11.215.210.209:59730 | bd02 | Query | 2 | Writing to net | SELECT COUNT(*) FROM T2 | | 362101 | user_bd02 | 11.215.210.209:60554 | bd02 | Sleep | 12 | | NULL | +--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+ mysql> SELECT -> USER, -> SUBSTRING_INDEX(HOST, ':', 1) HOST, -> DB, -> COUNT(*) QTD -> FROM -> INFORMATION_SCHEMA.PROCESSLIST -> GROUP BY USER, -> SUBSTRING_INDEX(HOST, ':', 1), -> DB -> ORDER BY QTD DESC ; +-----------------+----------------+-------+-----+ | USER | HOST | DB | QTD | +-----------------+----------------+-------+-----+ | user_bd01 | 11.215.210.209 | bd01 | 4 | | user_bd02 | 11.215.210.209 | bd02 | 2 | | root | 11.215.200.30 | NULL | 2 | | root | localhost | NULL | 1 | | event_scheduler | localhost | NULL | 1 | +-----------------+----------------+-------+-----+ MySQL – Listing Connections -- Current Thread ID mysql> SELECT CONNECTION_ID(); +-----------------+ | CONNECTION_ID() | +-----------------+ | 3435| +-----------------+ -- Current User mysql> select CURRENT_USER; +----------------+ | current_user | +----------------+ | root@localhost | +----------------+
  • 41. MySQL – Thread States https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html
  • 43. MySQL – Instance Structure • Query cache : Shared identical queries from memory. If an identical query from client found in query cache then, the mysql server retrieves the results from the query cache rather than parsing and executing that query again. It’s a shared cache for sessions , so a result set generated by one client can be sent in response to the same query issued by another client. If MySQL does find a match in the query cache, it must check privileges before returning the cached query. This is possible without parsing the query, because MySQL stores table information with the cached query. If the privileges are OK, MySQL retrieves the stored result from the query cache and sends it to the client, bypassing every other stage in query execution. The query is never parsed, optimized, or executed. • Parser: Check for SQL syntax by checking every character in SQL query and generate SQL_ID for each SQL query. • Metadata cache: Cache for object metadata information and statistics. • key cache: Cache table indexes.In MySQL keys are indexes. if index size is small then it will cache index structure and data leaf.If an index is large then it will only cache index structure.Used by MyISAM storage engine. • Optimizer : Created efficient query execution plan as per the storage engine.It will rewrite a query.Example: InnoDB has shared buffer so optimizer will get pre-cached data from it. Using table statistics optimizer will generate an execution plan for a SQL query.
  • 44. MySQL - SQL Execution Query Cache • Any query that contains a nondeterministic function, such as NOW() or CURRENT_DATE(), will not be cached. Similarly, functions such as CURRENT_USER() or CONNECTION_ID() might vary when executed by different users, thereby preventing a cache hit. • The query cache is not supported for partitioned tables, and is automatically disabled for queries involving partitioned tables. • Each client connection gets its own thread within the server process. The connection’s queries execute within that single thread, which in turn resides on one core or CPU. The server caches threads, so they don’t need to be created and destroyed for each new connection. • Before even parsing the query, though, the server consults the query cache, which can store only SELECT statements, along with their result sets. If anyone issues a query that’s identical to one already in the cache, the server doesn’t need to parse, optimize, or execute the query at all—it can simply pass back the stored result set.
  • 45. MySQL - Create DATABASE
  • 46. MySQL - Create DATABASE
  • 47. mysql> show variables like 'lower_case_table_names'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | lower_case_table_names | 0 | +------------------------+-------+ 1 row in set (0,06 sec) mysql> select count(*) from teste01.table_01; +----------+ | count(*) | +----------+ | 335872 | +----------+ 1 row in set (0,26 sec) mysql> select count(*) from teste01.TABLE_01; ERROR 1146 (42S02): Table 'teste01.TABLE_01' doesn't exist mysql> show variables like 'lower_case_table_names'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | lower_case_table_names | 1 | +------------------------+-------+ 1 row in set (0,03 sec) mysql> select count(*) from teste01.TABLE_01; +----------+ | count(*) | +----------+ | 335872 | +----------+ 1 row in set (0,16 sec) MySQL - Disabling Case Sensitive No linux, o mysql é case sensitive por padrão. No windows não. Para desabilitar a sensibilidade do Linux, hablite o parametro "lower_case_table_names" no MySQL Pare o seu mysql $ service mysqld stop Edite o seu my.conf Na seção [mysqld] adicione o parametro lower_case_table_names com o valor 1 [mysqld] ... lower_case_table_names=1 Salve e feche o arquivo e reinicie o serviço do mysql $ service mysqld start
  • 48. mysql> create database teste01; Query OK, 1 row affected (0,01 sec) mysql> USE teste01; Database changed mysql> SELECT @@character_set_database, @@collation_database; +--------------------------+----------------------+ | @@character_set_database | @@collation_database | +--------------------------+----------------------+ | latin1 | latin1_swedish_ci | +--------------------------+----------------------+ 1 row in set (0,00 sec) mysql> SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME -> FROM INFORMATION_SCHEMA.SCHEMATA WHERE -> SCHEMA_NAME = 'teste01'; +----------------------------+------------------------+ | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | +----------------------------+------------------------+ | latin1 | latin1_swedish_ci | +----------------------------+------------------------+ 1 row in set (0,01 sec) MySQL - Create DATABASE
  • 49. mysql> USE teste01; Database changed mysql> CREATE TABLE IF NOT EXISTS table_01 ( -> task_id INT(11) NOT NULL AUTO_INCREMENT, -> subject VARCHAR(45) DEFAULT NULL, -> PRIMARY KEY (task_id) -> ) ENGINE=InnoDB; Query OK, 0 rows affected (0,02 sec) mysql> CREATE TABLE IF NOT EXISTS table_02 ( -> task_id INT(11) NOT NULL AUTO_INCREMENT, -> subject VARCHAR(45) DEFAULT NULL, -> PRIMARY KEY (task_id) -> ) ENGINE=MyISAM; Query OK, 0 rows affected (0,01 sec) mysql> show tables; +-------------------+ | Tables_in_teste01 | +-------------------+ | table_01 | | table_02 | +-------------------+ 2 rows in set (0,00 sec) mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ MySQL - Create Table
  • 50. mysql> SHOW TABLE STATUSG *************************** 1. row *************************** Name: table_01 Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 335773 Avg_row_length: 35 Data_length: 12075008 Max_data_length: 0 Index_length: 6832128 Data_free: 4194304 Auto_increment: 430046 Create_time: 2017-05-02 12:43:52 Update_time: NULL Check_time: NULL Collation: latin1_swedish_ci *************************** 2. row *************************** Name: table_02 Engine: MyISAM Version: 10 Row_format: Dynamic Rows: 458752 Avg_row_length: 23 Data_length: 10780672 Max_data_length: 281474976710655 Index_length: 5847040 Data_free: 0 Auto_increment: 458753 Create_time: 2017-05-02 12:43:58 Update_time: 2017-05-02 15:07:01 Check_time: 2017-05-02 15:06:03 Collation: latin1_swedish_ci MySQL - Show Table Status Data Size in Bytes (~12MB) No Maximum (64Tb for tablespace) Index size in bytes (~6MB) Unused space (~4MB) mysql> show variables like 'innodb_page_size'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | innodb_page_size | 16384 | +------------------+-------+
  • 51. mysql> delete from table_02; Query OK, 458752 rows affected (22,73 sec) mysql> show table status like 'table_02'G *************************** 1. row *************************** Name: table_02 Engine: MyISAM Version: 10 Row_format: Dynamic Rows: 0 Avg_row_length: 0 Data_length: 10780672 Max_data_length: 281474976710655 Index_length: 5847040 Data_free: 10780672 Auto_increment: 458753 Create_time: 2017-05-02 12:43:58 Update_time: 2017-05-02 21:03:35 Check_time: 2017-05-02 15:06:03 Collation: latin1_swedish_ci mysql> show table status like 'table_02'G *************************** 1. row *************************** Name: table_02 Engine: MyISAM Version: 10 Row_format: Dynamic Rows: 0 Avg_row_length: 0 Data_length: 0 Max_data_length: 281474976710655 Index_length: 1024 Data_free: 0 Auto_increment: 458753 Create_time: 2017-05-02 12:43:58 Update_time: 2017-05-02 21:14:43 Check_time: 2017-05-02 21:14:43 Collation: latin1_swedish_ci MySQL - Optimize Table (MyISAM) mysql> optimize table table_02; +------------------+----------+----------+----------+ | Table | Op | Msg_type | Msg_text | +------------------+----------+----------+----------+ | teste01.table_02 | optimize | status | OK | +------------------+----------+----------+----------+ 1 row in set (0,08 sec) • To reclaim space to the filesystem run "OPTIMIZE TABLE" Creates a copy of the table, drops the original, and renames the copy • If frequent activity on the table run "ANALYZE TABLE"
  • 52. mysql> delete from table_01; Query OK, 335872 rows affected (9,37 sec) mysql> optimize table table_01; +------------------+----------+----------+-------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +------------------+----------+----------+-------------------------------------------------------------------+ | teste01.table_01 | optimize | note | Table does not support optimize, doing recreate + analyze instead | | teste01.table_01 | optimize | status | OK | +------------------+----------+----------+-------------------------------------------------------------------+ mysql> ANALYZE TABLE table_01; +------------------+---------+----------+----------+ | Table | Op | Msg_type | Msg_text | +------------------+---------+----------+----------+ | teste01.table_01 | analyze | status | OK | +------------------+---------+----------+----------+ mysql> show table status like 'table_01'G *************************** 1. row *************************** Name: table_01 Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 0 Avg_row_length: 0 Data_length: 16384 Max_data_length: 0 Index_length: 16384 Data_free: 0 Auto_increment: 430046 Create_time: 2017-05-02 21:40:16 Update_time: NULL Check_time: NULL Collation: latin1_swedish_ci MySQL - Optimize Table (InnoDB) • To reclaim space to the tablespace run "OPTIMIZE TABLE" • Reorganization process similar to MyISAMm "ANALYZE TABLE" rarely needed, as InnoDB updates stats
  • 53. SELECT * FROM ( SELECT table_schema, COUNT(table_schema) "tables", ROUND(SUM(data_length)/1024/1024,2) AS "data_size (MB)", ROUND(SUM(index_length)/1024/1024,2) AS "index_size (MB)", ROUND(SUM(data_length+index_length)/1024/1024,2) AS "total (MB)", ENGINE FROM information_schema.tables GROUP BY table_schema,ENGINE ) v1 WHERE TABLE_SCHEMA NOT IN('mysql','information_schema','performance_schema'); table_schema tables data_size (MB) index_size (MB) total (MB) ENGINE ------------------- ------ -------------- --------------- ---------- -------- bd01 414 38698.09 11547.17 50245.27 InnoDB bd01 3 0.00 0.01 0.01 MyISAM bd02 15 5.98 4.47 10.45 InnoDB bd02 58 404.73 150.33 555.06 InnoDB bd03 77 32.20 1.14 33.34 InnoDB bd03 5 0.00 0.01 0.01 MyISAM bd04 149 1771.77 120.52 1892.28 InnoDB bd04 69 6.28 1.78 8.06 MyISAM bd05 24 320.13 0.11 320.23 InnoDB bd05 187 289.27 46.27 335.54 MyISAM bd06 6 7.59 5.94 13.53 InnoDB MySQL - Information_Schema
  • 55. MySQL - Instance Files mysql> show variables like 'innodb_buffer_pool_dump_at_shutdown'; +-------------------------------------+-------+ | Variable_name | Value | +-------------------------------------+-------+ | innodb_buffer_pool_dump_at_shutdown | ON | +-------------------------------------+-------+ 1 row in set (0,05 sec) mysql> show variables like 'innodb_buffer_pool_load_at_startup'; +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | innodb_buffer_pool_load_at_startup | ON | +------------------------------------+-------+ 1 row in set (0,04 sec) mysql> show variables like 'innodb_buffer_pool_filename'; +-----------------------------+----------------+ | Variable_name | Value | +-----------------------------+----------------+ | innodb_buffer_pool_filename | ib_buffer_pool | +-----------------------------+----------------+ 1 row in set (0,05 sec) Every time you restart your MySQL server, it starts with a clean/empty buffer pool and usually it take some time to warm-up the buffer. To speed up this process, we can configure 2 variables that will dump and reload the pages reference stored in the buffer, this is a new functionality added on MySQL 5.6
  • 56. MySQL - Instance/Database Files mysql> show variables like 'innodb%'; +------------------------------------------+------------------------+ | Variable_name | Value | +------------------------------------------+------------------------+ | innodb_data_home_dir | | | innodb_data_file_path | ibdata1:12M:autoextend | | innodb_file_per_table | ON | | innodb_log_group_home_dir | ./ | | innodb_log_files_in_group | 2 | | innodb_log_file_size | 50331648 | | innodb_temp_data_file_path | ibtmp1:12M:autoextend | | innodb_flush_log_at_trx_commit | 1 | | innodb_buffer_pool_size | 134217728 | | etc. | | +------------------------------------------+------------------------+ • innodb_file_per_table This option causes the server to create one tablespace file per table. When innodb_file_per_table is enabled (the default), InnoDB stores the data and indexes for each newly created table in a separate .ibd file instead of the system tablespace. The storage for these tables is reclaimed when the tables are dropped or truncated. • innodb_log_group_home_dir: Specifies the location of the InnoDB log files eg: innodb_log_group_home=/var/lib/mysql • innodb_log_files_in_group: The number of log files in the log group. InnoDB writes to the files in a circular fashion. The default (and recommended) value is 2. • innodb_log_file_size: The size in bytes of each log file in a log group. • innodb_temp_data_file_path: In 5.7, InnoDB added a separate tablespace for all non-compressed InnoDB temporary tables. This new tablespace is named ibtmp1 and is located in the datadir by default. • innodb_flush_log_at_trx_commit: sets the behaviour of syncing on commit. The default value is 1. There are several possible values: 0 - if MySQL crashes, last second of txns lost; 1 - flush to disk each update/commit; 2 - flush to OS (if OS crashes, 1 sec txns are lost) • innodb_buffer_pool_size The size in bytes of the memory buffer to cache data and indexes of InnoDB‘s tables. This aims to reduce disk access to provide better performance. • innodb_data_home_dir (datadir=/var/lib/mysql) The directory (relative to :term:` datadir`) where the database server stores the files in a shared tablespace setup. This option does not affect the location of innodb_file_per_table. • innodb_data_file_path Specifies the names, sizes and location of shared tablespace files eg: innodb_data_file_path=ibdata1:50M;ibdata2:50M:autoextend
  • 57. MySQL - Instance/Database Files Ibdata: Default prefix for tablespace files, e.g. ibdata1 is a 10MB autoextensible file that MySQL creates for the shared tablespace by default. .frm: For each table, the server will create a file with the .frm extension containing the table definition (for all storage engines). .ibd: On a multiple tablespace setup (innodb_file_per_table enabled), MySQL will store each newly created table on a file with a .ibd extension. .MYD: Each MyISAM table has .MYD (MYData) file which contains the data on it. .MYI: Each MyISAM table has .MYI (MYIndex) file which contains the table’s indexes. .TRG: File containing the Triggers associated to a table, e.g. :file:`mytable.TRG. With the .TRN file, they represent all the Trigger definitions. .TRN: File containing the Triggers’ Names associated to a table, e.g. :file:`mytable.TRN. With the .TRG file, they represent all the Trigger definitions. .ARM: Each table with the Archive Storage Engine has .ARM file which contains the metadata of it. .ARZ: Each table with the Archive Storage Engine has .ARZ file which contains the data of it. .CSM: Each table with the CSV Storage Engine has .CSM file which contains the metadata of it. .CSV: Each table with the CSV Storage engine has .CSV file which contains the data of it (which is a standard Comma Separated Value file). .opt: MySQL stores options of a database (like charset) in a file with a .opt extension in the database directory. .par: Each partitioned table has .par file which contains metadata about the partitions.
  • 58. MySQL – CREATE TABLESPACE mysql> CREATE TABLESPACE TBS_DADOS ADD DATAFILE '/tmp/TBS_DADOS_DATAFILE_001.ibd'; Query OK, 0 rows affected (0,01 sec) mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES; +-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+ | SPACE | NAME | FLAG | FILE_FORMAT | ROW_FORMAT | PAGE_SIZE | ZIP_PAGE_SIZE | SPACE_TYPE | FS_BLOCK_SIZE | FILE_SIZE | ALLOCATED_SIZE | +-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+ | 2 | mysql/plugin | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 3 | mysql/servers | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 4 | mysql/help_topic | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 9437184 | 9453568 | | 5 | mysql/help_category | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 114688 | 118784 | | 6 | mysql/help_relation | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 131072 | 135168 | | 7 | mysql/help_keyword | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 245760 | 249856 | | 8 | mysql/time_zone_name | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 9 | mysql/time_zone | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 10 | mysql/time_zone_transition | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 11 | mysql/time_zone_transition_type | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 12 | mysql/time_zone_leap_second | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 13 | mysql/innodb_table_stats | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 14 | mysql/innodb_index_stats | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 15 | mysql/slave_relay_log_info | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 16 | mysql/slave_master_info | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 17 | mysql/slave_worker_info | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 18 | mysql/gtid_executed | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 19 | mysql/server_cost | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 20 | mysql/engine_cost | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 21 | sys/sys_config | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 98304 | 102400 | | 46 | teste01/table_01 | 33 | Barracuda | Dynamic | 16384 | 0 | Single | 4096 | 114688 | 118784 | | 49 | TBS_DADOS | 2048 | Any | Any | 16384 | 0 | General | 4096 | 65536 | 69632 | +-------+---------------------------------+------+-------------+------------+-----------+---------------+------------+---------------+-----------+----------------+ 22 rows in set (0,00 sec) mysql> USE teste01; Database changed mysql> CREATE TABLE customer (id int) TABLESPACE=TBS_DADOS; Query OK, 0 rows affected (0,03 sec)
  • 59. MySQL – CREATE TABLESPACE mysql> ALTER TABLESPACE TBS_DADOS ADD DATAFILE '/tmp/TBS_DADOS_DATAFILE_002.ibd'; ERROR 1178 (42000): The storage engine for the table doesn't support ALTER TABLESPACE mysql> SELECT FILE_NAME, FILE_TYPE, LOGFILE_GROUP_NAME, STATUS, EXTRA FROM INFORMATION_SCHEMA.FILES; +---------------------------------------+------------+--------------------+--------+-------+ | FILE_NAME | FILE_TYPE | LOGFILE_GROUP_NAME | STATUS | EXTRA | +---------------------------------------+------------+--------------------+--------+-------+ | ./ibdata1 | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/engine_cost.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/gtid_executed.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/help_category.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/help_keyword.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/help_relation.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/help_topic.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/innodb_index_stats.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/innodb_table_stats.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/plugin.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/server_cost.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/servers.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/slave_master_info.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/slave_relay_log_info.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/slave_worker_info.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/time_zone.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/time_zone_leap_second.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/time_zone_name.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/time_zone_transition.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./mysql/time_zone_transition_type.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./sys/sys_config.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./teste01/table_01.ibd | TABLESPACE | NULL | NORMAL | NULL | | ./ibtmp1 | TEMPORARY | NULL | NORMAL | NULL | | /tmp/TBS_DADOS_DATAFILE_001.ibd | TABLESPACE | NULL | NORMAL | NULL | +---------------------------------------+------------+--------------------+--------+-------+ 24 rows in set (0,01 sec) mysql> DROP TABLE customer; Query OK, 0 rows affected (0,01 sec) mysql> DROP TABLESPACE TBS_DADOS; Query OK, 0 rows affected (0,01 sec)
  • 61. MySQL - InnoDB vs. MyISAM Diferente da engine InnoDB que armazena em buffer de memória, tanto blocos de dados de tabelas, quanto blocos de dados de índices, a engine MyISAM armazena em buffer de memória apenas dados de blocos de índices. Com a evolução da engine InnoDB ao longo do tempo, a mesma vem oferecendo muitos recursos das quais a engine MyISAM não é capaz de oferecer. Em relação aos níveis de locks, a engine MyISAM possui tratamento de lock apenas no nível de tabela. • SELECTs travam UPDATEs, DELETEs mas não INSERTs • INSERTs, UPDATEs, DELETEs travam todas as instruções SQL A engine InnoDB oferece tratamento de locks no nível de linhas de tabelas, além de prover recursos como Foreign Keys que ajudam o otimizador do banco de dados a encontrar melhores planos de execução para os SQLs que são executados. A engine InnoDB é compatível com as propriedades ACID oferecendo recursos transacionais, além de ser mais seguro que a engine MyISAM no que se refere a corrupção de dados em caso de falhas. Em relação ao parâmetros de configuração, somente a engine InnoDB oferece parâmetros de ajustes finos.
  • 62. MySQL - Convert MyISAM to InnoDB mysql> SELECT CONCAT('ALTER TABLE `',table_schema,'`.`',table_name,'` ENGINE=InnoDB;') -> FROM information_schema.tables -> WHERE table_schema='bd01' -> AND ENGINE='MyISAM'; +--------------------------------------------------------------------------+ | CONCAT('ALTER TABLE `',table_schema,'`.`',table_name,'` ENGINE=InnoDB;') | +--------------------------------------------------------------------------+ | ALTER TABLE `bd01`.`cerber_acl` ENGINE=InnoDB; | | ALTER TABLE `bd01`.`cerber_blocks` ENGINE=InnoDB; | | ALTER TABLE `bd01`.`cerber_lab` ENGINE=InnoDB; | | ALTER TABLE `bd01`.`cerber_log` ENGINE=InnoDB; | | ALTER TABLE `bd01`.`wp_jc_commentmeta` ENGINE=InnoDB; | | ALTER TABLE `bd01`.`wp_jc_termmeta` ENGINE=InnoDB; | | ALTER TABLE `bd01`.`wp_jc_terms` ENGINE=InnoDB; | | ALTER TABLE `bd01`.`wp_jc_usermeta` ENGINE=InnoDB; | | ALTER TABLE `bd01`.`wp_jc_users` ENGINE=InnoDB; | +--------------------------------------------------------------------------+ 9 rows in set (0.04 sec) mysql> ALTER TABLE `bd01`.`cerber_log` ENGINE=InnoDB; ALTER TABLE `wordpress`.`wp_jc_posts` ENGINE=InnoDB; Query OK, 219 rows affected (0.10 sec) Records: 219 Duplicates: 0 Warnings: 0 ALTER TABLE `bd01`.`wp_jc_terms` ENGINE=InnoDB; Query OK, 642 rows affected (0.26 sec) Records: 642 Duplicates: 0 Warnings: 0 mysql> show full processlist; +----+------+---------------------+------+---------+------+-------------------+-------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+---------------------+------+---------+------+-------------------+-------------------------------+ | 5 | root | localhost | bd01 | Sleep | 804 | | NULL | | 6 | root | localhost | NULL | Query | 0 | starting | show full processlist | | 7 | root | 192.168.56.51:49970 | bd01 | Sleep | 103 | | NULL | | 8 | root | 192.168.56.51:49971 | NULL | Sleep | 9838 | | NULL | | 14 | root | localhost | bd01 | Query | 15 | copy to tmp table | alter table wp engine innodb | +----+------+---------------------+------+---------+------+-------------------+-------------------------------+
  • 63. MySQL - InnoDB vs. MyISAM
  • 64. MySQL - InnoDB vs. MyISAM
  • 66. MySQL - Autocommit mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ 1 row in set (0,03 sec) mysql> SET AUTOCOMMIT = 0; mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | OFF | +---------------+-------+ MySQL operates in AUTOCOMMIT mode by default. This means that unless you’ve explicitly begun a transaction, it automatically executes each query in a separate transaction. You can enable or disable AUTOCOMMIT for the current connection by setting a variable: The values 1 and ON are equivalent, as are 0 and OFF. When you run with AUTOCOMMIT=0, you are always in a transaction, until you issue a COMMIT or ROLLBACK. MySQL then starts a new transaction immediately. Changing the value of AUTOCOMMIT has no effect on nontransactional tables, such as MyISAM or Memory tables, which have no option of committing or rolling back changes.
  • 67. ISOLATION LEVEL (READ COMMITTED vs. REPEATABLE READ)
  • 68. ISOLATION LEVEL (READ COMMITTED vs. REPEATABLE READ)
  • 69. mysql> CREATE TABLE transaction_test( -> id INT UNSIGNED NOT NULL AUTO_INCREMENT, -> val VARCHAR(20) NOT NULL, -> created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -> PRIMARY KEY(id) -> ) ENGINE=InnoDB DEFAULT CHARSET latin1; Query OK, 0 rows affected (0.29 sec) mysql> INSERT INTO transaction_test(val) VALUES ('a'),('b'),('c'); Query OK, 3 rows affected (0.08 sec) mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM transaction_test; +----+-----+---------------------+ | id | val | created | +----+-----+---------------------+ | 1 | a | 2017-05-02 10:09:33 | | 2 | b | 2017-05-02 10:09:33 | | 3 | c | 2017-05-02 10:09:33 | +----+-----+---------------------+ 3 rows in set (0.00 sec) SESSION 1 ISOLATION LEVEL (REPEATABLE READ)
  • 70. mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO transaction_test(val) VALUES ('x'),('y'),('z'); COMMIT; SESSION 2 mysql> SELECT * FROM transaction_test; +----+-----+---------------------+ | id | val | created | +----+-----+---------------------+ | 1 | a | 2017-05-02 10:09:33 | | 2 | b | 2017-05-02 10:09:33 | | 3 | c | 2017-05-02 10:09:33 | +----+-----+---------------------+ 3 rows in set (0.00 sec) SESSION 1 ISOLATION LEVEL (REPEATABLE READ)
  • 71. mysql> SET SESSION tx_isolation='READ-COMMITTED'; TRUNCATE TABLE transaction_test; INSERT INTO transaction_test(val) VALUES ('a'),('b'),('c'); mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) SESSION 1 ISOLATION LEVEL (READ COMMITED) mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO transaction_test(val) VALUES ('x'); Query OK, 3 rows affected (0.00 sec) mysql> COMMIT; SESSION 2 mysql> SELECT * FROM transaction_test; +----+--------+---------------------+ | id | val | created | +----+--------+---------------------+ | 1 | a | 2017-05-02 10:18:08 | | 2 | b | 2017-05-02 10:18:08 | | 3 | c | 2017-05-02 10:18:08 | | 4 | x | 2017-05-02 10:19:00 | +----+--------+---------------------+ SESSION 1
  • 72. MySQL DEFAULT ISOLATION LEVEL mysql> show variables like '%isolation%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | tx_isolation | REPEATABLE-READ | +---------------+-----------------+ 1 row in set (0,06 sec)
  • 73. Locks
  • 74. mysql> START TRANSACTION; Query OK, 0 rows affected (0,00 sec) mysql> update t2 set id=10; Query OK, 1 row affected (0,01 sec) Rows matched: 1 Changed: 1 Warnings: 0 SESSION 1 MySQL - Locks / innodb_lock_wait_timeout mysql> update t2 set id=20; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction SESSION 2 mysql> show variables like 'innodb_lock_wait_timeout'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 50 | +--------------------------+-------+ 1 row in set (0,19 sec)
  • 75. mysql> START TRANSACTION; Query OK, 0 rows affected (0,00 sec) mysql> update t2 set id=10; Query OK, 1 row affected (0,01 sec) Rows matched: 1 Changed: 1 Warnings: 0 SESSION 1 MySQL - Locks / innodb_lock_wait_timeout mysql> SET innodb_lock_wait_timeout = 50000; Query OK, 0 rows affected (0,00 sec) mysql> update t2 set id=20; hang SESSION 2 mysql> show full processlist; +----+------+-----------+------+---------+------+----------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+----------+-----------------------+ | 4 | root | localhost | bd01 | Sleep | 818 | | NULL | | 5 | root | localhost | bd01 | Query | 97 | updating | update t2 set id=20 | | 6 | root | localhost | NULL | Query | 0 | starting | show full processlist | +----+------+-----------+------+---------+------+----------+-----------------------+ mysql> select trx_mysql_thread_id,trx_id,trx_state, -> trx_started,trx_isolation_level, -> trx_query FROM `information_schema`.`innodb_trx`; +---------------------+--------+-----------+---------------------+---------------------+---------------------+ | trx_mysql_thread_id | trx_id | trx_state | trx_started | trx_isolation_level | trx_query | +---------------------+--------+-----------+---------------------+---------------------+---------------------+ | 5 | 11792 | LOCK WAIT | 2017-04-21 11:37:06 | REPEATABLE READ | update t2 set id=20 | | 4 | 11790 | RUNNING | 2017-04-21 11:25:05 | REPEATABLE READ | NULL | +---------------------+--------+-----------+---------------------+---------------------+---------------------+ mysql> SELECT * FROM `information_schema`.`innodb_locks`; +--------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+ | lock_id | lock_trx_id | lock_mode | lock_type | lock_table | lock_index | lock_space | lock_page | lock_rec | lock_data | +--------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+ | 11792:48:3:2 | 11792 | X | RECORD | `bd01`.`t2` | GEN_CLUST_INDEX | 48 | 3 | 2 | 0x000000018300 | | 11790:48:3:2 | 11790 | X | RECORD | `bd01`.`t2` | GEN_CLUST_INDEX | 48 | 3 | 2 | 0x000000018300 | +--------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+
  • 76. mysql> SHOW ENGINE INNODB STATUS ------------ TRANSACTIONS ------------ Trx id counter 11794 Purge done for trx's n:o < 11789 undo n:o < 0 state: running but idle History list length 2 LIST OF TRANSACTIONS FOR EACH SESSION: ---TRANSACTION 18446744072350939916, not started 0 lock struct(s), heap size 1080, 0 row lock(s) ---TRANSACTION 18446744072350939356, not started 0 lock struct(s), heap size 1080, 0 row lock(s) ---TRANSACTION 11792, ACTIVE 2846 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 2 lock struct(s), heap size 1080, 1 row lock(s) MySQL thread id 5, OS thread handle 2763115376, query id 37 localhost root updating update t2 set id=20 ------- TRX HAS BEEN WAITING 2846 SEC FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 48 page no 3 n bits 72 index GEN_CLUST_INDEX of table `bd01`.`t2` trx id 11792 lock_mode X waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0 0: len 6; hex 000000018300; asc ;; 1: len 6; hex 000000002e0e; asc . ;; 2: len 7; hex 2c0000013b0c4e; asc , ; N;; 3: len 4; hex 8000000a; asc ;; ------------------ ---TRANSACTION 11790, ACTIVE 3567 sec 2 lock struct(s), heap size 1080, 2 row lock(s), undo log entries 1 MySQL thread id 4, OS thread handle 2763316080, query id 28 localhost root -------- MySQL - Locks / INNODB STATUS
  • 77. mysql> SELECT p.id, -> p.HOST, -> p.DB, -> p.time, -> t.trx_state, -> t.trx_query, -> t.trx_started -> FROM INFORMATION_SCHEMA.INNODB_TRX t -> JOIN INFORMATION_SCHEMA.PROCESSLIST p -> ON (p.ID = t.TRX_MYSQL_THREAD_ID) -> LEFT JOIN INFORMATION_SCHEMA.INNODB_LOCK_WAITS ot -> ON (ot.BLOCKING_TRX_ID = t.TRX_id); +----+-----------+------+------+-----------+---------------------+---------------------+ | id | HOST | DB | time | trx_state | trx_query | trx_started | +----+-----------+------+------+-----------+---------------------+---------------------+ | 4 | localhost | bd01 | 2858 | RUNNING | NULL | 2017-04-21 11:25:05 | | 5 | localhost | bd01 | 2137 | LOCK WAIT | update t2 set id=20 | 2017-04-21 11:37:06 | +----+-----------+------+------+-----------+---------------------+---------------------+ mysql> SELECT CONCAT('KILL ',id,';'),CONCAT('KILL QUERY ',id,';') -> FROM information_schema.processlist WHERE db = 'bd01'; +------------------------+------------------------------+ | CONCAT('KILL ',id,';') | CONCAT('KILL QUERY ',id,';') | +------------------------+------------------------------+ | KILL 4; | KILL QUERY 4; | | KILL 5; | KILL QUERY 5; | +------------------------+------------------------------+ 2 rows in set (0,02 sec) mysql> KILL 4; Query OK, 0 rows affected (0,00 sec) MySQL - Locks / Querying / Killing SESSION 2 mysql> update t2 set id=20; Query OK, 1 row affected (58 min 0,83 sec) Rows matched: 1 Changed: 1 Warnings: 0 SESSION 1 mysql> select 1; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 10 Current database: bd01 +---+ | 1 | +---+ | 1 | +---+ 1 row in set (0,06 sec)
  • 78. mysql> START TRANSACTION; Query OK, 0 rows affected (0,01 sec) mysql> update t2 set id=30; Query OK, 1 row affected (0,00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> alter table t2 modify column id bigint; hang mysql> SHOW FULL PROCESSLIST; +----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+ | 5 | root | localhost | bd01 | Query | 580 | Waiting for table metadata lock | alter table t2 modify column id bigint | | 6 | root | localhost | NULL | Query | 0 | starting | show full processlist | | 7 | root | 192.168.56.51:49970 | NULL | Sleep | 400 | | NULL | | 8 | root | 192.168.56.51:49971 | NULL | Sleep | 6733 | | NULL | | 10 | root | localhost | bd01 | Sleep | 682 | | NULL | +----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+ mysql> SELECT * -> FROM information_schema.processlist -> WHERE state IN ('Waiting for table flush', -> 'Locked', -> 'System lock', -> 'Table lock' ) -> OR state REGEXP 'Waiting for .* lock' -> ORDER BY TIME DESC; +----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+ | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | +----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+ | 5 | root | localhost | bd01 | Query | 606 | Waiting for table metadata lock | alter table t2 modify column id bigint | +----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+ MySQL - Metadata Locks SESSION 1 SESSION 2
  • 79. mysql> LOCK TABLES customer READ; Query OK, 0 rows affected (0,01 sec) mysql> delete from customer where name=1; hang mysql> SHOW FULL PROCESSLIST; +----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+ | 5 | root | localhost | bd01 | Query | 12 | Waiting for table metadata lock | delete from customer where name=1 | | 6 | root | localhost | NULL | Query | 0 | starting | show full processlist | | 7 | root | 192.168.56.51:49970 | NULL | Sleep | 899 | | NULL | | 8 | root | 192.168.56.51:49971 | NULL | Sleep | 7232 | | NULL | | 10 | root | localhost | bd01 | Sleep | 34 | | NULL | +----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+ mysql> SELECT * -> FROM information_schema.processlist -> WHERE state IN ('Waiting for table flush', -> 'Locked', -> 'System lock', -> 'Table lock' ) -> OR state REGEXP 'Waiting for .* lock' -> ORDER BY TIME DESC; +----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+ | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | +----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+ | 5 | root | localhost | bd01 | Query | 560 | Waiting for table metadata lock | delete from customer where name=1 | +----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+ mysql> UNLOCK TABLES; Query OK, 0 rows affected (0,01 sec) MySQL - Metadata Locks SESSION 1 SESSION 2 SESSION 1 mysql> delete from customer where name=1; Query OK, 1 row affected (11 min 39,52 sec) SESSION 2
  • 80. mysql> CREATE TABLE emp (id int) ENGINE=MyISAM; mysql> select count(*) from emp; +----------+ | count(*) | +----------+ | 2097153 | +----------+ 1 row in set (0,01 sec) mysql> update emp set id=20000; updating mysql> select count(*) from emp; hang mysql> SHOW FULL PROCESSLIST; +----+------+---------------------+------+---------+------+------------------------------+--------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+---------------------+------+---------+------+------------------------------+--------------------------+ | 5 | root | localhost | bd01 | Query | 4 | Waiting for table level lock | select count(*) from emp | | 6 | root | localhost | NULL | Query | 0 | starting | show full processlist | | 7 | root | 192.168.56.51:49970 | NULL | Sleep | 2705 | | NULL | | 8 | root | 192.168.56.51:49971 | NULL | Sleep | 9038 | | NULL | | 14 | root | localhost | bd01 | Query | 14 | updating | update emp set id=20000 | +----+------+---------------------+------+---------+------+------------------------------+--------------------------+ mysql> select count(*) from emp; +----------+ | count(*) | +----------+ | 2097153 | +----------+ 1 row in set (2 min 26,55 sec) MySQL - Table Locks MyISAM SESSION1 SESSION 2 SESSION 2 mysql> SHOW CREATE TABLE bd01.emp; +-------+---------------------------------------+ | Table | Create Table | +-------+---------------------------------------- | emp | CREATE TABLE `emp` ( | | `id` int DEFAULT NULL | | ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +-------+---------------------------------------+
  • 82. MySQL - Enabling Binary Logs mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | OFF | +---------------+-------+ 1 row in set (0,04 sec) [root@linux1~]# cat /etc/my.cnf server_id=1 log_bin=/var/lib/mysql/mysql-bin.log root@linux1~]# service mysqld restart Parando o mysqld: [ OK ] Iniciando o mysqld: [ OK ] mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0,04 sec) mysql> SHOW BINARY LOGS; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 154 | +------------------+-----------+ 1 row in set (0,00 sec) mysql> SELECT @@log_bin; +-----------+ | @@log_bin | +-----------+ | 1 | +-----------+ 1 row in set (0,01 sec) [root@linux1~]# ls -lh mysql-bin.* -rw-r----- 1 mysql mysql 154 Abr 12 15:44 mysql-bin.000001 -rw-r----- 1 mysql mysql 32 Abr 12 15:44 mysql-bin.index +---------------------------------+--------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------+ | log_bin_basename | /var/lib/mysql/mysql-bin | | log_bin_index | /var/lib/mysql/mysql-bin.index | | binlog_format | ROW | | max_binlog_size | 1073741824 | | expire_logs_days | 0 | | sync_binlog | 1 | +---------------------------------+--------------------------------+ mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+
  • 83. MySQL - Viewing Contents of Binary Logs [root@linux1~]# mysqlbinlog mysql-bin.000001 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #170419 20:21:30 server id 1 end_log_pos 123 CRC32 0x14c02170 Start: binlog v 4, server v 5.7.18-log created 170419 20:21:30 at startup ROLLBACK/*!*/; BINLOG ' +vD3WA8BAAAAdwAAAHsAAAAAAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAD68PdYEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA AXAhwBQ= '/*!*/; # at 123 #170419 20:21:30 server id 1 end_log_pos 154 CRC32 0xb9a3dfb0 Previous-GTIDs # [empty] # at 154 #170419 20:36:16 server id 1 end_log_pos 219 CRC32 0x8e618eb0 Anonymous_GTID last_committed=0 sequence_number=1 SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 219 #170419 20:36:16 server id 1 end_log_pos 291 CRC32 0x1930ff1a Query thread_id=7 exec_time=0 error_code=0 SET TIMESTAMP=1492644976/*!*/; SET @@session.pseudo_thread_id=7/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1436549152/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; BEGIN /*!*/; # at 291 #170419 20:36:16 server id 1 end_log_pos 338 CRC32 0x1381aefd Table_map: `bd01`.`t1` mapped to number 108 # at 338 #170419 20:36:16 server id 1 end_log_pos 382 CRC32 0x8c6cf04e Write_rows: table id 108 flags: STMT_END_F
  • 84. MySQL - Viewing Contents of Binary Logs mysql> SHOW BINARY LOGS; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 428 | +------------------+-----------+ 1 row in set (0,00 sec) mysql> SHOW BINLOG EVENTS IN 'mysql-bin.000001'; +------------------+-----+----------------+-----------+-------------+---------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+----------------+-----------+-------------+---------------------------------------+ | mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.18-log, Binlog ver: 4 | | mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 219 | Query | 1 | 294 | BEGIN | | mysql-bin.000001 | 294 | Table_map | 1 | 351 | table_id: 219 (teste01.table_01) | | mysql-bin.000001 | 351 | Write_rows | 1 | 397 | table_id: 219 flags: STMT_END_F | | mysql-bin.000001 | 397 | Xid | 1 | 428 | COMMIT /* xid=31 */ | +------------------+-----+----------------+-----------+-------------+---------------------------------------+ 7 rows in set (0,00 sec) mysql> create table table_03 (id int); Query OK, 0 rows affected (0,04 sec) mysql> SHOW BINLOG EVENTS IN 'mysql-bin.000001'; +------------------+-----+----------------+-----------+-------------+-----------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+----------------+-----------+-------------+-----------------------------------------------+ | mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.18-log, Binlog ver: 4 | | mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 219 | Query | 1 | 294 | BEGIN | | mysql-bin.000001 | 294 | Table_map | 1 | 351 | table_id: 219 (teste01.table_01) | | mysql-bin.000001 | 351 | Write_rows | 1 | 397 | table_id: 219 flags: STMT_END_F | | mysql-bin.000001 | 397 | Xid | 1 | 428 | COMMIT /* xid=31 */ | | mysql-bin.000001 | 428 | Anonymous_Gtid | 1 | 493 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000001 | 493 | Query | 1 | 603 | use `teste01`; create table table_03 (id int) | +------------------+-----+----------------+-----------+-------------+-----------------------------------------------+ 9 rows in set (0,01 sec)
  • 85. MySQL - Rotating Binary Logs (utilities) [root@linux1~]# rpm -ivh mysql-connector-python-2.1.6-1.el7.x86_64.rpm aviso: mysql-connector-python-2.1.6-1.el6.i686.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparando... ########################################### [100%] 1:mysql-connector-python ########################################### [100%] [root@linux1~]# rpm -ivh mysql-utilities-1.6.5-1.el7.noarch.rpm aviso: mysql-utilities-1.6.5-1.el7.noarch.rpm: Cabeçalho V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparando... ########################################### [100%] 1:mysql-utilities ########################################### [100%] [root@linux1 ~]# mysqlbinlogrotate --server=root:admin@localhost:3306 -v # Checking user permission to rotate binary logs... # # Active binlog file: 'mysql-bin.000001' (size: 931 bytes)' # The binlog file has been rotated. # New active binlog file: 'mysql-bin.000002' [root@linux1 ~]# mysqlbinlogrotate --server=root:admin@localhost:3306 -v # Checking user permission to rotate binary logs... # # Active binlog file: 'mysql-bin.000002' (size: 1449 bytes)' # The binlog file has been rotated. # New active binlog file: 'mysql-bin.000003‘ [root@linux1 ~]# mysqlbinlogrotate --server=root:admin@localhost:3306 -v # Checking user permission to rotate binary logs... # # Active binlog file: 'mysql-bin.000003' (size: 1345 bytes)' # The binlog file has been rotated. # New active binlog file: 'mysql-bin.000004' https://dev.mysql.com/downloads/connector/python/ https://dev.mysql.com/downloads/utilities/
  • 86. MySQL - Purging Binary Logs mysql> SHOW BINARY LOGS; +------------------+------------+ | Log_name | File_size | +------------------+------------+ | mysql-bin.000858 | 1073769033 | | mysql-bin.000859 | 1073772184 | | mysql-bin.000860 | 1073769005 | | mysql-bin.000861 | 1073809148 | | mysql-bin.000862 | 1073765575 | | mysql-bin.000863 | 1073797185 | | mysql-bin.000864 | 1073757152 | | mysql-bin.000865 | 1073763101 | | mysql-bin.000866 | 1073771131 | | mysql-bin.000867 | 1073770555 | | mysql-bin.000868 | 1073744076 | | mysql-bin.000869 | 1073818669 | | mysql-bin.000870 | 1073754790 | | mysql-bin.000871 | 1073770448 | | mysql-bin.000872 | 1073755079 | | mysql-bin.000873 | 1073744861 | | mysql-bin.000874 | 1073807803 | | mysql-bin.000875 | 1073777945 | | mysql-bin.000876 | 1073741999 | | mysql-bin.000877 | 718403419 | +------------------+------------+ 20 rows in set (0.00 sec) mysql> PURGE BINARY LOGS TO 'mysql-bin.000878'; ERROR 1373 (HY000): Target log not found in binlog index mysql> PURGE BINARY LOGS BEFORE '2017-04-22 13:00:00'; Query OK, 0 rows affected, 1 warning (0,00 sec) mysql> PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND; Query OK, 0 rows affected, 1 warning (0,00 sec) mysql> show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000003 | 154 | | mysql-bin.000004 | 154 | +------------------+-----------+ mysql> reset master; Query OK, 0 rows affected (0.02 sec) mysql> show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 154 | +------------------+-----------+ RESET MASTER: Deletes all binary log files listed in the index file, resets the binary log index file to be empty, and creates a new binary log file.
  • 87. MySQL - General Log File mysql> show variables like '%general%'; mysql> show variables like 'log_output'; +------------------+---------------------------+ +---------------+-------+ | Variable_name | Value | | Variable_name | Value | +------------------+---------------------------+ +---------------+-------+ | general_log | OFF | | log_output | FILE | | general_log_file | /var/lib/mysql/linux1.log | +---------------+-------+ +------------------+---------------------------+ mysql> USE teste01; Database changed mysql> create table t1(id int); Query OK, 0 rows affected (0,43 sec) mysql> select * from t1 order by 1; +------+ | id | +------+ | 10 | +------+ mysql> delete from t1; Query OK, 1 row affected (0,08 sec) mysql> select * from mysql.general_log; +----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+ | event_time | user_host | thread_id | server_id | command_type | argument | +----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+ | 2017-05-02 20:15:30.546899 | root[root] @ localhost [] | 15 | 0 | Query | SELECT DATABASE() | | 2017-05-02 20:15:30.556462 | root[root] @ localhost [] | 15 | 0 | Init DB | teste01 | | 2017-05-02 20:15:30.572724 | root[root] @ localhost [] | 15 | 0 | Query | show databases | | 2017-05-02 20:15:30.579491 | root[root] @ localhost [] | 15 | 0 | Query | show tables | | 2017-05-02 20:15:40.651512 | root[root] @ localhost [] | 15 | 0 | Query | create table t1(id int) | | 2017-05-02 20:15:52.616530 | root[root] @ localhost [] | 15 | 0 | Query | insert into t1 values (10) | | 2017-05-02 20:16:10.976444 | root[root] @ localhost [] | 15 | 0 | Query | select * from t1 order by 1 | | 2017-05-02 20:16:16.186997 | root[root] @ localhost [] | 15 | 0 | Query | delete from t1 | | 2017-05-02 20:16:29.026673 | root[root] @ localhost [] | 15 | 0 | Query | select * from mysql.general_log | +----------------------------+---------------------------+-----------+-----------+--------------+---------------------------------+ mysql> set GLOBAL general_log = 1; mysql> set GLOBAL general_log_file = 'TABLE'; mysql> show variables like 'general_log'; +---------------+--------+ | Variable_name | Value | +---------------+--------+ | general_log | ON | +---------------+--------+ mysql> show variables like 'log_output'; +---------------+--------+ | Variable_name | Value | +---------------+--------+ | log_output | TABLE | +---------------+--------+
  • 89. MySQL - Generate CSV Files mysql> select * from customer; +------+------+ | id | name | +------+------+ | 1 | Joe | | 2 | Jack | | 3 | Zack | +------+------+ 3 rows in set (0,00 sec) SELECT * INTO OUTFILE '/tmp/customer.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' FROM customer; ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement mysql> SHOW VARIABLES LIKE "secure_file_priv"; +------------------+-----------------------+ | Variable_name | Value | +------------------+-----------------------+ | secure_file_priv | /var/lib/mysql-files/ | +------------------+-----------------------+ 1 row in set (0,01 sec) mysql> SELECT * INTO OUTFILE '/var/lib/mysql-files/customer.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' FROM customer; Query OK, 3 rows affected (0,01 sec) mysql> system cat /var/lib/mysql-files/customer.csv "1","Joe" "2","Jack" "3","Zack"
  • 90. MySQL - Generate CSV Files (Headers) SET @table_name = 'customer'; SET @table_schema = 'bd01'; SET SESSION group_concat_max_len = 1000000; SET @col_names = ( SELECT GROUP_CONCAT(QUOTE(`column_name`)) AS columns FROM information_schema.columns WHERE table_schema = @table_schema AND table_name = @table_name); SET @cols = CONCAT('(SELECT ', @col_names, ')'); SET @query = CONCAT('(SELECT * FROM ', @table_schema, '.', @table_name, ' INTO OUTFILE '/var/lib/mysql-files/customer.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"')'); SET @sql = CONCAT(@cols, ' UNION ALL ', @query); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; system cat /var/lib/mysql-files/customer.csv; mysql> SET @table_name = 'customer'; Query OK, 0 rows affected (0.00 sec) mysql> SET @table_schema = 'bd01'; Query OK, 0 rows affected (0.00 sec) mysql> SET SESSION group_concat_max_len = 1000000; Query OK, 0 rows affected (0.00 sec) mysql> SET @col_names = ( -> SELECT GROUP_CONCAT(QUOTE(`column_name`)) AS columns -> FROM information_schema.columns -> WHERE table_schema = @table_schema -> AND table_name = @table_name); Query OK, 0 rows affected (0.00 sec) mysql> SET @cols = CONCAT('(SELECT ', @col_names, ')'); Query OK, 0 rows affected (0.00 sec) mysql> SET @query = CONCAT('(SELECT * FROM ', @table_schema, '.', @table_name, -> ' INTO OUTFILE '/var/lib/mysql-files/customer.csv' '> FIELDS TERMINATED BY ',' ENCLOSED BY '"')'); Query OK, 0 rows affected (0.00 sec) mysql> SET @sql = CONCAT(@cols, ' UNION ALL ', @query); Query OK, 0 rows affected (0.00 sec) mysql> PREPARE stmt FROM @sql; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> EXECUTE stmt; Query OK, 4 rows affected (0.00 sec) mysql> DEALLOCATE PREPARE stmt; Query OK, 0 rows affected (0.00 sec) mysql> system cat /var/lib/mysql-files/customer.csv; "id","name" "1","Joe" "2","Jack" "3","Zack"
  • 91. MySQL - LOAD CSV Files mysql> TRUNCATE TABLE customer; Query OK, 0 rows affected (0,02 sec) mysql> select * from customer; Empty set (0,00 sec) mysql> LOAD DATA LOCAL INFILE '/var/lib/mysql-files/customer.csv' INTO TABLE customer FIELDS TERMINATED BY ',' -> ENCLOSED BY '"' LINES TERMINATED BY 'n' -> (id,name); Query OK, 3 rows affected (0,02 sec) Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 mysql> select * from customer; +------+------+ | id | name | +------+------+ | 1 | Joe | | 2 | Jack | | 3 | Zack | +------+------+ 3 rows in set (0,00 sec) mysql> TRUNCATE TABLE customer; Query OK, 0 rows affected (0,02 sec) [root@linux1 ~]# mysqlimport --columns='id,name' --fields-terminated-by=',' --fields-enclosed-by '"' --local -uroot -padmin bd01 /var/lib/mysql-files/customer.csv mysql> select * from customer; +------+------+ | id | name | +------+------+ | 1 | Joe | | 2 | Jack | | 3 | Zack | +------+------+ 3 rows in set (0,00 sec) No caso do mysqlimport, o nome do arquivo tem que ser igual ao nome da tabela de destino
  • 92. MySQL - Executing External Scripts [root@linux1 ~]# cat script.sql show databases; select @@version; select now(); root@linux1 ~]# mysql -uroot -padmin -e "source /tmp/script.sql" +--------------------+ | Database | +--------------------+ | information_schema | | bd01 | | mysql | | performance_schema | | sys | +--------------------+ +------------+ | @@version | +------------+ | 5.7.18-log | +------------+ +---------------------+ | now() | +---------------------+ | 2017-05-02 15:12:50 | +---------------------+
  • 93. MySQL - Executing External Scripts TEE Script_Master.log use dbname source script1.sql source script2.sql NOTEE TEE script_Master_Rollback.log use dbname source script2.sql source script1.sql NOTEE mysql -uuser -ppass < Script_Master.sql --verbose > log.txt
  • 95. MySQL - Export (mysqldump) [root@linux1]# mysqldump -uroot -padmin --single-transaction --opt --add-drop-database -B teste02 --result-file=/tmp/teste02.sql [root@linux1]# cat /tmp/teste02.sql -- MySQL dump 10.13 Distrib 5.7.18, for Linux (i686) -- -- Host: localhost Database: teste02 -- ------------------------------------------------------ -- Server version 5.7.18-log -- Current Database: `teste02` /*!40000 DROP DATABASE IF EXISTS `teste02`*/; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `teste02` /*!40100 DEFAULT CHARACTER SET latin1 */; USE `teste02`; -- -- Table structure for table `a` -- DROP TABLE IF EXISTS `a`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `a` ( `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; LOCK TABLES `a` WRITE; /*!40000 ALTER TABLE `a` DISABLE KEYS */; INSERT INTO `a` VALUES (1); /*!40000 ALTER TABLE `a` ENABLE KEYS */; UNLOCK TABLES; . . . --opt  Shorthand for --add-drop-table --add- locks --create-options --disable-keys --extended- insert --lock-tables --quick --set-charset
  • 96. MySQL - Export (mysqldump) mysql> show grants for [email protected]; +------------------------------------------------------------------------------------------------------------------+ | Grants for [email protected] | +------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'backup'@'192.168.56.101' IDENTIFIED BY PASSWORD '*26C3C87785FA94A58BEF806DE0774FE6D14906' | | GRANT SELECT, SHOW VIEW, TRIGGER ON `mysql`.* TO 'backup'@'192.168.56.101' | | GRANT SELECT, LOCK TABLES, SHOW VIEW, TRIGGER ON `bd01`.* TO 'backup'@'192.168.56.101' | | GRANT SELECT, LOCK TABLES, SHOW VIEW, TRIGGER ON `bd02`.* TO 'backup'@'192.168.56.101' | +------------------------------------------------------------------------------------------------------------------+ Examples ------------ mysqldump --all-databases --user=root --password=admin --single-transaction --opt --add-drop-database --routines --triggers --events > /tmp/all-databases.sql mysqldump --all-databases --user=root --password=admin --single-transaction --opt --add-drop-database --routines --triggers --events | gzip > /tmp/all-databases.sql.gz mysqldump -uroot -padmin --single-transaction --opt --add-drop-database --databases teste01 > /tmp/teste01.sql mysqldump -uroot -padmin --single-transaction --opt --add-drop-database -B teste01 > /tmp/teste01.sql (-B ou --databases  create database) mysqldump -uroot -padmin --single-transaction --opt teste01 > /tmp/teste01.sql mysqldump -uroot -padmin --single-transaction --opt --add-drop-database --databases teste01 teste02 > /tmp/teste01_02.sql mysqldump -uroot -padmin --single-transaction --opt teste01 customer > /tmp/customer.sql mysqldump -uroot -padmin --single-transaction --opt teste02 customer emp > /tmp/customer_emp.sql -- Only DDL mysqldump -uroot -padmin --no-data --routines --triggers --events teste02 > teste02_ddl.sql -- Only Data mysqldump -uroot -padmin --single-transaction --opt --no-create-info teste02 > teste02_data.sql -- Ignore table mysqldump -uroot -padmin --single-transaction --opt --add-drop-database -B teste02 --ignore-table=teste02.customer > teste02_ignore.sql http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html Ignora os bancos sys, information_schema, performance_schema
  • 97. MySQL - Import (mysql) -- O comando CREATE DATABASE está no DUMP mysql -uroot -padmin < bd01.sql -- O comando CREATE DATABASE NÃO está no DUMP mysql -uroot -padmin bd01 < bd01.sql -- Import com log mysql -uroot -padmin < /tmp/all-databases.sql --verbose > /tmp/import.log -- Import com logs separados mysql -uroot -padmin < /tmp/all-databases.sql --verbose > /tmp/import.log 2> /tmp/import_erro.log -- Import de tabela customer para o banco bd01 mysql -uroot -padmin bd01 < customer.sql -- Import remoto mysql -hlinux1 -p3306 -uroot -padmin bd01 < bd01.sql -- Extrair scripts de um único banco de dados do arquivo all-databases.sql para ser importado sed -n '/^-- Current Database: `bd01`/,/^-- Current Database: `/p' /tmp/all-databases.sql > /tmp/bd01.sql
  • 98. MySQL - Point-in-Time Recovery Using the Binary Log mysql> use bd01 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A mysql> show tables; +----------------+ | Tables_in_bd01 | +----------------+ | t1 | +----------------+ 1 row in set (0,00 sec) mysql> desc t1; +-------+-----------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | data | timestamp | NO | | CURRENT_TIMESTAMP | | +-------+-----------+------+-----+-------------------+----------------+ 2 rows in set (0,00 sec) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0,00 sec) mysql> select * from t1; Empty set (0,01 sec)
  • 99. Mysql - Point-in-Time Recovery Using the Binary Log [root@linux1 ~]# mysqldump -uroot -padmin --single-transaction --opt --add-drop-database -B bd01 > /tmp/bd01.sql [root@linux1 ~]# ls -lh /tmp/bd01.sql -rw-r--r-- 1 root root 2,0K Mai 2 20:34 /tmp/bd01.sql -- Insert executado N vezes e depois executado o LOG ROTATE mysql> INSERT INTO t1 VALUES (null,null); [root@linux1 ~]# mysqlbinlogrotate --server=root:admin@localhost:3306 –v RESULTADO mysql> SHOW BINARY LOGS; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 978 | | mysql-bin.000002 | 1496 | | mysql-bin.000003 | 1755 | | mysql-bin.000004 | 1496 | | mysql-bin.000005 | 154 | +------------------+-----------+ 5 rows in set (0,00 sec) mysql> select * from bd01.t1; +----+---------------------+ | id | data | +----+---------------------+ | 1 | 2017-05-02 20:36:16 | | 2 | 2017-05-02 20:36:33 | | 3 | 2017-05-02 20:36:52 | | 4 | 2017-05-02 20:53:26 | | 5 | 2017-05-02 20:53:27 | | 6 | 2017-05-02 20:53:28 | | 7 | 2017-05-02 20:53:29 | | 8 | 2017-05-02 20:53:30 | | 9 | 2017-05-02 20:58:19 | | 10 | 2017-05-02 20:58:20 | | 11 | 2017-05-02 20:58:22 | | 12 | 2017-05-02 20:58:23 | | 13 | 2017-05-02 20:58:24 | | 14 | 2017-05-02 20:58:25 | | 15 | 2017-05-02 21:10:37 | | 16 | 2017-05-02 21:10:38 | | 17 | 2017-05-02 21:10:39 | | 18 | 2017-05-02 21:10:42 | | 19 | 2017-05-02 21:10:43 | +----+---------------------+ 19 rows in set (0,02 sec)
  • 100. MySQL - Point-in-Time Recovery Using the Binary Log mysql> drop database bd01; Query OK, 1 row affected (0,11 sec) mysql -uroot -padmin bd01 < /tmp/bd01.sql mysql> select * from bd01.t1; Empty set (0,01 sec) [root@linux1 ~]# mysqlbinlog mysql-bin.000001 | mysql -uroot -padmin [root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1" +----+---------------------+ | id | data | +----+---------------------+ | 1 | 2017-05-02 20:36:16 | | 2 | 2017-05-02 20:36:33 | | 3 | 2017-05-02 20:36:52 | +----+---------------------+ 3 rows in set (0,00 sec) [root@linux1 ~]# mysqlbinlog mysql-bin.000002 | mysql -uroot -padmin [root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1" +----+---------------------+ | id | data | +----+---------------------+ | 1 | 2017-05-02 20:36:16 | | 2 | 2017-05-02 20:36:33 | | 3 | 2017-05-02 20:36:52 | | 4 | 2017-05-02 20:53:26 | | 5 | 2017-05-02 20:53:27 | | 6 | 2017-05-02 20:53:28 | | 7 | 2017-05-02 20:53:29 | | 8 | 2017-05-02 20:53:30 | +----+---------------------+ 8 rows in set (0,02 sec)
  • 101. MySQL - Point-in-Time Recovery Using the Binary Log [root@linux1 ~]# mysqlbinlog mysql-bin.000003 mysql-bin.000004 | mysql -uroot -padmin [root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1" +----+---------------------+ | id | data | +----+---------------------+ | 1 | 2017-05-02 20:36:16 | | 2 | 2017-05-02 20:36:33 | | 3 | 2017-05-02 20:36:52 | | 4 | 2017-05-02 20:53:26 | | 5 | 2017-05-02 20:53:27 | | 6 | 2017-05-02 20:53:28 | | 7 | 2017-05-02 20:53:29 | | 8 | 2017-05-02 20:53:30 | | 9 | 2017-05-02 20:58:19 | | 10 | 2017-05-02 20:58:20 | | 11 | 2017-05-02 20:58:22 | | 12 | 2017-05-02 20:58:23 | | 13 | 2017-05-02 20:58:24 | | 14 | 2017-05-02 20:58:25 | | 15 | 2017-05-02 21:10:37 | | 16 | 2017-05-02 21:10:38 | | 17 | 2017-05-02 21:10:39 | | 18 | 2017-05-02 21:10:42 | | 19 | 2017-05-02 21:10:43 | +----+---------------------+ [root@linux1 ~]# mysql -uroot -padmin -e "truncate table bd01.t1" [root@linux1 ~]# mysqlbinlog mysql-bin.000004 | mysql -uroot -padmin [root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1" +----+---------------------+ | id | data | +----+---------------------+ | 15 | 2017-05-02 21:10:37 | | 16 | 2017-05-02 21:10:38 | | 17 | 2017-05-02 21:10:39 | | 18 | 2017-05-02 21:10:42 | | 19 | 2017-05-02 21:10:43 | +----+---------------------+ [root@linux1 ~]# mysqlbinlog mysql-bin.000003 mysql-bin.000004 | mysql -uroot -padmin ERROR 1062 (23000) at line 35: Duplicate entry '9' for key 'PRIMARY'
  • 102. MySQL - Point-in-Time Recovery Using the Binary Log [root@linux1 ~]# mysql -uroot -padmin -e "SHOW BINLOG EVENTS IN 'mysql-bin.000003'" +------------------+------+----------------+-----------+-------------+---------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+------+----------------+-----------+-------------+---------------------------------------+ | mysql-bin.000003 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.17-log, Binlog ver: 4 | | mysql-bin.000003 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000003 | 219 | Query | 1 | 291 | BEGIN | | mysql-bin.000003 | 291 | Table_map | 1 | 338 | table_id: 108 (bd01.t1) | | mysql-bin.000003 | 338 | Write_rows | 1 | 382 | table_id: 108 flags: STMT_END_F | | mysql-bin.000003 | 382 | Xid | 1 | 413 | COMMIT /* xid=162 */ | | mysql-bin.000003 | 413 | Anonymous_Gtid | 1 | 478 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | | mysql-bin.000003 | 478 | Query | 1 | 550 | BEGIN | | mysql-bin.000003 | 550 | Table_map | 1 | 597 | table_id: 108 (bd01.t1) | | mysql-bin.000003 | 597 | Write_rows | 1 | 641 | table_id: 108 flags: STMT_END_F | | mysql-bin.000003 | 641 | Xid | 1 | 672 | COMMIT /* xid=163 */ | | mysql-bin.000003 | 737 | Query | 1 | 809 | BEGIN | | mysql-bin.000003 | 809 | Table_map | 1 | 856 | table_id: 108 (bd01.t1) | | mysql-bin.000003 | 856 | Write_rows | 1 | 900 | table_id: 108 flags: STMT_END_F | | mysql-bin.000003 | 900 | Xid | 1 | 931 | COMMIT /* xid=164 */ | | mysql-bin.000003 | 996 | Query | 1 | 1068 | BEGIN | | mysql-bin.000003 | 1068 | Table_map | 1 | 1115 | table_id: 108 (bd01.t1) | | mysql-bin.000003 | 1115 | Write_rows | 1 | 1159 | table_id: 108 flags: STMT_END_F | | mysql-bin.000003 | 1159 | Xid | 1 | 1190 | COMMIT /* xid=165 */ | | mysql-bin.000003 | 1708 | Rotate | 1 | 1755 | mysql-bin.000004;pos=4 | +------------------+------+----------------+-----------+-------------+---------------------------------------+ [root@linux1 ~]# mysql -uroot -padmin -e "truncate table bd01.t1" [root@linux1 ~]# mysqlbinlog --start-position=4 --stop-position=900 mysql-bin.000003 | mysql -uroot -padmin [root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1" +----+---------------------+ | id | data | +----+---------------------+ | 9 | 2017-05-02 20:58:19 | | 10 | 2017-05-02 20:58:20 | +----+---------------------+
  • 103. MySQL - Point-in-Time Recovery Using Event Times mysqlbinlog --start-datetime="2017-04-19 20:36:16" --stop-datetime="2017-04-19 21:10:43" /var/lib/mysql/mysql-bin.0* | mysql -u root -padmin [root@linux1 ~]# mysql -uroot -padmin -e "select * from bd01.t1" +----+---------------------+ | id | data | +----+---------------------+ | 1 | 2017-05-02 20:36:16 | | 2 | 2017-05-02 20:36:33 | | 3 | 2017-05-02 20:36:52 | | 4 | 2017-05-02 20:53:26 | | 5 | 2017-05-02 20:53:27 | | 6 | 2017-05-02 20:53:28 | | 7 | 2017-05-02 20:53:29 | | 8 | 2017-05-02 20:53:30 | | 9 | 2017-05-02 20:58:19 | | 10 | 2017-05-02 20:58:20 | | 11 | 2017-05-02 20:58:22 | | 12 | 2017-05-02 20:58:23 | | 13 | 2017-05-02 20:58:24 | | 14 | 2017-05-02 20:58:25 | | 15 | 2017-05-02 21:10:37 | | 16 | 2017-05-02 21:10:38 | | 17 | 2017-05-02 21:10:39 | | 18 | 2017-05-02 21:10:42 | | 19 | 2017-05-02 21:10:43 | +----+---------------------+ Obs.: É possível direcionar o resultado da execução dos binary logs para um arquivo ao invés de direcionar para o MySQL • mysqlbinlog mysql-bin.000003 > /tmp/mysql-bin.000003.sql • mysqlbinlog --start-datetime="2017-04-19 20:36:16" --stop-datetime="2017-04-19 21:10:43" /var/lib/mysql/mysql-bin.0* > /tmp/script.sql
  • 104. COLD / HOT Backups
  • 105. MySQL – COLD Backup [root@linux1 ~]# systemctl stop mysqld [root@linux1 ~]# cp -a /var/lib/mysql /backup/ [root@linux1 ~]# ls -lh /backup/mysql/ total 109M -rw-r-----. 1 mysql mysql 56 May 4 00:27 auto.cnf drwxr-x---. 2 mysql mysql 4.0K May 4 00:30 bd01 -rw-------. 1 mysql mysql 1.7K May 4 00:27 ca-key.pem -rw-r--r--. 1 mysql mysql 1.1K May 4 00:27 ca.pem -rw-r--r--. 1 mysql mysql 1.1K May 4 00:27 client-cert.pem -rw-------. 1 mysql mysql 1.7K May 4 00:27 client-key.pem -rw-r-----. 1 mysql mysql 358 May 4 00:45 ib_buffer_pool -rw-r-----. 1 mysql mysql 12M May 4 00:45 ibdata1 -rw-r-----. 1 mysql mysql 48M May 4 00:45 ib_logfile0 -rw-r-----. 1 mysql mysql 48M May 4 00:27 ib_logfile1 drwxr-x---. 2 mysql mysql 4.0K May 4 00:27 mysql drwxr-x---. 2 mysql mysql 4.0K May 4 00:27 performance_schema -rw-------. 1 mysql mysql 1.7K May 4 00:27 private_key.pem -rw-r--r--. 1 mysql mysql 451 May 4 00:27 public_key.pem -rw-r--r--. 1 mysql mysql 1.1K May 4 00:27 server-cert.pem -rw-------. 1 mysql mysql 1.7K May 4 00:27 server-key.pem drwxr-x---. 2 mysql mysql 12K May 4 00:27 sys [root@linux1 ~]# systemctl start mysqld Obs.: Lembrar de incluir o backup do arquivo /etc/my.cnf
  • 106. MySQL – HOT Backup (XtraBackup) https://www.percona.com/downloads/XtraBackup/LATEST/ http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
  • 107. MySQL – HOT Backup (XtraBackup) [root@linux1 ~]# yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm -y Loaded plugins: fastestmirror, langpacks percona-release-0.1-4.noarch.rpm | 6.4 kB 00:00:00 Examining /var/tmp/yum-root-YMmTLa/percona-release-0.1-4.noarch.rpm: percona-release-0.1-4.noarch Marking /var/tmp/yum-root-YMmTLa/percona-release-0.1-4.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package percona-release.noarch 0:0.1-4 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================= Package Arch Version Repository Size ======================================================================================================= Installing: percona-release noarch 0.1-4 /percona-release-0.1-4.noarch 5.8 k Transaction Summary ======================================================================================================= Install 1 Package Total size: 5.8 k Installed size: 5.8 k Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : percona-release-0.1-4.noarch 1/1 Verifying : percona-release-0.1-4.noarch 1/1 Installed: percona-release.noarch 0:0.1-4 Complete!
  • 108. MySQL – HOT Backup (XtraBackup) root@linux1 ~]# yum install percona-xtrabackup-24 -y Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * updates: mirror.globo.com Resolving Dependencies --> Running transaction check ---> Package percona-xtrabackup-24.x86_64 0:2.4.7-1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =========================================================================================================== Package Arch Version Repository Size =========================================================================================================== Installing: percona-xtrabackup-24 x86_64 2.4.7-1.el7 percona-release-x86_64 7.3 M Transaction Summary =========================================================================================================== Install 1 Package Total download size: 7.3 M Installed size: 31 M Downloading packages: percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm | 7.3 MB 00:00:00 Transaction test succeeded Running transaction Installing : percona-xtrabackup-24-2.4.7-1.el7.x86_64 1/1 Verifying : percona-xtrabackup-24-2.4.7-1.el7.x86_64 1/1 Installed: percona-xtrabackup-24.x86_64 0:2.4.7-1.el7 Complete! root@linux1 ~]# whereis innobackupex innobackupex: /usr/bin/innobackupex /usr/share/man/man1/innobackupex.1.gz
  • 109. MySQL – HOT Backup (XtraBackup) [root@linux1 ~]# innobackupex --user=root --password=admin --no-timestamp /backup/new_backup 170504 00:38:30 innobackupex: Starting the backup operation 170504 00:38:30 version_check Connecting to MySQL server 170504 00:38:30 version_check Connected to MySQL server 170504 00:38:30 Connecting to MySQL server host: localhost Using server version 5.7.18 innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) xtrabackup: cd to /var/lib/mysql xtrabackup: open files limit requested 0, set to 1024 xtrabackup: using the following InnoDB configuration: xtrabackup: innodb_data_home_dir = . xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 50331648 170504 00:38:31 [01] Copying ./ibdata1 to /backup/new_backup/ibdata1 170504 00:38:31 [01] ...done 170504 00:38:31 [01] Copying ./bd01/t1.ibd to /backup/new_backup/bd01/t1.ibd 170504 00:38:31 [01] ...done 170504 00:38:31 >> log scanned up to (2541003) 170504 00:38:32 Starting to backup non-InnoDB tables and files xtrabackup: Stopping log copying thread. 170504 00:38:34 Executing UNLOCK TABLES 170504 00:38:34 All tables unlocked 170504 00:38:34 [00] Copying ib_buffer_pool to /backup/new_backup/ib_buffer_pool 170504 00:38:34 [00] ...done 170504 00:38:34 Backup created in directory '/backup/new_backup/' 170504 00:38:34 [00] Writing backup-my.cnf 170504 00:38:34 [00] ...done 170504 00:38:34 [00] ...done xtrabackup: Transaction log of lsn (2540994) to (2541003) was copied. 170504 00:38:34 completed OK! [root@linux1 ~]# innobackupex --user=root --password=admin /backup --no-timestamp indica que não deverá ser gerado um diretório e que o mesmo será informado manualmente new_backup é o diretório onde o backup deverá ser gerado. Um diretório será criado automaticamente dentro de /backup com nome no formato YYYY-MM-DD_HH-MI-SS
  • 110. MySQL – HOT Backup (XtraBackup) [root@linux1 ~]# ls -lh /backup total 8.0K drwx------. 6 root root 4.0K May 4 00:38 new_backup drwxr-x---. 6 root root 4.0K May 4 00:42 2017-05-04_00-42-01 [root@linux1 ~]# ls -lh /backup/new_backup/ total 13M -rw-r-----. 1 root root 424 May 4 00:38 backup-my.cnf drwxr-x---. 2 root root 4.0K May 4 00:38 bd01 -rw-r-----. 1 root root 419 May 4 00:38 ib_buffer_pool -rw-r-----. 1 root root 12M May 4 00:38 ibdata1 drwxr-x---. 2 root root 4.0K May 4 00:38 mysql drwxr-x---. 2 root root 4.0K May 4 00:38 performance_schema drwxr-x---. 2 root root 12K May 4 00:38 sys -rw-r-----. 1 root root 113 May 4 00:38 xtrabackup_checkpoints -rw-r-----. 1 root root 442 May 4 00:38 xtrabackup_info -rw-r-----. 1 root root 2.5K May 4 00:38 xtrabackup_logfile
  • 111. MySQL – Prepare HOT Backup (XtraBackup) [root@linux1 ~]# innobackupex --apply-log /backup/new_backup 170504 00:44:01 innobackupex: Starting the apply-log operation innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 6f7a799) xtrabackup: cd to /backup/new_backup/ xtrabackup: This target seems to be already prepared. InnoDB: Number of pools: 1 xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'. xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = . xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: Starting InnoDB instance for recovery. xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter) InnoDB: Creating shared tablespace for temporary tables InnoDB: File './ibtmp1' size is now 12 MB. InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active. InnoDB: 32 non-redo rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.7.13 started; log sequence number 2541143 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 2541162 xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = . xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend InnoDB: Creating shared tablespace for temporary tables InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 2541181 170504 00:44:04 completed OK!
  • 112. MySQL – Restore HOT Backup (XtraBackup) [root@linux1 ~]# systemctl stop mysqld [root@linux1 ~]# mkdir /tmp/mysql [root@linux1 ~]# mv /var/lib/mysql/* /tmp/mysql [root@linux1 ~]# innobackupex --copy-back /backup/new_backup 170504 00:46:29 innobackupex: Starting the copy-back operation innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 6f7a799) 170504 00:46:29 [01] Copying ib_logfile0 to /var/lib/mysql/ib_logfile0 170504 00:46:29 [01] ...done 170504 00:46:29 [01] Copying ib_logfile1 to /var/lib/mysql/ib_logfile1 170504 00:46:30 [01] ...done 170504 00:46:30 [01] Copying ibdata1 to /var/lib/mysql/ibdata1 170504 00:46:30 [01] ...done 170504 00:46:30 [01] Copying ./sys/sys_config_insert_set_user.TRN to /var/lib/mysql/sys/sys_config_insert_set_user.TRN 170504 00:46:30 [01] ...done 170504 00:46:30 [01] Copying ./sys/db.opt to /var/lib/mysql/sys/db.opt 170504 00:46:30 [01] ...done 170504 00:46:30 [01] Copying ./sys/host_summary_by_file_io_type.frm to /var/lib/mysql/sys/host_io_type.frm 170504 00:46:30 [01] ...done 170504 00:46:30 [01] Copying ./sys/sys_config.ibd to /var/lib/mysql/sys/sys_config.ibd 170504 00:46:30 [01] ...done 170504 00:46:30 [01] Copying ./sys/schema_index_statistics.frm to /var/lib/mysql/sys/schema_index_statistics.frm 170504 00:46:31 [01] ...done 170504 00:46:31 [01] Copying ./mysql/time_zone.ibd to /var/lib/mysql/mysql/time_zone.ibd 170504 00:46:31 [01] ...done 170504 00:46:31 [01] Copying ./mysql/event.frm to /var/lib/mysql/mysql/event.frm 170504 00:46:31 [01] ...done 170504 00:46:32 [01] Copying ./mysql/columns_priv.MYD to /var/lib/mysql/mysql/columns_priv.MYD 170504 00:46:32 [01] ...done 170504 00:46:33 [01] Copying ./performance_schema/event_name.frm to 170504 00:46:33 [01] ...done 170504 00:46:33 [01] Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info 170504 00:46:33 [01] ...done 170504 00:46:33 completed OK! [root@linux1 ~]# chown -R mysql.mysql /var/lib/mysql [root@linux1 ~]# systemctl start mysqld
  • 113. MySQL – Incremental HOT Backup (XtraBackup) BACKUP FULL / INCREMENTAL ------------------------- 1) innobackupex --user=root --password=admin /backup (BACKUP FULL) 2) innobackupex --incremental /backup --incremental-basedir=/backup/2017-05-04_05-27-23 --user=root --password=admin (BACKUP INC) 3) innobackupex --apply-log --redo-only /backup/2017-05-04_05-27-23 --user=root --password=admin (PREPARE FULL) 4) innobackupex --apply-log --redo-only /backup/2017-05-04_05-27-23 --incremental-dir=2017-05-04_05-28-34 --user=root --password=admin (PREPARE INC) 5) innobackupex --incremental /backup --incremental-basedir=/backup/2017-05-04_05-27-23 --user=root --password=admin (BACKUP INC) 6) innobackupex --apply-log --redo-only /backup/2017-05-04_05-27-23 --incremental-dir=2017-05-04_05-31-35 --user=root --password=admin (PREPARE INC) [root@linux1 ~]# ls -lh /backup total 12K drwxr-x---. 6 root root 4.0K May 4 05:32 2017-05-04_05-27-23 (FULL) drwxr-x---. 6 root root 4.0K May 4 05:30 2017-05-04_05-28-34 (INCREMENTAL) drwxr-x---. 6 root root 4.0K May 4 05:32 2017-05-04_05-31-35 (INCREMENTAL) RESTORE FULL ------------ [root@linux1 ~]# systemctl stop mysqld [root@linux1 ~]# mkdir /tmp/mysql [root@linux1 ~]# mv /var/lib/mysql/* /tmp/mysql [root@linux1 ~]# innobackupex --copy-back /backup/2017-05-04_05-27-23 (FULL) [root@linux1 ~]# chown -R mysql.mysql /var/lib/mysql [root@linux1 ~]# systemctl start mysqld
  • 114. MySQL – Change datadir (move from /var/lib/mysql) # cat /etc/selinux/config SELINUX=disabled # systemctl stop mysqld # mkdir -p /data/lib # cp -a /var/lib/mysql /data/lib # mv /var/lib/mysql /var/lib/mysql_old # chown -R mysql.mysql /data/lib/mysql # yum install policycoreutils-python # semanage fcontext -a -s system_u -t mysqld_db_t "/data/lib/mysql(/.*)?" # restorecon -Rv /data/lib/mysql # cat /etc/my.cnf [mysqld] datadir=/data/lib/mysql socket=/data/lib/mysql/mysql.sock [client] port=3306 socket=/data/lib/mysql/mysql.sock # systemctl start mysqld
  • 115. Events
  • 116. MySQL - Events mysql> show global variables like 'event_scheduler'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | event_scheduler | OFF | +-----------------+-------+ 1 row in set (0.01 sec) mysql> SET GLOBAL event_scheduler = ON; Query OK, 0 rows affected (0.00 sec) mysql> show global variables like 'event_scheduler'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | event_scheduler | ON | +-----------------+-------+ 1 row in set (0.02 sec) mysql> SHOW FULL PROCESSLIST; +----+-----------------+-----------+--------------------+---------+------+------------------------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-----------------+-----------+--------------------+---------+------+------------------------+-----------------------+ | 7 | root | localhost | performance_schema | Query | 0 | starting | SHOW FULL PROCESSLIST | | 8 | event_scheduler | localhost | NULL | Daemon | 8 | Waiting on empty queue | NULL | +----+-----------------+-----------+--------------------+---------+------+------------------------+-----------------------+ 2 rows in set (0.00 sec) https://dev.mysql.com/doc/refman/5.7/en/create-event.html
  • 117. MySQL - Events mysql> show create table t1; +-------+-----------------------------------------------+ | Table | Create Table | +-------+-----------------------------------------------+ | t1 | CREATE TABLE `t1` ( | | `id` int(11) NOT NULL AUTO_INCREMENT, | | `data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | | PRIMARY KEY (`id`) | |) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+-----------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT CURRENT_USER(),SCHEMA(); +----------------+----------+ | CURRENT_USER() | SCHEMA() | +----------------+----------+ | root@localhost | bd01 | +----------------+----------+ 1 row in set (0.00 sec) mysql> USE bd01; Database changed mysql> CREATE EVENT event_insert_t1 ON SCHEDULE -> EVERY 1 MINUTE -> STARTS (NOW() + INTERVAL 1 MINUTE) -> ON COMPLETION PRESERVE ENABLE -> DO insert into t1 values (NULL,NULL); Query OK, 0 rows affected (0.00 sec) mysql> SELECT NAME,interval_value,STARTS,execute_at,last_executed,on_completion,STATUS FROM mysql.event; +-----------------+----------------+---------------------+------------+---------------+---------------+---------+ | NAME | interval_value | STARTS | execute_at | last_executed | on_completion | STATUS | +-----------------+----------------+---------------------+------------+---------------+---------------+---------+ | event_insert_t1 | 1 | 2017-05-02 13:08:38 | NULL | NULL | PRESERVE | ENABLED | +-----------------+----------------+---------------------+------------+---------------+---------------+---------+ 1 row in set (0.00 sec)
  • 118. MySQL - Events mysql> select * from t1; +----+---------------------+ | id | data | +----+---------------------+ | 1 | 2017-05-02 13:08:38 | | 2 | 2017-05-02 13:09:38 | | 3 | 2017-05-02 13:10:38 | | 4 | 2017-05-02 13:11:38 | +----+---------------------+ mysql> SELECT NAME,interval_value,STARTS,execute_at,last_executed,on_completion,STATUS FROM mysql.event; +-----------------+----------------+---------------------+------------+---------------------+---------------+---------+ | NAME | interval_value | STARTS | execute_at | last_executed | on_completion | STATUS | +-----------------+----------------+---------------------+------------+---------------------+---------------+---------+ | event_insert_t1 | 1 | 2017-05-02 13:08:38 | NULL | 2017-05-02 13:11:38 | PRESERVE | ENABLED | +-----------------+----------------+---------------------+------------+---------------------+---------------+---------+ mysql> show eventsG *************************** 1. row *************************** Db: bd01 Name: event_insert_t1 Definer: root@localhost Time zone: SYSTEM Type: RECURRING Execute at: NULL Interval value: 1 Interval field: MINUTE Starts: 2017-05-02 13:08:38 Ends: NULL Status: ENABLED Originator: 0 character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: latin1_swedish_ci mysql> drop event event_insert_t1; Query OK, 0 rows affected (0.00 sec)
  • 119. MySQL - Events mysql> desc INFORMATION_SCHEMA.events; +----------------------+---------------+------+-----+---------------------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------------+---------------+------+-----+---------------------+-------+ | EVENT_CATALOG | varchar(64) | NO | | | | | EVENT_SCHEMA | varchar(64) | NO | | | | | EVENT_NAME | varchar(64) | NO | | | | | DEFINER | varchar(93) | NO | | | | | TIME_ZONE | varchar(64) | NO | | | | | EVENT_BODY | varchar(8) | NO | | | | | EVENT_DEFINITION | longtext | NO | | NULL | | | EVENT_TYPE | varchar(9) | NO | | | | | EXECUTE_AT | datetime | YES | | NULL | | | INTERVAL_VALUE | varchar(256) | YES | | NULL | | | INTERVAL_FIELD | varchar(18) | YES | | NULL | | | SQL_MODE | varchar(8192) | NO | | | | | STARTS | datetime | YES | | NULL | | | ENDS | datetime | YES | | NULL | | | STATUS | varchar(18) | NO | | | | | ON_COMPLETION | varchar(12) | NO | | | | | CREATED | datetime | NO | | 0000-00-00 00:00:00 | | | LAST_ALTERED | datetime | NO | | 0000-00-00 00:00:00 | | | LAST_EXECUTED | datetime | YES | | NULL | | | EVENT_COMMENT | varchar(64) | NO | | | | | ORIGINATOR | bigint(10) | NO | | 0 | | | CHARACTER_SET_CLIENT | varchar(32) | NO | | | | | COLLATION_CONNECTION | varchar(32) | NO | | | | | DATABASE_COLLATION | varchar(32) | NO | | | | +----------------------+---------------+------+-----+---------------------+-------+
  • 121. Linux - Workload (top) [root@linux1 ~]# free -m total used free shared buffers cached Mem: 30099 29953 145 0 10 22254 -/+ buffers/cache: 7688 22410 Swap: 16383 3716 12667 [root@linux1 ~]# nice top –d1 top - 12:53:25 up 209 days, 18:08, 1 user, load average: 1.11, 5.30, 15.35 Tasks: 1431 total, 1 running, 936 sleeping, 26 stopped, 468 zombie Cpu(s): 2.7%us, 1.2%sy, 0.5%ni, 92.3%id, 3.4%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 16329264k total, 16123804k used, 205460k free, 24664k buffers Swap: 16777208k total, 7544608k used, 9232600k free, 10381940k cached Cpu0 : 38.9%us, 27.7%sy, 0.0%ni, 4.3%id, 91.1%wa, 0.0%hi, 18.1%si, 0.0%st Cpu1 : 44.7%us, 10.6%sy, 0.0%ni, 41.5%id, 3.2%wa, 0.0%hi, 0.0%si, 0.0%st Cpu2 : 17.2%us, 9.7%sy, 0.0%ni, 68.8%id, 4.3%wa, 0.0%hi, 0.0%si, 0.0%st Cpu3 : 28.7%us, 10.6%sy, 0.0%ni, 56.4%id, 4.3%wa, 0.0%hi, 0.0%si, 0.0%st Cpu4 : 22.0%us, 2.0%sy, 0.0%ni, 75.0%id, 1.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu5 : 12.2%us, 7.1%sy, 0.0%ni, 77.6%id, 3.1%wa, 0.0%hi, 0.0%si, 0.0%st Cpu6 : 9.0%us, 5.0%sy, 0.0%ni, 86.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu7 : 12.1%us, 6.1%sy, 0.0%ni, 77.8%id, 4.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu8 : 4.9%us, 2.9%sy, 0.0%ni, 91.2%id, 1.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 30821572k total, 30481844k used, 339728k free, 6456k buffers Swap: 16777208k total, 3153372k used, 13623836k free, 21027488k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 10987 mysql 20 0 2898m 494m 6160 S 98.0 3.1 119:30.47 /usr/sbin/mysqld 9959 oracle 20 0 22.2g 2.4g 2.4g S 56.8 8.0 1:28.77 oracle (LOCAL=NO) 25530 oracle 20 0 22.2g 4.4g 4.4g S 43.3 14.9 6:19.42 oracle (LOCAL=NO) 9961 oracle 20 0 22.2g 3.4g 3.4g S 38.5 11.5 3:00.94 oracle (LOCAL=NO) 16268 oracle 20 0 22.2g 1.3g 1.3g S 37.5 4.4 0:05.55 oracle (LOCAL=NO) Load Average (CPU + I/O) A grosso modo, dizemos que um servidor está a 100% se o valor do Load Average coincide com o número de CPUs do servidor. Por exemplo, em um servidor com 8 CPUs: Load Average: 2 = 25% da capacidade total Load Average: 4 = 50% da capacidade total Load Average: 8 = 100% da capacidade total Load Average: 16 = 200% da capacidade total VIRT VIRT representa o tamanho virtual de um processo, que é a soma de memória que ele [processo] está realmente usando (por exemplo RAM da placa de vídeo para o servidor X), os arquivos no disco que foram mapeados para ele [processo] (bibliotecas mais notavelmente compartilhadas), e memória compartilhada com outros processos. VIRT representa a quantidade de memória do programa que é capaz de acessar no momento presente. RES RES representa o tamanho residente, que é uma representação precisa da quantidade de memória física real consumida de um processo. Também corresponde diretamente à coluna MEM%. Será virtualmente sempre menor do que o tamanho VIRT, uma vez que a maioria dos programas dependem da biblioteca C.
  • 122. Linux - Workload (iostat) [root@linux1 ~]# nice iostat -dm -p 5 Linux 2.6.32-431.el6.x86_64 (ip-172-31-39-64) 05/04/2017 _x86_64_ (16 CPU) Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn xvdf 0.00 0.00 0.00 0 0 xvdg 0.00 0.00 0.00 0 0 xvdt 3.00 0.00 0.01 0 0 xvdaf 0.00 0.00 0.00 0 0 xvdu 0.00 0.00 0.00 0 0 xvds 0.00 0.00 0.00 0 0 xvdr 0.20 0.00 0.00 0 0 xvdq 11.40 0.00 0.04 0 0 xvdae 0.20 0.00 0.00 0 0 xvdad 107.00 0.08 0.39 0 1 xvdw 262.20 9.29 9.83 16 18 [root@linux1 ~]# nice iostat -xdm 5 Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await svctm %util xvdf 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 xvdg 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 xvdt 0.00 0.00 0.00 1.40 0.00 0.01 8.00 0.00 0.71 0.43 0.06 xvdaf 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 xvdu 0.00 0.00 0.00 3.00 0.00 0.01 8.00 0.01 2.33 1.13 0.34 xvds 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 xvdr 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 xvdq 0.00 0.00 1.40 7.40 0.01 0.03 9.27 0.01 1.43 0.77 0.68 xvdae 0.00 0.00 0.00 0.60 0.00 0.00 8.00 0.00 0.67 0.33 0.02 xvdad 0.00 0.00 75.60 49.40 0.90 0.19 17.91 0.13 1.03 0.62 7.76 xvdw 0.00 9.80 419.40 348.40 9.47 9.16 87.07 9.87 9.31 0.42 98.20
  • 123. Linux - Workload (iotop) [root@linux1 ~]# yum install iotop -y Loaded plugins: fastestmirror, langpacks base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package iotop.noarch 0:0.6-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================ Package Arch Version Repository Size ============================================================================================================ Installing: iotop noarch 0.6-2.el7 base 52 k Transaction Summary ============================================================================================================ Install 1 Package Total download size: 52 k Installed size: 156 k Downloading packages: iotop-0.6-2.el7.noarch.rpm | 52 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : iotop-0.6-2.el7.noarch 1/1 Verifying : iotop-0.6-2.el7.noarch 1/1 Installed: iotop.noarch 0:0.6-2.el7 Complete!
  • 124. Linux - Workload (iotop) [root@linux1 ~]# nice iotop -o Total DISK READ: 4.95 M/s | Total DISK WRITE: 1660.44 K/s TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND 3991 be/4 mysql 999.27 K/s 1973.45 K/s 0.00 % 95.16 % /usr/sbin/mysqld --basedir=/usr --datadir= 9959 be/4 oracle 753.15 K/s 330.26 K/s 0.00 % 24.31 % oracle (LOCAL=NO) 806 be/4 oracle 1482.67 K/s 0.00 B/s 0.00 % 22.32 % oracle (LOCAL=NO) 2562 be/4 oracle 834.04 K/s 0.00 B/s 0.00 % 11.12 % oracle (LOCAL=NO) 8582 be/4 oracle 230.72 K/s 0.00 B/s 0.00 % 0.87 % oracle (LOCAL=NO) 8132 be/4 oracle 121.03 K/s 0.00 B/s 0.00 % 0.64 % oracle (LOCAL=NO) 16795 be/4 oracle 56.73 K/s 0.00 B/s 0.00 % 0.57 % oracle (LOCAL=NO) 4214 be/4 oracle 109.69 K/s 0.00 B/s 0.00 % 0.46 % oracle (LOCAL=NO) 21397 be/4 oracle 166.42 K/s 0.00 B/s 0.00 % 0.46 % oracle (LOCAL=NO) 19870 be/4 oracle 204.25 K/s 0.00 B/s 0.00 % 0.43 % oracle (LOCAL=NO) 20236 be/4 oracle 105.91 K/s 0.00 B/s 0.00 % 0.39 % oracle (LOCAL=NO) 8822 be/4 oracle 94.56 K/s 0.00 B/s 0.00 % 0.39 % oracle (LOCAL=NO) 5058 be/4 oracle 185.33 K/s 0.00 B/s 0.00 % 0.38 % oracle (LOCAL=NO) 1495 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.27 % [jbd2/xvdo1-8] 20880 be/4 oracle 105.91 K/s 0.00 B/s 0.00 % 0.24 % oracle (LOCAL=NO) 25479 be/4 oracle 30.26 K/s 0.00 B/s 0.00 % 0.22 % oracle (LOCAL=NO) 1532 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.19 % [jbd2/xvdaa1-8] 1538 be/3 root 0.00 B/s 7.56 K/s 0.00 % 0.17 % [jbd2/xvdj1-8] 7269 be/4 oracle 22.69 K/s 0.00 B/s 0.00 % 0.17 % oracle (LOCAL=NO) 1536 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.16 % [jbd2/xvdz1-8] 29391 be/4 oracle 22.69 K/s 0.00 B/s 0.00 % 0.16 % oracle (LOCAL=NO) 1499 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.16 % [jbd2/xvdq1-8] 24565 be/4 oracle 98.34 K/s 0.00 B/s 0.00 % 0.14 % oracle (LOCAL=NO) 1540 be/3 root 0.00 B/s 0.00 B/s 0.00 % 0.13 % [jbd2/xvdw1-8] 20240 be/4 oracle 15.13 K/s 0.00 B/s 0.00 % 0.11 % oracle (LOCAL=NO) 26552 be/4 oracle 15.13 K/s 0.00 B/s 0.00 % 0.10 % oracle (LOCAL=NO) 7050 be/4 oracle 37.82 K/s 0.00 B/s 0.00 % 0.10 % oracle (LOCAL=NO) 18935 be/4 oracle 30.26 K/s 0.00 B/s 0.00 % 0.10 % oracle (LOCAL=NO)
  • 125. Linux - Workload (vmstat) * High numbers in the blocked processes column (b) indicates slow disks. * (r) should always be higher than (b); if it is not, it usually means you have a CPU bottleneck * Whenever the value of the (r) column exceeds the number of CPUs on the server, tasks are forced to wait for execution * Remember that we need to know the number of CPUs on our server because the vmstat (r) value must never exceed the number of CPUs. (r) value of 13 is perfectly acceptable for a 16-CPU server, while a value of 16 would be a serious problem for a 12-CPU server. [root@linux1 ~]# cat /proc/cpuinfo|grep processor|wc -l 16 [root@linux1 ~]# nice vmstat -SM 5 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 6 0 3846 1906 7 19812 0 0 7874 15245 61567 40740 13 8 77 2 0 4 2 3845 1763 7 19932 0 0 1429 21140 48832 30933 11 6 81 1 0 11 0 3845 1678 7 20020 0 0 1452 9647 58602 36188 11 8 80 1 0 8 1 3845 1561 7 20154 0 0 10593 7233 60044 37682 16 8 75 1 0 4 0 3844 899 7 20803 0 0 49169 90868 45795 29962 8 6 84 2 0 8 1 3844 880 8 20818 0 0 1510 1837 41939 27825 7 5 87 0 0 9 0 3843 851 8 20830 0 0 1550 9910 56914 35578 30 11 59 1 0 8 0 3842 829 8 20837 0 0 499 1131 43510 26554 25 6 69 0 0 5 0 3842 835 8 20846 0 0 803 13694 34505 22058 22 5 72 0 0 9 0 3842 809 8 20860 0 0 1626 1486 35354 22317 25 6 69 0 0 4 0 3841 783 9 20880 0 0 2308 2097 34873 21853 29 6 64 1 0 5 0 3841 711 9 20928 0 0 913 9214 34264 19545 16 5 78 0 0 7 1 3838 531 9 21114 0 0 4815 34359 44799 27590 16 6 75 2 0 5 3 3838 328 9 21220 0 0 4259 20427 39931 22669 23 7 69 1 0 5 0 3837 268 9 21371 0 0 4283 40010 31617 19840 15 5 79 1 0 6 1 3837 252 9 21389 0 0 2118 2403 31822 21441 9 4 86 1 0 7 0 3836 225 9 21414 0 0 3614 2380 41579 27585 12 5 82 1 0 11 1 3835 189 9 21453 0 0 5604 3347 49111 32252 20 6 72 1 0 7 0 3834 142 9 21490 0 0 6078 11898 60447 40898 19 8 71 2 0
  • 126. Linux - mytop [root@linux1 ~]# yum install epel-release -y [root@linux1 ~]# yum install mytop –y [root@linux1 ~]# whereis mytop mytop: /usr/bin/mytop /usr/share/man/man1/mytop.1.gz [root@linux1 ~]# mytop -hlocalhost -port3306 -uroot -padmin -dmysql -s1 MySQL on localhost (5.7.18) up 50+05:05:24 [20:47:06] Queries: 11.1M qps: 3 Slow: 535.0 Se/In/Up/De(%): 43/14/01/01 qps now: 6 Slow qps: 0.0 Threads: 13 ( 5/ 0) 29/00/00/00 Key Efficiency: 100.0% Bps in/out: 998.3/ 6.4k Now in/out: 275.5/11.3k Id User Host/IP DB Time Cmd Query or State -- ---- ------- -- ---- --- ---------- 909077 root 11.315.260.30 mysql 0 Query show full processlist 909000 user01 11.315.260.209 bd01 2 Sleep 909107 user02 11.315.260.246 4 Query SELECT CASE WHEN (SELECT DISTINCT 1 FROM... 909090 root 11.315.200.30 bd02 5 Sleep 909091 root 11.315.210.30 105 Sleep 908873 user02 11.315.210.209 bd02 542 Sleep 1 event_sch localhost 2071 Daemon Waiting for next activation 905245 user03 11.315.206.209 bd02 10653 Sleep 905246 user03 11.315.201.209 bd02 10659 Sleep 906711 user04 11.315.209.209 bd02 10653 Sleep 904528 user04 11.315.203.103 21162 Sleep 904526 user05 11.315.220.103 bd03 21163 Sleep
  • 127. MySQL - slow_query_log mysql> show variables like '%_query_%'; +------------------------------+--------------------------------+ | Variable_name | Value | +------------------------------+--------------------------------+ | long_query_time | 10.000000 | | slow_query_log | OFF | | slow_query_log_file | /var/lib/mysql/linux1-slow.log | +------------------------------+--------------------------------+ mysql> show variables like 'log_output'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | FILE | +---------------+-------+ mysql> SET GLOBAL slow_query_log=1; Query OK, 0 rows affected (0,07 sec) mysql> SET GLOBAL long_query_time=5; Query OK, 0 rows affected (0,00 sec) mysql> SET GLOBAL log_output='TABLE'; Query OK, 0 rows affected (0,00 sec) mysql> show global variables like '%_query_%'; +------------------------------+--------------------------------+ | Variable_name | Value | +------------------------------+--------------------------------+ | long_query_time | 5.000000 | | slow_query_log | ON | | slow_query_log_file | /var/lib/mysql/linux1-slow.log | +------------------------------+--------------------------------+ mysql> show variables like 'log_output'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | TABLE | +---------------+-------+
  • 128. MySQL - slow_query_log mysql> SELECT CONNECTION_ID(); +-----------------+ | CONNECTION_ID() | +-----------------+ | 61 | +-----------------+ mysql> select count(*) from bd01.emp; +----------+ | count(*) | +----------+ | 2097153 | +----------+ 1 row in set (33,67 sec) mysql> select count(*) from bd01.emp where id<>34; +----------+ | count(*) | +----------+ | 2097153 | +----------+ 1 row in set (4,60 sec) mysql> SELECT -> start_time, user_host, -> lock_time, query_time, -> lock_time, rows_sent, -> sql_text, thread_id -> FROM mysql.slow_log; +----------------------------+---------------------------+-----------------+-----------------+-----------+-------------------------------+-----------+ | start_time | user_host | query_time | lock_time | rows_sent | sql_text | thread_id | +----------------------------+---------------------------+-----------------+-----------------+-----------+-------------------------------+-----------+ | 2017-04-21 15:52:19.265806 | root[root] @ localhost [] | 00:00:33.739565 | 00:00:00.000504 | 1 | select count(*) from bd01.emp | 61 | +----------------------------+---------------------------+-----------------+-----------------+-----------+-------------------------------+-----------+ 1 row in set (0,00 sec)
  • 132. MySQL – Query Performance Em relação às consultas que utilizam o operador LIKE, é possível melhorar a performance de sua execução fazendo uso de índices FULLTEXT que oferece suporte na engine MyISAM e na engine InnoDB da versão 5.6 do MySQL. Segue abaixo um comparativo de uma das consultas que demoraram mais de 10 segundos para serem executadas. No lado esquerdo da imagem se encontra a consulta original e a do lado direito a consulta alterada para fazer uso de índices FULLTEXT.
  • 133. MySQL – Query Performance Testes realizados comprovam a eficácia de utilização de índices FULLTEXT para consultas complexas que fazem uso do operador LIKE. A imagem ao lado demonstra que o tempo de resposta (eixo vertical) diminui na medida em que o número de registros aumenta (eixo horizontal).
  • 134. MySQL – Explain Plan / Profiles mysql> EXPLAIN EXTENDED select count(*) from bd01.emp where id<>34; +----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | emp | NULL | ALL | NULL | NULL | NULL | NULL | 2087295 | 90.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+ 1 row in set, 2 warnings (0,00 sec) mysql> set profiling=1; Query OK, 0 rows affected, 1 warning (0,01 sec) mysql> select count(*) from bd01.emp where id<>34; +----------+ | count(*) | +----------+ | 2097153 | +----------+ 1 row in set (4,92 sec) mysql> show profiles; +----------+------------+--------------------------------------------+ | Query_ID | Duration | Query | +----------+------------+--------------------------------------------+ | 1 | 4.93420575 | select count(*) from bd01.emp where id<>34 | +----------+------------+--------------------------------------------+ 1 row in set, 1 warning (0,01 sec)
  • 135. MySQL – Explain Plan / Profiles mysql> show profile cpu for query 1; +----------------------+----------+----------+------------+ | Status | Duration | CPU_user | CPU_system | +----------------------+----------+----------+------------+ | starting | 0.000216 | 0.000000 | 0.000000 | | checking permissions | 0.000186 | 0.000000 | 0.000000 | | Opening tables | 0.000100 | 0.000000 | 0.000000 | | init | 0.000106 | 0.000000 | 0.000000 | | System lock | 0.001301 | 0.001000 | 0.001000 | | optimizing | 0.000107 | 0.000000 | 0.000000 | | statistics | 0.000020 | 0.000000 | 0.000000 | | preparing | 0.000073 | 0.000000 | 0.000000 | | executing | 0.000070 | 0.000000 | 0.000000 | | Sending data | 4.917282 | 4.518313 | 0.117982 | | end | 0.000674 | 0.000000 | 0.000999 | | query end | 0.000146 | 0.000000 | 0.000000 | | closing tables | 0.000156 | 0.000000 | 0.000000 | | freeing items | 0.013413 | 0.000000 | 0.002000 | | cleaning up | 0.000359 | 0.000000 | 0.000000 | +----------------------+----------+----------+------------+ 15 rows in set, 1 warning (0,00 sec) * User CPU Time: Amount of time the processor worked on the specific programm. * System CPU Time: Amount of time the processor worked on operating system's functions connected to that specific programm.
  • 136. MySQL – Explain Plan / Profiles mysql> SET @query_id = 1; Query OK, 0 rows affected (0,01 sec) mysql> SELECT state, -> Sum(duration) AS Total_R, -> Round(100 * Sum(duration) / (SELECT Sum(duration) -> FROM information_schema.profiling -> WHERE query_id = @query_id), 2) AS Pct_R, -> Count(*) AS Calls, -> Sum(duration) / Count(*) AS "R/Call" -> FROM information_schema.profiling -> WHERE query_id = @query_id -> GROUP BY state -> ORDER BY total_r DESC; +----------------------+----------+-------+-------+--------------+ | state | Total_R | Pct_R | Calls | R/Call | +----------------------+----------+-------+-------+--------------+ | Sending data | 4.917282 | 99.66 | 1 | 4.9172820000 | | freeing items | 0.013413 | 0.27 | 1 | 0.0134130000 | | System lock | 0.001301 | 0.03 | 1 | 0.0013010000 | | end | 0.000674 | 0.01 | 1 | 0.0006740000 | | cleaning up | 0.000359 | 0.01 | 1 | 0.0003590000 | | starting | 0.000216 | 0.00 | 1 | 0.0002160000 | | checking permissions | 0.000186 | 0.00 | 1 | 0.0001860000 | | closing tables | 0.000156 | 0.00 | 1 | 0.0001560000 | | query end | 0.000146 | 0.00 | 1 | 0.0001460000 | | optimizing | 0.000107 | 0.00 | 1 | 0.0001070000 | | init | 0.000106 | 0.00 | 1 | 0.0001060000 | | Opening tables | 0.000100 | 0.00 | 1 | 0.0001000000 | | preparing | 0.000073 | 0.00 | 1 | 0.0000730000 | | executing | 0.000070 | 0.00 | 1 | 0.0000700000 | | statistics | 0.000020 | 0.00 | 1 | 0.0000200000 | +----------------------+----------+-------+-------+--------------+ 15 rows in set, 16 warnings (0,11 sec)
  • 137. MySQL - SHOW [GLOBAL] STATUS mysql> SHOW GLOBAL STATUS; +-----------------------------------------------+---------+ | Variable_name | Value | +-----------------------------------------------+---------+ | Bytes_received | 2119 | | Bytes_sent | 5351 | | Com_select | 234 | | Com_delete | 4 | | Com_update | 38 | | Com_insert | 5 | | Com_drop_index | 3 | | Com_drop_table | 5 | | Com_drop_user | 0 | | Com_kill | 3 | | Com_show_processlist | 9 | | Com_show_tables | 12 | | Created_tmp_disk_tables | 10 | | Created_tmp_tables | 77 | | Innodb_buffer_pool_read_requests | 2293 | | Innodb_buffer_pool_reads | 1526 | | Innodb_rows_deleted | 0 | | Innodb_rows_inserted | 0 | | Innodb_rows_read | 8 | | Innodb_rows_updated | 0 | | Key_read_requests | 6 | | Key_reads | 3 | | Key_write_requests | 0 | | Key_writes | 0 | | Qcache_free_memory | 1040184 | | Qcache_hits | 12 | | Qcache_inserts | 5 | | Qcache_lowmem_prunes | 3 | | Connections | 565 | | Threads_connected | 12 | | Threads_created | 45 | | Threads_running | 5 | | Uptime | 48410 | +-----------------------------------------------+---------+ • SHOW GLOBAL STATUS will give you status variables that have updated since mysqld started for all sessions that are connected or have ever been connected. • SHOW STATUS will give you status variables that have updated within your session. The command can also be expressed as SHOW SESSION STATUS. FLUSH STATUS -> Reset SESSION values Stop/Start MySQL -> Reset GLOBAL values • Key_reads - Index blocks read from disk • Key_read_requests - Index blocks read from cache • Key_writes - Index blocks written on disk • Innodb_buffer_pool_reads -Pages read from disk • Innodb_buffer_pool_read_requests - Pages read from the cache • Qcache_hits - Queries satisfied from the cache • QCache_inserts - Query result sets added to the cache • Qcache_lowmem_prunes - Query results flushed due to lack of memory • Qcache_free_memory - Bytes free • Created_tmp_tables - The number of internal temporary tables created by the server while executing statements. • Created_tmp_disk_tables - The number of internal on-disk temporary tables created by the server while executing statements. • Connection - The number of connection attempts (successful or not) to the MySQL server • Threads_connected - The number of currently open connections • Threads_running - The number of threads that are not sleeping https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html
  • 138. MySQL – SHOW [GLOBAL] STATUS mysql> SELECT A.VARIABLE_NAME,A.VARIABLE_VALUE GLOBAL,B.VARIABLE_VALUE SESSION -> FROM information_schema.global_status A INNER join information_schema.session_status B -> USING (VARIABLE_NAME) WHERE A.VARIABLE_VALUE <> B.VARIABLE_VALUE; ERROR 3167 (HY000): The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation for 'show_compatibility_56' mysql> set GLOBAL show_compatibility_56=ON; Query OK, 0 rows affected (0,00 sec) +----------------------------+----------+-----------+ | VARIABLE_NAME | GLOBAL | SESSION | +----------------------------+----------+-----------+ | BYTES_RECEIVED | 74334 | 456 | | BYTES_SENT | 1384004 | 1486 | | COM_CHANGE_DB | 2 | 0 | | COM_GRANT | 1 | 0 | | COM_INSERT | 2 | 0 | | COM_INSERT_SELECT | 42 | 0 | | COM_SELECT | 102 | 2 | | COM_SET_OPTION | 156 | 0 | | COM_SHOW_COLLATIONS | 1 | 0 | | COM_SHOW_CREATE_TABLE | 562 | 0 | | COM_SHOW_DATABASES | 5 | 0 | | COM_SHOW_FIELDS | 2 | 0 | | COM_SHOW_PROCESSLIST | 8 | 0 | | COM_SHOW_VARIABLES | 1 | 0 | | CREATED_TMP_DISK_TABLES | 77 | 0 | | CREATED_TMP_TABLES | 719 | 4 | | LAST_QUERY_COST | 0.000000 | 21.650822 | | LAST_QUERY_PARTIAL_PLANS | 0 | 3 | | OPENED_TABLE_DEFINITIONS | 221 | 0 | | OPENED_TABLES | 393 | 0 | | QUESTIONS | 1158 | 2 | | SELECT_FULL_JOIN | 17 | 2 | | SELECT_SCAN | 306 | 2 | | SORT_ROWS | 593 | 0 | | SORT_SCAN | 37 | 0 | +----------------------------+----------+-----------+
  • 139. MySQL – Query Cache mysql> show variables like '%query_cache%'; +------------------------------+---------+ | Variable_name | Value | +------------------------------+---------+ | have_query_cache | YES | | query_cache_limit | 1048576 | | query_cache_min_res_unit | 4096 | | query_cache_size | 1048576 | | query_cache_type | OFF | | query_cache_wlock_invalidate | OFF | +------------------------------+---------+ mysql> set GLOBAL query_cache_type=1; ERROR 1651 (HY000): Query cache is disabled; restart the server with query_cache_type=1 to enable it • have_query_cache - indicates whether the query cache is available. • query_cache_type - Whether the query cache is enabled. Possible values are OFF, ON, or DEMAND, where the latter means that only queries containing the SQL_CACHE modifier are eligible for caching. • query_cache_size - The total memory to allocate to the query cache, in bytes. Setting it to 0 disables the query cache, as does setting query_cache_type=0. By default, the query cache is disabled. • query_cache_min_res_unit - The minimum size when allocating a block. • query_cache_limit - The largest result set that MySQL will cache. Queries whose results are larger than this setting will not be cached. If the result exceeds the specified limit, MySQL will increment the Qcache_not_cachedstatus variable and discard the result cached so far. • query_cache_wlock_invalidate - Whether to serve cached results that refer to tables other connections have locked. This really doesn’t matter for most applications, so the default is generally fine.
  • 140. MySQL – Query Cache [root@linux1 ~]# cat /etc/my.cnf query_cache_type=1 query_cache_size=250M query_cache_limit=2M [root@linux1 ~]# service mysqld restart Parando o mysqld: [ OK ] Iniciando o mysqld: [ OK ] mysql> show variables like '%query_cache%'; +------------------------------+-----------+ | Variable_name | Value | +------------------------------+-----------+ | have_query_cache | YES | | query_cache_limit | 2097152 | | query_cache_min_res_unit | 4096 | | query_cache_size | 262144000 | | query_cache_type | ON | | query_cache_wlock_invalidate | OFF | +------------------------------+-----------+ 6 rows in set (0,03 sec) mysql> SHOW GLOBAL STATUS LIKE "qcache%"; +-------------------------+-----------+ | Variable_name | Value | +-------------------------+-----------+ | Qcache_free_blocks | 1 | The number of free memory blocks in the query cache. | Qcache_free_memory | 262135240 | The amount of free memory for the query cache. | Qcache_hits | 0 | The number of query cache hits. | Qcache_inserts | 0 | The number of queries added to the query cache. | Qcache_lowmem_prunes | 0 | The number of queries that were deleted from the query cache because of low memory. | Qcache_not_cached | 1 | The number of noncached queries (not cacheable). | Qcache_queries_in_cache | 0 | The number of queries registered in the query cache. | Qcache_total_blocks | 1 | The total number of blocks in the query cache. +-------------------------+-----------+ 8 rows in set (0,07 sec) "Be cautious about sizing the query cache excessively large, which increases the overhead required to maintain the cache, possibly beyond the benefit of enabling it. Sizes in tens of megabytes are usually beneficial. Sizes in the hundreds of megabytes might not be." FLUSH QUERY CACHE - This command compacts the query cache by moving all blocks "upward" and removing the free space between them, leaving a single free block at the bottom. RESET QUERY CACHE - Remove queries from the cache. https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html
  • 141. MySQL – Query Cache Em relação ao parâmetro query_cache_size, o mesmo não estava habilitado e seu valor foi ajustado de forma a armazenar resultados de consultas SQL que poderão ser reutilizados por instruções SQL reincidentes. Ao lado o gráfico que demonstra este buffer armazenando resultados de cerca de 68 mil instruções SQL ao longo do dia. Mais baixo a imagem demonstra a eficiência do cache na qual se observa um Hit de acerto de mais de 40 mil instruções SQL com pico de mais de 100 mil instruções. Neste caso, mais de 40 mil instruções SQL que foram executadas tiveram seus resultados provenientes do cache em memória ao invés vez do disco. A coluna Inserts demonstra a quantidades de vezes que novas instruções foram inseridas no cache, enquanto que a coluna Lowmem Prunes mostra a quantidade de instruções que precisaram ser removidas do cache para dar lugar à novas instruções SQL. Quanto menor for o valor de Lowmem Prunes e maior o valor de Hits, melhor a eficiência do cache.
  • 142. MySQL – Other System Variables innodb_buffer_pool_size: (128M) The size in bytes of the memory buffer innodb uses to cache data and indexes of its tables key_buffer_size : (8M) Very important if you use MyISAM tables Find the All MyISAM index Size: SELECT SUM(INDEX_LENGTH)/(1024*1024) 'Index Size' FROM information_schema.TABLES where ENGINE='MyISAM' AND TABLE_SCHEMA NOT IN('mysql','information_schema','performance_schema'); (Index Size)/key_buffer_size(in MB) * 100 if result is (<=100 ) then your all indexes are cached into key_buffer if result is (>100) then your all indexes are not cached into key_buffer you may gain performance boost by increasing key_buffer_size. join_buffer_size : (256K) Increase the value of join_buffer_size to get a faster full join when adding indexes is not possible. sort_buffer_size : (256K) Improves large and complex sorts. Each thread that needs to do a sort allocates a buffer of this size. Increase this value for faster ORDER BY or GROUP BY operations. tmp_table_size : (16M) If an in-memory temporary table exceeds the limit, MySQL automatically converts it to an on- disk MyISAM table. Increased value can improve performance for many advanced GROUP BY queries. It’s possible compare the number of internal on-disk temporary tables created to the total number of internal temporary tables created by comparing the values of the Created_tmp_disk_tables and Created_tmp_tables variables. slow_query_log : (OFF) Whether the slow query log is enabled. general_log : (OFF) Whether the general query log is enabled. innodb_flush_log_at_trx_commit: (1) sets the behaviour of syncing on commit 0 - if MySQL crashes, last second of txns lost 1 - flush to disk each update/commit 2 - flush to OS (if OS crashes, 1 sec txns are lost)
  • 143. MySQL - Resizing the InnoDB Buffer Pool Online In MySQL 5.7.5, the innodb_buffer_pool_size parameter is now dynamic, allowing you to resize the buffer pool without restarting the server. The resizing operation, which involves moving pages to a new location in memory, is performed chunks. Chunk size is configurable using the new innodb_buffer_pool_chunk_size configuration option. You can monitor resizing progress using the new innodb_buffer_pool_resize_status status variable. SHOW GLOBAL STATUS LIKE 'innodb_buffer_pool_resize_status' Variable_name Value -------------------------------- -------------------------------------------------- Innodb_buffer_pool_resize_status Completed resizing buffer pool at 170522 13:15:13.
  • 144. MySQL – innodb_flush_log_at_trx_commit CREATE TABLE t1 (cod INT, ascii_char CHAR(10)) ENGINE=INNODB; DELIMITER $$ CREATE PROCEDURE insert_loop(IN p_limit INT) BEGIN DECLARE i INT DEFAULT 1; WHILE (i<=p_limit) DO INSERT INTO t1 VALUES(i,CHAR(i)); SET i=i+1; END WHILE; END$$ DELIMITER ; use bd01 CALL insert_loop(100000); SHOW GLOBAL VARIABLES LIKE 'innodb_flush_log_at_trx_commit'; SET GLOBAL innodb_flush_log_at_trx_commit=?; (? = 1,0,2) -- INSERT LOOP 100.000 registros InnoDB 1-Execution Time : 2 min 54.18 sec 0-Execution Time : 5.877 sec 2-Execution Time : 7.538 sec -- INSERT LOOP 100.000 registros MyISAM 1-Execution Time : 4.595 sec 0-Execution Time : 4.566 sec 2-Execution Time : 4.562 sec -- INSERT LOOP 1.000.000 registros InnoDB 1-Execution Time : 29 min 12.29 sec 0-Execution Time : 57.391 sec 2-Execution Time : 59.564 sec -- INSERT LOOP 1.000.000 registros MyISAM 1-Execution Time : 45.089 sec 0-Execution Time : 44.437 sec 2-Execution Time : 44.608 sec
  • 145. MySQL – MySQL Workbench https://www.mysql.com/products/workbench/ Design MySQL Workbench enables a DBA, developer, or data architect to visually design, model, generate, and manage databases. It includes everything a data modeler needs for creating complex ER models, forward and reverse engineering, and also delivers key features for performing difficult change management and documentation tasks that normally require much time and effort. Develop MySQL Workbench delivers visual tools for creating, executing, and optimizing SQL queries. The SQL Editor provides color syntax highlighting, auto-complete, reuse of SQL snippets, and execution history of SQL. The Database Connections Panel enables developers to easily manage standard database connections, including MySQL Fabric. The Object Browser provides instant access to database schema and objects. Administer MySQL Workbench provides a visual console to easily administer MySQL environments and gain better visibility into databases. Developers and DBAs can use the visual tools for configuring servers, administering users, performing backup and recovery, inspecting audit data, and viewing database health. Visual Performance Dashboard MySQL Workbench provides a suite of tools to improve the performance of MySQL applications. DBAs can quickly view key performance indicators using the Performance Dashboard. Performance Reports provide easy identification and access to IO hotspots, high cost SQL statements, and more. Plus, with 1 click, developers can see where to optimize their query with the improved and easy to use Visual Explain Plan. Database Migration MySQL Workbench now provides a complete, easy to use solution for migrating Microsoft SQL Server, Microsoft Access, Sybase ASE, PostreSQL, and other RDBMS tables, objects and data to MySQL. Developers and DBAs can quickly and easily convert existing applications to run on MySQL both on Windows and other platforms. Migration also supports migrating from earlier versions of MySQL to the latest releases.
  • 146. MySQL – MySQL Workbench
  • 147. MySQL – MySQL Workbench (Performance Schema) mysql> show variables like 'performance_schema'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | performance_schema | OFF | +--------------------+-------+ Parametro estático -> my.cnf mysql> show variables like 'performance_schema'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | performance_schema | ON | +--------------------+-------+ A partir da versão 5.7 o padrão é ON
  • 148. MySQL – Performance Schema • Top File I/O Activity Report: Show the files performing the most IOs (in bytes) • Top I/O by File by Time: Show the highest IO usage by file and latency • Top I/O by Event Category: Show the highest IO Data usage by event categories • Top I/O in Time by Event Categories: Show the highest IO time consumers by event categories • Top I/O Time by User/Thread: Show the top IO time consumers by user/thread • Statement Analysis: Lists statements with aggregated statistics • Statements in Highest 5 Percent by Runtime: Lists the top 5% statements with the highest runtime (in microseconds) • Using Temp Tables: Lists all statements that use temporary tables -- accesses the highest number of disk temporary tables, then memory temp tables • With Sorting: List all normalized statements that have done sorts, and accesses them in the following priority order -- sort_merge_passes, sort_scans, then sort_rows • Full Table Scans: Lists statements that performed a full table scan. Accesses query performance and the where clause(s), and if no index is used then it • recommends adding indexes for large tables • Errors or Warnings: Lists statements that have raised errors or warnings • Schema Object Overview (High Overhead): Shows counts by object type for each schema • Schema Index Statistics • Schema Table Statistics • Schema Table Statistics (with InnoDB buffer) • Tables with Full Table Scans: Finds tables that are being accessed by full table scans, ordering them by the number of rows scanned (DESC) • Unused Indexes: List of indexes that were never used since the server started or since P_S data collection started • Waits by Time: Lists the top wait events by their total time, ignoring idle (this often contain large values) • Waits by User by Time: Lists the top wait events by their total time, ignoring idle (this often contain large values) • Wait Classes by Time: Lists the top wait classes by total time, ignoring idle (this often contain large values) • Waits Classes by Average Time: Lists the top wait classes by average time, ignoring idle (this often contain large values) • InnoDB Buffer Stats by Schema: Summarizes the output of the INFORMATION_SCHEMA.INNODB_BUFFER_PAGE table, aggregating it by schema • InnoDB Buffer Stats by Table: Summarizes the output of the INFORMATION_SCHEMA.INNODB_BUFFER_PAGE table, aggregating it by schema and table name
  • 149. MySQL – Performance Schema (overhead) • PERFORMANCE SCHEMA disabled (NO PS) • PERFORMANCE SCHEMA enabled, with all consumers ON (PS on) • PERFORMANCE SCHEMA enabled, but only global_instrumentation consumer enabled. (PS only global) • PERFORMANCE SCHEMA enabled, but all consumers OFF (PS all off)
  • 150. MySQL – SYS Schema in MySQL 5.7.7 Who is taking up all the resources on my database server? mysql> show tables like 'user%'; +-----------------------------------+ | Tables_in_sys (user%) | +-----------------------------------+ | user_summary | | user_summary_by_file_io | | user_summary_by_file_io_type | | user_summary_by_stages | | user_summary_by_statement_latency | | user_summary_by_statement_type | +-----------------------------------+ 6 rows in set (0.00 sec) mysql> select * from user_summary_by_file_io_type where user = 'user01'; +----------+------------------------------------+-------+-----------+-------------+ | user | event_name | total | latency | max_latency | +----------+------------------------------------+-------+-----------+-------------+ | user01 | wait/io/file/sql/FRM | 36929 | 21.95 s | 239.56 ms | | user01 | wait/io/file/sql/file_parser | 1266 | 4.44 s | 250.93 ms | | user01 | wait/io/file/innodb/innodb_data_file | 1086 | 1.35 s | 647.00 ms | | user01 | wait/io/file/csv/metadata | 124 | 1.15 s | 192.03 ms | | user01 | wait/io/file/myisam/kfile | 4376 | 221.53 ms | 23.71 ms | | user01 | wait/io/file/innodb/innodb_log_file | 274 | 68.37 ms | 14.65 ms | | user01 | wait/io/file/myisam/dfile | 3544 | 56.23 ms | 6.66 ms | | user01 | wait/io/file/csv/data | 56 | 9.90 ms | 1.91 ms | | user01 | wait/io/file/sql/trigger | 2 | 3.86 ms | 3.77 ms | | user01 | wait/io/file/archive/data | 349 | 2.58 ms | 27.89 us | | user01 | wait/io/file/sql/dbopt | 14 | 601.64 us | 117.95 us | | user01 | wait/io/file/sql/misc | 13 | 540.14 us | 69.84 us | +----------+------------------------------------+-------+-----------+-------------+ 12 rows in set (0.01 sec) mysql> select * from sys.user_summary_by_file_io; +------------+-------+------------+ | user | ios | io_latency | +------------+-------+------------+ | root | 19514 | 2.87 s | | background | 5916 | 1.91 s | +------------+-------+------------+ 2 rows in set (0.00 sec) mysql> select * from sys.x$user_summary_by_file_io; +------------+-------+---------------+ | user | ios | io_latency | +------------+-------+---------------+ | root | 19514 | 2871847094292 | | background | 5916 | 1905079715132 | +------------+-------+---------------+
  • 151. MySQL – SYS Schema in MySQL 5.7.7 Which hosts are hitting my database server those most? mysql> show tables like 'host%'; +-----------------------------------+ | Tables_in_sys (host%) | +-----------------------------------+ | host_summary | | host_summary_by_file_io | | host_summary_by_file_io_type | | host_summary_by_stages | | host_summary_by_statement_latency | | host_summary_by_statement_type | +-----------------------------------+ 6 rows in set (0.04 sec)
  • 152. MySQL – SYS Schema in MySQL 5.7.7 Which objects are accessed the most, and how? mysql> select * from sys.schema_table_statistics limit 1G *************************** 1. row *************************** table_schema: bd01 table_name: customer total_latency: 1.01 m rows_fetched: 8171 fetch_latency: 321.97 ms rows_inserted: 9524 insert_latency: 58.24 s rows_updated: 8171 update_latency: 2.16 s rows_deleted: 0 delete_latency: 0 ps io_read_requests: 37 io_read: 57.82 KiB io_read_latency: 86.55 ms io_write_requests: 354 io_write: 3.99 MiB io_write_latency: 2.40 ms io_misc_requests: 140 io_misc_latency: 500.78 ms
  • 153. MySQL – SYS Schema in MySQL 5.7.7 What statements have the highest overall latency, and what statistics did they have? mysql> select * from statement_analysis limit 1G *************************** 1. row *************************** query: SELECT `t` . `THREAD_ID` AS `t ... _NUMBER_OF_BYTES_USED` ) DESC db: NULL full_scan: * exec_count: 20835 err_count: 4 warn_count: 0 total_latency: 38.96 m max_latency: 1.84 s avg_latency: 112.21 ms lock_latency: 33.91 s rows_sent: 844 rows_sent_avg: 0 rows_examined: 216255376 rows_examined_avg: 10379 rows_affected: 0 rows_affected_avg: 0 tmp_tables: 83340 tmp_disk_tables: 41666 rows_sorted: 1419984 sort_merge_passes: 0 digest: 2a8440817e00ec634e3d0b88c8e85ca4 first_seen: 2017-05-02 10:23:49 last_seen: 2017-05-02 11:11:49
  • 154. MySQL – SYS Schema in MySQL 5.7.7 Which statements are using temporary tables on disk? mysql> select * from statements_with_temp_tables limit 2G; *************************** 1. row *************************** query: SELECT `r` . `trx_wait_started ... ode` , `b` . `trx_started` AS db: NULL exec_count: 478707 total_latency: 13.43 m memory_tmp_tables: 3350949 disk_tmp_tables: 957414 avg_tmp_tables_per_query: 7 tmp_tables_to_disk_pct: 29 first_seen: 2017-05-02 09:49:18 last_seen: 2017-05-02 13:12:36 digest: 529f34e4046a3f19b7023aca37d6a394 *************************** 2. row *************************** query: SELECT `t` . `THREAD_ID` AS `t ... _NUMBER_OF_BYTES_USED` ) DESC db: NULL exec_count: 20835 total_latency: 38.96 m memory_tmp_tables: 83340 disk_tmp_tables: 41666 avg_tmp_tables_per_query: 4 tmp_tables_to_disk_pct: 50 first_seen: 2017-05-02 10:23:49 last_seen: 2017-05-02 11:11:49 digest: 2a8440817e00ec634e3d0b88c8e85ca4
  • 155. MySQL – SYS Schema in MySQL 5.7.7 Where is all the memory going on my instance? mysql> select * from memory_global_by_current_bytes limit 10; +---------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+ | event_name | current_count | current_alloc | current_avg_alloc | high_count | high_alloc | high_avg_alloc | +---------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+ | memory/innodb/buf_buf_pool | 1 | 131.06 MiB | 131.06 MiB | 1 | 131.06 MiB | 131.06 MiB | | memory/innodb/mem0mem | 12476 | 17.17 MiB | 1.41 KiB | 13197 | 32.27 MiB | 2.50 KiB | | memory/innodb/log0log | 9 | 16.01 MiB | 1.78 MiB | 9 | 16.01 MiB | 1.78 MiB | | memory/innodb/lock0lock | 9279 | 14.41 MiB | 1.59 KiB | 9279 | 14.41 MiB | 1.59 KiB | | memory/performance_schema/events_s_h_long | 1 | 13.50 MiB | 13.50 MiB | 1 | 13.50 MiB | 13.50 MiB | | memory/performance_schema/events_s_sry_by_digest | 1 | 9.77 MiB | 9.77 MiB | 1 | 9.77 MiB | 9.77 MiB | | memory/performance_schema/events_s_h_long.sqltext | 1 | 9.77 MiB | 9.77 MiB | 1 | 9.77 MiB | 9.77 MiB | | memory/performance_schema/events_s_h_long.tokens | 1 | 9.77 MiB | 9.77 MiB | 1 | 9.77 MiB | 9.77 MiB | | memory/performance_schema/table_handles | 1 | 9.00 MiB | 9.00 MiB | 1 | 9.00 MiB | 9.00 MiB | | memory/performance_schema/events_sta_name | 1 | 8.58 MiB | 8.58 MiB | 1 | 8.58 MiB | 8.58 MiB | +---------------------------------------------------+---------------+---------------+-------------------+------------+------------+----------------+
  • 156. MySQL – SYS Schema in MySQL 5.7.7 Which tables take the most space in my InnoDB buffer pool? mysql> select * from innodb_buffer_stats_by_table limit 10; +-------------------+----------------------------------------------------+------------+------------+-------+--------------+-----------+-------------+ | object_schema | object_name | allocated | data | pages | pages_hashed | pages_old | rows_cached | +-------------------+----------------------------------------------------+------------+------------+-------+--------------+-----------+-------------+ | mem__quan | normalized_statements_by_server_by_schema_data | 8.39 MiB | 10.87 MiB | 1074 | 1074 | 1074 | 36995 | | InnoDB System | SYS_TABLES | 1.70 MiB | 1.43 MiB | 109 | 109 | 109 | 4656 | | mem__advisor_text | localized_text | 1.45 MiB | 1.11 MiB | 93 | 93 | 93 | 1514 | | mysql | innodb_index_stats | 784.00 KiB | 380.36 KiB | 49 | 49 | 49 | 3182 | | mem__instruments | globaleventwaitsfilesummaryadvisor_fileioeventdata | 600.00 KiB | 792.25 KiB | 75 | 75 | 75 | 2265 | | mem__quan | example_statements | 560.00 KiB | 550.59 KiB | 70 | 70 | 70 | 1414 | | mem__bean_config | serialized_states | 480.00 KiB | 190.83 KiB | 30 | 30 | 30 | 481 | | mem__events | events | 320.00 KiB | 216.23 KiB | 20 | 20 | 20 | 169 | | InnoDB System | SYS_COLUMNS | 288.00 KiB | 203.33 KiB | 18 | 18 | 18 | 3055 | | mem__quan | normalized_statements | 288.00 KiB | 355.21 KiB | 36 | 36 | 36 | 716 | +-------------------+----------------------------------------------------+------------+------------+-------+--------------+-----------+-------------+ 10 rows in set (0.22 sec)
  • 157. MySQL – SYS Schema in MySQL 5.7.7 Which database files cause the most IO, and what kind of IO pattern do they have? mysql> select * from io_global_by_file_by_latency limit 10; +----------------------------------------------+---------+---------------+------------+--------------+-------------+---------------+ | file | total | total_latency | count_read | read_latency | count_write | write_latency | +----------------------------------------------+---------+---------------+------------+--------------+-------------+---------------+ | @@datadir/ib_logfile0 | 70001 | 57.67 s | 4 | 12.47 us | 35507 | 3.59 s | | @@datadir/ibdata1 | 37512 | 37.15 s | 715 | 1.14 s | 32690 | 623.45 ms | | @@datadir/ib_logfile1 | 46994 | 24.88 s | 2 | 18.75 ms | 23495 | 399.44 ms | | @@datadir/mysql/proc.MYD | 3072639 | 14.14 s | 2048438 | 12.02 s | 7 | 217.13 us | | @@datadir/mem__quan/data.ibd | 3134 | 2.59 s | 7 | 31.52 ms | 2793 | 397.90 ms | | @@datadir/mem__instruments/globaldata.ibd | 778 | 1.50 s | 10 | 10.37 ms | 629 | 272.66 ms | | @@datadir/mem__instruments/network.ibd | 424 | 1.30 s | 15 | 1.09 ms | 337 | 351.11 ms | | @@datadir/mem__instruments/fsstatistics.ibd | 397 | 1.21 s | 9 | 11.39 ms | 328 | 44.60 ms | | @@datadir/mem__events/events.ibd | 1518 | 1.15 s | 15 | 6.11 ms | 1292 | 11.03 ms | | @@datadir/mem__inventory/httpserver.ibd | 158 | 588.31 ms | 6 | 906.05 us | 98 | 650.16 us | +----------------------------------------------+---------+---------------+------------+--------------+-------------+---------------+
  • 158. MySQL – SYS Schema in MySQL 5.7.7 Where is the time being spent the most within my instance? mysql> select * from waits_global_by_latency limit 20; +----------------------------------------------+----------+---------------+-------------+-------------+ | events | total | total_latency | avg_latency | max_latency | +----------------------------------------------+----------+---------------+-------------+-------------+ | wait/io/table/sql/handler | 651645 | 7.54 m | 694.20 us | 2.31 s | | wait/io/socket/sql/client_connection | 8171190 | 3.74 m | 27.44 us | 215.29 ms | | wait/synch/sxlock/innodb/dict_operation_lock | 270734 | 1.60 m | 354.35 us | 1.21 s | | wait/io/file/innodb/innodb_log_file | 118289 | 1.40 m | 707.62 us | 820.19 ms | | wait/io/file/innodb/innodb_data_file | 82271 | 1.19 m | 869.42 us | 2.98 s | | wait/synch/mutex/innodb/log_sys_mutex | 4154064 | 37.93 s | 9.13 us | 238.31 ms | | wait/synch/mutex/sql/LOCK_table_cache | 4239803 | 35.55 s | 8.38 us | 13.47 ms | | wait/synch/sxlock/innodb/index_tree_rw_lock | 2447691 | 27.68 s | 11.31 us | 228.94 ms | | wait/synch/mutex/innodb/buf_pool_mutex | 3465947 | 18.26 s | 5.27 us | 2.31 s | | wait/synch/mutex/innodb/dict_sys_mutex | 1104503 | 15.95 s | 14.44 us | 1.18 s | | wait/io/file/myisam/dfile | 3185357 | 14.69 s | 4.61 us | 130.09 ms | | wait/io/file/sql/FRM | 26965 | 13.40 s | 497.05 us | 96.99 ms | | wait/synch/mutex/innodb/trx_sys_mutex | 5830470 | 10.92 s | 1.87 us | 83.22 ms | | wait/synch/mutex/innodb/lock_mutex | 1015659 | 6.13 s | 6.03 us | 81.22 ms | | wait/synch/sxlock/innodb/checkpoint_lock | 2036 | 5.19 s | 2.55 ms | 355.62 ms | | wait/synch/sxlock/innodb/fil_space_latch | 7061817 | 4.85 s | 686.52 ns | 528.87 ms | | wait/synch/mutex/sql/THD::LOCK_thd_data | 37813109 | 4.08 s | 107.82 ns | 29.66 ms | | wait/synch/sxlock/innodb/btr_search_latch | 451144 | 3.78 s | 8.39 us | 27.38 ms | | wait/synch/mutex/innodb/fil_system_mutex | 12971063 | 2.79 s | 215.27 ns | 32.92 ms | | wait/io/socket/sql/server_unix_socket | 275681 | 2.03 s | 7.35 us | 7.39 ms | +----------------------------------------------+----------+---------------+-------------+-------------+
  • 159. MySQL – SYS Schema in MySQL 5.7.7 Oracle´s V$SESSION? mysql> SELECT conn_id,user,db,command,state,time,current_statement,program_name,pid,last_statement_latency,last_wait FROM sys.session; +---------+-------------------+------+---------+--------------+------+----------------------+----------------+------+------------------------+-----------+ | conn_id | user | db | command | state | time | current_statement | program_name | pid | last_statement_latency | last_wait | +---------+-------------------+------+---------+--------------+------+----------------------+----------------+------+------------------------+-----------+ | 17 | [email protected] | NULL | Sleep | NULL | 130 | NULL | NULL | 3524 | 68.41 ms | NULL | | 12 | [email protected] | sys | Sleep | NULL | 121 | NULL | MySQLWorkbench | 6928 | 203.05 us | NULL | | 13 | [email protected] | NULL | Sleep | NULL | 121 | NULL | MySQLWorkbench | 6928 | 122.16 us | NULL | | 16 | [email protected] | sys | Sleep | NULL | 4 | NULL | NULL | 3524 | 2.55 ms | NULL | | 15 | [email protected] | NULL | Sleep | NULL | 1 | NULL | MySQLWorkbench | 6928 | 2.56 ms | NULL | | 14 | [email protected] | sys | Sleep | NULL | 0 | NULL | MySQLWorkbench | 6928 | 888.02 us | NULL | | 19 | root@localhost | NULL | Query | Sending data | 0 | SELECT conn_id,USER, | mysql | 4961 | NULL | NULL | +---------+-------------------+------+---------+--------------+------+----------------------+----------------+------+------------------------+-----------+ 7 rows in set (0.10 sec)
  • 160. MySQL – Jet Profiler https://www.jetprofiler.com/download/ MySQL Enterprise Monitor (free for a 30 day trial) MonYog (14-day evaluation)
  • 161. MySQL – mysqltuner.pl http://mysqltuner.pl/ [root@linux1 ~]# wget http://mysqltuner.pl/ -O mysqltuner.pl --no-check-certificate Resolvendo mysqltuner.pl... 217.70.184.38 Conectando-se a mysqltuner.pl|217.70.184.38|:80... conectado. Localização: https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl [redirecionando] --2017-05-02 14:56:44-- https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl Resolvendo raw.githubusercontent.com... 151.101.0.133, 151.101.64.133, 151.101.128.133, ... Conectando-se a raw.githubusercontent.com|151.101.0.133|:443... conectado. A requisição HTTP foi enviada, aguardando resposta... 200 OK Tamanho: 213310 (208K) [text/plain] Salvando em: “mysqltuner.pl” 100%[============================================================================>] 213.310 --.-K/s em 0,1s [root@linux1 ~]# chmod 775 mysqltuner.pl [root@linux1 ~]# ls -lh mysqltuner.pl -rwxrwxr-x 1 root root 209K Abr 26 14:56 mysqltuner.pl >> MySQLTuner 1.7.1 - Major Hayden <[email protected]> >> Bug reports, feature requests, and downloads at http://mysqltuner.com/ >> Run with '--help' for additional options and output filtering Pré-requisito: yum install perl-Data-Dumper.x86_64 -y
  • 162. MySQL – mysqltuner.pl [root@linux1 ~]# perl mysqltuner.pl –-host localhost --user root --pass admin --port 3306 --forcemem 4096 >> MySQLTuner 1.7.1 - Major Hayden <[email protected]> >> Bug reports, feature requests, and downloads at http://mysqltuner.com/ >> Run with '--help' for additional options and output filtering [--] Skipped version check for MySQLTuner script Please enter your MySQL administrative login: root Please enter your MySQL administrative password: [OK] Currently running supported MySQL version 5.7.18 [OK] Operating on 64-bit architecture -------- Log file Recommendations ------------------------------------------------------------------ [--] Log file: /var/log/mysqld.log(294K) [OK] Log file /var/log/mysqld.log exists [OK] Log file /var/log/mysqld.log is readable. [OK] Log file /var/log/mysqld.log is not empty [OK] Log file /var/log/mysqld.log is smaller than 32 Mb [!!] /var/log/mysqld.log contains 235 warning(s). [!!] /var/log/mysqld.log contains 11 error(s). [--] 34 start(s) detected in /var/log/mysqld.log [--] 1) 2017-05-02T17:49:31.850245Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 10) 2017-05-02T18:59:27.934180Z 0 [Note] /usr/sbin/mysqld: ready for connections. [--] 33 shutdown(s) detected in /var/log/mysqld.log [--] 1) 2017-05-02T20:35:24.502292Z 0 [Note] /usr/sbin/mysqld: Shutdown complete [--] 10) 2017-05-02T18:59:23.651679Z 0 [Note] /usr/sbin/mysqld: Shutdown complete -------- Storage Engine Statistics ----------------------------------------------------------------- [--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA [--] Data in MyISAM tables: 28M (Tables: 1) [--] Data in InnoDB tables: 32K (Tables: 2) [OK] Total fragmented tables: 0 -------- Security Recommendations ------------------------------------------------------------------ [OK] There are no anonymous accounts for any database users [OK] All database users have passwords assigned [!!] User 'root@%' hasn't specific host restriction. [!!] There is no basic password file list! Análise em servidor remoto: --forcemem <size> Amount of RAM installed in megabytes
  • 163. MySQL – mysqltuner.pl -------- Performance Metrics ----------------------------------------------------------------------- [--] Up for: 25m 23s (199 q [0.131 qps], 75 conn, TX: 331K, RX: 20K) [--] Reads / Writes: 100% / 0% [--] Binary logging is disabled [--] Physical Memory : 1006.1M [--] Max MySQL memory : 578.4M [--] Other process memory: 34.3M [--] Total buffers: 418.0M global + 1.1M per thread (151 max threads) [--] P_S Max memory usage: 36B [--] Galera GCache Max memory usage: 0B [OK] Maximum reached memory usage: 419.1M (41.65% of installed RAM) [OK] Maximum possible memory usage: 578.4M (57.49% of installed RAM) [OK] Overall possible memory usage with other process is compatible with memory available [OK] Slow queries: 0% (0/199) [OK] Highest usage of available connections: 0% (1/151) [!!] Aborted connections: 5.33% (4/75) [!!] Query cache may be disabled by default due to mutex contention. [!!] Query cache efficiency: 0.0% (0 cached / 95 selects) [OK] Query cache prunes per day: 0 [OK] Sorts requiring temporary tables: 0% (0 temp sorts / 4 sorts) [OK] No joins without indexes [OK] Temporary tables created on disk: 4% (82 on disk / 1K total) [OK] Thread cache hit rate: 98% (1 created / 75 connections) [OK] Table cache hit rate: 68% (529 open / 776 opened) [OK] Open file limit used: 0% (49/5K) [OK] Table locks acquired immediately: 100% (198 immediate / 198 locks) -------- Performance schema ------------------------------------------------------------------------ [--] Memory used by P_S: 36B [--] Sys schema is installed. -------- ThreadPool Metrics ------------------------------------------------------------------------ [--] ThreadPool stat is disabled. -------- MyISAM Metrics ---------------------------------------------------------------------------- [!!] Key buffer used: 11.6% (973K used / 8M cache) [OK] Key buffer size / total MyISAM indexes: 8.0M/44.0K [OK] Read Key buffer hit rate: 96.2% (186 cached / 7 reads)
  • 164. MySQL – mysqltuner.pl -------- InnoDB Metrics ---------------------------------------------------------------------------- [--] InnoDB is enabled. [--] InnoDB Thread Concurrency: 0 [OK] InnoDB File per table is activated [OK] InnoDB buffer pool / data size: 128.0M/32.0K [!!] Ratio InnoDB log file size / InnoDB Buffer pool size (75 %): 48.0M * 2/128.0M should be equal 25% [OK] InnoDB buffer pool instances: 1 [--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s) [OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances [OK] InnoDB Read buffer efficiency: 92.84% (3032 hits/ 3266 total) [!!] InnoDB Write Log efficiency: 0% (2 hits/ 0 total) [OK] InnoDB log waits: 0.00% (0 waits / 2 writes) -------- XtraDB Metrics ---------------------------------------------------------------------------- [--] XtraDB is disabled. -------- Galera Metrics ---------------------------------------------------------------------------- [--] Galera is disabled. -------- Replication Metrics ----------------------------------------------------------------------- [--] Galera Synchronous replication: NO [--] No replication slave(s) for this server. [--] This is a standalone server. -------- Recommendations --------------------------------------------------------------------------- General recommendations: Control warning line(s) into /var/log/mysqld.log file Control error line(s) into /var/log/mysqld.log file Restrict Host for user@% to user@SpecificDNSorIp MySQL started within last 24 hours - recommendations may be inaccurate Reduce or eliminate unclosed connections and network issues Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1 Variables to adjust: query_cache_size (=0) query_cache_type (=0) query_cache_limit (> 2M, or use smaller result sets) innodb_log_file_size * innodb_log_files_in_group should be equals to 1/4 of buffer pool size (=64M) if possible.
  • 165. MySQL in the CLOUD
  • 167. MySQL – Amazon RDS – Multi-AZ Deployments
  • 168. MySQL – Amazon RDS – Multi-AZ Deployments
  • 178. MySQL – The End THE END