O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Curso de MySQL 5.7

162 visualizações

Publicada em

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

Publicada em: Tecnologia
  • Seja o primeiro a comentar

Curso de MySQL 5.7

  1. 1. Eduardo Legatti - DBA
  2. 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
  3. 3. History
  4. 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
  5. 5. MySQL - History
  6. 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
  7. 7. Download / Install
  8. 8. MySQL - Download
  9. 9. MySQL - Download
  10. 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. 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. 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)
  13. 13. Configuration
  14. 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. 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. 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. 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. 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. 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. 20. MySQL - Files or Resources
  21. 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. 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. 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. 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. 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
  26. 26. Users / Privileges
  27. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 41. MySQL – Thread States https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html
  42. 42. Instance Structures / Engines
  43. 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. 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. 45. MySQL - Create DATABASE
  46. 46. MySQL - Create DATABASE
  47. 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. 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. 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. 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. 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. 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. 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
  54. 54. MySQL - Instance Files
  55. 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. 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. 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. 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. 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)
  60. 60. InnoDB vs. MyISAM
  61. 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. 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. 63. MySQL - InnoDB vs. MyISAM
  64. 64. MySQL - InnoDB vs. MyISAM
  65. 65. Transaction Isolation Level
  66. 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. 67. ISOLATION LEVEL (READ COMMITTED vs. REPEATABLE READ)
  68. 68. ISOLATION LEVEL (READ COMMITTED vs. REPEATABLE READ)
  69. 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. 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. 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. 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. 73. Locks
  74. 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. 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. 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. 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. 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. 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. 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 | +-------+---------------------------------------+
  81. 81. Binary Log
  82. 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. 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. 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. 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. 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. 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 | +---------------+--------+
  88. 88. CSV Files
  89. 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. 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. 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. 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. 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
  94. 94. Exporting / Importing

×