Heim >Datenbank >MySQL-Tutorial >MySQL常用技巧都包括哪些?
以下的文章主要介绍的是MySQL常用技巧,我们主要是讲述七个实际应用技巧,如果你对这七个MySQL常用技巧感兴趣的话,你就可以浏览以下的文章了,望会给你带来一些帮助在此学习方面。 1)MySQL常用技巧之一,用户权限管理最好是细分到DB级 或 Table级,不要轻
以下的文章主要介绍的是MySQL常用技巧,我们主要是讲述七个实际应用技巧,如果你对这七个MySQL常用技巧感兴趣的话,你就可以浏览以下的文章了,望会给你带来一些帮助在此学习方面。
1)MySQL常用技巧之一,用户权限管理最好是细分到DB级 或 Table级,不要轻易开通全局权限;
2)MySQL常用技巧之二,用grant 和 revoke,进行用户授权和收权;
<ol class="dp-xml"> <li class="alt">MySQL<span><span class="tag">></span><span> grant select on db.* to user@host identified by ‘passwd’; </span></span> </li> <li>MySQL<span class="tag">></span><span> revoke all on db.* from user@host; </span> </li> <li class="alt">MySQL<span class="tag">></span><span> flush privileges; </span> </li> </ol>
注意:对用户权限作变更后需运行flush使变更生效;
3)MySQL常用技巧之三,查看服务器运行状况的几个重要命令;
show status; 显示系统的运行状态
show variables; 显示系统运行的参数设置与环境变量
show processlist; 显示现有的访问连接;
对master slave系统还有:show master/slave status;
4)MySQL常用技巧之四,设置最大的并发响应连接数、等待响应队列的最大等待连接数(上限与具体操作系统有关)、非活动连接超时时间
最大连接数
查看:MySQL> show variables like ‘max_connections’;
设置:MySQL> set global max_connections = 200;
默认为100,若设置为0,则表示不作限制;
瞬时并发等待连接数
查看:MySQL> show variables like ‘back_log’;
设置:MySQL> set global back_log = 200;
默认为50;
非活动连接超时时间
MySQL> set wait_timeout = 3600;
默认为28800,即8小时,单位秒;
5)MySQL常用技巧之五,表优化(碎片整理)
倘若一个数据量很大的表进行了大量的修改,那么可以通过命令
MySQL> optimize table table_name;
来达到碎片整理的目的;
6)MySQL常用技巧之六,使用MySQLhotcopy进行数据库文件热备份
/home/MySQL/bin/MySQLhotcopy db_name[./table_regex/] [new_db_name | direc tory]
示例:
/home/MySQL/bin/MySQLhotcopy -u root -p ‘xxxx’ test./^tt$/ ./
注意:MySQLhotcopy是一个perl程序,需要DBI和DBD perl模块的支持
7)MySQL常用技巧之七,错误日志与binlog
错误记录日志一般是在数据目录下,如:
var/.err
binlog可以记录数据库上发生的所有操作记录,通过my.cnf中的log-bin选项来开启,如果被注释就代表关闭,binlog的内容可以通过以下命令来查看:
<ol class="dp-xml"><li class="alt">MySQL<span>binlog [options] log-files </span> </li></ol>
附录 权限名称与描述列表
<ol class="dp-xml"> <li class="alt"><span><span>Privilege </span></span></li> <li><span>Meaning </span></li> <li class="alt"><span>ALL [PRIVILEGES] </span></li> <li><span>Sets all simple privileges except GRANT OPTION </span></li> <li class="alt"><span>ALTER </span></li> <li><span>Allows use of ALTER TABLE </span></li> <li class="alt"><span>CREATE </span></li> <li><span>Allows use of CREATE TABLE </span></li> <li class="alt"><span>CREATE TEMPORARY TABLES </span></li> <li><span>Allows use of CREATE TEMPORARY TABLE </span></li> <li class="alt"><span>DELETE </span></li> <li><span>Allows use of DELETE </span></li> <li class="alt"><span>DROP </span></li> <li><span>Allows use of DROP TABLE </span></li> <li class="alt"><span>EXECUTE </span></li> <li><span>Not implemented </span></li> <li class="alt"><span>FILE </span></li> <li><span>Allows use of SELECT … INTO OUTFILE and LOAD DATA INFILE </span></li> <li class="alt"><span>INDEX </span></li> <li><span>Allows use of CREATE INDEX and DROP INDEX </span></li> <li class="alt"><span>INSERT </span></li> <li><span>Allows use of INSERT </span></li> <li class="alt"><span>LOCK TABLES </span></li> <li><span>Allows use of LOCK TABLES on tables for which you have the SELECT privilege </span></li> <li class="alt"><span>PROCESS </span></li> <li><span>Allows use of SHOW FULL PROCESSLIST </span></li> <li class="alt"><span>REFERENCES </span></li> <li><span>Not implemented </span></li> <li class="alt"><span>RELOAD </span></li> <li><span>Allows use of FLUSH </span></li> <li class="alt"><span>REPLICATION CLIENT </span></li> <li><span>Allows the user to ask where slave or master servers are </span></li> <li class="alt"><span>REPLICATION SLAVE </span></li> <li><span>Needed for replication slaves (to read binary log events from the master) </span></li> <li class="alt"><span>SELECT </span></li> <li><span>Allows use of SELECT </span></li> <li class="alt"><span>SHOW DATABASES </span></li> <li><span>SHOW DATABASES shows all databases </span></li> <li class="alt"><span>SHUTDOWN </span></li> <li> <span>Allows use of </span>MySQL<span>admin shutdown </span> </li> <li class="alt"><span>SUPER </span></li> <li><span>Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL statements, </span></li> <li> <span>the </span>MySQL<span>admin debug command; allows you to connect (once) even if max_connections is reached </span> </li> <li class="alt"><span>UPDATE </span></li> <li><span>Allows use of UPDATE </span></li> <li class="alt"><span>USAGE </span></li> <li><span>Synonym for “no privileges” </span></li> <li class="alt"><span>GRANT OPTION </span></li> <li><span>Allows privileges to be granted </span></li> </ol>
以上的相关内容就是对MySQL常用技巧的介绍,望你能有所收获。