這些日常的Mysql命令你是否還記得?

2021-10-06 19:46:56 字數 2695 閱讀 4596

前言

記錄mysql常用命令操作。

基礎操作

mysql -u使用者名稱 -p使用者密碼
alter table order_info_tbl add column create_time timestamp

default current_timestamp comment '建立時間';

alter table order_info_tbl add column update_time timestamp

default current_timestamp on update current_timestamp comment '更新時間'

修改密碼
update user set password=password("root1234") where user="root";
alter user 'root'@'%' identified with mysql_native_password by ''root;
表分割槽
alter table tache_stat_tbl_20190120 partition by hash(province) partitions 31
表、索引、執行計畫
select table_name, concat(truncate(data_length/1024/1024,2),' mb') as data_size,

concat(truncate(index_length/1024/1024,2),' mb') as index_size

from information_schema.tables

# where table_schema = 'yourdb'

group by table_name

order by data_length desc;

盡量避免廉價的建立索引,可以先根據資料區分度來判斷,是否有必要建立索引。

select count(distinct 將要建立索引的字段) / count(*)
using index 表示使用了覆蓋索引(covering index)

using where using where的作用提示了用where來過濾結果集。

using temporary 說明mysql需要使用臨時表來儲存結果集,常見於排序和分組查詢

using filesort mysql中無法利用索引完成的排序操作稱為「檔案排序」

常用維護操作

select concat('kill ', id, ';') from information_schema.processlist where command != 'sleep' and time > 2*60 order by time desc
grant all privileges on *.* to 'yourusername'@'%' identified by "yourpassword";
資料匯入匯出
mysqldump -uroot -proot --all-databases >/all.sql
mysqldump -uroot -proot --no-data --databases db1 > /table_with_no_data.sql
mysqldump --host=h1 -uroot -proot --databases db1 |mysql --host=h2 -uroot -proot db2
預設情況下,連線協議為socket,如遇到下述錯誤,可以嘗試更換協議。

mysqldump: got error: 2002: "can't connect to local mysql server through socket

'/var/lib/mysql/mysql.sock'

樣例語句:

mysqldump -h127.0.0.1  -uroot -proot --protocol=tcp --database db1
mysqldump  -uroot -p --host=localhost --all-databases --routines
mysql -uroot -e 'select * from cb_mon.t_book limit 10' > mytest.txt
drop procedure if exists test_insert ;

delimiter ;;

create procedure test_insert ()

begin

declare i int default 1;# can not be 0

while i<1000

doinsert into ss_book values (i, concat("00000",i) , concat('book',i), 1, concat('book_description',i));

set i=i+1;

end while ;

commit;

end;;

call test_insert();

MYSQL日常使用命令整理

一.檢視mysql表結構的方法有三種 1 desc tablename 2 show create table tablename 3 use information schema select from columns where table name tablename 如果要檢視怎麼建立資料表...

mysql的日常操作 mysql日常操作命令

1.mysql連線 埠要用大寫p,與密碼p加以區分 mysql h127.0.0.1 p3306 uroot p 2.檢視mysql的資料庫列表 show databases 3.使用某個庫 use 資料庫名 4.檢視表列表 show tables 5.檢視資料庫的建立sql show create...

基礎的 Linux 網路命令,你值得擁有

有抱負的 linux 系統管理員和 linux 狂熱者必須知道的 最重要的 而且基礎的 linux 網路命令合集。在 it s foss 我們並非每天都談論 linux 的 命令列方面 基本上,我更專注於 linux 的桌面端。但你們讀者中的一些人在內部調查 僅面向 it s foss newsle...