网站首页 > 开源技术 正文
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 |
+----+
猜你喜欢
- 2024-10-24 从零开始学MySQL(二):MySQL数据库安装
- 2024-10-24 MySQL基础知识:启动管理和账号管理
- 2024-10-24 从零开始:Ubuntu Server中MySQL 8.0的安装与Django数据库配置详解
- 2024-10-24 等保测评2.0:MySQL身份鉴别(mysql身份认证和授权)
- 2024-10-24 Mysql:下载、安装、部署、修改密码步骤
- 2024-10-24 MySQL开放远程权限,竟然连不上?可能是这几个地方没做好
- 2024-10-24 MySQL 9.1正式发布,有哪些值得关注的新特性?
- 2024-10-24 linux安装mysql客户端(只是客户端)
- 2024-10-24 Python3.7如何快速安装mysqlclient
- 2024-07-17 还再用Navicat?试试这款正版MySQL客户端,真香!
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)