本文共 3000 字,大约阅读时间需要 10 分钟。
1.设置root密码
方法1: 用SET PASSWORD命令
mysql -u root
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
方法2:用mysqladmin
mysqladmin -u root password "newpass"
方法3:升级设置
mysql -u root
use mysql;
UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
FLUSH PRIVILEGES;
2.在丢失root密码的时候,可以这样
mysqld_safe --skip-grant-tables&
mysql -u root mysql
UPDATE user SET password=PASSWORD("new password") WHEREuser='root';
FLUSH PRIVILEGES;
3.授权用户查询、插入、更新、删除权限
grant select,insert,update,delete on redmine1.* touser@"%" identified by "user";
4.新增超级权限并允许远程访问
GRANT ALL PRIVILEGES ON *.* TO 'user'@'192.168.1.3' IDENTIFIED BY'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
5.切换数据库
use mysql
6.查看所有用户
select user,host from mysql.user;
7.查询、删除匿名用户
select from mysql.user where user='';
delete from mysql.user where user='';
8.查看用户列表
SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS queryFROM mysql.user;
9.查看用户权限
show grants for root@'localhost';
10.查看用户具体权限
select * from mysql.user where user='root' \G
11.查看表的权限
use mysql
desc mysql.user;
12.运行中的mysql状态查看
对正在运行的mysql进行监控,其中一个方式就是查看mysql运行状态。
(1)QPS(每秒Query量)
QPS= Questions(or Queries) / seconds
show global status like 'Question%';
(2)TPS(每秒事务量)
TPS= (Com_commit + Com_rollback) / seconds
show global status like 'Com_commit';
show global status like 'Com_rollback';
(3)keyBuffer命中率
show global status like 'key%';
key_buffer_read_hits= (1-key_reads / key_read_requests) * 100%
key_buffer_write_hits= (1-key_writes / key_write_requests) * 100%
(4)InnoDBBuffer命中率 www.2cto.com
show status like 'innodb_buffer_pool_read%';
innodb_buffer_read_hits= (1 - innodb_buffer_pool_reads / innodb_buffer_pool_read_requests) * 100%
(5)QueryCache命中率
show status like 'Qcache%';
Query_cache_hits= (Qcahce_hits / (Qcache_hits + Qcache_inserts )) * 100%;
(6)TableCache状态量
show global status like'open%';
比较 open_tables 与 opend_tables 值
(7)ThreadCache命中率
show global status like 'Thread%';
show global status like 'Connections';
Thread_cache_hits= (1 - Threads_created / connections ) * 100%
(8)锁定状态
show global status like'%lock%';
Table_locks_waited/Table_locks_immediate=0.3%如果这个比值比较大的话,说明表锁造成的阻塞比较严重。
Innodb_row_lock_waitsinnodb行锁,太大可能是间隙锁造成的
(9)查看主、从状态
show master status;
show slave status;
(10)Tmp Table状况(临时表状况)
show status like 'Create_tmp%';
Created_tmp_disk_tables/Created_tmp_tables
比值最好不要超过10%,如果Created_tmp_tables值比较大, 可能是排序句子过多或者是连接句子不够优化
(11)Binlog Cache使用状况
show status like 'Binlog_cache%';
如果Binlog_cache_disk_use值不为0 ,可能需要调大 binlog_cache_size大小
(12)Innodb_log_waits量
show status like 'innodb_log_waits';
Innodb_log_waits值不等于0的话,表明 innodb log buffer 因为空间不足而等待
13.查看数据状态:
show global status;
14.查看连接数量
show processlist;
15.查看包含内容表
show global status like %查询内容%;
16.查看数据库状态
status;
17.查看版本信息
select version();
18.备份数据库
mysqldump -u root -p test >test.sqlEnter password: ***
19.查看权限
SHOW GRANTS;
HOW GRANTS FOR root@'localhost';
SELECT * FROM mysql.user WHERE USER='root' AND HOST='localhost'\G
20.回收权限
REVOKE EXECUTE ON *.*FROM user@'%';
转载地址:http://wcima.baihongyu.com/