1. Introduction to mysql show command
The show command in the mysql database is a very useful command. The SHOW command is used to display the information in the MySQL database. It can be used to display information about various objects such as databases, tables, columns, indexes, and users. We commonly use show databases, show tables, show full processlist, etc. In fact, there are many more that can be used, including more than 30 subcommands, and more database information that can be viewed with optional parameters. This blog post introduces the mysql show command by way of example. The experimental environment of the blog post is as follows:
- Operating system: centos7.6
- Database version: mysql5.7.26
2. Examples of the most commonly used show commands
1. View the database list
What we use the most is show databases. You can view the database list. If you are a root user, you can see all the databases. If you are an ordinary user, you can only see the library and information_schema library that you have permission.
mysql> show databases;
2. View the database connection process
show processlist can view the database connection process, full is optional, the difference is that show shows the first 100 threads, and show full shows all of them. Both root users and ordinary users can execute this command. The difference is that root users can see the connection information of all users, and ordinary users can only see the connection information of this user. The connection information includes process ID, user name, connection source address and port number, connected database, command status, connection time, execution status, and details of the executed command. show processlist is often used to query the process ID when there is a lock, and then manually kill the ID to unlock it.
mysql> show processlist;
mysql> show full processlist;
3. View table list
show tables is used to view the list of tables under the library, and it can be executed only after entering a certain library. Of course, we can also use show tables from db_name to view the data tables of a database, provided that we have permission to view them.
mysql> use testdb;
mysql> show tables;
mysql> show tables from mysql;
4. Check the table structure
Use SHOW CREATE TABLE tbl_name to view the table structure of a table. It is often used to confirm the table information and is often used before modifying the table structure.
mysql> show create table tb_0001;
5. View variable information
Display the value of the configuration variable of the MySQL server. This command is often used when troubleshooting to view the parameter configuration of the mysql instance. It is often used in conjunction with like to filter specified parameters. You can use show global variables or show session variables to view global or session parameters. The default is to view session parameters.
mysql> show variables like ‘%time%’;
6. View the index information of the data table
If we need to check which indexes have been created for a certain table, we can use the show index from table_name method to view. By default, there are primary key indexes. If other indexes are created, we can see them in this way.
mysql> show index from tb_0001;
7. View the permissions of a user
Use show grants for user@host to view the user's authorization, if the user's host is any source, you can omit it; you can also directly enter show grants to view the current user's authorization.
mysql> show grants for bak@‘192.168.0.%’;
mysql> show grants;
8. Get show command help
mysql> help show;
3. Example of using the show command that requires privileged execution
The execution of the following show commands requires privileges and can be executed by non-ordinary users.
1. View the master-slave status
If the master-slave mode of mysql is set up, we need to check the master-slave status to use this command. If the role of master or slave is configured, you can see the master-slave status information, otherwise it is empty.
mysql> show master status;
mysql> show slave status;
2. View slave node host information
mysql> show slave hosts;
±----------±-----±-----±----------±-------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
±----------±-----±-----±----------±-------------------------------------+
| 2 | | 3306 | 3306 | 4ef97f7e-cfe9-11ea-8214-3448edf3158c |
±----------±-----±-----±----------±-------------------------------------+
1 row in set (0.00 sec)
3. View the binary log location
mysql> show master logs;
mysql> show binary logs;
4. View binlog log events
mysql> SHOW BINLOG EVENTS;
4. Examples of using other show commands
1. View available character sets
mysql> SHOW CHARACTER SET;
mysql> SHOW CHARACTER SET like ‘%gbk%’;
2. Check and display the available character proofreading rules
mysql> SHOW COLLATION;
3. View all columns of a table in a database
mysql> SHOW full COLUMNS FROM tb_0001 from testdb;
4. View database creation information
Use show create to view information about creating databases, events, functions, stored procedures, triggers, views, etc. The corresponding names are required.
mysql> show create database testdb;
SHOW CREATE DATABASE db_name
SHOW CREATE EVENT event_name
SHOW CREATE FUNCTION func_name
SHOW CREATE PROCEDURE proc_name
SHOW CREATE TRIGGER trigger_name
SHOW CREATE VIEW view_name
5. View recent events
mysql> show events;
Empty set (0.00 sec)
6. View recent alarms
mysql> show warnings;
Empty set (0.00 sec)
7. View recent errors
mysql> show errors;
Empty set (0.00 sec)
8. View engine status
View the status of the innodb engine,
mysql> SHOW ENGINE innodb status;
9. View installed plugins
mysql>SHOW PLUGINS
10. View the status of the database table
View the status of the database table, which is very useful when backing up and restoring a single table. By viewing the last update time of the table, we can select the backup file of the corresponding date to restore. When we need to perform business operations on the table, we can also Through analysis to help determine when to perform database operations is more appropriate.
mysql> show table status from testdb;
11. View all open tables
mysql> show open tables;
12. View database triggers
We can view all triggers through show triggers, or show triggers from db_name to view the triggers of the specified database.
mysql> show triggers from testdb;
13. View function or stored procedure status
mysql> SHOW FUNCTION STATUS like ‘%version_patch%’\G
mysql> SHOW PROCEDURE STATUS like ‘table_exists’\G
14. Use profile statement to analyze sql performance
We can use the profile statement to analyze the performance of the sql execution that needs to be executed, and we can see the resource consumption of each stage of sql execution. The SHOW PROFILE statement supports selecting ALL, CPU, BLOCK IO, CONTEXT SWITCH, and PAGE FAULTS to view specific details information. However, this function is about to be eliminated. In the new version, the Performance Schema library is used to analyze resource consumption and usage.
mysql> set profiling=1;
mysql> select count(*) from testdb.tb_0001;
mysql> show profiles;
mysql> show profile for query 1;
mysql> show profile cpu for query 1;
5. Description of common parameters of show status
Use the SHOW STATUS statement to obtain some status information of the MySQL server, which is mainly the performance parameters of the MySQL database. The syntax of the SHOW STATUS statement is as follows:
SHOW [SESSION | GLOBAL] STATUS LIKE ‘status_name’;
Among them, SESSION means to obtain the performance parameters of the current session level, GLOBAL means to obtain the performance parameters of the global level, and SESSION and GLOBAL can be omitted, if omitted, the default is SESSION. status_name indicates the parameter value of the query. Proficiency in the use of these parameters can better understand the execution frequency of SQL statements. The common parameters are described as follows:
parameter value | Parameter Description |
---|---|
Connections | Number of times to connect to the MySQL server |
Uptime MySQL | The continuous working time after the server starts |
Slow_queries | The number of slow queries |
Com insert | The number of times to insert data, when inserting multiple pieces of data in batches, only add 1 |
With delete | The number of times to delete data, adding 1 each time |
Com update | The number of times to modify the data, adding 1 each time |
Com select | The number of times to query data, one query operation accumulates 1 |
Innodb rows read | The number of data rows returned when querying data |
Innodb rows inserted | The number of records returned when inserting data |
Innodb rows updated | The number of records returned when updating data |
Innodb rows deleted | The number of records returned when deleting data |