15个常用的MySQL使用管理命令示例

数据库 MySQL
此文章主要讲述的是15个常用的MySQL使用管理命令,此文章对于MySQL数据库的学习新手来说收获不小,下面就是文章的主要内容。

以下的文章主要向大家描述的是15个MySQL使用管理命令的描述,我前两天在相关网站看见15个MySQL数据库使用管理命令的资料,觉得挺好,就拿出来供大家分享,望大家浏览之后会有所收获。

  1. How to change the MySQL root user password?  
  2. # MySQLadmin -u root -ptmppassword password 'newpassword'  
  3. # MySQL -u root -pnewpassword  
  4. Welcome to the MySQL monitor. Commands end with ; or \g.  
  5. Your MySQL connection id is 8  
  6. Server version: 5.1.25-rc-community MySQL Community Server (GPL)  
  7. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  
  8. MySQL> 
  9. How to check whether MySQL Server is up and running?  
  10. # MySQLadmin -u root -p ping  
  11. Enter password:  
  12. MySQLd is alive3. How do I find out what version of MySQL I am running?  
  13. Apart from giving the ‘Server version’, this command also displays the current status of the MySQL server.  
  14. # MySQLadmin -u root -ptmppassword version  
  15. MySQLadmin Ver 8.42 Distrib 5.1.25-rc, for redhat-linux-gnu on i686  
  16. Copyright (C) 2000-2006 MySQL AB  
  17. This software comes with ABSOLUTELY NO WARRANTY. This is free software,  
  18. and you are welcome to modify and redistribute it under the GPL license  
  19. Server version 5.1.25-rc-community  
  20. Protocol version 10  
  21. Connection Localhost via UNIX socket  
  22. UNIX socket /var/lib/MySQL/MySQL.sock  
  23. Uptime: 107 days 6 hours 11 min 44 sec  
  24. Threads: 1 Questions: 231976 Slow queries: 0 Opens: 17067  
  25. Flush tables: 1 Open tables: 64 Queries per second avg: 0.254. What is the current status of MySQL server?  
  26. # MySQLadmin -u root -ptmppassword status  
  27. Uptime: 9267148  
  28. Threads: 1 Questions: 231977 Slow queries: 0 Opens: 17067  
  29. Flush tables: 1 Open tables: 64 Queries per second avg: 0.25The status command displays the following information:  
  30. Uptime: Uptime of the MySQL server in seconds   
  31. Threads: Total number of clients connected to the server.   
  32. Questions: Total number of queries the server has executed since the startup.   
  33. Slow queries: Total number of queries whose execution time waas more than long_query_time variable’s value.   
  34. Opens: Total number of tables opened by the server.   
  35. Flush tables: How many times the tables were flushed.   
  36. Open tables: Total number of open tables in the database.   
  37. 5. How to view all the MySQL Server status variable and it’s current value?  
  38. # MySQLadmin -u root -ptmppassword extended-status  
  39. +-----------------------------------+-----------+  
  40. | Variable_name | Value |  
  41. +-----------------------------------+-----------+  
  42. | Aborted_clients | 579 |  
  43. | Aborted_connects | 8 |  
  44. | Binlog_cache_disk_use | 0 |  
  45. | Binlog_cache_use | 0 |  
  46. | Bytes_received | 41387238 |  
  47. | Bytes_sent | 308401407 |  
  48. | Com_admin_commands | 3524 |  
  49. | Com_assign_to_keycache | 0 |  
  50. | Com_alter_db | 0 |  
  51. | Com_alter_db_upgrade | 0 |6. How to display all MySQL server system variables and the values?  
  52. # MySQLadmin -u root -ptmppassword variables  
  53. +---------------------------------+---------------------------------+  
  54. | Variable_name | Value |  
  55. +---------------------------------+---------------------------------+  
  56. | auto_increment_increment | 1 |  
  57. | basedir | / |  
  58. | big_tables | OFF |  
  59. | binlog_format | MIXED |  
  60. | bulk_insert_buffer_size | 8388608 |  
  61. | character_set_client | latin1 |  
  62. | character_set_database | latin1 |  
  63. | character_set_filesystem | binary |  
  64. skip.....  
  65. | time_format | %H:%i:%s |  
  66. | time_zone | SYSTEM |  
  67. | timed_mutexes | OFF |  
  68. | tmpdir | /tmp |  
  69. | tx_isolation | REPEATABLE-READ |  
  70. | unique_checks | ON |  
  71. | updatable_views_with_limit | YES |  
  72. | version | 5.1.25-rc-community |  
  73. | version_comment | MySQL Community Server (GPL) |  
  74. | version_compile_machine | i686 |  
  75. | version_compile_os | redhat-linux-gnu |  
  76. | wait_timeout | 28800 |  
  77. +---------------------------------+---------------------------------+
    7. How to display all the running process/queries in the MySQL database?  
  78. # MySQLadmin -u root -ptmppassword processlist  
  79. +----+------+-----------+----+---------+------+-------+------------------+  
  80. | Id | User | Host | db | Command | Time | State | Info |  
  81. +----+------+-----------+----+---------+------+-------+------------------+  
  82. | 20 | root | localhost | | Sleep | 36 | | |  
  83. | 23 | root | localhost | | Query | 0 | | show processlist |  
  84. +----+------+-----------+----+---------+------+-------+------------------+You can use this command effectively to debug any performance issue and identify the query that is causing problems, by running the command automatically every 1 second as shown below.  
  85. # MySQLadmin -u root -ptmppassword -i 1 processlist  
  86. +----+------+-----------+----+---------+------+-------+------------------+  
  87. | Id | User | Host | db | Command | Time | State | Info |  
  88. +----+------+-----------+----+---------+------+-------+------------------+  
  89. | 20 | root | localhost | | Sleep | 36 | | |  
  90. | 23 | root | localhost | | Query | 0 | | show processlist |  
  91. +----+------+-----------+----+---------+------+-------+------------------+  
  92. +----+------+-----------+----+---------+------+-------+------------------+  
  93. | Id | User | Host | db | Command | Time | State | Info |  
  94. +----+------+-----------+----+---------+------+-------+------------------+  
  95. | 24 | root | localhost | | Query | 0 | | show processlist |  
  96. +----+------+-----------+----+---------+------+-------+------------------+8. How to create a MySQL Database?  
  97. # MySQLadmin -u root -ptmppassword create testdb  
  98. # MySQL -u root -ptmppassword  
  99. Welcome to the MySQL monitor. Commands end with ; or \g.  
  100. Your MySQL connection id is 705  
  101. Server version: 5.1.25-rc-community MySQL Community Server (GPL)  
  102. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  
  103. MySQL> show databases;  
  104. +--------------------+  
  105. | Database |  
  106. +--------------------+  
  107. | information_schema |  
  108. | MySQL |  
  109. | sugarcrm |  
  110. | testdb |  
  111. +--------------------+  
  112. 4 rows in set (0.00 sec)  
  113. Note: To display all tables in a database, total number of columns, row, column types, indexes etc., use the MySQLshow command that we discussed in our previous articles.  
  114. 9. How to Delete/Drop an existing MySQL database?  
  115. # MySQLadmin -u root -ptmppassword drop testdb  
  116. Dropping the database is potentially a very bad thing to do.  
  117. Any data stored in the database will be destroyed.  
  118. Do you really want to drop the 'testdb' database [y/N] y  
  119. Database “testdb” dropped  
  120. # MySQL -u root -ptmppassword  
  121. Welcome to the MySQL monitor. Commands end with ; or \g.  
  122. Your MySQL connection id is 707  
  123. Server version: 5.1.25-rc-community MySQL Community Server (GPL)  
  124. Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.  
  125. MySQL> show databases;  
  126. +——————–+  
  127. | Database |  
  128. +——————–+  
  129. | information_schema |  
  130. | MySQL |  
  131. | sugarcrm |  
  132. +——————–+  
  133. 3 rows in set (0.00 sec)10. How to reload/refresh the privilege or the grants tables?  
  134. # MySQLadmin -u root -ptmppassword reload;Refresh command will flush all the tables and close/open log files.  
  135. # MySQLadmin -u root -ptmppassword refresh11. What is the safe method to shutdown the MySQL server?  
  136. # MySQLadmin -u root -ptmppassword shutdown  
  137. # MySQL -u root -ptmppassword  
  138. ERROR 2002 (HY000): Can't connect to local MySQL server  
  139. through socket '/var/lib/MySQL/MySQL.sock'Note: 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”  
  140. 12. List of all MySQLadmin flush commands.  
  141. # MySQLadmin -u root -ptmppassword flush-hosts  
  142. # MySQLadmin -u root -ptmppassword flush-logs  
  143. # MySQLadmin -u root -ptmppassword flush-privileges  
  144. # MySQLadmin -u root -ptmppassword flush-status  
  145. # MySQLadmin -u root -ptmppassword flush-tables  
  146. # MySQLadmin -u root -ptmppassword flush-threadsflush-hosts: Flush all information in the host cache.   
  147. flush-privileges: Reload the grant tables (same as reload).   
  148. flush-status: Clear status variables.   
  149. flush-threads: Flush the thread cache.   
  150. 13. How to kill a hanging MySQL Client Process?  
  151. First identify the hanging MySQL client process using the processlist command.  
  152. # MySQLadmin -u root -ptmppassword processlist  
  153. +----+------+-----------+----+---------+------+-------+------------------+  
  154. | Id | User | Host | db | Command | Time | State | Info |  
  155. +----+------+-----------+----+---------+------+-------+------------------+  
  156. | 20 | root | localhost | | Sleep | 64 | | |  
  157. | 24 | root | localhost | | Query | 0 | | show processlist |  
  158. +----+------+-----------+----+---------+------+-------+------------------+Now, use the kill command and pass the process_id as shown below. To kill multiple process you can pass comma separated process id’s.  
  159. # MySQLadmin -u root -ptmppassword kill 20  
  160. # MySQLadmin -u root -ptmppassword processlist  
  161. +----+------+-----------+----+---------+------+-------+------------------+  
  162. | Id | User | Host | db | Command | Time | State | Info |  
  163. +----+------+-----------+----+---------+------+-------+------------------+  
  164. | 26 | root | localhost | | Query | 0 | | show processlist |  
  165. +----+------+-----------+----+---------+------+-------+------------------+14. How to start and stop MySQL replication on a slave server?  
  166. # MySQLadmin -u root -ptmppassword stop-slave  
  167. Slave stopped  
  168. # MySQLadmin -u root -ptmppassword start-slave  
  169. MySQLadmin: Error starting slave: The server is not configured as slave;  
  170. fix in config file or with CHANGE MASTER TO15. How to combine multiple MySQLadmin commands together?  
  171. In the example below, you can combine process-list, status and version command to get all the output together as shown below.  
  172. # MySQLadmin -u root -ptmppassword process status version  
  173. +----+------+-----------+----+---------+------+-------+------------------+  
  174. | Id | User | Host | db | Command | Time | State | Info |  
  175. +----+------+-----------+----+---------+------+-------+------------------+  
  176. | 43 | root | localhost | | Query | 0 | | show processlist |  
  177. +----+------+-----------+----+---------+------+-------+------------------+  
  178. Uptime: 3135  
  179. Threads: 1 Questions: 80 Slow queries: 0 Opens: 15 Flush tables: 3  
  180. Open tables: 0 Queries per second avg: 0.25  
  181. MySQLadmin Ver 8.42 Distrib 5.1.25-rc, for redhat-linux-gnu on i686  
  182. Copyright (C) 2000-2006 MySQL AB  
  183. This software comes with ABSOLUTELY NO WARRANTY. This is free software,  
  184. and you are welcome to modify and redistribute it under the GPL license  
  185. Server version 5.1.25-rc-community  
  186. Protocol version 10  
  187. Connection Localhost via UNIX socket  
  188. UNIX socket /var/lib/MySQL/MySQL.sock  
  189. Uptime: 52 min 15 secYou can also use the short form as shown below:  
  190. # MySQLadmin -u root -ptmppassword pro stat verUse the option -h, 
    to connect to a remote MySQL server and execute the MySQLadmin commands as shown below.  
  191. # MySQLadmin -h 192.168.1.112 -u root -ptmppassword pro stat ver  

 

 

