首页 > 数据库 > MySQL > 全面的MySQL基础运维知识点(一)
例如:使用EXPLAIN+SQL查询:
mysql> explain select * from cons1 where id<=10; +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | 1 | SIMPLE | cons1 | NULL | range | PRIMARY | PRIMARY | 8 | NULL | 10 | 100.00 | Using where | +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (5.75 sec)
mysql> explain select * from cons1 where id<=(select id3 from cons2 where id=10); +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+ | 1 | PRIMARY | cons1 | NULL | range | PRIMARY | PRIMARY | 8 | NULL | 10 | 100.00 | Using where | | 2 | SUBQUERY | cons2 | NULL | const | PRIMARY | PRIMARY | 8 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+ 2 rows in set, 1 warning (0.00 sec)
参数
|
说明 |
SIMPLE
|
简单查询 |
PRIMARY |
最外层查询 |
SUBQUERY |
映射为子查询 |
DERIVED |
子查询 |
UNION |
联合 |
UNION RESULT |
使用联合的结果
|
参数 |
说明
|
ALL |
全数据表扫描 |
index |
全索引表扫描 |
RANGE |
对索引列进行范围查找 |
INDEX_MERGE |
合并索引,使用多个单列索引搜索 |
REF |
根据索引查找一个或多个值 |
EQ_REF |
搜索时使用primary key 或 unique类型 |
CONST |
常量,表最多有一个匹配行, 因为仅有一行,在这行的列值可被优化器剩余部分认为是常数,const表很快,因为它们只读取一次。 |
SYSTEM |
系统,表仅有一行(=系统表)。这是const联接类型的一个特例。 |
possible_keys:提示使用哪个索引会在该表中找到行,不太重要
key:MYSQL真实使用的索引,简单且重要
key_len:MySQL中使用索引字节长度
ref:ref列显示使用哪个列或常数与key一起从表中选择行。
rows:显示MYSQL执行查询的行数,简单且重要,数值越大越不好,说明没有用好索引
mysql> grant all on *.* to 'ljw'@'10.211.55.%';
mysql> grant all on test.* to 'ljw'@'10.211.55.%';
mysql> grant select, insert on test.ljw to 'ljw'@'10.211.55.%';
mysql> select user,host from mysql.user; +---------------+---------------+ | user | host | +---------------+---------------+ | sstuser | 192.168.111.% | | test | 192.168.111.% | | bkpuser | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | | sstuser | localhost | +---------------+---------------+ 7 rows in set (0.63 sec)
mysql> show grants for sstuser@'192.168.111.%'; +--------------------------------------------------------------------------------------------+ | Grants for sstuser@192.168.111.% | +--------------------------------------------------------------------------------------------+ | GRANT RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'192.168.111.%' | +--------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
权限具体参数语法参考官网https://dev.mysql.com/doc/refman/8.0/en/grant.html
show variables like 'event_scheduler';
set global event_scheduler = on;
CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
DO event_body;
schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]
interval:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
CREATE EVENT `NewEvent`
ON SCHEDULE EVERY 1 DAY STARTS '2021-06-17 02:00:00'
ON COMPLETION PRESERVE
ENABLE
DO
INSERT INTO events_list VALUES('event_now', NOW());
CREATE EVENT IF NOT EXISTS event_test ON SCHEDULE EVERY 1 SECOND ON COMPLETION PRESERVE ENABLE DO CALL e_test();
CREATE EVENT EVENT1 ON SCHEDULE EVERY 9 DAY STARTS NOW() ON COMPLETION PRESERVE ENABLE DO BEGIN CALL TOTAL(); END
CREATE EVENT EVENT2 ON SCHEDULE EVERY 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR) ON COMPLETION PRESERVE ENABLE DO BEGIN CALL STAT(); END
SELECT * FROM INFORMATION_SCHEMA.EVENTS; select * from mysql.event;
ALTER EVENT e_delete_upvote DISABLE[ENABLE];
ALTER EVENT olddb.myevent RENAME TO newdb.myevent;
DROP EVENT e_delete_upvote;
1.查看mysql进程
shell> ps -ef|grep mysql root 24571 1 0 04:00 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr mysql 25470 24571 0 04:00 ? 00:01:25 /usr/sbin/mysqld --basedir=/usr --datadir=/mysql/data --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --wsrep-provider=/usr/lib64/galera3/libgalera_smm.so --log-error=/mysql/log/error.log --open-files-limit=65535 --pid-file=/mysql/run/mysql.pid --socket=/mysql/run/mysql.sock --port=3306 --wsrep_start_position=2ac50ed4-c9d4-11eb-8299-0f79163b603a:21059 root 31302 29472 0 11:09 pts/2 00:00:00 grep --color=auto mysql
2.kill进程
shell> kill -9 24571 shell> kill -9 25470
3.跳过权限表,启动数据库
shell> mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
4.重置root密码
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user root set authentication_string=password('ljw') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
5.重启数据库
shell> mysqladmin -uroot -pljw shutdown mysqladmin: [Warning] Using a password on the command line interface can be insecure. shell> mysqld_safe --defaults-file=/etc/my.cnf &
6.使用新密码登陆
shell> mysql -uroot -pljw mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.26-ndb-7.6.10-cluster-gpl-log MySQL Cluster Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30310891/viewspace-2777068/ ,如需转载,请注明出处,否则将追究法律责任。