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
14. [root@linux1 tmp]# service mysqld start (systemctl start mysqld)
Inicializando o banco de dados MySQL: [ OK ]
Installing validate password plugin: [ OK ]
Iniciando o mysqld: [ OK ]
[root@linux1]# grep 'temporary password' /var/log/mysqld.log
2017-05-02T10:30 [Note] A temporary password is generated for root@localhost: 8V*vubKtFQp(
[root@linux1 ~]# mysql -uroot –p'8V*vubKtFQp('
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5 Server version: 5.7.18
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing
this statement.
MySQL - Initial Configuration
15. mysql> alter user root@localhost identified by '@#4Jhfjk43jkdjk3#@';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user root@localhost identified by 'Asdjer3jk4kjd4@#';
Query OK, 0 rows affected (0,00 sec)
mysql> show variables like '%password_policy';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| validate_password_policy | MEDIUM |
+--------------------------+--------+
1 row in set (0,07 sec)
mysql> SET GLOBAL validate_password_policy=LOW;
Query OK, 0 rows affected (0,03 sec)
mysql> alter user root@localhost identified by 'admin';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user root@localhost identified by '12345678';
Query OK, 0 rows affected (0,01 sec)
MySQL - Configuration (reset root password)
16. mysql> show plugins;
+----------------------------+----------+--------------------+----------------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+----------------------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL |
| partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
| validate_password | ACTIVE | VALIDATE PASSWORD | validate_password.so | GPL |
+----------------------------+----------+--------------------+----------------------+---------+
45 rows in set (0,01 sec)
mysql> UNINSTALL PLUGIN validate_password;
Query OK, 0 rows affected (0,07 sec)
mysql> alter user root@localhost identified by 'admin';
Query OK, 0 rows affected (0,00 sec)
mysql> INSTALL PLUGIN validate_password SONAME 'validate_password.so';
Query OK, 0 rows affected (0,03 sec)
MySQL - Configuration (reset root password)
17. [root@linux1]# whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
mysql> help
For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (?) Synonym for `help'.
clear (c) Clear the current input statement.
connect (r) Reconnect to the server. Optional arguments are db and host.
delimiter (d) Set statement delimiter.
edit (e) Edit command with $EDITOR.
ego (G) Send command to mysql server, display result vertically.
exit (q) Exit mysql. Same as quit.
go (g) Send command to mysql server.
help (h) Display this help.
nopager (n) Disable pager, print to stdout.
notee (t) Don't write into outfile.
pager (P) Set PAGER [to_pager]. Print the query results via PAGER.
print (p) Print current command.
prompt (R) Change your mysql prompt.
quit (q) Quit mysql.
rehash (#) Rebuild completion hash.
source (.) Execute an SQL script file. Takes a file name as an argument.
status (s) Get status information from the server.
system (!) Execute a system shell command.
tee (T) Set outfile [to_outfile]. Append everything into given outfile.
use (u) Use another database. Takes database name as argument.
charset (C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (W) Show warnings after every statement.
nowarning (w) Don't show warnings after every statement.
resetconnection(x) Clean session context.
For server side help, type 'help contents'
MySQL - mysql utility
mysql> help contents;
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item>
is one of the following
categories:
Account Management
Administration
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Procedures
Storage Engines
Table Maintenance
Transactions
User-Defined Functions
Utility
18. [root@linux1 ~]# systemctl start mysqld
[root@linux1 ~]# systemctl status mysqld
* mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2017-05-02 13:45:55 BST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3105 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3084 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3109 (mysqld)
CGroup: /system.slice/mysqld.service
??3109 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Mai 2 13:45:50 linux1 systemd[1]: Starting MySQL Server...
Mai 2 13:45:55 linux1 systemd[1]: Started MySQL Server.
[root@linux1 ~]# systemctl stop mysqld
[root@linux1 ~]# systemctl status mysqld
* mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2017-05-02 13:47:00 BST; 904ms ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3105 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3084 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3109 (code=exited, status=0/SUCCESS)
Mai 2 13:45:50 linux1 systemd[1]: Starting MySQL Server...
Mai 2 13:45:55 linux1 systemd[1]: Started MySQL Server.
Mai 2 13:46:59 linux1 systemd[1]: Stopping MySQL Server...
Mai 2 13:47:00 linux1 systemd[1]: Stopped MySQL Server.
MySQL - Status / Stop / Start
19. [root@linux1 ~]# service mysqld status
mysqld (pid 3489) está em execução...
[root@linux1 ~]# ps aux | grep mysql
root 3244 0.0 0.1 5152 1368 pts/0 S 15:00 0:01
/bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock
--pid-file=/var/run/mysqld/mysqld.pid--basedir=/usr --user=mysql
mysql 3489 1.6 16.7 555380 172072 pts/0 Sl 15:00 1:37 /usr/sbin/mysqld --basedir=/usr
--datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql
--log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
[root@linux1 ~]# service mysqld stop (systemctl stop mysqld)
Parando o mysqld: [ OK ]
[root@linux1 ~]# service mysqld start (systemctl start mysqld)
Iniciando o mysqld: [ OK ]
[root@linux1 ~]# service mysqld restart (systemctl restart mysqld)
Parando o mysqld: [ OK ]
Iniciando o mysqld: [ OK ]
mysql> show variables like '%bind%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| bind_address | * |
+---------------+-------+
[root@linux1 ~]# netstat -na | grep 3306
tcp 0 0 :::3306 :::* LISTEN
MySQL - Status / Stop / Start
mysql> help SHUTDOWN;
Name: 'SHUTDOWN'
This statement stops the MySQL server. It requires the SHUTDOWN
privilege.
SHUTDOWN was added in MySQL 5.7.9. It provides an SQL-level interface
to the same functionality available using the mysqladmin shutdown
command or the mysql_shutdown() C API function.
mysql> shutdown;
Query OK, 0 rows affected (0,03 sec)
mysql> select 1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)
ERROR:
Can't connect to the server
21. [root@linux1 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
MySQL - Configuration File
22. [root@linux1 ~]# tail -20 /var/log/mysqld.log
2017-05-02T19:41:01.660047Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s)
are active.
2017-05-02T19:41:01.660148Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2017-05-02T19:41:01.674840Z 0 [Note] InnoDB: Waiting for purge to start
2017-05-02T19:41:01.730361Z 0 [Note] InnoDB: 5.7.17 started; log sequence number 2537004
2017-05-02T19:41:01.749315Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2017-05-02T19:41:01.759440Z 0 [Note] Plugin 'FEDERATED' is disabled.
2017-05-02T19:41:01.854483Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170408 16:41:01
2017-05-02T19:41:02.093335Z 0 [Warning] CA certificate ca.pem is self signed.
2017-05-02T19:41:02.211370Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2017-05-02T19:41:02.218146Z 0 [Note] IPv6 is available.
2017-05-02T19:41:02.218559Z 0 [Note] - '::' resolves to '::';
2017-05-02T19:41:02.219456Z 0 [Note] Server socket created on IP: '::'.
2017-05-02T19:41:02.428972Z 0 [Note] Event Scheduler: Loaded 0 events
[root@linux1 /]# service mysqld start
MySQL Daemon failed to start.
Iniciando o mysqld: [FALHOU]
[root@linux1 ~]# cat /var/log/mysqld.log | grep ERROR
2017-05-02T20:35:39.247229Z 0 [ERROR] unknown variable 'not_exist=legatti'
2017-05-02T20:35:39.247309Z 0 [ERROR] Aborting
MySQL - Error Log File
23. mysql> show variables like 'log_timestamps';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| log_timestamps | UTC |
+----------------+-------+
mysql> set GLOBAL log_timestamps=SYSTEM;
Query OK, 0 rows affected (0,00 sec)
[root@linux1 ~]# cat /var/log/mysqld.log | grep ERROR
2017-05-02T20:35:39.247229Z 0 [ERROR] unknown variable 'not_exist=legatti'
2017-05-02T20:35:39.247309Z 0 [ERROR] Aborting
MySQL - Error Log File
As of MySQL 5.7.2, the log_timestamps system variable controls the timestamp time zone of
messages written to the error log (as well as to general query log and slow query log files).
Permitted values are UTC (the default) and SYSTEM (local system time zone). Before MySQL
5.7.2, messages use the local system time zone
24. [root@linux1 ~]# mysql –u<user> -p<password> mysql –uroot –padmin
mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 5.7.18 |
+-----------+
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
mysql> show variables like 'bind_address';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| bind_address | * |
+---------------+-------+
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
MySQL - Connecting Locally
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@linux1 ~]# mysql -uroot -padmin -e "show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| bd01 |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@linux1 ~]# echo "show databases" | mysql -uroot -padmin
Database
information_schema
mysql
performance_schema
sys
25. [root@linux1 ~]# mysql –h<host> –u<user> -p<password> --port<port>
C:>mysql -h192.168.56.101 -uroot -padmin --port 3306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 24
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
MySQL - Connecting Remotely
27. mysql> CREATE USER 'admin01'@'192.168.56.102' IDENTIFIED BY '#a1234@';
Query OK, 0 rows affected (0,00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin02'@'%' IDENTIFIED BY '#b1234@';
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> CREATE USER 'admin03'@'192.168.%' IDENTIFIED BY '#c1234@';
Query OK, 0 rows affected (0,01 sec)
mysql> select user,host,authentication_string from mysql.user order by 1;
+-----------+----------------+-------------------------------------------+
| user | host | authentication_string |
+-----------+----------------+-------------------------------------------+
| admin01 | 192.168.56.102 | *1260D0031D9EDAC141609A3723FBD16C2260425F |
| admin02 | % | *E9190029100941FBE4D00E22146E8EDA75667142 |
| admin03 | 192.168.% | *3A9EF2D8B8D45B60FD5D978A872D28C62FD139BB |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| root | localhost | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| root | % | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
+-----------+----------------+-------------------------------------------+
6 rows in set (0,00 sec)
MySQL - Creating Users
CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'hostname' IDENTIFIED BY 'password' WITH GRANT OPTION;
28. mysql> show grants for scott;
+------------------------------------+
| Grants for scott@% |
+------------------------------------+
| GRANT SELECT ON *.* TO 'scott'@'%' |
+------------------------------------+
1 row in set (0.00 sec)
mysql> RENAME USER 'scott'@'%' TO 'adam'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for adam;
+-----------------------------------+
| Grants for adam@% |
+-----------------------------------+
| GRANT SELECT ON *.* TO 'adam'@'%' |
+-----------------------------------+
1 row in set (0.00 sec)
MySQL - Renaming Users
RENAME USER 'old_user' TO 'new_user';
The RENAME USER statement renames existing MySQL accounts. An error occurs for old accounts that do not exist or new
accounts that already exist. To use RENAME USER, you must have the global CREATE USER privilege, or the UPDATE privilege
for the mysql database.
29. mysql> create user 'scott'@'%' identified by 'tiger';
Query OK, 0 rows affected (0.00 sec)
mysql> grant select on *.* to 'scott'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> alter user scott ACCOUNT LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@linux1 ~]# mysql -uscott -ptiger
ERROR 3118 (HY000): Access denied for user 'scott'@'localhost'. Account is locked.
Mysql> alter user scott ACCOUNT UNLOCK;
MySQL - Creating Users (account lock – 5.7)
30. mysql> SHOW PRIVILEGES;
+-------------------------+---------------------------------------+-------------------------------------------------------+
| Privilege | Context | Comment |
+-------------------------+---------------------------------------+-------------------------------------------------------+
| Alter | Tables | To alter the table |
| Alter routine | Functions,Procedures | To alter or drop stored functions/procedures |
| Create | Databases,Tables,Indexes | To create new databases and tables |
| Create routine | Databases | To use CREATE FUNCTION/PROCEDURE |
| Create temporary tables | Databases | To use CREATE TEMPORARY TABLE |
| Create view | Tables | To create new views |
| Create user | Server Admin | To create new users |
| Delete | Tables | To delete existing rows |
| Drop | Databases,Tables | To drop databases, tables, and views |
| Event | Server Admin | To create, alter, drop and execute events |
| Execute | Functions,Procedures | To execute stored routines |
| File | File access on server | To read and write files on the server |
| Grant option | Databases,Tables,Functions,Procedures | To give to other users those privileges you possess |
| Index | Tables | To create or drop indexes |
| Insert | Tables | To insert data into tables |
| Lock tables | Databases | To use LOCK TABLES (together with SELECT privilege) |
| Process | Server Admin | To view the plain text of currently executing queries |
| Proxy | Server Admin | To make proxy user possible |
| References | Databases,Tables | To have references on tables |
| Reload | Server Admin | To reload or refresh tables, logs and privileges |
| Replication client | Server Admin | To ask where the slave or master servers are |
| Replication slave | Server Admin | To read binary log events from the master |
| Select | Tables | To retrieve rows from table |
| Show databases | Server Admin | To see all databases with SHOW DATABASES |
| Show view | Tables | To see views with SHOW CREATE VIEW |
| Shutdown | Server Admin | To shut down the server |
| Super | Server Admin | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc. |
| Trigger | Tables | To use triggers |
| Create tablespace | Server Admin | To create/alter/drop tablespaces |
| Update | Tables | To update existing rows |
| Usage | Server Admin | No privileges - allow connect only |
+-------------------------+---------------------------------------+-------------------------------------------------------+
MySQL - Privileges
31. GRANT ALTER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT ALTER ROUTINE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE ROUTINE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE TABLESPACE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE TEMPORARY TABLES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE USER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT CREATE VIEW ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT DELETE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT DROP ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT EVENT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT EXECUTE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT FILE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT INDEX ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT INSERT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT LOCK TABLES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT PROCESS ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT REFERENCES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT RELOAD ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT REPLICATION CLIENT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT REPLICATION SLAVE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SELECT ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SHOW DATABASES ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SHOW VIEW ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SHUTDOWN ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT SUPER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT TRIGGER ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
GRANT UPDATE ON *.* TO 'mysql.sys'@'localhost' WITH GRANT OPTION
MySQL - Creating Users (Privileges)
https://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html
32. mysql> SHOW CREATE USER 'admin02'@'%';
+-------------------------------------------------------------------------------------------------------------------+
| CREATE USER for admin02@% |
+-------------------------------------------------------------------------------------------------------------------+
| CREATE USER 'admin02'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*E9190029100941FBE4D00E22146E8EDA75667142' |
+-------------------------------------------------------------------------------------------------------------------+
1 row in set (0,01 sec)
mysql> SHOW GRANTS FOR 'admin02'@'%';
+----------------------------------------------+
| Grants for admin02@% |
+----------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'admin02'@'%' |
+----------------------------------------------+
1 row in set (0,00 sec)
mysql> SELECT CONCAT('SHOW GRANTS FOR ''',USER,'''@''',HOST,''';') FROM mysql.user WHERE USER <> '';
+------------------------------------------------------+
| CONCAT('SHOW GRANTS FOR ''',USER,'''@''',HOST,''';') |
+------------------------------------------------------+
| SHOW GRANTS FOR 'admin01'@'192.168.56.102'; |
| SHOW GRANTS FOR 'admin02'@'%'; |
| SHOW GRANTS FOR 'admin03'@'192.168.%'; |
| SHOW GRANTS FOR 'root'@'%'; |
| SHOW GRANTS FOR 'mysql.sys'@'localhost'; |
| SHOW GRANTS FOR 'root'@'localhost'; |
+------------------------------------------------------+
6 rows in set (0,00 sec)
MySQL - Extracting Users Information
SHOW CREATE USER 'username'@'hostname';
SHOW GRANTS FOR 'username'@'hostname';
33. mysql> select current_user;
+----------------+
| current_user |
+----------------+
| root@localhost |
+----------------+
1 row in set (0,00 sec)
mysql> SHOW GRANTS FOR CURRENT_USER;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0,00 sec)
mysql> show grants for root;
+-------------------------------------------+
| Grants for root@% |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' |
+-------------------------------------------+
1 row in set (0,00 sec)
mysql> show grants for root@localhost;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0,00 sec)
MySQL - Extracting Users Information
34. mysql> desc mysql.user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| authentication_string | text | YES | | NULL | |
|... | | | | | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
MySQL - Extracting Users Information
mysql.user: User level privileges
35. mysql> desc mysql.db;
+-----------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
+-----------------------+---------------+------+-----+---------+-------+
MySQL - Extracting Users Information
mysql.db: Database level privileges
36. mysql> desc mysql.tables_priv;
+-------------+--------------------------------------------------------------------------------------------+
| Field | Type |
+-------------+--------------------------------------------------------------------------------------------+
| Host | char(60) |
| Db | char(64) |
| User | char(32) |
| Table_name | char(64) |
| Grantor | char(93) |
| Timestamp | timestamp |
| Table_priv | set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index',etc.) |
| Column_priv | set('Select','Insert','Update','References') |
+-------------+--------------------------------------------------------------------------------------------+
MySQL - Extracting Users Information
mysql.tables_priv: Table level privileges
mysql> desc mysql.columns_priv;
+-------------+----------------------------------------------+------+-----+
| Field | Type | Null | Key |
+-------------+----------------------------------------------+------+-----+
| Host | char(60) | NO | PRI |
| Db | char(64) | NO | PRI |
| User | char(32) | NO | PRI |
| Table_name | char(64) | NO | PRI |
| Column_name | char(64) | NO | PRI |
| Timestamp | timestamp | NO | |
| Column_priv | set('Select','Insert','Update','References') | NO | |
+-------------+----------------------------------------------+------+-----+
mysql.columns_priv: Column level privileges
37. mysql> desc mysql.procs_priv;
+--------------+----------------------------------------+------+-----+
| Field | Type | Null | Key |
+--------------+----------------------------------------+------+-----+
| Host | char(60) | NO | PRI |
| Db | char(64) | NO | PRI |
| User | char(32) | NO | PRI |
| Routine_name | char(64) | NO | PRI |
| Routine_type | enum('FUNCTION','PROCEDURE') | NO | PRI |
| Grantor | char(93) | NO | MUL |
| Proc_priv | set('Execute','Alter Routine','Grant') | NO | |
| Timestamp | timestamp | NO | |
+--------------+----------------------------------------+------+-----+
MySQL - Extracting Users Information
mysql.procs_priv: Procedure / Function privileges
38. mysql> ALTER USER 'admin02'@'%' WITH MAX_USER_CONNECTIONS 5;
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> SELECT USER,HOST,MAX_USER_CONNECTIONS FROM mysql.user ORDER BY 1;
+-----------+----------------+----------------------+
| USER | HOST | MAX_USER_CONNECTIONS |
+-----------+----------------+----------------------+
| admin01 | 192.168.56.102 | 0 |
| admin02 | % | 5 |
| admin03 | 192.168.% | 0 |
| mysql.sys | localhost | 0 |
| root | localhost | 0 |
| root | % | 0 |
| teste | % | 0 |
+-----------+----------------+----------------------+
7 rows in set (0,00 sec)
[root@linux1 ~]# mysql -uadmin02 –p****
ERROR 1226 (42000): User 'admin02' has exceeded the 'max_user_connections' resource (current value: 5)
mysql> SHOW FULL PROCESSLIST;
+----+---------+----------------------+------+---------+------+----------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+---------+----------------------+------+---------+------+----------+-----------------------+
| 33 | admin02 | 192.168.56.102:55235 | NULL | Sleep | 309 | | NULL |
| 34 | admin02 | 192.168.56.102:55236 | NULL | Sleep | 309 | | NULL |
| 36 | admin02 | 192.168.56.102:55238 | NULL | Sleep | 300 | | NULL |
| 37 | admin02 | 192.168.56.102:55239 | NULL | Sleep | 300 | | NULL |
| 38 | admin02 | 192.168.56.102:55240 | NULL | Sleep | 289 | | NULL |
| 42 | root | localhost | NULL | Query | 0 | starting | SHOW FULL PROCESSLIST |
+----+---------+----------------------+------+---------+------+----------+-----------------------+
6 rows in set (0,00 sec)
MySQL - Limiting Connections
39. mysql> set GLOBAL max_connections = 3;
Query OK, 0 rows affected (0,00 sec)
[root@linux1 ~]# mysql –uadmin02 –p****
ERROR 1040 (HY000): Too many connections
mysql> set GLOBAL max_connections = 200;
Query OK, 0 rows affected (0,00 sec)
mysql> set GLOBAL max_user_connections = 2;
Query OK, 0 rows affected (0,00 sec)
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
[root@linux1 ~]# mysql -uadmin02 –p****
ERROR 1226 (42000): User 'admin02' has exceeded the 'max_user_connections' resource (current value: 5)
[root@linux1 ~]# mysql -uadmin01 -p****
[root@linux1 ~]# mysql -uadmin01 -p****
[root@linux1 ~]# mysql -uadmin01 -p****
ERROR 1203 (42000): User admin01 already has more than 'max_user_connections' active connections
MySQL - Limiting Connections
Priority
[MAX_CONNECTIONS]
[ALTER USER...WITH MAX_USER_CONNECTIONS]
[MAX_USER_CONNECTIONS]
40. mysql> SHOW FULL PROCESSLIST;
+--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+
| 1 | event_scheduler | localhost | NULL | Daemon | 2128 | Waiting for next activation | NULL |
| 358213 | user_bd01 | 11.215.210.209:60440 | bd01 | Query | 1 | Sending Data | SELECT id FROM T1 |
| 358214 | user_bd01 | 11.215.210.209:60441 | bd01 | Sleep | 363 | | NULL |
| 358216 | user_bd01 | 11.215.210.209:60464 | bd01 | Query | 544 | Sorting for order | SELECT * FROM T3,T2 WHERE...|
| 358217 | user_bd01 | 11.215.210.209:60465 | bd01 | Query | 0 | Creating sort index | SELECT * FROM T3 ORDER BY 2 |
| 361836 | root | 11.215.200.30:55730 | NULL | Sleep | 28 | | NULL |
| 361837 | root | 11.215.200.30:55731 | NULL | Sleep | 1502 | | NULL |
| 361865 | root | localhost | NULL | Query | 0 | init | SHOW FULL PROCESSLIST |
| 361975 | user_bd02 | 11.215.210.209:59730 | bd02 | Query | 2 | Writing to net | SELECT COUNT(*) FROM T2 |
| 362101 | user_bd02 | 11.215.210.209:60554 | bd02 | Sleep | 12 | | NULL |
+--------+-----------------+----------------------+-------+---------+------+-----------------------------+-----------------------------+
mysql> SELECT
-> USER,
-> SUBSTRING_INDEX(HOST, ':', 1) HOST,
-> DB,
-> COUNT(*) QTD
-> FROM
-> INFORMATION_SCHEMA.PROCESSLIST
-> GROUP BY USER,
-> SUBSTRING_INDEX(HOST, ':', 1),
-> DB
-> ORDER BY QTD DESC ;
+-----------------+----------------+-------+-----+
| USER | HOST | DB | QTD |
+-----------------+----------------+-------+-----+
| user_bd01 | 11.215.210.209 | bd01 | 4 |
| user_bd02 | 11.215.210.209 | bd02 | 2 |
| root | 11.215.200.30 | NULL | 2 |
| root | localhost | NULL | 1 |
| event_scheduler | localhost | NULL | 1 |
+-----------------+----------------+-------+-----+
MySQL – Listing Connections
-- Current Thread ID
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 3435|
+-----------------+
-- Current User
mysql> select CURRENT_USER;
+----------------+
| current_user |
+----------------+
| root@localhost |
+----------------+
41. MySQL – Thread States
https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html
43. MySQL – Instance Structure
• Query cache : Shared identical queries from memory. If an identical query from client found in query cache then, the mysql server retrieves the results from
the query cache rather than parsing and executing that query again. It’s a shared cache for sessions , so a result set generated by one client can be sent in
response to the same query issued by another client. If MySQL does find a match in the query cache, it must check privileges before returning the cached
query. This is possible without parsing the query, because MySQL stores table information with the cached query. If the privileges are OK, MySQL
retrieves the stored result from the query cache and sends it to the client, bypassing every other stage in query execution. The query is never parsed,
optimized, or executed.
• Parser: Check for SQL syntax by checking every character in SQL query and generate SQL_ID for each SQL query.
• Metadata cache: Cache for object metadata information and statistics.
• key cache: Cache table indexes.In MySQL keys are indexes. if index size is small then it will cache index structure and data leaf.If an index is large then it will
only cache index structure.Used by MyISAM storage engine.
• Optimizer : Created efficient query execution plan as per the storage engine.It will rewrite a query.Example: InnoDB has shared buffer so optimizer will get
pre-cached data from it. Using table statistics optimizer will generate an execution plan for a SQL query.
44. MySQL - SQL Execution
Query Cache
• Any query that contains a nondeterministic function, such
as NOW() or CURRENT_DATE(), will not be cached.
Similarly, functions such as CURRENT_USER() or
CONNECTION_ID() might vary when executed by different
users, thereby preventing a cache hit.
• The query cache is not supported for partitioned tables,
and is automatically disabled for queries involving
partitioned tables.
• Each client connection gets its own thread within the
server process. The connection’s queries execute within
that single thread, which in turn resides on one core or
CPU. The server caches threads, so they don’t need to be
created and destroyed for each new connection.
• Before even parsing the query, though, the server consults
the query cache, which can store only SELECT statements,
along with their result sets. If anyone issues a query that’s
identical to one already in the cache, the server doesn’t
need to parse, optimize, or execute the query at all—it
can simply pass back the stored result set.
47. mysql> show variables like 'lower_case_table_names';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_table_names | 0 |
+------------------------+-------+
1 row in set (0,06 sec)
mysql> select count(*) from teste01.table_01;
+----------+
| count(*) |
+----------+
| 335872 |
+----------+
1 row in set (0,26 sec)
mysql> select count(*) from teste01.TABLE_01;
ERROR 1146 (42S02): Table 'teste01.TABLE_01' doesn't exist
mysql> show variables like 'lower_case_table_names';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_table_names | 1 |
+------------------------+-------+
1 row in set (0,03 sec)
mysql> select count(*) from teste01.TABLE_01;
+----------+
| count(*) |
+----------+
| 335872 |
+----------+
1 row in set (0,16 sec)
MySQL - Disabling Case Sensitive
No linux, o mysql é case sensitive por padrão. No windows não.
Para desabilitar a sensibilidade do Linux, hablite o parametro "lower_case_table_names"
no MySQL
Pare o seu mysql
$ service mysqld stop
Edite o seu my.conf
Na seção [mysqld] adicione o parametro lower_case_table_names com o valor 1
[mysqld]
...
lower_case_table_names=1
Salve e feche o arquivo e reinicie o serviço do mysql
$ service mysqld start
48. mysql> create database teste01;
Query OK, 1 row affected (0,01 sec)
mysql> USE teste01;
Database changed
mysql> SELECT @@character_set_database, @@collation_database;
+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| latin1 | latin1_swedish_ci |
+--------------------------+----------------------+
1 row in set (0,00 sec)
mysql> SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
-> FROM INFORMATION_SCHEMA.SCHEMATA WHERE
-> SCHEMA_NAME = 'teste01';
+----------------------------+------------------------+
| DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME |
+----------------------------+------------------------+
| latin1 | latin1_swedish_ci |
+----------------------------+------------------------+
1 row in set (0,01 sec)
MySQL - Create DATABASE
49. mysql> USE teste01;
Database changed
mysql> CREATE TABLE IF NOT EXISTS table_01 (
-> task_id INT(11) NOT NULL AUTO_INCREMENT,
-> subject VARCHAR(45) DEFAULT NULL,
-> PRIMARY KEY (task_id)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0,02 sec)
mysql> CREATE TABLE IF NOT EXISTS table_02 (
-> task_id INT(11) NOT NULL AUTO_INCREMENT,
-> subject VARCHAR(45) DEFAULT NULL,
-> PRIMARY KEY (task_id)
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0,01 sec)
mysql> show tables;
+-------------------+
| Tables_in_teste01 |
+-------------------+
| table_01 |
| table_02 |
+-------------------+
2 rows in set (0,00 sec)
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
MySQL - Create Table
50. mysql> SHOW TABLE STATUSG
*************************** 1. row ***************************
Name: table_01
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 335773
Avg_row_length: 35
Data_length: 12075008
Max_data_length: 0
Index_length: 6832128
Data_free: 4194304
Auto_increment: 430046
Create_time: 2017-05-02 12:43:52
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
*************************** 2. row ***************************
Name: table_02
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 458752
Avg_row_length: 23
Data_length: 10780672
Max_data_length: 281474976710655
Index_length: 5847040
Data_free: 0
Auto_increment: 458753
Create_time: 2017-05-02 12:43:58
Update_time: 2017-05-02 15:07:01
Check_time: 2017-05-02 15:06:03
Collation: latin1_swedish_ci
MySQL - Show Table Status
Data Size in Bytes (~12MB)
No Maximum (64Tb for tablespace)
Index size in bytes (~6MB)
Unused space (~4MB)
mysql> show variables like 'innodb_page_size';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| innodb_page_size | 16384 |
+------------------+-------+
51. mysql> delete from table_02;
Query OK, 458752 rows affected (22,73 sec)
mysql> show table status like 'table_02'G
*************************** 1. row ***************************
Name: table_02
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 10780672
Max_data_length: 281474976710655
Index_length: 5847040
Data_free: 10780672
Auto_increment: 458753
Create_time: 2017-05-02 12:43:58
Update_time: 2017-05-02 21:03:35
Check_time: 2017-05-02 15:06:03
Collation: latin1_swedish_ci
mysql> show table status like 'table_02'G
*************************** 1. row ***************************
Name: table_02
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 0
Max_data_length: 281474976710655
Index_length: 1024
Data_free: 0
Auto_increment: 458753
Create_time: 2017-05-02 12:43:58
Update_time: 2017-05-02 21:14:43
Check_time: 2017-05-02 21:14:43
Collation: latin1_swedish_ci
MySQL - Optimize Table (MyISAM)
mysql> optimize table table_02;
+------------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------+----------+----------+----------+
| teste01.table_02 | optimize | status | OK |
+------------------+----------+----------+----------+
1 row in set (0,08 sec)
• To reclaim space to the filesystem run "OPTIMIZE TABLE"
Creates a copy of the table, drops the original, and
renames the copy
• If frequent activity on the table run "ANALYZE TABLE"
52. mysql> delete from table_01;
Query OK, 335872 rows affected (9,37 sec)
mysql> optimize table table_01;
+------------------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+------------------+----------+----------+-------------------------------------------------------------------+
| teste01.table_01 | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| teste01.table_01 | optimize | status | OK |
+------------------+----------+----------+-------------------------------------------------------------------+
mysql> ANALYZE TABLE table_01;
+------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------+---------+----------+----------+
| teste01.table_01 | analyze | status | OK |
+------------------+---------+----------+----------+
mysql> show table status like 'table_01'G
*************************** 1. row ***************************
Name: table_01
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 16384
Data_free: 0
Auto_increment: 430046
Create_time: 2017-05-02 21:40:16
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
MySQL - Optimize Table (InnoDB)
• To reclaim space to the tablespace run "OPTIMIZE TABLE"
• Reorganization process similar to MyISAMm "ANALYZE
TABLE" rarely needed, as InnoDB updates stats
55. MySQL - Instance Files
mysql> show variables like 'innodb_buffer_pool_dump_at_shutdown';
+-------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------+-------+
| innodb_buffer_pool_dump_at_shutdown | ON |
+-------------------------------------+-------+
1 row in set (0,05 sec)
mysql> show variables like 'innodb_buffer_pool_load_at_startup';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| innodb_buffer_pool_load_at_startup | ON |
+------------------------------------+-------+
1 row in set (0,04 sec)
mysql> show variables like 'innodb_buffer_pool_filename';
+-----------------------------+----------------+
| Variable_name | Value |
+-----------------------------+----------------+
| innodb_buffer_pool_filename | ib_buffer_pool |
+-----------------------------+----------------+
1 row in set (0,05 sec)
Every time you restart your MySQL server, it starts with a clean/empty buffer pool and usually it take some time to warm-up the buffer. To
speed up this process, we can configure 2 variables that will dump and reload the pages reference stored in the buffer, this is a new
functionality added on MySQL 5.6
56. MySQL - Instance/Database Files
mysql> show variables like 'innodb%';
+------------------------------------------+------------------------+
| Variable_name | Value |
+------------------------------------------+------------------------+
| innodb_data_home_dir | |
| innodb_data_file_path | ibdata1:12M:autoextend |
| innodb_file_per_table | ON |
| innodb_log_group_home_dir | ./ |
| innodb_log_files_in_group | 2 |
| innodb_log_file_size | 50331648 |
| innodb_temp_data_file_path | ibtmp1:12M:autoextend |
| innodb_flush_log_at_trx_commit | 1 |
| innodb_buffer_pool_size | 134217728 |
| etc. | |
+------------------------------------------+------------------------+
• innodb_file_per_table
This option causes the server to create one tablespace file per table. When innodb_file_per_table is enabled (the default), InnoDB stores the data and
indexes for each newly created table in a separate .ibd file instead of the system tablespace. The storage for these tables is reclaimed when the tables are
dropped or truncated.
• innodb_log_group_home_dir: Specifies the location of the InnoDB log files eg: innodb_log_group_home=/var/lib/mysql
• innodb_log_files_in_group: The number of log files in the log group. InnoDB writes to the files in a circular fashion. The default (and recommended) value is 2.
• innodb_log_file_size: The size in bytes of each log file in a log group.
• innodb_temp_data_file_path: In 5.7, InnoDB added a separate tablespace for all non-compressed InnoDB temporary tables. This new tablespace is named
ibtmp1 and is located in the datadir by default.
• innodb_flush_log_at_trx_commit: sets the behaviour of syncing on commit. The default value is 1. There are several possible values:
0 - if MySQL crashes, last second of txns lost;
1 - flush to disk each update/commit;
2 - flush to OS (if OS crashes, 1 sec txns are lost)
• innodb_buffer_pool_size
The size in bytes of the memory buffer to cache data and indexes of InnoDB‘s tables. This aims to reduce disk access to provide better performance.
• innodb_data_home_dir (datadir=/var/lib/mysql)
The directory (relative to :term:` datadir`) where the database server
stores the files in a shared tablespace setup. This option does not affect the
location of innodb_file_per_table.
• innodb_data_file_path
Specifies the names, sizes and location of shared tablespace files eg:
innodb_data_file_path=ibdata1:50M;ibdata2:50M:autoextend
57. MySQL - Instance/Database Files
Ibdata: Default prefix for tablespace files, e.g. ibdata1 is a 10MB autoextensible file that MySQL creates for the
shared tablespace by default.
.frm: For each table, the server will create a file with the .frm extension containing the table definition (for all
storage engines).
.ibd: On a multiple tablespace setup (innodb_file_per_table enabled), MySQL will store each newly created table
on a file with a .ibd extension.
.MYD: Each MyISAM table has .MYD (MYData) file which contains the data on it.
.MYI: Each MyISAM table has .MYI (MYIndex) file which contains the table’s indexes.
.TRG: File containing the Triggers associated to a table, e.g. :file:`mytable.TRG. With the .TRN file, they represent
all the Trigger definitions.
.TRN: File containing the Triggers’ Names associated to a table, e.g. :file:`mytable.TRN. With the .TRG file, they
represent all the Trigger definitions.
.ARM: Each table with the Archive Storage Engine has .ARM file which contains the metadata of it.
.ARZ: Each table with the Archive Storage Engine has .ARZ file which contains the data of it.
.CSM: Each table with the CSV Storage Engine has .CSM file which contains the metadata of it.
.CSV: Each table with the CSV Storage engine has .CSV file which contains the data of it (which is a standard
Comma Separated Value file).
.opt: MySQL stores options of a database (like charset) in a file with a .opt extension in the database directory.
.par: Each partitioned table has .par file which contains metadata about the partitions.
61. MySQL - InnoDB vs. MyISAM
Diferente da engine InnoDB que armazena em buffer de memória, tanto blocos de dados de tabelas, quanto blocos de dados de índices, a
engine MyISAM armazena em buffer de memória apenas dados de blocos de índices. Com a evolução da engine InnoDB ao longo do tempo, a
mesma vem oferecendo muitos recursos das quais a engine MyISAM não é capaz de oferecer. Em relação aos níveis de locks, a engine
MyISAM possui tratamento de lock apenas no nível de tabela.
• SELECTs travam UPDATEs, DELETEs mas não INSERTs
• INSERTs, UPDATEs, DELETEs travam todas as instruções SQL
A engine InnoDB oferece tratamento de locks no nível de linhas de tabelas, além de prover recursos como Foreign Keys que ajudam o
otimizador do banco de dados a encontrar melhores planos de execução para os SQLs que são executados. A engine InnoDB é compatível com
as propriedades ACID oferecendo recursos transacionais, além de ser mais seguro que a engine MyISAM no que se refere a corrupção de
dados em caso de falhas. Em relação ao parâmetros de configuração, somente a engine InnoDB oferece parâmetros de ajustes finos.
62. MySQL - Convert MyISAM to InnoDB
mysql> SELECT CONCAT('ALTER TABLE `',table_schema,'`.`',table_name,'` ENGINE=InnoDB;')
-> FROM information_schema.tables
-> WHERE table_schema='bd01'
-> AND ENGINE='MyISAM';
+--------------------------------------------------------------------------+
| CONCAT('ALTER TABLE `',table_schema,'`.`',table_name,'` ENGINE=InnoDB;') |
+--------------------------------------------------------------------------+
| ALTER TABLE `bd01`.`cerber_acl` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`cerber_blocks` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`cerber_lab` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`cerber_log` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_commentmeta` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_termmeta` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_terms` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_usermeta` ENGINE=InnoDB; |
| ALTER TABLE `bd01`.`wp_jc_users` ENGINE=InnoDB; |
+--------------------------------------------------------------------------+
9 rows in set (0.04 sec)
mysql> ALTER TABLE `bd01`.`cerber_log` ENGINE=InnoDB;
ALTER TABLE `wordpress`.`wp_jc_posts` ENGINE=InnoDB;
Query OK, 219 rows affected (0.10 sec)
Records: 219 Duplicates: 0 Warnings: 0
ALTER TABLE `bd01`.`wp_jc_terms` ENGINE=InnoDB;
Query OK, 642 rows affected (0.26 sec)
Records: 642 Duplicates: 0 Warnings: 0
mysql> show full processlist;
+----+------+---------------------+------+---------+------+-------------------+-------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+-------------------+-------------------------------+
| 5 | root | localhost | bd01 | Sleep | 804 | | NULL |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
| 7 | root | 192.168.56.51:49970 | bd01 | Sleep | 103 | | NULL |
| 8 | root | 192.168.56.51:49971 | NULL | Sleep | 9838 | | NULL |
| 14 | root | localhost | bd01 | Query | 15 | copy to tmp table | alter table wp engine innodb |
+----+------+---------------------+------+---------+------+-------------------+-------------------------------+
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.
69. mysql> CREATE TABLE transaction_test(
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> val VARCHAR(20) NOT NULL,
-> created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
-> PRIMARY KEY(id)
-> ) ENGINE=InnoDB DEFAULT CHARSET latin1;
Query OK, 0 rows affected (0.29 sec)
mysql> INSERT INTO transaction_test(val) VALUES ('a'),('b'),('c');
Query OK, 3 rows affected (0.08 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM transaction_test;
+----+-----+---------------------+
| id | val | created |
+----+-----+---------------------+
| 1 | a | 2017-05-02 10:09:33 |
| 2 | b | 2017-05-02 10:09:33 |
| 3 | c | 2017-05-02 10:09:33 |
+----+-----+---------------------+
3 rows in set (0.00 sec)
SESSION 1
ISOLATION LEVEL (REPEATABLE READ)
70. mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO transaction_test(val) VALUES ('x'),('y'),('z');
COMMIT;
SESSION 2
mysql> SELECT * FROM transaction_test;
+----+-----+---------------------+
| id | val | created |
+----+-----+---------------------+
| 1 | a | 2017-05-02 10:09:33 |
| 2 | b | 2017-05-02 10:09:33 |
| 3 | c | 2017-05-02 10:09:33 |
+----+-----+---------------------+
3 rows in set (0.00 sec)
SESSION 1
ISOLATION LEVEL (REPEATABLE READ)
71. mysql> SET SESSION tx_isolation='READ-COMMITTED';
TRUNCATE TABLE transaction_test;
INSERT INTO transaction_test(val) VALUES ('a'),('b'),('c');
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
SESSION 1
ISOLATION LEVEL (READ COMMITED)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO transaction_test(val) VALUES ('x');
Query OK, 3 rows affected (0.00 sec)
mysql> COMMIT;
SESSION 2
mysql> SELECT * FROM transaction_test;
+----+--------+---------------------+
| id | val | created |
+----+--------+---------------------+
| 1 | a | 2017-05-02 10:18:08 |
| 2 | b | 2017-05-02 10:18:08 |
| 3 | c | 2017-05-02 10:18:08 |
| 4 | x | 2017-05-02 10:19:00 |
+----+--------+---------------------+
SESSION 1
72. MySQL DEFAULT ISOLATION LEVEL
mysql> show variables like '%isolation%';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| tx_isolation | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0,06 sec)
76. mysql> SHOW ENGINE INNODB STATUS
------------
TRANSACTIONS
------------
Trx id counter 11794
Purge done for trx's n:o < 11789 undo n:o < 0 state: running but idle
History list length 2
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 18446744072350939916, not started
0 lock struct(s), heap size 1080, 0 row lock(s)
---TRANSACTION 18446744072350939356, not started
0 lock struct(s), heap size 1080, 0 row lock(s)
---TRANSACTION 11792, ACTIVE 2846 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1080, 1 row lock(s)
MySQL thread id 5, OS thread handle 2763115376, query id 37 localhost root updating
update t2 set id=20
------- TRX HAS BEEN WAITING 2846 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 48 page no 3 n bits 72 index GEN_CLUST_INDEX of table `bd01`.`t2` trx id 11792 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
0: len 6; hex 000000018300; asc ;;
1: len 6; hex 000000002e0e; asc . ;;
2: len 7; hex 2c0000013b0c4e; asc , ; N;;
3: len 4; hex 8000000a; asc ;;
------------------
---TRANSACTION 11790, ACTIVE 3567 sec
2 lock struct(s), heap size 1080, 2 row lock(s), undo log entries 1
MySQL thread id 4, OS thread handle 2763316080, query id 28 localhost root
--------
MySQL - Locks / INNODB STATUS
77. mysql> SELECT p.id,
-> p.HOST,
-> p.DB,
-> p.time,
-> t.trx_state,
-> t.trx_query,
-> t.trx_started
-> FROM INFORMATION_SCHEMA.INNODB_TRX t
-> JOIN INFORMATION_SCHEMA.PROCESSLIST p
-> ON (p.ID = t.TRX_MYSQL_THREAD_ID)
-> LEFT JOIN INFORMATION_SCHEMA.INNODB_LOCK_WAITS ot
-> ON (ot.BLOCKING_TRX_ID = t.TRX_id);
+----+-----------+------+------+-----------+---------------------+---------------------+
| id | HOST | DB | time | trx_state | trx_query | trx_started |
+----+-----------+------+------+-----------+---------------------+---------------------+
| 4 | localhost | bd01 | 2858 | RUNNING | NULL | 2017-04-21 11:25:05 |
| 5 | localhost | bd01 | 2137 | LOCK WAIT | update t2 set id=20 | 2017-04-21 11:37:06 |
+----+-----------+------+------+-----------+---------------------+---------------------+
mysql> SELECT CONCAT('KILL ',id,';'),CONCAT('KILL QUERY ',id,';')
-> FROM information_schema.processlist WHERE db = 'bd01';
+------------------------+------------------------------+
| CONCAT('KILL ',id,';') | CONCAT('KILL QUERY ',id,';') |
+------------------------+------------------------------+
| KILL 4; | KILL QUERY 4; |
| KILL 5; | KILL QUERY 5; |
+------------------------+------------------------------+
2 rows in set (0,02 sec)
mysql> KILL 4;
Query OK, 0 rows affected (0,00 sec)
MySQL - Locks / Querying / Killing
SESSION 2
mysql> update t2 set id=20;
Query OK, 1 row affected (58 min 0,83 sec)
Rows matched: 1 Changed: 1 Warnings: 0
SESSION 1
mysql> select 1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 10
Current database: bd01
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0,06 sec)
78. mysql> START TRANSACTION;
Query OK, 0 rows affected (0,01 sec)
mysql> update t2 set id=30;
Query OK, 1 row affected (0,00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> alter table t2 modify column id bigint;
hang
mysql> SHOW FULL PROCESSLIST;
+----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+
| 5 | root | localhost | bd01 | Query | 580 | Waiting for table metadata lock | alter table t2 modify column id bigint |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
| 7 | root | 192.168.56.51:49970 | NULL | Sleep | 400 | | NULL |
| 8 | root | 192.168.56.51:49971 | NULL | Sleep | 6733 | | NULL |
| 10 | root | localhost | bd01 | Sleep | 682 | | NULL |
+----+------+---------------------+------+---------+------+---------------------------------+----------------------------------------+
mysql> SELECT *
-> FROM information_schema.processlist
-> WHERE state IN ('Waiting for table flush',
-> 'Locked',
-> 'System lock',
-> 'Table lock' )
-> OR state REGEXP 'Waiting for .* lock'
-> ORDER BY TIME DESC;
+----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+
| 5 | root | localhost | bd01 | Query | 606 | Waiting for table metadata lock | alter table t2 modify column id bigint |
+----+------+-----------+------+---------+------+---------------------------------+----------------------------------------+
MySQL - Metadata Locks
SESSION 1
SESSION 2
79. mysql> LOCK TABLES customer READ;
Query OK, 0 rows affected (0,01 sec)
mysql> delete from customer where name=1;
hang
mysql> SHOW FULL PROCESSLIST;
+----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+
| 5 | root | localhost | bd01 | Query | 12 | Waiting for table metadata lock | delete from customer where name=1 |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
| 7 | root | 192.168.56.51:49970 | NULL | Sleep | 899 | | NULL |
| 8 | root | 192.168.56.51:49971 | NULL | Sleep | 7232 | | NULL |
| 10 | root | localhost | bd01 | Sleep | 34 | | NULL |
+----+------+---------------------+------+---------+------+---------------------------------+-----------------------------------+
mysql> SELECT *
-> FROM information_schema.processlist
-> WHERE state IN ('Waiting for table flush',
-> 'Locked',
-> 'System lock',
-> 'Table lock' )
-> OR state REGEXP 'Waiting for .* lock'
-> ORDER BY TIME DESC;
+----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+
| 5 | root | localhost | bd01 | Query | 560 | Waiting for table metadata lock | delete from customer where name=1 |
+----+------+-----------+------+---------+------+---------------------------------+-----------------------------------+
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0,01 sec)
MySQL - Metadata Locks
SESSION 1
SESSION 2
SESSION 1
mysql> delete from customer where name=1;
Query OK, 1 row affected (11 min 39,52 sec)
SESSION 2
80. mysql> CREATE TABLE emp (id int) ENGINE=MyISAM;
mysql> select count(*) from emp;
+----------+
| count(*) |
+----------+
| 2097153 |
+----------+
1 row in set (0,01 sec)
mysql> update emp set id=20000;
updating
mysql> select count(*) from emp;
hang
mysql> SHOW FULL PROCESSLIST;
+----+------+---------------------+------+---------+------+------------------------------+--------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+---------+------+------------------------------+--------------------------+
| 5 | root | localhost | bd01 | Query | 4 | Waiting for table level lock | select count(*) from emp |
| 6 | root | localhost | NULL | Query | 0 | starting | show full processlist |
| 7 | root | 192.168.56.51:49970 | NULL | Sleep | 2705 | | NULL |
| 8 | root | 192.168.56.51:49971 | NULL | Sleep | 9038 | | NULL |
| 14 | root | localhost | bd01 | Query | 14 | updating | update emp set id=20000 |
+----+------+---------------------+------+---------+------+------------------------------+--------------------------+
mysql> select count(*) from emp;
+----------+
| count(*) |
+----------+
| 2097153 |
+----------+
1 row in set (2 min 26,55 sec)
MySQL - Table Locks MyISAM
SESSION1
SESSION 2
SESSION 2
mysql> SHOW CREATE TABLE bd01.emp;
+-------+---------------------------------------+
| Table | Create Table |
+-------+----------------------------------------
| emp | CREATE TABLE `emp` ( |
| `id` int DEFAULT NULL |
| ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-------+---------------------------------------+
82. MySQL - Enabling Binary Logs
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | OFF |
+---------------+-------+
1 row in set (0,04 sec)
[root@linux1~]# cat /etc/my.cnf
server_id=1
log_bin=/var/lib/mysql/mysql-bin.log
root@linux1~]# service mysqld restart
Parando o mysqld: [ OK ]
Iniciando o mysqld: [ OK ]
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0,04 sec)
mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 154 |
+------------------+-----------+
1 row in set (0,00 sec)
mysql> SELECT @@log_bin;
+-----------+
| @@log_bin |
+-----------+
| 1 |
+-----------+
1 row in set (0,01 sec)
[root@linux1~]# ls -lh mysql-bin.*
-rw-r----- 1 mysql mysql 154 Abr 12 15:44 mysql-bin.000001
-rw-r----- 1 mysql mysql 32 Abr 12 15:44 mysql-bin.index
+---------------------------------+--------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------+
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| binlog_format | ROW |
| max_binlog_size | 1073741824 |
| expire_logs_days | 0 |
| sync_binlog | 1 |
+---------------------------------+--------------------------------+
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
83. MySQL - Viewing Contents of Binary Logs
[root@linux1~]# mysqlbinlog mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#170419 20:21:30 server id 1 end_log_pos 123 CRC32 0x14c02170 Start: binlog v 4,
server v 5.7.18-log created 170419 20:21:30 at startup
ROLLBACK/*!*/;
BINLOG '
+vD3WA8BAAAAdwAAAHsAAAAAAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAD68PdYEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AXAhwBQ=
'/*!*/;
# at 123
#170419 20:21:30 server id 1 end_log_pos 154 CRC32 0xb9a3dfb0 Previous-GTIDs
# [empty]
# at 154
#170419 20:36:16 server id 1 end_log_pos 219 CRC32 0x8e618eb0 Anonymous_GTID last_committed=0 sequence_number=1
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#170419 20:36:16 server id 1 end_log_pos 291 CRC32 0x1930ff1a Query thread_id=7 exec_time=0 error_code=0
SET TIMESTAMP=1492644976/*!*/;
SET @@session.pseudo_thread_id=7/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 291
#170419 20:36:16 server id 1 end_log_pos 338 CRC32 0x1381aefd Table_map: `bd01`.`t1` mapped to number 108
# at 338
#170419 20:36:16 server id 1 end_log_pos 382 CRC32 0x8c6cf04e Write_rows: table id 108 flags: STMT_END_F
89. MySQL - Generate CSV Files
mysql> select * from customer;
+------+------+
| id | name |
+------+------+
| 1 | Joe |
| 2 | Jack |
| 3 | Zack |
+------+------+
3 rows in set (0,00 sec)
SELECT * INTO OUTFILE '/tmp/customer.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' FROM customer;
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0,01 sec)
mysql> SELECT * INTO OUTFILE '/var/lib/mysql-files/customer.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' FROM customer;
Query OK, 3 rows affected (0,01 sec)
mysql> system cat /var/lib/mysql-files/customer.csv
"1","Joe"
"2","Jack"
"3","Zack"
90. MySQL - Generate CSV Files (Headers)
SET @table_name = 'customer';
SET @table_schema = 'bd01';
SET SESSION group_concat_max_len = 1000000;
SET @col_names = (
SELECT GROUP_CONCAT(QUOTE(`column_name`)) AS columns
FROM information_schema.columns
WHERE table_schema = @table_schema
AND table_name = @table_name);
SET @cols = CONCAT('(SELECT ', @col_names, ')');
SET @query = CONCAT('(SELECT * FROM ', @table_schema, '.', @table_name,
' INTO OUTFILE '/var/lib/mysql-files/customer.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"')');
SET @sql = CONCAT(@cols, ' UNION ALL ', @query);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
system cat /var/lib/mysql-files/customer.csv;
mysql> SET @table_name = 'customer';
Query OK, 0 rows affected (0.00 sec)
mysql> SET @table_schema = 'bd01';
Query OK, 0 rows affected (0.00 sec)
mysql> SET SESSION group_concat_max_len = 1000000;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @col_names = (
-> SELECT GROUP_CONCAT(QUOTE(`column_name`)) AS columns
-> FROM information_schema.columns
-> WHERE table_schema = @table_schema
-> AND table_name = @table_name);
Query OK, 0 rows affected (0.00 sec)
mysql> SET @cols = CONCAT('(SELECT ', @col_names, ')');
Query OK, 0 rows affected (0.00 sec)
mysql> SET @query = CONCAT('(SELECT * FROM ', @table_schema, '.', @table_name,
-> ' INTO OUTFILE '/var/lib/mysql-files/customer.csv'
'> FIELDS TERMINATED BY ',' ENCLOSED BY '"')');
Query OK, 0 rows affected (0.00 sec)
mysql> SET @sql = CONCAT(@cols, ' UNION ALL ', @query);
Query OK, 0 rows affected (0.00 sec)
mysql> PREPARE stmt FROM @sql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE stmt;
Query OK, 4 rows affected (0.00 sec)
mysql> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)
mysql> system cat /var/lib/mysql-files/customer.csv;
"id","name"
"1","Joe"
"2","Jack"
"3","Zack"
91. MySQL - LOAD CSV Files
mysql> TRUNCATE TABLE customer;
Query OK, 0 rows affected (0,02 sec)
mysql> select * from customer;
Empty set (0,00 sec)
mysql> LOAD DATA LOCAL INFILE '/var/lib/mysql-files/customer.csv' INTO TABLE customer FIELDS TERMINATED BY ','
-> ENCLOSED BY '"' LINES TERMINATED BY 'n'
-> (id,name);
Query OK, 3 rows affected (0,02 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from customer;
+------+------+
| id | name |
+------+------+
| 1 | Joe |
| 2 | Jack |
| 3 | Zack |
+------+------+
3 rows in set (0,00 sec)
mysql> TRUNCATE TABLE customer;
Query OK, 0 rows affected (0,02 sec)
[root@linux1 ~]# mysqlimport --columns='id,name' --fields-terminated-by=',' --fields-enclosed-by '"'
--local -uroot -padmin
bd01
/var/lib/mysql-files/customer.csv
mysql> select * from customer;
+------+------+
| id | name |
+------+------+
| 1 | Joe |
| 2 | Jack |
| 3 | Zack |
+------+------+
3 rows in set (0,00 sec)
No caso do mysqlimport, o nome
do arquivo tem que ser igual ao
nome da tabela de destino