1、查询数据库密码策略
mysql> show variables like '%password%' ;
+
| Variable_name | Value |
+
| default_password_lifetime | 360 |
| disconnect_on_expired_password | ON |
| log_builtin_as_identified_by_password | OFF |
| mysql_native_password_proxy_users | OFF |
| old_passwords | 0 |
| report_password | |
| sha256_password_proxy_users | OFF |
+
7 rows in set ( 0.01 sec)
2、修改密码
mysql> alter user root@'localhost' identified by '123456' ;
3、查询数据库当前链接数
mysql> show global status like 'Threads_connected' ;
mysql> show full processlist;
4、数据库的创建、插入数据、删除
mysql> create database database_1;
mysql> show databases ;
mysql> use database_1;
mysql> create table table_1( id int ( 2 ) , name varchar ( 10 ) , mail varchar ( 2 ) ) ;
mysql> desc table_1;
mysql> select * from table_1;
mysql> select count ( * ) from table_1;
mysql> insert into table_1( id, name, mail) values ( 1 , 'cc' , 'cc@qin.com' ) ;
mysql> insert into table_1 values ( 5 , 'cc' , 'cc@qin.com' ) , ( 6 , 'cx' , 'cx@qin.com' ) ;
mysql> delete from table_1 where id= 5 ;
mysql> update table_1 set name= 'ed' where id= 2 ;
mysql> create table table_2 like table_1;
mysql> insert into table_2 select * from table_1;
mysql> select * from table_2;
mysql> create table table_3 as select * from table_1;
mysql> select * form table_3;
mysql> alter table table_1 add newlist varchar ( 20 ) ;
mysql> select * from table_1;
mysql> alter table table_1 drop newlist;
mysql> alter table table_1 add firstlist varchar ( 20 ) first ;
mysql> alter table table_1 add afterid varchar ( 20 ) after id;
mysql> alter table table_1 modify name varchar ( 20 ) ;
mysql> alter table table_1 change id new_id int ( 2 ) ;
mysql> alter table table_1 rename new_table;
mysql> delete from table_2;
mysql> truncate table_3;
mysql> drop table table_2;
mysql> drop database database_1;
5、索引的创建和删除
mysql> SHOW INDEX FROM table_1;
mysql> SHOW CREATE TABLE table_1;
mysql> create table table_1 (
ID INT
, NAME VARCHAR ( 20 )
, INDEX idx_name( name)
) ;
mysql> create INDEX idx_name on table_1( name) ;
mysql> drop INDEX idx_name on table_1;
6、主键的创建和删除
mysql> SHOW CREATE TABLE my_table;
mysql> CREATE TABLE my_table (
id INT PRIMARY KEY ,
name VARCHAR ( 50 )
) ;
mysql> ALTER TABLE my_table ADD PRIMARY KEY ( id) ;
mysql> ALTER TABLE my_table drop PRIMARY KEY ;
7、外键的创建和删除
mysql> SHOW CREATE TABLE my_table;
mysql> alter table my_table add constraint 外键名 foreign key ( id) references table_1( id) on update cascade on delete cascade ;
mysql> alter table my_table drop foreign key 外键名;
8、 查询数据库的大小
mysql> use information_schema;
mysql> select concat( round ( sum ( DATA_LENGTH/ 1024 / 1024 ) , 2 ) , 'MB' ) as data from TABLES ;
mysql> select table_schema, sum ( AVG_ROW_LENGTH* TABLE_ROWS+ INDEX_LENGTH) / 1024 / 1024 as total_mb from information_schema. TABLES group by table_schema;
9、查看指定数据库大小
mysql> select concat( round ( sum ( DATA_LENGTH/ 1024 / 1024 ) , 2 ) , 'MB' ) as data from TABLES where table_schema= '数据库名' ;
10、查看指定数据库的指定表单的大小
mysql> select concat( round ( sum ( DATA_LENGTH/ 1024 / 1024 ) , 2 ) , 'MB' ) as data from TABLES where table_schema= 数据库名' and table_name=' 表名';
11、查询数据库有哪些函数
mysql> show function status \G;
12、查找某个表在哪个库
mysql> SELECT table_schema FROM information_schema. TABLES WHERE table_name = '表名' ;
13、查看数据库当前连接数,可以看到ip,谁连接了,连接了多少个,处于什么状态,连接时长等等
mysql> SHOW FULL processlist;
14、查看数据库配置的最大连接数
mysql> show variables like '%max_connections%' ;
15、查看数据库所有配置
mysql> show global variables;
16、修改存储引擎
mysql> show engines;
mysql> alter table 库名. 表名 ENGINE = InnoDB ;
mysql> create table 库名 ( name char ( 4 ) ) engine = innodb ;
17、无主键表信息列表
mysql> select distinct a. TABLE_SCHEMA, a. TABLE_NAME, b. TABLE_NAME from information_schema. COLUMNS a left join ( select distinct TABLE_SCHEMA, TABLE_NAME from information_schema. COLUMNS where COLUMN_KEY = 'PRI' ) b on a. TABLE_SCHEMA = b. TABLE_SCHEMA and a. TABLE_NAME = b. TABLE_NAME where b. TABLE_NAME is null and a. TABLE_SCHEMA not in ( 'information_schema' , 'mysql' , 'performance_schema' , 'sys' ) ;
18、普通用户的创建、授权:
mysql> create user ‘cc’@‘localhost’ identified by ‘abc123. ’;
mysql> grant select , update , delete , insert ON mysql. * to cc@'localhost' ;
mysql> grant all privileges on 库名. * to ‘用户名’@'%' identified by "密码" ;
mysql> revoke select , update , delete , insert on mysql. * from cc@'localhost' ;
mysql> show grants for cc@'localhost' ;
mysql> drop user cc;
mysql> delete from user where user = 'cc' and host= 'localhost' ;
19、查看当前锁状态
mysql> show status like 'table_lock%' ;