资讯专栏INFORMATION COLUMN

比CRUD多一点儿(一):MySQL常用命令

darry / 1702人阅读

摘要:比多一点儿一常用命令这是系列笔记的第一部分,本系列笔记希望能按照笔者自己学习技术的经历来记录,避免纯粹按照内容一块一块总结,也就是不同于一般按内容分配章节的书籍的结构,有一个平滑的阅读曲线。第一篇的主要内容会是最常用的一些命令。

比CRUD多一点儿(一):MySQL常用命令

这是MySQL系列笔记的第一部分,本系列笔记希望能按照笔者自己学习MySQL技术的经历来记录,避免纯粹按照内容一块一块总结,也就是不同于一般按内容分配章节的书籍的结构,有一个平滑的阅读曲线。内容比较丰富的技术点会按照专题在多个学习笔记中逐渐深入。文章列表点此。

首先,请如果还完全为接触过MySQL的读者先阅读w3c的SQL教程,再来读本篇内容。

这部分的标题叫比CRUD多一丁点儿,比起最基础的w3c的SQL教程之外,只多一点的扩展,满足应付从纯粹阅读入门资料到可以上手完成一个简单的工作的需求。

第一篇的主要内容会是最常用的一些mysql命令。因为虽然有很多图形化的工具,但在实际的工作中因为需要去线上服务查看数据,处理问题,字符界面的命令行工具是必不可少的。

客户端程序mysql: 连接交互式终端

</>复制代码

  1. mysql -h $host -P $port -u $user -p$passsword $database_name

例如用户root使用密码mydb@xxx用链接到地址为192.168.1.99,端口为3306的数mysql进程,并默认使用上名为testdb的数据库(即自动执行use testdb)。

</>复制代码

  1. mysql -h 192.168.1.99 -P 3306 -u"root" -p"mydb@xx" testdb

各参数如果有@,&等bash的关键字,则需要用""引起来。

非交互式的执行SQL

有时候需要在命令行执行某句SQL,则建议使用 -Bse 参数。-B参数的含义是执行多条语句(batch)这样可以执行多条,-e即是执行(execute),-s参数意思是静默执行(silent)可以让输出格式精简一些。

</>复制代码

  1. mysql -h $host -P $port -u $user -p$passsword -Bse "command1;command2;....;commandn"

例如,常见的将执行结果导出到文件中方便留存和阅读。

</>复制代码

  1. mysql -h 192.168.1.99 -P 3306 -u"root" -p"mydb@xx" -Bse "select id,name from testdb.Account;" > result.txt
导出程序mysqldump

mysqldump是做数据导出的命令行工具,也是mysql安装后默认会带的。作用是将mysql数据库中的数据导出出来。

导出特定的表的内容

</>复制代码

  1. mysql -h $host -P $port -u $user -p$passsword $database_name $table1 $table2 ...

例如

</>复制代码

  1. mysqldump -h 192.168.1.99 -P 3306 -u"root" -p"mydb@xx" mydb table1 table2 > result.sql
只导出表结构

使用--no-data参数只导出表结构,

</>复制代码

  1. mysqldump -h 192.168.1.99 -P 3306 -u"root" -p"mydb@xx" --no-data mydb > result.sql
比较表结构mysqldiff

在开发实践中难免会遇到校验数据表结构不同,或者根据开发环境和目标环境的表结构不同来生成对应的表结构修改语句。mysql在5.7版本就提供了一个自带的mysqldiff工具。参数比较多,直接举例说明生成difftype=sql的ALTER语句的命令写法,如下:

</>复制代码

  1. mysqldiff --server1=root@host1 --server2=root@host2
  2. --show-reverse --difftype=sql
  3. db1.table1:dbx.table3
  4. # server1 on host1: ... connected.
  5. # server2 on host2: ... connected.
  6. # Comparing db1.table1 to dbx.table3 [FAIL]
  7. # Transformation statements:
  8. # --destination=server1:
  9. ALTER TABLE db1.table1
  10. ADD COLUMN notes char(30) AFTER a,
  11. CHANGE COLUMN misc misc char(55);
  12. # --destination=server2:
  13. # ALTER TABLE dbx.table3
  14. # DROP COLUMN notes,
  15. # CHANGE COLUMN misc misc char(30);

具体看mysql的官方文档:https://dev.mysql.com/doc/mys...

如果在mysql5.7版本以下就使用mysqldump命令参数输出简洁的表结构,随后diff文件然后自行编写ALTER语句吧。

</>复制代码

  1. mysqldump --skip-comments --skip-extended-insert -u root -p dbName1>file1.sql
  2. mysqldump --skip-comments --skip-extended-insert -u root -p dbName2>file2.sql
  3. diff file1.sql file2.sql
内建?, s命令

心急火燎的进入mysql终端处理线上问题,这时候一个语法拼不对,还得切出去查手册?万一环境是内网不能上网就更是麻烦,其实mysql内建了帮助手册,可以直接在终端查询。

? 是交互式mysql终端内建的帮助命令。可以按照此帮助查阅文档等。其输出如下

</>复制代码

  1. mysql> ?
  2. For information about MySQL products and services, visit:
  3. http://www.mysql.com/
  4. For developer information, including the MySQL Reference Manual, visit:
  5. http://dev.mysql.com/
  6. To buy MySQL Enterprise support, training, or other products, visit:
  7. https://shop.mysql.com/
  8. List of all MySQL commands:
  9. Note that all text commands must be first on line and end with ";"
  10. ? (?) Synonym for `help".
  11. clear (c) Clear the current input statement.
  12. connect (
  13. ) Reconnect to the server. Optional arguments are db and host.
  14. delimiter (d) Set statement delimiter.
  15. edit (e) Edit command with $EDITOR.
  16. ego (G) Send command to mysql server, display result vertically.
  17. exit (q) Exit mysql. Same as quit.
  18. go (g) Send command to mysql server.
  19. help (h) Display this help.
  20. nopager (
  21. ) Disable pager, print to stdout.
  22. notee (
  23. ) Don"t write into outfile.
  24. pager (P) Set PAGER [to_pager]. Print the query results via PAGER.
  25. print (p) Print current command.
  26. prompt (R) Change your mysql prompt.
  27. quit (q) Quit mysql.
  28. rehash (#) Rebuild completion hash.
  29. source (.) Execute an SQL script file. Takes a file name as an argument.
  30. status (s) Get status information from the server.
  31. system (!) Execute a system shell command.
  32. tee (T) Set outfile [to_outfile]. Append everything into given outfile.
  33. use (u) Use another database. Takes database name as argument.
  34. charset (C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
  35. warnings (W) Show warnings after every statement.
  36. nowarning (w) Don"t show warnings after every statement.
  37. resetconnection(x) Clean session context.

s 可查看当前状态,版本,客户端ip,QPS等,!可以在mysql终端中执行shell命令。在是很多处理问题的时候终端界面一进入mysql的交互式终端,就找不到ip、端口等在bash中的信息了又需要频繁切出,这两个命令都非常有用。s输出如下:

</>复制代码

  1. mysql> s
  2. --------------
  3. mysql Ver 14.14 Distrib 5.7.12, for osx10.11 (x86_64) using EditLine wrapper
  4. Connection id: 2
  5. Current database: rizhiyi_system
  6. Current user: root@localhost
  7. SSL: Not in use
  8. Current pager: less
  9. Using outfile: ""
  10. Using delimiter: ;
  11. Server version: 5.7.12 MySQL Community Server (GPL)
  12. Protocol version: 10
  13. Connection: 127.0.0.1 via TCP/IP
  14. Server characterset: utf8
  15. Db characterset: utf8
  16. Client characterset: utf8
  17. Conn. characterset: utf8
  18. TCP port: 3306
  19. Uptime: 21 sec
  20. Threads: 1 Questions: 74 Slow queries: 0 Opens: 171 Flush tables: 1 Open tables: 164 Queries per second avg: 3.523
  21. --------------

? contents 可以看内建的帮助手册

</>复制代码

  1. mysql> ? contents
  2. You asked for help about help category: "Contents"
  3. For more information, type "help ", where is one of the following
  4. categories:
  5. Account Management
  6. Administration
  7. Compound Statements
  8. Data Definition
  9. Data Manipulation
  10. Data Types
  11. Functions
  12. Functions and Modifiers for Use with GROUP BY
  13. Geographic Features
  14. Help Metadata
  15. Language Structure
  16. Plugins
  17. Procedures
  18. Storage Engines
  19. Table Maintenance
  20. Transactions
  21. User-Defined Functions
  22. Utility

然后就可以继续查看子菜单帮助手册的内容,比如? Functions

</>复制代码

  1. mysql> ? Functions
  2. You asked for help about help category: "Functions"
  3. For more information, type "help ", where is one of the following
  4. categories:
  5. Bit Functions
  6. Comparison operators
  7. Control flow functions
  8. Date and Time Functions
  9. Encryption Functions
  10. Information Functions
  11. Logical operators
  12. Miscellaneous Functions
  13. Numeric Functions
  14. String Functions

? insert 帮助命令还可以直接按关键字进行模糊查询,如:

</>复制代码

  1. mysql> ? insert
  2. Name: "INSERT"
  3. Description:
  4. Syntax:
  5. INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
  6. [INTO] tbl_name
  7. [PARTITION (partition_name,...)]
  8. [(col_name,...)]
  9. {VALUES | VALUE} ({expr | DEFAULT},...),(...),...
  10. [ ON DUPLICATE KEY UPDATE
  11. col_name=expr
  12. [, col_name=expr] ... ]
  13. Or:
  14. INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
  15. [INTO] tbl_name
  16. [PARTITION (partition_name,...)]
  17. SET col_name={expr | DEFAULT}, ...
  18. [ ON DUPLICATE KEY UPDATE
  19. col_name=expr
  20. [, col_name=expr] ... ]
  21. Or:
  22. INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
  23. [INTO] tbl_name
  24. [PARTITION (partition_name,...)]
  25. [(col_name,...)]
  26. SELECT ...
  27. [ ON DUPLICATE KEY UPDATE
  28. col_name=expr
  29. [, col_name=expr] ... ]
  30. INSERT inserts new rows into an existing table. The INSERT ... VALUES
  31. and INSERT ... SET forms of the statement insert rows based on
  32. explicitly specified values. The INSERT ... SELECT form inserts rows
  33. selected from another table or tables. INSERT ... SELECT is discussed
  34. further in [HELP INSERT SELECT].
  35. When inserting into a partitioned table, you can control which
  36. partitions and subpartitions accept new rows. The PARTITION option
  37. takes a comma-separated list of the names of one or more partitions or
  38. subpartitions (or both) of the table. If any of the rows to be inserted
  39. by a given INSERT statement do not match one of the partitions listed,
  40. the INSERT statement fails with the error Found a row not matching the
  41. given partition set. See
  42. http://dev.mysql.com/doc/refman/5.7/en/partitioning-selection.html, for
  43. more information and examples.
  44. In MySQL 5.7, the DELAYED keyword is accepted but ignored by the
  45. server. See [HELP INSERT DELAYED], for the reasons for this.
  46. URL: http://dev.mysql.com/doc/refman/5.7/en/insert.html

本文中常用命令的更详细内容可看MySQL手册:
https://dev.mysql.com/doc/refman/5.7/en/programs-overview.html

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/17648.html

相关文章

  • CRUD(三):UPDATE、DELETE语句

    摘要:语句最基础的用法不再赘述。而这样删掉整张表的语句是不会清空自增值的。 showImg(https://segmentfault.com/img/bV1ybF?w=2216&h=1288); 这是MySQL系列笔记的第一部分,本系列笔记希望能按照笔者自己学习MySQL技术的经历来记录,避免纯粹按照内容一块一块总结,也就是不同于一般按内容分配章节的书籍的结构,会有一个平滑的阅读曲线。内容比...

    IntMain 评论0 收藏0

发表评论

0条评论

darry

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<