上述的相关内容就是对15个MySQL使用管理命令的描述,希望会给你带来一些帮助在此方面。

原文标题:15个MySQL使用管理命令

连接:http://www.cnblogs.com/alon/archive/2010/01/21/1652849.html

【编辑推荐】

  1. 在Linux下安装MySQL 和配置 MySQL实战演习
  2. .NET访问MySQL数据库的经验漫谈
  3. Java连接MYSQL 数据库的连接步骤
  4. MySQL存储过程的创建步骤描述
  5. 忘记MySQLroot密码的解决方案

 

责任编辑:佚名 来源: 博客园
相关推荐

2013-10-08 16:24:34

Linux find命

2013-10-08 15:51:03

Linux find命

2015-03-25 10:36:57

lsLinux

2020-05-20 13:45:52

Linuxtouch命令

2020-04-16 14:20:16

Linuxtouch命令

2021-11-24 22:57:23

MySQLSQL数据库

2024-01-18 15:34:58

Windows操作系统

2011-08-15 22:51:34

Oraclerman

2022-07-06 23:59:57

NumPyPython工具

2023-10-30 09:02:24

2010-06-01 18:26:33

Rsync 使用

2021-07-11 12:06:07

Linux Date命令

2021-09-14 10:55:16

LinuxDate命令

2011-03-03 15:54:27

MySQL数据库管理

2010-05-19 18:23:34

2010-10-08 11:00:40

常用mysql命令

2014-07-29 14:25:43

Unix命令

2018-10-26 14:10:21

2010-05-25 09:40:01

MySQL数据库

2016-09-27 13:12:14

tar命令示例 Unix
点赞
收藏

51CTO技术栈公众号