最近整理笔记,分享一篇常用命令给大家。值得收藏。hah....
1、查看表状态
show table status like '%tablename%';
2、改密码
方法一:
./mysqladmin -uroot -hlocalhost --socket=/data/mysql_3306/tmp/mysql.sock -p password
方法二:
ALTER USER 'root'@'localhost' IDENTIFIED with mysql_native_password BY 'password';
3、杀特定用户链接
select concat('KILL ',id,';')from information_schema.processlist where user='21xmt_user';
4、binlog解析
https://cloud.tencent.com/developer/article/1925495
mysqlbinlog --no-defaults -vv --base64-output=decode-rows mysql-bin.000201
5、删除用户
drop user 'xxx'; 只删除 'xxx'@'%' 账户
6、修改主键(小表,大表用pt-osc等工具)
alter table xxx drop primary key,add primary key(task_id, aaa);
7、查看表大小
select table_name , table_rows from inforation_schema.tables where table_name='xxx';
8、权限查询
SELECT
CONCAT(
'show grants for '',
user,
''@'',
Host,
'';'
) AS ShowGrants
FROM
mysql.`user`
WHERE
`User` NOT IN (
'root',
'mysql.session',
'mysql.sys'
);
9、PS库内存使用
To control memory instrumentation state at server startup, use lines like these in your my.cnf file:
Enable:
[mysqld]performance-schema-instrument='memory/%=ON'Disable:
[mysqld]performance-schema-instrument='memory/%=OFF'To control memory instrumentation state at runtime, update the ENABLED column of the relevant instruments in the setup_instruments table:
Enable:
UPDATE performance_schema.setup_instrumentsSET ENABLED = 'YES'WHERE NAME LIKE 'memory/%';Disable:
UPDATE performance_schema.setup_instrumentsSET ENABLED = 'NO'WHERE NAME LIKE 'memory/%';
10、trace
SET SESSION OPTIMIZER_TRACE="enabled=on"; # enable tracing
; # like SELECT, EXPLAIN SELECT, UPDATE, DELETE...
SELECT * FROM information_schema.OPTIMIZER_TRACE;
[ repeat last two steps at will ]
SET SESSION OPTIMIZER_TRACE="enabled=off"; # disable tracing
11、innodb状态查看
show engine innodb statusG;
12、event 操作
13、造测试数据
create table t1(id int primary key, a int, b int, index(a));
drop procedure idata;
delimiter ;;
create procedure idata()
begin
declare i int;
set i=1;
while(i 60 ORDER BY trx_started;
19、mysql 终端中操作启用事务(DML操作记得加)
begin;
要执行的sql;
commit/rollback;
20、修改表的字符集
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4;
21、查看用户自定义视图
存储过程、triggers查询方法类似,自己找相关表查询即可。
SELECT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.VIEWS
WHERE TABLE_SCHEMA NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys');