mysql客户端工具在/usr/local/mysql/bin目录下,常用的客户端有:mysql和mysqladmin
mysql命令的工作模式:交互式和脚本式
交互式:
交互式模式分为服务器端命令和客户端命令;
服务器端命令:
For server side help, type ‘help contents‘ mysql> help contents You asked for help about help category: "Contents" For more information, type ‘help <item>‘, where <item> is one of the following categories: Account Management Administration Compound Statements Data Definition Data Manipulation Data Types Functions Functions and Modifiers for Use with GROUP BY Geographic Features Help Metadata Language Structure Plugins Procedures Storage Engines Table Maintenance Transactions User-Defined Functions Utility
客户端命令:
mysql> help For information about MySQL products and services, visit: http://www.mysql.com/ For developer information, including the MySQL Reference Manual, visit: http://dev.mysql.com/ To buy MySQL Enterprise support, training, or other products, visit: https://shop.mysql.com/ List of all MySQL commands: Note that all text commands must be first on line and end with ‘;‘ ? (\?) Synonym for `help‘. clear (\c) Clear the current input statement. connect (\r) Reconnect to the server. Optional arguments are db and host. delimiter (\d) Set statement delimiter. edit (\e) Edit command with $EDITOR. ego (\G) Send command to mysql server, display result vertically. exit (\q) Exit mysql. Same as quit. go (\g) Send command to mysql server. help (\h) Display this help. nopager (\n) Disable pager, print to stdout. notee (\t) Don‘t write into outfile. pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. print (\p) Print current command. prompt (\R) Change your mysql prompt. quit (\q) Quit mysql. rehash (\#) Rebuild completion hash. source (\.) Execute an SQL script file. Takes a file name as an argument. status (\s) Get status information from the server. system (\!) Execute a system shell command. tee (\T) Set outfile [to_outfile]. Append everything into given outfile. use (\u) Use another database. Takes database name as argument. charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. warnings (\W) Show warnings after every statement. nowarning (\w) Don‘t show warnings after every statement.
脚本式:
脚本式模式的语法格式:mysql < /path/to/script.sql
mysql命令行选项:
-u: 指定登录用户
-p: 指定登录用户的密码
-h: 指定mysql服务器的主机名或ip
-U: 拒绝使用无where语句的update 和 delete命令
-P: 指定mysql服务器的侦听端口
-e: 指定要执行的命令
-E: 查询结果垂直输出(相当于\G)
-D: 指定登录使用的默认数据库
-C: 在服务器和客户端连接之间启用压缩
-T: 在exit时打印debug信息
-H: 查询结果以html格式输出
-X: 查询结果以xml格式输出
--prompt=value: 设置mysql交互式模式提示符(默认为mysql>)
--protocol=name: 指定使用的协议,name可以设置为{tcp|socket|pipe|memory}
mysql命令可以读取my.cnf中的配置选项,mysql默认读取顺序为:
/etc/mysql/my.cnf > /etc/my.cnf > ~/.my.cnf
对于相同的配置选项,以最后的配置文件有效
对于不同的配置选项,是三个文件的配置选项的并集
mysqladmin:
mysqladmin [OPTIONS] command
常用命令选项:
-h: 指定mysql服务器主机名或ip
-P: 指定mysql服务器侦听端口
-u: 指定登录的用户
-p: 指定用户的密码
-C: 启用压缩
-c: 指定mysqladmin的执行交数,常与选项-i配合使用
-i: 指定执行命令之间等待的时间
command:
create databasename: 创建数据库
drop databasename: 删除数据库(包括所有的表)
extended-status: 打印服务器的扩展状态(相当于执行show global status;)
debug: 找开调试日志,请写入error log中
flush-hosts: 清空主机相关的缓存,包括DNS缓存,因为连接错误次数过多而被拒绝的主机列表
flush-logs: 日志滚动
flush-status: 重置状态变量的值
flush-tables: 在手动lock之前执行此命令
flush-threads: 清空线程缓存
flush-privileges: 重新加载授权表(相关于reload)
kill id1,id2...idn: 杀死mysqld线程
ping: 测试mysqld状态
processlist: 查看线程列表
shutdown: 关闭mysqld
status: 查看mysqld状态
start-slave: 启动从服务器进程
stop-slave: 关闭从服务器进程
variables: 打印变量值