首頁 > 資料庫 > mysql教程 > Mysql怎麼查詢資料庫連線狀態及連線訊息

Mysql怎麼查詢資料庫連線狀態及連線訊息

王林
發布: 2023-06-02 13:40:13
轉載
2023 人瀏覽過

查看顯示所有資料庫

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| INVOICE            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
 
mysql>
登入後複製

查看目前使用的資料庫

mysql> select database();
+------------+
| database() |
+------------+
| INVOICE    |
+------------+
1 row in set (0.00 sec)
 
mysql>
登入後複製

查看資料庫使用連接埠

mysql> show variables  like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)
登入後複製

查看目前資料庫大小

例如,我要查看INVOICE資料庫的大小,那麼可以透過下面SQL查看

mysql> use  information_schema
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select concat(round(sum(data_length)/(1024*1024),2) + round(sum(index_length)/(1024*1024),2),'MB') as 'DB Size'
    -> from tables 
    -> where table_schema='INVOICE';
+-----------+
| DB Size   |
+-----------+
| 7929.58MB |
+-----------+
1 row in set, 1 warning (0.00 sec)
登入後複製

查看資料所佔的空間大小

mysql> use information_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select concat(round(sum(data_length)/(1024*1024),2),'MB') as 'DB Size'
    -> from tables
    -> where table_schema='INVOICE';
+-----------+
| DB Size   |
+-----------+
| 6430.26MB |
+-----------+
1 row in set, 1 warning (0.00 sec)
 
mysql>
登入後複製

查看索引所佔的空間大小

mysql> select concat(round(sum(index_length)/(1024*1024),2),'MB') as 'DB Size' 
    -> from tables 
    -> where table_schema='INVOICE';
+-----------+
| DB Size   |
+-----------+
| 1499.32MB |
+-----------+
1 row in set, 1 warning (0.13 sec)
 
mysql>
登入後複製

查看資料庫編碼

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
登入後複製
  • character_set_client 為客戶端編碼方式;

  • character_set_connection 為建立連線所使用的編碼;

  • #character_set_database 為資料庫的編碼;

  • character_set_results 為結果集的編碼;

  • character_set_server 為資料庫伺服器的編碼;

character_set_server 為資料庫伺服器的編碼;

只要保證以上所採用的程式設計方式一樣,就不會出現亂碼問題。

mysql> show variables like 'collation%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | utf8_general_ci   |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
登入後複製

status也可以查看資料庫的編碼

mysql> status;
--------------
mysql  Ver 14.14 Distrib 5.6.20, for Linux (x86_64) using  EditLine wrapper
 
Connection id:          1
Current database:       INVOICE
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.6.20-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 5 hours 18 min 51 sec
 
Threads: 1  Questions: 10884  Slow queries: 0  Opens: 650  Flush tables: 1  Open tables: 268  Queries per second avg: 0.568
--------------
mysql>
登入後複製

查看資料庫的表格資訊

