mysql疑難雜症記錄

mysqldump 無法連接數據庫

#配置文件中添加mysqldump命令的mysql.sock路徑
[mysqldump]
socket=/data/mysql/mysql.sock

查看mysql連接數

#查看線程數,連接數
show status like 'Threads%';
show STATUS LIKE '%Connection%';  
#最大連接數
show VARIABLES LIKE '%max_connections%';  
#當前線程狀態
show PROCESSLIST;  
show FULL PROCESSLIST;  

查看my.cnf目錄

查看mysql默認讀取my.cnf的目錄
mysql --help|grep 'my.cnf'

鎖解表

FLUSH TABLES WITH READ LOCK
unlock tables

查看引擎

show engines  

查看mysql數據庫大小、表大小、數據大小、索引大小

select sum(DATA_LENGTH) from information_schema.TABLES where information_schema.TABLES.TABLE_SCHEMA='database' and information_schema.TABLES.TABLE_NAME='table'

表結構

select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE,COLUMN_COMMENT from information_schema.columns where TABLE_SCHEMA='table'

查看數據庫、表大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB,concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB from tables where table_schema='庫名' and table_name = '表名';

查看實例下所有庫大小

select table_schema, sum(data_length+index_length)/1024/1024 as total_mb, sum(data_length)/1024/1024 as data_mb, sum(index_length)/1024/1024 as index_mb,count(*) as tables, curdate() as today from information_schema.tables group by table_schema order by 2 desc;  

查看某個庫中的所有表佔用並按表大小倒敘

select table_name, (data_length/1024/1024) as data_mb , (index_length/1024/1024)   as index_mb,((data_length+index_length)/1024/1024) as all_mb, table_rows from information_schema.tables where table_schema = 'bmdb' GROUP BY data_mb desc;  

刪數據後磁盤未釋放

#MySQL刪除數據幾種情況以及是否釋放磁盤空間:
#縮表
optimize table  表名 
#立刻釋放磁盤空間 ,不管是 Innodb和MyISAM ;
drop table table_name 
#立刻釋放磁盤空間 ,不管是 Innodb和MyISAM 。truncate table其實有點類似於drop table 然後creat,只不過這個create table 的過程做了優化,比如表結構文件之前已經有了等等。所以速度上應該是接近drop table的速度;
truncate table table_name 
#刪除表的全部數據,對於MyISAM 會立刻釋放磁盤空間 (應該是做了特別處理,也比較合理),InnoDB 不會釋放磁盤空間;
#對於delete from table_name where xxx帶條件的刪除, 不管是innodb還是MyISAM都不會釋放磁盤空間;
#delete操作以後使用optimize table table_name 會立刻釋放磁盤空間。不管是innodb還是myisam 。所以要想達到釋放磁盤空間的目的,delete以後執行optimize table 操作。
#delete from表以後雖然未釋放磁盤空間,但是下次插入數據的時候,仍然可以使用這部分空間。 
delete from table_name
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章