0
点赞
收藏
分享

微信扫一扫

MySQL Client链接实用options

简介

MySQL客户端是DBA常用的工具,交互式,短连接方式,直连对应的数据库,进而对数据库进行操作。遇到紧急情况,可以通过Control+C将中断当前语句。这里[client] Options提供了诸多功能,可以有效的提高处理效果。

下面来挖掘下(MySQL8.0版本)一些比较实用的功能,。

Client Options

–auto-rehash

使用mysql客户端登录的时候,可以读取表信息和列信息,自动补全。如:Linux命令行里,使用Tab键进行自动补全一样。

shell# mysql --help | grep auto
--auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
                      (Defaults to on; use --skip-auto-rehash to disable.)
  -A, --no-auto-rehash 
                      No automatic rehashing. One has to use 'rehash' to get
                      table and field completion. This gives a quicker start of
                      mysql and disables rehashing on reconnect.

==当配置补全参数登录的时,就需要数据库,表,字段信息全部加载到客户端对应的缓存里。这样导致卡顿慢的等问题,间接的给MySQL服务带来负面影响。所以实际运维场景中不建议使用这个参数。==

参数配置:

[mysql]
socket                             = /opt/data/mysql.sock
no-auto-rehash
#或登录的时候指定 -A方式
shell # mysql -uroot -p****** -A

看下实际效果:

shell #mysql -uroot -p****** --auto-rehash 
	Reading table information for completion of table and column names
	You can turn off this feature to get a quicker startup with -A

mysql> SELECT * FROM t1;
+----+--------------+------+------+-------+------+
| id | name         | age  | addr | addr1 | t0   |
+----+--------------+------+------+-------+------+
|  1 | CCC          |   10 | NULL | NULL  | NULL |
|  2 | BBB          |   10 | NULL | NULL  | NULL |
。。。
|  3 | AAA          |   10 | NULL | NULL  | NULL |
+----+--------------+------+------+-------+------+
10 rows in set (0.00 sec)
mysql> SELECT t1.id
    -> t1.   #这里可以按Tab键二次
t1.addr   t1.addr1  t1.age    t1.id     t1.name   t1.t0

–binary-as-hex

此选项,mysql将使用十六进制表示法(0xvalue)显示二进制数据。无论整体输出显示格式是表格、垂直、HTML还是XML,都会出现这种情况。从MySQL 8.0.19开始,当MySQL以交互模式运行时,默认情况下启用此选项。

#默认方式
shell# mysql -uroot -p****** 

mysql> SELECT CHAR(0x41), UNHEX('41');
+------------------------+--------------------------+
| CHAR(0x41)             | UNHEX('41')              |
+------------------------+--------------------------+
| 0x41                   | 0x41                     |
+------------------------+--------------------------+
1 row in set (0.00 sec)

#禁用binary-as-hex
shell# mysql -uroot -p****** --skip-binary-as-hex

mysql]> SELECT CHAR(0x41), UNHEX('41');
+------------+-------------+
| CHAR(0x41) | UNHEX('41') |
+------------+-------------+
| A          | A           |
+------------+-------------+
1 row in set (0.00 sec)

从上面对比可以看出,这个参数决定着客户端输出界面,是否能正常显示内容。

–database=db_name, -D db_name

指定要使用的数据库。因为某些用户是只有对对应的数据库有访问权限的。

shell# mysql -uroot -p****** -A  -D db1
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@localhost: 10:58:  [db1]>    #直接指导对应的数据库

–execute=statement, -e statement

执行语句并退出,可以是单个语句 或 多个语句。默认输出格式与–batch生成的格式类似。使用此选项,不会记录历史文件.mysql_history。

mysql# mysql -uroot -p****** -e "USE test; SHOW PROCESSLIST;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+------+-----------+------+---------+------+-----------+------------------+
| Id | User | Host      | db   | Command | Time | State     | Info             |
+----+------+-----------+------+---------+------+-----------+------------------+
| 19 | root | localhost | NULL | Query   |    0 | executing | show processlist |
+----+------+-----------+------+---------+------+-----------+------------------+

–batch

使用制表符作为列分隔符打印结果,每行都在新行上。使用此选项,不会记录历史文件.mysql_history。