mysql> show tables;
+---------------------------------------+
| Tables_in_information_schema          |
+---------------------------------------+
| CHARACTER_SETS                        |
| COLLATIONS                            |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS                               |
| COLUMN_PRIVILEGES                     |
| ENGINES                               |
| EVENTS                                |
| FILES                                 |
| GLOBAL_STATUS                         |
| GLOBAL_VARIABLES                      |
| KEY_COLUMN_USAGE                      |
| OPTIMIZER_TRACE                       |
| PARAMETERS                            |
| PARTITIONS                            |
| PLUGINS                               |
| PROCESSLIST                           |
| PROFILING                             |
| REFERENTIAL_CONSTRAINTS               |
| ROUTINES                              |
| SCHEMATA                              |
| SCHEMA_PRIVILEGES                     |
| SESSION_STATUS                        |
| SESSION_VARIABLES                     |
| STATISTICS                            |
| TABLES                                |
| TABLESPACES                           |
| TABLE_CONSTRAINTS                     |
| TABLE_PRIVILEGES                      |
| TRIGGERS                              |
| USER_PRIVILEGES                       |
| VIEWS                                 |
| INNODB_LOCKS                          |
| INNODB_TRX                            |
| INNODB_SYS_DATAFILES                  |
| INNODB_LOCK_WAITS                     |
| INNODB_SYS_TABLESTATS                 |
| INNODB_CMP                            |
| INNODB_METRICS                        |
| INNODB_CMP_RESET                      |
| INNODB_CMP_PER_INDEX                  |
| INNODB_CMPMEM_RESET                   |
| INNODB_FT_DELETED                     |
| INNODB_BUFFER_PAGE_LRU                |
| INNODB_SYS_FOREIGN                    |
| INNODB_SYS_COLUMNS                    |
| INNODB_SYS_INDEXES                    |
| INNODB_FT_DEFAULT_STOPWORD            |
| INNODB_SYS_FIELDS                     |
| INNODB_CMP_PER_INDEX_RESET            |
| INNODB_BUFFER_PAGE                    |
| INNODB_CMPMEM                         |
| INNODB_FT_INDEX_TABLE                 |
| INNODB_FT_BEING_DELETED               |
| INNODB_SYS_TABLESPACES                |
| INNODB_FT_INDEX_CACHE                 |
| INNODB_SYS_FOREIGN_COLS               |
| INNODB_SYS_TABLES                     |
| INNODB_BUFFER_POOL_STATS              |
| INNODB_FT_CONFIG                      |
+---------------------------------------+
59 rows in set (0.00 sec)
登入後複製

或使用下面SQL語句查看某個資料庫的表格資訊。

select * from information_schema.tables where table_schema=‘databasename';
登入後複製

查看某種具體表的資訊

select * from information_schema.tables where table_name =‘table_name'
登入後複製

查看資料庫的所有使用者資訊

mysql>  select distinct concat('user: ''',user,'''@''',host,''';') as query from mysql.user;
+-------------------------------------+
| query                               |
+-------------------------------------+
| user: 'root'@'127.0.0.1';           |
| user: 'root'@'::1';                 |
| user: 'root'@'gettesx20.test.com'; |
| user: 'root'@'localhost';           |
+-------------------------------------+
4 rows in set (0.00 sec)
 
mysql>
登入後複製

查看某個特定使用者的權限

mysql> show grants for 'root'@'localhost';
+---------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+---------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*C7B1594FD74578DA3A92A61720AC67C6DBE6FC23' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+---------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
登入後複製

查看資料庫的最大連線數

mysql>  show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0.00 sec)
 
mysql>
登入後複製

查看資料庫目前連線數,並發數。

mysql> show status like 'Threads%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_cached    | 0     |
| Threads_connected | 1     |
| Threads_created   | 1     |
| Threads_running   | 1     |
+-------------------+-------+
4 rows in set (0.00 sec)
登入後複製

Threads_cached : 代表目前此時此刻執行緒快取中有多少空閒執行緒。

Threads_connected :代表目前已建立連線的數量,因為一個連線就需要一個線程,所以也可以看成目前被使用的線程數。

Threads_created :代表從最近一次服務啟動,已建立執行緒的數量。

###這句話可以重寫為: Threads_running表示目前有多少個執行緒處於啟動狀態,即非睡眠狀態。並不是代表正在使用的執行緒數,有時候連線已建立,但是連線處於sleep狀態,這裡相對應的執行緒也是sleep狀態。 ######查看資料檔案存放路徑###
mysql> show variables like '%datadir%';
+---------------+-------------------+
| Variable_name | Value             |
+---------------+-------------------+
| datadir       | /mysqldata/mysql/ |
+---------------+-------------------+
1 row in set (0.00 sec)
 
mysql>
登入後複製

以上是Mysql怎麼查詢資料庫連線狀態及連線訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板