MySQL常用技巧都包括哪些?
以下的文章主要介紹的是MySQL常用技巧,我們主要是講述七個(gè)實(shí)際應(yīng)用技巧,如果你對(duì)這七個(gè)MySQL常用技巧感興趣的話(huà),你就可以瀏覽以下的文章了,望會(huì)給你帶來(lái)一些幫助在此學(xué)習(xí)方面。
1)MySQL常用技巧之一,用戶(hù)權(quán)限管理最好是細(xì)分到DB級(jí) 或 Table級(jí),不要輕易開(kāi)通全局權(quán)限;
2)MySQL常用技巧之二,用grant 和 revoke,進(jìn)行用戶(hù)授權(quán)和收權(quán);
- MySQL> grant select on db.* to user@host identified by ‘passwd’;
- MySQL> revoke all on db.* from user@host;
- MySQL> flush privileges;
注意:對(duì)用戶(hù)權(quán)限作變更后需運(yùn)行flush使變更生效;
3)MySQL常用技巧之三,查看服務(wù)器運(yùn)行狀況的幾個(gè)重要命令;
show status; 顯示系統(tǒng)的運(yùn)行狀態(tài)
show variables; 顯示系統(tǒng)運(yùn)行的參數(shù)設(shè)置與環(huán)境變量
show processlist; 顯示現(xiàn)有的訪問(wèn)連接;
對(duì)master slave系統(tǒng)還有:show master/slave status;
4)MySQL常用技巧之四,設(shè)置最大的并發(fā)響應(yīng)連接數(shù)、等待響應(yīng)隊(duì)列的最大等待連接數(shù)(上限與具體操作系統(tǒng)有關(guān))、非活動(dòng)連接超時(shí)時(shí)間
最大連接數(shù)
查看:MySQL> show variables like ‘max_connections’;
設(shè)置:MySQL> set global max_connections = 200;
默認(rèn)為100,若設(shè)置為0,則表示不作限制;
瞬時(shí)并發(fā)等待連接數(shù)
查看:MySQL> show variables like ‘back_log’;
設(shè)置:MySQL> set global back_log = 200;
默認(rèn)為50;
非活動(dòng)連接超時(shí)時(shí)間
MySQL> set wait_timeout = 3600;
默認(rèn)為28800,即8小時(shí),單位秒;
5)MySQL常用技巧之五,表優(yōu)化(碎片整理)
倘若一個(gè)數(shù)據(jù)量很大的表進(jìn)行了大量的修改,那么可以通過(guò)命令
MySQL> optimize table table_name;
來(lái)達(dá)到碎片整理的目的;
6)MySQL常用技巧之六,使用MySQLhotcopy進(jìn)行數(shù)據(jù)庫(kù)文件熱備份
/home/MySQL/bin/MySQLhotcopy db_name[./table_regex/] [new_db_name | direc tory]
示例:
/home/MySQL/bin/MySQLhotcopy -u root -p ‘xxxx’ test./^tt$/ ./
注意:MySQLhotcopy是一個(gè)perl程序,需要DBI和DBD perl模塊的支持
7)MySQL常用技巧之七,錯(cuò)誤日志與binlog
錯(cuò)誤記錄日志一般是在數(shù)據(jù)目錄下,如:
var/.err
binlog可以記錄數(shù)據(jù)庫(kù)上發(fā)生的所有操作記錄,通過(guò)my.cnf中的log-bin選項(xiàng)來(lái)開(kāi)啟,如果被注釋就代表關(guān)閉,binlog的內(nèi)容可以通過(guò)以下命令來(lái)查看:
- MySQLbinlog [options] log-files
附錄 權(quán)限名稱(chēng)與描述列表
- Privilege
- Meaning
- ALL [PRIVILEGES]
- Sets all simple privileges except GRANT OPTION
- ALTER
- Allows use of ALTER TABLE
- CREATE
- Allows use of CREATE TABLE
- CREATE TEMPORARY TABLES
- Allows use of CREATE TEMPORARY TABLE
- DELETE
- Allows use of DELETE
- DROP
- Allows use of DROP TABLE
- EXECUTE
- Not implemented
- FILE
- Allows use of SELECT … INTO OUTFILE and LOAD DATA INFILE
- INDEX
- Allows use of CREATE INDEX and DROP INDEX
- INSERT
- Allows use of INSERT
- LOCK TABLES
- Allows use of LOCK TABLES on tables for which you have the SELECT privilege
- PROCESS
- Allows use of SHOW FULL PROCESSLIST
- REFERENCES
- Not implemented
- RELOAD
- Allows use of FLUSH
- REPLICATION CLIENT
- Allows the user to ask where slave or master servers are
- REPLICATION SLAVE
- Needed for replication slaves (to read binary log events from the master)
- SELECT
- Allows use of SELECT
- SHOW DATABASES
- SHOW DATABASES shows all databases
- SHUTDOWN
- Allows use of MySQLadmin shutdown
- SUPER
- Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL statements,
- the MySQLadmin debug command; allows you to connect (once) even if max_connections is reached
- UPDATE
- Allows use of UPDATE
- USAGE
- Synonym for “no privileges”
- GRANT OPTION
- Allows privileges to be granted
以上的相關(guān)內(nèi)容就是對(duì)MySQL常用技巧的介紹,望你能有所收獲。
【編輯推薦】