Mysql 命令行 tips.md

128 阅读1分钟

作为一名命令行爱好者分享以下几点 tips

  1. ~/.my.cnf 中配置如下参数可实现免输入账号密码直接进入数据库
 [client]
 host=localhost
 user=root
 password=

进入终端输入 mysql

 xxx➜~(master)» mysql                                                                                                               [23:34:53]
 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 Your MariaDB connection id is 4
 Server version: 10.1.9-MariaDB Homebrew
 ​
 Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
 ​
 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 ​
 MariaDB [(none)]>
  1. 利用别名机制省略 use database;
 1. vi ~/.zshrc 或者~/.bashrc
 2. alias xxxdb='mysql xxx'
 3. source ~/.zshrc 或者 source ~/.bashrc
  1. 在终端直接输入 xxxdb 就可以进入当前选择的数据库
 xxx➜~(master)» in4db                                       [17:47:47]
 Reading table information for completion of table and column names
 You can turn off this feature to get a quicker startup with -A
 ​
 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 Your MariaDB connection id is 49
 Server version: 10.1.9-MariaDB Homebrew
 ​
 Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
 ​
 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 ​
 MariaDB [in4]>
  1. mysql 常用命令 上/下键:调出上一个/下一个输入的命令 crtl+A:把光标移到输入行的开头 crtl+E:把光标移到输入航的末尾 crtl+K:删除从光标位置到输入行末尾的内容 crtl+l:清屏
  2. 对于常用的查询可以写成 sql 脚本在命令行运行 如:mysql db < xxx.sql 默认情况下输出内容以制表符分隔,使用 -t 参数可以格式化输出。 如:mysql -t db < xxx.sql 如果想把输出结果保存起来,可以重定向至一个文件 如:mysql -t db < xxx.sql > xxx.out 如果你已经运行了 mysql,可以通过 source 来运行 sql 如:surce xxx.sql