mysql# mysql -uroot -p****** -e "USE test; SHOW PROCESSLIST;" --batch
mysql: [Warning] Using a password on the command line interface can be insecure.
Id	User	Host	db	Command	Time	State	Info
21	root	localhost	test	Query	0	executing	show processlist

–show-warnings

如果有警告,则在每条语句后显示警告。此选项适用于交互式和批处理模式。

mysql# mysql -uroot -p****** -e "SELECT 1; SHOW PROCESSLISTS; SELECT 2; " 
mysql: [Warning] Using a password on the command line interface can be insecure.
+---+
| 1 |
+---+
| 1 |
+---+

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'processlists' at line 1

备注:遇到错误就不会继续执行。前面的语句是执行成功的。客户端导入sql文件时,出错不想继续执行,就可以添加这个参数。

–html, -H / --xml, -X

生成HTML/XML并输出。可以直接生成html/XML代码。结合代码实现普遍的web方式展示。

shell# mysql -uroot -p123456 -H

mysql> show databases;
<TABLE BORDER=1><TR><TH>Database</TH></TR>
<TR><TD>db1</TD></TR>
<TR><TD>db2</TD></TR>
<TR><TD>db3</TD></TR>
<TR><TD>information_schema</TD></TR>
<TR><TD>mysql</TD></TR>
<TR><TD>performance_schema</TD></TR>
<TR><TD>worldb</TD></TR>
</TABLE>29 rows in set (0.00 sec)

shell# mysql -uroot -p****** -X

mysql> show databases;
<?xml version="1.0"?>
<resultset statement="show databases;" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>	<field name="Database">db1</field>   </row>
  <row>	<field name="Database">db2</field>  </row>
  <row>	<field name="Database">db3</field>  </row>
  <row>	<field name="Database">information_schema</field>  </row>
  <row>	<field name="Database">mysql</field>  </row>
  <row>	<field name="Database">performance_schema</field>  </row>
  <row>	<field name="Database">sys</field>  </row>
  <row>	<field name="Database">worldb</field>  </row>
</resultset>

–ignore-spaces, -i

允许在函数名和字符之间使用空格。这会导致内置函数名被视为保留字。跟sql-mode的IGNORE_SPACE同等效果。
比如:

shell# mysql -uroot -p******  -i

mysql> show variables like '%sql_mode%';
+---------------+-----------------------------------------------------------+
| Variable_name | Value                                                                                                           |
+---------------+-----------------------------------------------------------+
| sql_mode      | IGNORE_SPACE,STRICT_TRANS_TABLES,。。。
+---------------+-----------------------------------------------------------+
1 row in set (0.01 sec)

#count是关键字 需要使用间隔号
mysql>  CREATE TABLE count (i INT);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'count (i INT)' at line 1
mysql>  CREATE TABLE `count`(i INT);
Query OK, 0 rows affected (0.01 sec)

–line-numbers

写入错误的行号。用mysql客户端导入一些sql文件的候还是非常有用的。

shell# mysql -uroot -p******  --line-numbers <  b.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1062 (23000) at line 6: Duplicate entry '1' for key 'ttt.PRIMARY'
#显示第6行导入数据,主键冲突


shell# vim b.sql
  1 use db1;
  2 insert into ttt(id) values(1);
  3 insert into ttt(id) values(2);
  4 insert into ttt(id) values(3);
  5 insert into ttt(id) values(4);
~~6 insert into ttt(id) values(1);
  7 insert into ttt(id) values(5);
  8 

shell#  sed -n "6p"  b.sql
insert into ttt(id) values(1);

–select-limit --sql_safe_updates

SELECT语句返回的限制行数。这样有效的限制客户端返回行数过多,刷屏问题。

shell> mysql -uroot -p******  --select-limit=3 --safe-updates

mysql> select * from t1;
+----+------+------+------+-------+------+
| id | name | age  | addr | addr1 | t0   |
+----+------+------+------+-------+------+
|  1 | CCC  |   10 | NULL | NULL  | NULL |
|  2 | BBB  |   10 | NULL | NULL  | NULL |
+----+------+------+------+-------+------+

mysql> SHOW VARIABLES  WHERE Variable_name in('sql_safe_updates','sql_select_limit');
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| sql_safe_updates | ON    |
| sql_select_limit | 3     |
+------------------+-------+
2 rows in set (0.00 sec)

–reconnect

