当前位置:   article > 正文

不可不知的 MySQL Client Program_mysql client programs

mysql client programs

 Executing SQL Statements Interactively

  1. [root@MaxwellDBA ~]# mysql -u root -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 184
  5. Server version: 8.0.26 Source distribution
  6. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> CREATE USER 'cbuser'@'localhost' IDENTIFIED BY 'cbpass';
  12. Query OK, 0 rows affected (0.01 sec)
  13. mysql> GRANT ALL ON cookbook.* TO 'cbuser'@'localhost';
  14. Query OK, 0 rows affected (0.00 sec)
  15. mysql> GRANT PROCESS ON *.* to `cbuser`@`localhost` ;
  16. Query OK, 0 rows affected (0.01 sec)
  17. mysql> quit
  18. Bye
  19. [root@MaxwellDBA ~]#
  1. [root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 185
  5. Server version: 8.0.26 Source distribution
  6. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> show databases;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | cookbook |
  16. | information_schema |
  17. +--------------------+
  18. 2 rows in set (0.01 sec)
  19. mysql> use cookbook;
  20. Reading table information for completion of table and column names
  21. You can turn off this feature to get a quicker startup with -A
  22. Database changed
  23. mysql> show tables;
  24. +-----------------------+
  25. | Tables_in_cookbook |
  26. +-----------------------+
  27. | actors |
  28. | adcount |
  29. | al_winner |
  30. | app_log |
  31. | artist |
  32. | book_authors |
  33. | book_vendor |
  34. | booksales |
  35. | catalog_list |
  36. | cd |
  37. | city |
  38. | color |
  39. | cow_color |
  40. | cow_order |
  41. | date_val |
  42. | datetbl |
  43. | datetime_val |
  44. | die |
  45. | doremi |
  46. | drawing |
  47. | driver_log |
  48. | expt |
  49. | formula1 |
  50. | goods_characteristics |
  51. | goods_shops |
  52. | groceries |
  53. | groceries_order_items |
  54. | hitcount |
  55. | hitlog |
  56. | hostip |
  57. | hostname |
  58. | housewares |
  59. | housewares2 |
  60. | housewares3 |
  61. | housewares4 |
  62. | httpdlog |
  63. | httpdlog2 |
  64. | hw_category |
  65. | image |
  66. | ingredient |
  67. | insect |
  68. | inv_item |
  69. | invoice |
  70. | item |
  71. | limbs |
  72. | mail |
  73. | marathon |
  74. | mark_log |
  75. | metal |
  76. | money |
  77. | movies |
  78. | movies_actors |
  79. | movies_actors_link |
  80. | name |
  81. | news |
  82. | newsstaff |
  83. | numbers |
  84. | obs |
  85. | occasion |
  86. | painting |
  87. | passtbl |
  88. | passwd |
  89. | patients |
  90. | perl_session |
  91. | person |
  92. | php_session |
  93. | phrase |
  94. | player_stats |
  95. | player_stats2 |
  96. | poi |
  97. | poll_vote |
  98. | profile |
  99. | profile_contact |
  100. | rainfall |
  101. | rand_names |
  102. | rank |
  103. | ranks |
  104. | reviews |
  105. | roster |
  106. | ruby_session |
  107. | sales_region |
  108. | sales_tax_rate |
  109. | sales_volume |
  110. | sibling |
  111. | standings1 |
  112. | standings2 |
  113. | states |
  114. | str_val |
  115. | sundays |
  116. | testscore |
  117. | testscore_withmisses |
  118. | testscore_withmisses2 |
  119. | time_val |
  120. | tmp |
  121. | tomcat_role |
  122. | tomcat_session |
  123. | tomcat_user |
  124. | top_names |
  125. | trip_leg |
  126. | trip_log |
  127. | tsdemo |
  128. | weatherdata |
  129. | weekday |
  130. +-----------------------+
  131. 103 rows in set (0.00 sec)
  132. mysql> select * from limbs;
  133. +--------------+------+------+
  134. | thing | legs | arms |
  135. +--------------+------+------+
  136. | armchair | 4 | 2 |
  137. | centipede | 99 | 0 |
  138. | fish | 0 | 0 |
  139. | human | 2 | 2 |
  140. | insect | 6 | 0 |
  141. | Peg Leg Pete | 1 | 2 |
  142. | phonograph | 0 | 1 |
  143. | space alien | NULL | NULL |
  144. | squid | 0 | 10 |
  145. | table | 4 | 0 |
  146. | tripod | 3 | 0 |
  147. +--------------+------+------+
  148. 11 rows in set (0.00 sec)
  149. mysql> select NOW();
  150. +---------------------+
  151. | NOW() |
  152. +---------------------+
  153. | 2022-10-13 17:04:33 |
  154. +---------------------+
  155. 1 row in set (0.00 sec)
  156. mysql> select
  157. -> NOW()\g
  158. +---------------------+
  159. | NOW() |
  160. +---------------------+
  161. | 2022-10-13 17:05:00 |
  162. +---------------------+
  163. 1 row in set (0.00 sec)
  164. mysql> show full columns from limbs like 'thing'\G
  165. *************************** 1. row ***************************
  166. Field: thing
  167. Type: varchar(20)
  168. Collation: utf8mb4_0900_ai_ci
  169. Null: NO
  170. Key: PRI
  171. Default: NULL
  172. Extra:
  173. Privileges: select,insert,update,references
  174. Comment:
  175. 1 row in set (0.00 sec)
  176. mysql>

2.Controlling mysql Output Destination and Format.

Producing tabular or tab-delimited output

  1. [root@MaxwellDBA ~]# echo "select * from limbs where legs=0" | mysql -h localhost -u cbuser -p cookbook
  2. Enter password:
  3. thing legs arms
  4. fish 0 0
  5. phonograph 0 1
  6. squid 0 10
  7. [root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p cookbook
  8. Enter password:
  9. Reading table information for completion of table and column names
  10. You can turn off this feature to get a quicker startup with -A
  11. Welcome to the MySQL monitor. Commands end with ; or \g.
  12. Your MySQL connection id is 199
  13. Server version: 8.0.26 Source distribution
  14. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  15. Oracle is a registered trademark of Oracle Corporation and/or its
  16. affiliates. Other names may be trademarks of their respective
  17. owners.
  18. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  19. mysql> select * from limbs where legs=0;
  20. +------------+------+------+
  21. | thing | legs | arms |
  22. +------------+------+------+
  23. | fish | 0 | 0 |
  24. | phonograph | 0 | 1 |
  25. | squid | 0 | 10 |
  26. +------------+------+------+
  27. 3 rows in set (0.00 sec)
  28. mysql> quit
  29. Bye
  30. [root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p cookbook -e "select * from limbs where legs=4"|sed -e "s/table/squid/g"
  31. Enter password:
  32. thing legs arms
  33. armchair 4 2
  34. squid 4 0
  35. [root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p cookbook -e "select * from limbs where legs=4"|sed -e "s/table/XXXXX/g"
  36. Enter password:
  37. thing legs arms
  38. armchair 4 2
  39. XXXXX 4 0
  40. [root@MaxwellDBA ~]#

Producing HTML or XML output

mysql generate an HTML table from each query result set if you use the -H(or -html) option.

  1. [root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p -e "SELECT * FROM limbs where legs=0" cookbook
  2. Enter password:
  3. +------------+------+------+
  4. | thing | legs | arms |
  5. +------------+------+------+
  6. | fish | 0 | 0 |
  7. | phonograph | 0 | 1 |
  8. | squid | 0 | 10 |
  9. +------------+------+------+
  10. [root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p -H -e "SELECT * FROM limbs where legs=0" cookbook
  11. Enter password:
  12. <TABLE BORDER=1><TR><TH>thing</TH><TH>legs</TH><TH>arms</TH></TR><TR><TD>fish</TD><TD>0</TD><TD>0</TD></TR><TR><TD>phonograph</TD><TD>0</TD><TD>1</TD></TR><TR><TD>squid</TD><TD>0</TD><TD>10</TD></TR></TABLE>[root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p -H -e "SELECT * FROM limbs where legs=0" cookbook > limbs.xhtml
  13. Enter password:
  14. [root@MaxwellDBA ~]# ll
  15. total 4
  16. -rw-r--r-- 1 root root 207 Oct 13 18:11 limbs.xhtml
  17. [root@MaxwellDBA ~]# open -a safari limbs.xhtml
  18. open: invalid option -- 'a'
  19. Usage: open [OPTIONS] -- command
  20. This utility help you to start a program on a new virtual terminal (VT).
  21. Options:
  22. -c, --console=NUM use the given VT number;
  23. -e, --exec execute the command, without forking;
  24. -f, --force force opening a VT without checking;
  25. -l, --login make the command a login shell;
  26. -u, --user figure out the owner of the current VT;
  27. -s, --switch switch to the new VT;
  28. -w, --wait wait for command to complete;
  29. -v, --verbose print a message for each action;
  30. -V, --version print program version and exit;
  31. -h, --help output a brief help message.
  32. [root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p -X -e "SELECT * FROM limbs where legs=0" cookbook
  33. Enter password:
  34. <?xml version="1.0"?>
  35. <resultset statement="SELECT * FROM limbs where legs=0
  36. " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  37. <row>
  38. <field name="thing">fish</field>
  39. <field name="legs">0</field>
  40. <field name="arms">0</field>
  41. </row>
  42. <row>
  43. <field name="thing">phonograph</field>
  44. <field name="legs">0</field>
  45. <field name="arms">1</field>
  46. </row>
  47. <row>
  48. <field name="thing">squid</field>
  49. <field name="legs">0</field>
  50. <field name="arms">10</field>
  51. </row>
  52. </resultset>
  53. [root@MaxwellDBA ~]#

Using User-Defined Variables in SQL Statements

  1. [root@MaxwellDBA ~]# mysql -h localhost -u cbuser -p cookbook
  2. Enter password:
  3. Reading table information for completion of table and column names
  4. You can turn off this feature to get a quicker startup with -A
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 210
  7. Server version: 8.0.26 Source distribution
  8. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql> select database();
  14. +------------+
  15. | database() |
  16. +------------+
  17. | cookbook |
  18. +------------+
  19. 1 row in set (0.00 sec)
  20. mysql> select max(arms+legs) into @max_limbs from limbs;
  21. Query OK, 1 row affected (0.00 sec)
  22. mysql> select * from limbs where arms+legs=@max_limbs;
  23. +-----------+------+------+
  24. | thing | legs | arms |
  25. +-----------+------+------+
  26. | centipede | 99 | 0 |
  27. +-----------+------+------+
  28. 1 row in set (0.00 sec)
  29. mysql> select @last_id:=last_insert_id();
  30. +----------------------------+
  31. | @last_id:=last_insert_id() |
  32. +----------------------------+
  33. | 0 |
  34. +----------------------------+
  35. 1 row in set, 1 warning (0.00 sec)
  36. mysql> select thing from limbs where legs=0;
  37. +------------+
  38. | thing |
  39. +------------+
  40. | fish |
  41. | phonograph |
  42. | squid |
  43. +------------+
  44. 3 rows in set (0.00 sec)
  45. mysql> select thing into @name from limbs where legs=0;
  46. ERROR 1172 (42000): Result consisted of more than one row
  47. mysql> select @name;
  48. +-------+
  49. | @name |
  50. +-------+
  51. | fish |
  52. +-------+
  53. 1 row in set (0.00 sec)
  54. mysql> select thing into @name2 from limbs where legs<0;
  55. Query OK, 0 rows affected, 1 warning (0.00 sec)
  56. mysql> show warnings;
  57. +---------+------+-----------------------------------------------------+
  58. | Level | Code | Message |
  59. +---------+------+-----------------------------------------------------+
  60. | Warning | 1329 | No data - zero rows fetched, selected, or processed |
  61. +---------+------+-----------------------------------------------------+
  62. 1 row in set (0.00 sec)
  63. mysql> select @name2;
  64. +----------------+
  65. | @name2 |
  66. +----------------+
  67. | NULL |
  68. +----------------+
  69. 1 row in set (0.00 sec)
  70. mysql> SET @sum= 4 + 7;
  71. Query OK, 0 rows affected (0.00 sec)
  72. mysql> select @sum;
  73. +------+
  74. | @sum |
  75. +------+
  76. | 11 |
  77. +------+
  78. 1 row in set (0.00 sec)
  79. mysql> SET @max_limbs=(SELECT MAX(arms+legs) FROM limbs);
  80. Query OK, 0 rows affected (0.00 sec)
  81. mysql> SET @x=1,@X=2;SELECT @x,@X;
  82. Query OK, 0 rows affected (0.00 sec)
  83. +------+------+
  84. | @x | @X |
  85. +------+------+
  86. | 2 | 2 |
  87. +------+------+
  88. 1 row in set (0.00 sec)
  89. mysql>

Using External Programs

  1. mysql>
  2. mysql> select @@datadir;
  3. +-----------------+
  4. | @@datadir |
  5. +-----------------+
  6. | /var/lib/mysql/ |
  7. +-----------------+
  8. 1 row in set (0.00 sec)
  9. mysql> system ls /var/lib/mysql/
  10. auto.cnf binlog.000003 ca-key.pem client-key.pem ib_buffer_pool ibtmp1 mysql.ibd mysqlx.sock public_key.pem undo_001
  11. backup binlog.000004 ca.pem cookbook ibdata1 '#innodb_temp' mysql.sock mysqlx.sock.lock server-cert.pem undo_002
  12. binlog.000001 binlog.000005 carrefour '#ib_16384_0.dblwr' ib_logfile0 menagerie mysql.sock.lock performance_schema server-key.pem
  13. binlog.000002 binlog.index client-cert.pem '#ib_16384_1.dblwr' ib_logfile1 mysql mysql_upgrade_info private_key.pem sys
  14. mysql>

Filtering and Processing Output

  1. mysql>
  2. mysql>
  3. mysql> pager less -F -X
  4. PAGER set to 'less -F -X'
  5. mysql> SELECT * FROM city;
  6. +----------------+----------------+----------------+
  7. | state | capital | largest |
  8. +----------------+----------------+----------------+
  9. | Alabama | Montgomery | Birmingham |
  10. | Alaska | Juneau | Anchorage |
  11. | Arizona | Phoenix | Phoenix |
  12. | Arkansas | Little Rock | Little Rock |
  13. | California | Sacramento | Los Angeles |
  14. | Colorado | Denver | Denver |
  15. | Connecticut | Hartford | Bridgeport |
  16. | Delaware | Dover | Wilmington |
  17. | Florida | Tallahassee | Jacksonville |
  18. | Georgia | Atlanta | Atlanta |
  19. | Hawaii | Honolulu | Honolulu |
  20. | Idaho | Boise | Boise |
  21. | Illinois | Springfield | Chicago |
  22. | Indiana | Indianapolis | Indianapolis |
  23. | Iowa | Des Moines | Des Moines |
  24. | Kansas | Topeka | Wichita |
  25. | Kentucky | Frankfort | Louisville |
  26. | Louisiana | Baton Rouge | New Orleans |
  27. | Maine | Augusta | Portland |
  28. | Maryland | Annapolis | Baltimore |
  29. | Massachusetts | Boston | Boston |
  30. | Michigan | Lansing | Detroit |
  31. | Minnesota | St. Paul | Minneapolis |
  32. | Mississippi | Jackson | Jackson |
  33. | Missouri | Jefferson City | Kansas City |
  34. | Montana | Helena | Billings |
  35. | Nebraska | Lincoln | Omaha |
  36. | Nevada | Carson City | Las Vegas |
  37. | New Hampshire | Concord | Manchester |
  38. | New Jersey | Trenton | Newark |
  39. | New Mexico | Santa Fe | Albuquerque |
  40. | New York | Albany | New York City |
  41. | North Carolina | Raleigh | Charlotte |
  42. | North Dakota | Bismarck | Fargo |
  43. | Ohio | Columbus | Columbus |
  44. | Oklahoma | Oklahoma City | Oklahoma City |
  45. | Oregon | Salem | Portland |
  46. | Pennsylvania | Harrisburg | Philadelphia |
  47. | Rhode Island | Providence | Providence |
  48. | South Carolina | Columbia | Columbia |
  49. | South Dakota | Pierre | Sioux Falls |
  50. | Tennessee | Nashville | Memphis |
  51. | Texas | Austin | Houston |
  52. | Utah | Salt Lake City | Salt Lake City |
  53. | Vermont | Montpelier | Burlington |
  54. | Virginia | Richmond | Virginia Beach |
  55. | Washington | Olympia | Seattle |
  56. | West Virginia | Charleston | Charleston |
  57. | Wisconsin | Madison | Milwaukee |
  58. | Wyoming | Cheyenne | Cheyenne |
  59. +----------------+----------------+----------------+
  60. 50 rows in set (0.01 sec)
  61. mysql> SELECT * FROM movies;
  62. +----+------+----------------------------+
  63. | id | year | movie |
  64. +----+------+----------------------------+
  65. | 1 | 1997 | The Fifth Element |
  66. | 2 | 1999 | The Phantom Menace |
  67. | 3 | 2001 | The Fellowship of the Ring |
  68. | 4 | 2005 | Kingdom of Heaven |
  69. | 5 | 2010 | Red |
  70. | 6 | 2011 | Unknown |
  71. +----+------+----------------------------+
  72. 6 rows in set (0.00 sec)
  73. mysql> \P grep "History list length"
  74. PAGER set to 'grep "History list length"'
  75. mysql> SHOW ENGINE INNODB STATUS\G
  76. History list length 0
  77. 1 row in set (0.00 sec)
  78. mysql> SELECT SLEEP(60);
  79. 1 row in set (1 min 0.00 sec)
  80. mysql> nopager
  81. PAGER set to stdout
  82. mysql> pager md5sum
  83. PAGER set to 'md5sum'
  84. mysql> select 'Output of this statement is a bash';
  85. ecff0c81accc3be98d2190f8b9098052 -
  86. 1 row in set (0.00 sec)
  87. mysql> pager cat> /dev/null
  88. PAGER set to 'cat> /dev/null'
  89. mysql> select 'Output of this statement goes to nowhere';
  90. 1 row in set (0.00 sec)
  91. mysql> pager
  92. Default pager wasn't set, using stdout.
  93. mysql> select 'Output of this statement is visible';
  94. +-------------------------------------+
  95. | Output of this statement is visible |
  96. +-------------------------------------+
  97. | Output of this statement is visible |
  98. +-------------------------------------+
  99. 1 row in set (0.00 sec)
  100. mysql> select * from limbs;
  101. +--------------+------+------+
  102. | thing | legs | arms |
  103. +--------------+------+------+
  104. | armchair | 4 | 2 |
  105. | centipede | 99 | 0 |
  106. | fish | 0 | 0 |
  107. | human | 2 | 2 |
  108. | insect | 6 | 0 |
  109. | Peg Leg Pete | 1 | 2 |
  110. | phonograph | 0 | 1 |
  111. | space alien | NULL | NULL |
  112. | squid | 0 | 10 |
  113. | table | 4 | 0 |
  114. | tripod | 3 | 0 |
  115. +--------------+------+------+
  116. 11 rows in set (0.00 sec)
  117. mysql>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/536478
推荐阅读
相关标签
  

闽ICP备14008679号