网站首页 > 技术文章 正文
mysql是连接数据库的客户端工具。
语法如下:
mysql [options] [database]
1.连接选项
--user=user_name, -u user_name:用于连接到服务器的MySQL帐户的用户名
--password[=password], -p[password]:用于连接服务器的MySQL帐户的密码
--host=host_name, -h host_name:连接到给定主机上的MySQL服务器
--port=port_num, -P port_num:对于TCP / IP连接,使用的端口号。
--socket=path, -S path:连接localhost,要使用的Unix套接字文件
连接到mysql服务器:
[root@localhost~]# mysql -u root -p -P 3306 -h localhost -S /var/lib/mysql/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
login-path 便捷登陆
--login-path=name 从.mylogin.cnf中读取登录路径选项
login-path是MySQL5.6开始支持的新特性。通过借助mysql_config_editor工具将登陆MySQL服务的认证信息加密保存在.mylogin.cnf文件(默认位于用户主目录) 。MySQL客户端工具可通过读取该加密文件连接MySQL,避免重复输入登录信息,避免敏感信息暴露。
配置login-path:
mysql_config_editor set --login-path=3306 --user=root --host=localhost --port=3306 --password
其中可配置项
-h,--host=name 添加host到登陆文件中
-G,--login-path=name 在登录文件中为login path添加名字(默认为client)
-p,--password 在登陆文件中添加密码(该密码会被mysql_config_editor自动加密)
-u,--user 添加用户名到登陆文件中
-S,--socket=name 添加sock文件路径到登陆文件中
-P,--port=name 添加登陆端口到登陆文件中
查看login-path配置
mysql_config_editor print --login-path=3306
查看所有login-path信息
mysql_config_editor print --all
删除login-path配置:
mysql_config_editor remove --login-path=3306
重置login-path配置:
mysql_config_editor reset --login-path=3306
使用login-path登录:
mysql --login-path=3306
mysql --login-path=3306 test
2.客户端字符集选项
--default-character-set=charset_name
用 charset_name作为客户端连接的默认字符集
配置客户端连接字符集:
a.在my.cnf的[mysql]组中配置default-character-set=charset_name
b.在mysql的命令行中添加--default-character-set=charset_name
c.在mysql客户端连接成功后执行 set names charset
示例
[root@localhost ~]# mysql -u root -p -P 3306 -h localhost -S /var/lib/mysql/mysql.sock --default-character-set=gbk
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
备注:
上面的三个gbk字符集是客户端选项设置的结果
3.执行选项
--execute=statement, -e statement
在mysql客户端执行SQL语句并退出,常用于批处理脚本
查看test表数据:
[root@localhost ~]# mysql -uroot -p -e "use test;select * from test;";
Enter password:
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
4.格式化选项
--vertical, -E 输出方式按照字段顺序竖着显示
--silent, -s 去掉mysql中的线条框显示
[root@localhost ~]# mysql -uroot -p -E -e "use test;select * from test;";
Enter password:
*************************** 1. row ***************************
id: 1
*************************** 2. row ***************************
id: 2
*************************** 3. row ***************************
id: 3
[root@localhost ~]# mysql -uroot -p -s -e "use test;select * from test;";
Enter password:
id
1
2
3
5.错误处理选项
--force, -f 即使发生SQL错误,也要继续。
--verbose, -v 显示详细模式
--show-warnings 显示警告
示例
[root@localhost ~]# more test.sql
truncate table test;
insert into test values(1);
insert into test values('a');
insert into test values(3);
[root@localhost ~]# mysql -uroot -p test < test.sql
Enter password:
ERROR 1366 (HY000) at line 3: Incorrect integer value: 'a' for column 'id' at row 1
[root@mysqlnode1 ~]# mysql -uroot -p -e "use test;select * from test;";
Enter password:
+----+
| id |
+----+
| 1 |
+----+
[root@localhost ~]# mysql -uroot -p test -f < test.sql
Enter password:
ERROR 1366 (HY000) at line 3: Incorrect integer value: 'a' for column 'id' at row 1
[root@localhost ~]# mysql -uroot -p -e "use test;select * from test;";
Enter password:
+----+
| id |
+----+
| 1 |
| 3 |
+----+
[root@localhost ~]# mysql -uroot -p test -f -v --show-warnings < test.sql
Enter password:
--------------
truncate table test
--------------
--------------
insert into test values(1)
--------------
--------------
insert into test values('a')
--------------
ERROR 1366 (HY000) at line 3: Incorrect integer value: 'a' for column 'id' at row 1
--------------
insert into test values(3)
--------------
[root@localhost ~]# mysql -uroot -p -e "use test;select * from test;";
Enter password:
+----+
| id |
+----+
| 1 |
| 3 |
+----+
猜你喜欢
- 2025-07-27 管理Linux不想用命令行?可以试试Webmin!最新CentOS8上体验一下
- 2025-07-27 fdisk命令详解(fdisk-l命令)
- 2025-07-27 【收藏】Linux服务器常用巡检命令
- 2024-10-27 Windows环境中Oracle数据库ORA-01034错误的处理过程
- 2024-10-27 超详细的centos7部署PG数据库命令行工具--pgcli教程
- 2024-10-27 Oracle里的执行计划——使用explain plan命令
- 2024-10-27 SQL知识——重要的命令 sql的重要性
- 2024-10-27 shell脚本中的 exec 命令 shell脚本里面执行命令
- 2024-10-27 基于centos7部署PG数据库命令行工具--pgcli
- 2024-10-27 Linux服务器常用巡检命令 linux服务器常用巡检命令有哪些
你 发表评论:
欢迎- 634℃几个Oracle空值处理函数 oracle处理null值的函数
- 626℃Oracle分析函数之Lag和Lead()使用
- 614℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 609℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 606℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 600℃【数据统计分析】详解Oracle分组函数之CUBE
- 588℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 574℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
-
- CVE-2025-30762|Oracle(java oracle)
- 低代码可能铲不掉“屎山”,但能让这个它更有「型」
- 科技大事件:新苹果手表可通过击掌或握手来传递信息
- 你的百万级上下文窗口大模型,可能并没有你想象中那么强
- DApp 开发中的安全测试(软件测试过程中安全测试的具体应用场景和测试思路)
- 盘点Java中最没用的知识⑧:这3个过时套路,你还在代码里硬撑?
- 机房硬件设备及Oracle数据库软件维护服务项目竞争性磋商公告
- 微软与甲骨文扩大合作关系,推出Oracle Database@Azure
- JPA实体类注解,看这篇就全会了(java实体类注解)
- Java反射机制最全详解(图文全面总结)
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- 前端获取当前时间 (50)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)