mysqladmin是經(jīng)常使用的命令,一般用在需要從mysql執(zhí)行一次命令獲取想要的信息,常見(jiàn)于腳本中使用
# 使用格式:mysqladmin [options] command [arg] [,command [arg]] …
# 常用的command包括:
create DB_Name:# 創(chuàng)建數(shù)據(jù)庫(kù)
drop DB_Name:# 刪除數(shù)據(jù)庫(kù)
debug:# 打開(kāi)調(diào)試日志并記錄于error log中
status:# 顯示簡(jiǎn)要狀態(tài)信息
--sleep#:設(shè)置間隔時(shí)長(zhǎng)
--count#:設(shè)置顯示的批次
extended-status:# 顯示擴(kuò)展信息锯梁,輸出mysqld的各狀態(tài)變量及賦值,相當(dāng)于執(zhí)行“mysql> show global status”
variables:# 輸出mysqld的各服務(wù)器變量
flush-hosts:# 清空主機(jī)相關(guān)的緩存:DNS解析緩存;此前因?yàn)檫B接錯(cuò)誤次數(shù)過(guò)多而被拒絕訪問(wèn)mysqld的主機(jī)列表
flush-logs:# 日志滾動(dòng)宁脊,只能滾動(dòng)二進(jìn)制日志和中繼日志
refresh:# 相當(dāng)于同時(shí)使用flush-hosts和flush-logs
flush-privileges:# 通知mysqld重讀授權(quán)表
reload:# 功能同“flush-privileges”
flush-status:# 重置狀態(tài)變量的值
flush-tables:# 關(guān)閉當(dāng)前打開(kāi)的表文件句柄
flush-threads:# 清空線程緩存
kill:# 殺死指定的線程,需指定線程ID贤姆;可以一次殺死多個(gè)線程榆苞,以逗號(hào)分隔,但不能有多余空格
password:# 修改當(dāng)前用戶(hù)的密碼
ping:# 模擬ping操作霞捡,檢測(cè)mysqld是否在線
processlist:# 顯示mysqld線程列表
shutdown:# 關(guān)閉mysqld進(jìn)程
start-slave坐漏,stop-slave:# 啟動(dòng)/關(guān)閉從服務(wù)器線程
mysqladmin 工具的使用格式:
mysqladmin [option] command [command option] command ......
option 選項(xiàng):
-c number 自動(dòng)運(yùn)行次數(shù)統(tǒng)計(jì),必須和 -i 一起使用
-i number 間隔多長(zhǎng)時(shí)間重復(fù)執(zhí)行
每個(gè)兩秒查看一次服務(wù)器的狀態(tài)碧信,總共重復(fù)5次仙畦。
./mysqladmin -uroot -p -i 2 -c 5 status
-#, --debug[=name] Output debug log. Often this is 'd:t:o,filename'.
-f, --force Don't ask for confirmation on drop database; with
multiple commands, continue even if an error occurs. 禁用錯(cuò)誤,drop 數(shù)據(jù)庫(kù)時(shí)不提示音婶,執(zhí)行多條命令時(shí)出錯(cuò)繼續(xù)執(zhí)行
-C, --compress Use compression in server/client protocol.
--character-sets-dir=name 字符集所在目錄
Directory where character sets are.
--default-character-set=name 設(shè)置默認(rèn)字符集
Set the default character set.
-?, --help Display this help and exit. 顯示幫助
-h, --host=name Connect to host. 連接的主機(jī)名或iP
-p, --password[=name] 登錄密碼慨畸,如果不寫(xiě)于參數(shù)后,則會(huì)提示輸入
Password to use when connecting to server. If password is
not given it's asked from the tty.
-P, --port=# Port number to use for connection. 指定數(shù)據(jù)庫(kù)端口
--protocol=name The protocol of connection (tcp,socket,pipe,memory). 指定連接協(xié)議
-r, --relative Show difference between current and previous values when
used with -i. Currently works only with extended-status. 顯示前后變化的值衣式,必須結(jié)合- i
./mysqladmin -uroot -p -i 2 -r extended-status
假如此處顯示的uptime 將永遠(yuǎn)是2寸士,因?yàn)榕c之前取的結(jié)果比只相差2.
-O, --set-variable=name
Change the value of a variable. Please note that this
option is deprecated; you can set variables directly with
--variable-name=value.修改變量的值,該選項(xiàng)已經(jīng)不再使用碴卧,請(qǐng)使用--variable-name=value 的方式修改變量值
-s, --silent Silently exit if one can't connect to server.
-S, --socket=name Socket file to use for connection. 指定socket file
-i, --sleep=# Execute commands again and again with a sleep between. 間隔一段時(shí)間執(zhí)行一次
-u, --user=name User for login if not current user.登錄數(shù)據(jù)庫(kù)用戶(hù)名
-v, --verbose Write more information. 寫(xiě)更多的信息
-V, --version Output version information and exit. 顯示版本
./mysql -uroot -p -V
-E, --vertical Print output vertically. Is similar to --relative, but
prints output vertically.
-w, --wait[=#] Wait and retry if connection is down. 如果連接斷開(kāi)弱卡,等待w 指定的時(shí)間后重試
--connect_timeout=#
--shutdown_timeout=#
1、查看服務(wù)器的狀況:status
./mysql -uroot -p status
顯示結(jié)果:
Uptime: 4883162 Threads: 1 Questions: 86 Slow queries: 0 Opens: 0 Flush tables: 1 Open tables: 18 Queries per second avg: 0.000
2.修改root 密碼:
mysqladmin -u root -poldpassword password 'newpassword'
3.檢查mysqlserver是否可用:
mysqladmin -uroot -p ping
顯示結(jié)果:
mysqld is alive
4.查詢(xún)服務(wù)器的版本
mysqladmin -uroot -p version
5.查看服務(wù)器狀態(tài)的當(dāng)前值:
mysqladmin -uroot -p extended-status
顯示結(jié)果:
+-----------------------------------+----------+
| Variable_name | Value |
+-----------------------------------+----------+
| Aborted_clients | 12 |
| Aborted_connects | 300 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Bytes_received | 23608 |
| .......
|Threads_created | 3 |
| Threads_running | 1 |
| Uptime | 4884294 |
+-----------------------------------+----------+
6.查詢(xún)服務(wù)器系統(tǒng)變量值:
mysqladmin -uroot -p variables
顯示結(jié)果:
+---------------------------------+----------------------------------------+
| Variable_name | Value |
+---------------------------------+----------------------------------------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
| automatic_sp_privileges | ON |
| back_log | 50 |
| basedir | /usr/local/mysql/ |
..
7.顯示服務(wù)器所有運(yùn)行的進(jìn)程:
mysqladmin -uroot -p processlist
mysqladmin -uroot -p-i 1 processlist 每秒刷新一次
顯示結(jié)果:
+-----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+-----------+----+---------+------+-------+------------------+
| 331 | root | localhost | | Query | 0 | | show processlist |
+-----+------+-----------+----+---------+------+-------+------------------+
8.創(chuàng)建數(shù)據(jù)庫(kù)
./mysqladmin -uroot -p create daba-test
驗(yàn)證:
./mysql -uroot -p 登錄后:show databases;
9.顯示服務(wù)器上的所有數(shù)據(jù)庫(kù)
./mysqlshow -uroot -p
10.顯示數(shù)據(jù)庫(kù)daba-test下有些什么表:
./mysqlshow -uroot -p daba-test
11.統(tǒng)計(jì)daba-test 下數(shù)據(jù)庫(kù)表列的匯總
./mysqlshow -uroot -p daba-test -v
12.統(tǒng)計(jì)daba-test 下數(shù)據(jù)庫(kù)表的列數(shù)和行數(shù)
./mysqlshow -uroot -p daba-test -v -v
13. 刪除數(shù)據(jù)庫(kù) daba-test
./mysqladmin -uroot -p drop daba-test
14. 重載權(quán)限信息
./mysqladmin -uroot -p reload
15.刷新所有表緩存住册,并關(guān)閉和打開(kāi)log
./mysqladmin -uroot -p refresh
16.使用安全模式關(guān)閉數(shù)據(jù)庫(kù)
./mysqladmin -uroot -p shutdown
You can also use “/etc/rc.d/init.d/mysqld stop” to shutdown the server. To start the server, execute “/etc/rc.d/init.d/mysql start”--如果不是以服務(wù)來(lái)運(yùn)行則這兩條命令無(wú)效
17.mysqladmin flush commands
# mysqladmin -u root -ptmppassword flush-hosts
# mysqladmin -u root -ptmppassword flush-logs
# mysqladmin -u root -ptmppassword flush-privileges
# mysqladmin -u root -ptmppassword flush-status
# mysqladmin -u root -ptmppassword flush-tables
# mysqladmin -u root -ptmppassword flush-threads
?flush-hosts: Flush all information in the host cache.
?flush-privileges: Reload the grant tables (same as reload).
?flush-status: Clear status variables.
?flush-threads: Flush the thread cache.
18 .mysqladmin 執(zhí)行kill 進(jìn)程:
./mysqladmin -uroot -p processlist
./mysqladmin -uroot -p kill idnum
19.停止和啟動(dòng)MySQL replication on a slave server
mysqladmin -u root -p stop-slave
mysqladmin -u root -p start-slave
20 .同時(shí)執(zhí)行多個(gè)命令
mysqladmin -u root -p process status version