MySQL客户端登录时,如果与服务器的连接丢失,自动尝试重新连接。每次连接丢失时都会进行一次重新连接尝试。要限制重新连接行为,使用–skip-reconnect

mysql> show processlist;
ERROR 4031 (HY000): The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.
No connection. Trying to reconnect...
Connection id:    18
Current database: *** NONE ***

+----+------+-----------+------+---------+------+-----------+------------------+
| Id | User | Host      | db   | Command | Time | State     | Info             |
+----+------+-----------+------+---------+------+-----------+------------------+
| 18 | root | localhost | NULL | Query   |    0 | executing | show processlist |
+----+------+-----------+------+---------+------+-----------+------------------+
1 row in set (0.00 sec)

备注:==这里有可能执行的sql语句失效 或则 重新执行。有一定的隐患。== 比如:执行的语句因为断开链接再次执行。

–tee

将输出副本附加到给定文件。此选项仅在交互模式下有效

shell# mysql -uroot -p******  --tee /tmp/mysql.log
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
。。。
| performance_schema |
| mysql              |
| worldb             |
+--------------------+
29 rows in set (0.01 sec)

tee输出日志信息:

shell# cat /tmp/mysql.log
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@localhost: 12:17:  [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
。。。
| performance_schema |
| mysql              |
| worldb             |
+--------------------+
29 rows in set (0.00 sec)

–syslog, -j

该选项使mysql向系统日志记录语句。在Unix上,这是syslog;在Windows上,它是Windows事件日志。记录的消息显示的目标取决于系统。在Linux上,通常是/var/log/messages文件。

shell# mysql -uroot -p******  --syslog
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
。。。
| performance_schema |
| mysql              |
| worldb             |
+--------------------+
29 rows in set (0.01 sec)

系统日志记录信息:

shell# tail -f  /var/log/messages 
Mar 15 12:11:40 schouse MysqlClient[186056]: SYSTEM_USER:'kevindba', MYSQL_USER:'root', CONNECTION_ID:20, DB_SERVER:'--', DB:'--', QUERY:'exit'
Mar 15 12:11:52 schouse MysqlClient[186059]: SYSTEM_USER:'kevindba', MYSQL_USER:'root', CONNECTION_ID:21, DB_SERVER:'--', DB:'--', QUERY:'show databases;'

–pager[=command]

使用给定的命令对查询输出分页。如less、more、cat [> filename]等等。此选项仅适用于Unix,且仅适用于交互模式。

shell# mysql -uroot -p******   --pager=less
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
。。。
| performance_schema |
| mysql              |
| worldb             |
+--------------------+
(END)

–init-command=str

连接到服务器后要执行的SQL语句。如下面简单的用户登录记录。

shell]# mysql -uroot -p****** --init-command="insert into db1.login_tbl(login) values(user())"
mysql> select * from db1.login_tbl;
+----+----------------+---------------------+
| id | login          | create_time         |
+----+----------------+---------------------+
|  1 | root@localhost | 2023-03-21 10:50:42 |
+----+----------------+---------------------+
1 row in set (0.00 sec)

–quick, -q
迫使mysql每次从服务器检索一行结果,而不是检索整个结果集,并在显示之前将其缓冲到内存中。这是通过使用客户端/服务器库中的mysql_use_result() C API函数而不是mysql_store_result()返回结果集来完成的。如果遇到由于内存不足而无法运行大型结果集的问题,非常有效。

–no-defaults

不读取任何选项文件。主要是防止/etc/my.cnf文件下[mysql][client]配置信息,导致链接错误或则 一些限制。比如:sock配置,max_allowed_packet,buffer,用户等

–login-path

免密码登录方式,通过mysql_config_editor信息,从.mylogin.cnf读取登录路径选项。

–load-data-local-dir --local-infile

LOAD DATA相关命令,一个是读取LOCAL语句中命名的文件的目录,-infile启用或禁用功能

–password1、–password2和–password3

用于指定多个密码password1、password2和password3。对于使用C API的应用程序,mysql_options4() C API函数的新MYSQL_OPT_USER_PASSWORD选项启用了相同的功能。
这部分功能在MySQL8.0.27企业版现在支持使用智能卡、安全密钥和生物识别阅读器等设备对MySQL服务器进行身份验证。该认证方法基于FIDO (Fast Identity Online)标准有关。

参考:

https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html

举报

相关推荐

0 条评论