mysql(MySQL客户端连接工具)
在MySQL提供的工具中,DBA使用最频繁的莫过于mysql。这里的mysql不是指MySQL服务,也不是mysql数据库,而是连接数据库的客户端工具。类似于Oracle的sqlplus。
1 2 | 语法: mysql [options][database] |
options
是mysql的可用选项,一次可以写一个或者多个,甚至可以不写。database
表示连接的数据库,一次只能写一个或者不写,如果不写,在登录数据库后还需要使用use dbname
选择数据库。
mysql的选项通常有两种表达方式:
-
+选项单词的缩写+选项值**;--
+选项的完整单词+=
+选项的实际值;
1 2 3 | 例如: mysql -uroot -ppassword mysql --user=root --password=password |
1.连接选项
-u,--user=name 指定连接的用户名
-p,--password=password 指定连接密码
-h,--host=name 指定服务器IP或域名
-P,--port=3308 指定连接端口
在默认情况下,如果这些选项都不写,mysql将会使用'用户'@'localhost'和空密码连接本机的3306端口。空用户会在mysql安装完毕后自动生成,这也就是仅使用mysql命令就能连到数据库的原因。
如果客户端和服务器在用一台机器上,通常不需要指定-h选项,否则需要指定mysql服务所在的IP或主机名。如果不指定端口,默认连接到3306端口。示例如下:
1 2 3 4 5 6 7 8 9 10 11 | # mysql -h 10.10.200.202 -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 75520 Server version: 5.6.28 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 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. mysql> |
2.指定客户端字符集
--default-character-set=charset-name
为服务器的字符集选项。该选项可以配置在my.cnf的[mysqld]
组中,同样也可以作为客户端字符集选项,也可以配置在[mysql]
组中。在使用mysql命令登录数据库时,使用--default-character-set
选项可以指定客户端的字符集。例如,如果未使用--default-character-set
选项登录数据库时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # mysql -h 10.10.200.202 -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 75520 Server version: 5.6.28 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 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. mysql> show variables like 'chara%' ; +--------------------------+-----------------------------------------+ | Variable_name | Value | +--------------------------+-----------------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.6.28/share/charsets/ | +--------------------------+-----------------------------------------+ |
当使用--default-character-set
选项登录数据库时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # mysql -h 10.10.200.202 -uroot -p --default-character-set=utf8 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 75542 Server version: 5.6.28 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 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. mysql> show variables like 'chara%' ; +--------------------------+-----------------------------------------+ | Variable_name | Value | +--------------------------+-----------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.6.28/share/charsets/ | +--------------------------+-----------------------------------------+ 8 rows in set (0.01 sec) |
3.执行选项
-e,--execute=name
执行sql语句并退出
此选项可以直接在客户端执行SQL语句,而不用连接到MySQL数据库后再执行,对于一些脚本的执行,使用此法较为便利。可以使用此种方法连续执行多条SQL语句,语句之间用分号(;)分隔例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # mysql -uroot -p mysql -e "select host,user from user" Enter password: +--------------------+--------+ | host | user | +--------------------+--------+ | 10.10.200.201 | root | | 10.10.200.201 | zabbix | | 127.0.0.1 | root | | ::1 | root | | localhost | | | localhost | root | | localhost | zabbix | | tcxx-ops-mysql-202 | | | tcxx-ops-mysql-202 | root | +--------------------+--------+ |
4.格式化选项
-E,--vertical
将输出按字段顺序垂直显示 -s,--silent
去掉SQL输出结果中的线条框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # mysql -uroot -p mysql -e "select host,user from user" -E Enter password: *************************** 1. row *************************** host: 127.0.0.1 user: root *************************** 2. row *************************** host: ::1 user: root *************************** 3. row *************************** host: localhost user: *************************** 4. row *************************** host: localhost user: root *************************** 5. row *************************** host: test-server user: *************************** 6. row *************************** host: test-server user: root |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