Mysql常用命令

这里记录一些常用的mysql命令,主要用来备查。

1、数据库

查看有多少个数据库

show databases;

创建新的数据库

create database simr default charset utf8;

切换到你需要的数据库

use simr;

改密码,这里有三个方法

# 方法一
mysql -u root mysql
UPDATE user SET password=PASSWORD("new password") WHERE user='hunte';
FLUSH PRIVILEGES;

# 方法二
mysql -u root mysql
SET PASSWORD FOR hunte=PASSWORD('new password');

# 方法三
mysqladmin -u root "old password" "new password"

 

2、表结构

查看表结构

desc table_name;

改变字段

alter table user modify column name varchar(300);

表改名

alter table product_removebackgroud_image rename to product_image_handle;

加字段

alter table product_image_handle add cost int(10) comment '费用';
alter table product_image_handle add original_src varchar(255) comment '原图地址';

改字段类型(加长度)

alter TABLE product_image MODIFY upfrom VARCHAR(50);

修改默认值

alter table users_info alter column role_id set default 1;

 

3、索引

CREATE INDEX data_weight on image(create_date,weight) COMMENT '默认查询方式';
drop index index_name on table_name ;

 

4、其他操作

4.1、执行中的SQL

通过查询processlist表查看当前正常执行的sql,常用来查询耗时sql

查询执行最耗时的前20条语句

select id,user,host,db,command,time,state from information_schema.processlist order by time desc limit 0, 20;

查询语句耗时排序

select id,user,host,db,command,time,state from information_schema.processlist where command = 'Query' order by time desc limit 0, 20;

超过10秒的查询语句

select id,user,host,db,command,time,state from information_schema.processlist where command = 'Query' and time > 10 order by time desc;