前言
记录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_sizefrom information_schema.tables# where TABLE_SCHEMA = 'yourdb'group by TABLE_NAMEorder by data_length desc;
尽量避免廉价的建立索引,可以先根据数据区分度来判断,是否有必要建立索引 。
select count(distinct 将要建立索引的字段) / count(*)
Using index表示使用了覆盖索引(Covering Index)
Using whereUsing where的作用提示了用where来过滤结果集 。
Using temporary说明MySQL需要使用临时表来存储结果集,常见于排序和分组查询
Using filesortMySQL中无法利用索引完成的排序操作称为“文件排序”
常用维护操作
- 查询执行时间超过2分钟的线程,然后拼接成 kill 语句
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,如遇到下述错误,可以尝试更换协议 。
【运维常用Mysql命令你是否还记得?】mysqldump: Got error: 2002: "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'方案一:重启数据库会重新创建mysql.sock 。方案二:若暂时无法重启数据库,可以通过TCP协议连接数据库 。--protocol=name The protocol to use for connection (tcp, socket, pipe,memory).样例语句:
mysqldump -h127.0.0.1 -uroot -proot --protocol=TCP --database db1 --tables conf_area_tbl conf_App_tbl > 1.sql
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 ()BEGINDECLARE i INT DEFAULT 1;# can not be 0WHILE i<1000DOinsert 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();
推荐阅读
-
-
底牌|中国不再低调,主动亮出自己的“底牌”,俄:十分震撼!
-
央视网|服贸会探馆丨一起“康康”专题展里的冬季运动“大礼包”
-
-
新消费内参|如何撼动“维秘”巨头地位,实现7.5亿美元市场估值,ThirdLove:
-
台风“白鹿”在福建东山沿海登陆 暂未接到人员伤亡报告
-
-
-
雷电|注意 | 济南发布强对流天气来袭和雷电黄色预警信号
-
疫情|华菱钢铁:预计2020年半年度净利润盈利29.3亿~31.3亿元
-
环球网|日本选定三菱重工为下一代战机主承包商 预计本世纪30年代首飞
-
Filecoin分叉币FileStar预计10月30号上线
-
《声生不息3》:周深的尴尬,揭开了娱乐圈的三大现实面
-
-
无名渔夫|2020年有什么副业能够稳定月入千元?分析赚钱的方法
-
舟谊会展 Resilience,泰国曼谷工业自动化机器人展览会Maintenance
-
中金网 Chain生态发起人AlanShaw出席欧盟区块链生态论坛,AWS
-
关晓彤说自己是在逃格格|5G冲浪少年!关晓彤说自己是在逃格格,秒变新晋接梗王
-
城市大看|2020怎么穿出时髦感?泫雅示范最新潮流!大秀细腰美腿打造好身材
-
『火星』火星上也有沙尘暴?科学家:每10年爆发一次,连太阳都会被遮住