最近整理笔记,分享一篇常用命令给大家。值得收藏。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
<statement to trace>; # 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 status\G;
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 <= 1000000)do
insert into t1 values(i, 1000001 - i, i);
set i = i+1;
end while;
end;;
delimiter ;
call idata();
14、修改root密码
配置文件添加skip-grant-tables
alter user 'root'@'localhost' identified by 'password';
flush privileges;
配置文件去掉skip-grant-tables
systemctl restart mysqld;
15、添加自增属性
alter table xxx modify id bigint auto_increment;
15、rename库名 (所有表操作即可完成库名重命名)
RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
16、表碎片整理
alter table tablename engine innodb;
OPTIMIZE [NO_WRITE_TO_BINLOG | LOCAL]
TABLE tbl_name [, tbl_name] ...;
17、查看表的最近更新时间
SELECT
`TABLE_NAME`, `CREATE_TIME`, `UPDATE_TIME`
FROM
`information_schema`.`TABLES`
WHERE
`information_schema`.`TABLES`.`TABLE_SCHEMA` = 'ob1_dg_log'
AND
`information_schema`.`TABLES`.`TABLE_NAME` = 'w_logbattle';
18、创建索引时间查询
SELECT trx_id, trx_started, (NOW() - trx_started) trx_duration_seconds, id processlist_id, user, IF(LEFT(HOST, (LOCATE(':', host) - 1)) = '', host, LEFT(HOST, (LOCATE(':', host) - 1))) host, command, time, REPLACE(SUBSTRING(info,1,25),'\n','') info_25 FROM information_schema.innodb_trx JOIN information_schema.processlist ON innodb_trx.trx_mysql_thread_id = processlist.id WHERE (NOW() - trx_started) > 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');
欢迎关注公众号:DBA札记,一起交流数据库技术。欢迎觉得读完本文有收获,可以转发给其他朋友,大家一起学习进步!谢谢大家。