These Mysql common commands you remember?

Foreword

Recording operation common commands mysql

Basic Operation
  • Mysql command line login
 mysql -u用户名 -p用户密码
  • To increase table creation time and update time
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 '更新时间'
change Password
  • ordinary
update user set password=password("root1234") where user="root";
  • With plug-ins
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY ''root;
Partition Table
  • According to data partitions 31 provinces
ALTER TABLE tache_stat_tbl_20190120 PARTITION BY HASH(province) PARTITIONS 31
Tables, indexes, execution plan
  • View the case of table space (designated database)
select TABLE_NAME, concat(truncate(data_length/1024/1024,2),' MB') as data_size,
concat(truncate(index_length/1024/1024,2),' MB') as index_size
from information_schema.tables
# where TABLE_SCHEMA = 'yourdb'
group by TABLE_NAME
order by data_length desc;
  • Indexing

Avoid indexing inexpensive, can be determined according to the discrimination data, the need to establish an index.

select count(distinct 将要建立索引的字段) / count(*)
  • Several types of interpretation of extra execution plan

Using index
represents the use of a covering index (Covering Index)

Using where
the role of Using where with where the tips to filter the result set.

Using temporary
Description MySQL need to use a temporary table to store the result set, common in the sorting and grouping queries

Filesort the Using
MySQL can not use indexes to complete the sorting operation is called "Sort files"

Common maintenance operations
  • Query execution time over two minutes of the thread, and then spliced ​​into the kill statement
select concat('kill ', id, ';') from information_schema.processlist where command != 'Sleep' and time > 2*60 order by time desc 
  • Grant the user all permissions
GRANT ALL PRIVILEGES ON *.* TO 'YourUserName'@'%' IDENTIFIED BY "YourPassword";
Data import and export
  • Export all data including system database libraries, including
mysqldump -uroot -proot --all-databases >/all.sql
  • Export table structure only, not export data
mysqldump -uroot -proot --no-data --databases db1 > /table_with_no_data.sql
  • Cross-server export and import data, the target database must exist, otherwise it will error
mysqldump --host=h1 -uroot -proot --databases db1 |mysql --host=h2 -uroot -proot db2
  • Export several solutions mysql.sock wrong data Times

By default, the connection protocol socket, in case of the following error, can try to replace protocol.

mysqldump: Got error: 2002: "Can't connect to local MySQL server through socket 
'/var/lib/mysql/mysql.sock'

Option One: Restart the database will be re-created mysql.sock.
Option Two: if temporarily unable to restart the database, the database can be connected via TCP protocol.
. --protocol = name The protocol to use for connection (tcp, socket, pipe, memory)
Sample sentence:

mysqldump -h127.0.0.1  -uroot -proot --protocol=TCP --database db1 
--tables  conf_area_tbl  conf_app_tbl > 1.sql
  • Exporting stored procedures and custom functions
mysqldump  -uroot -p --host=localhost --all-databases --routines
  • Terminal performs sql, the output to a file
mysql -uroot -e 'select * from cb_mon.t_book limit 10' > mytest.txt
  • Generating data stored using a batch process
DROP PROCEDURE if exists test_insert ;
DELIMITER ;;
CREATE PROCEDURE test_insert ()
BEGIN

DECLARE i INT DEFAULT 1;# can not be 0
WHILE i<1000
DO

insert 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();

Guess you like

Origin www.cnblogs.com/hyq0823/p/11588188.html