退出mysql命令行客户端的方法是

发布时间: 2023-11-21 12:53 阅读: 文章来源:1MUMB4408PS

mysql命令行工具

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.sockEnter password:Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 8Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners. 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=3306mysql --login-path=3306 test2.客户端字符集选项--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=gbkEnter password:Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 10Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners. 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:id1235.错误处理选项--force, -f  即使发生SQL错误,也要继续。--verbose, -v  显示详细模式--show-warnings  显示警告

示例

[root@localhost ~]# more test.sqltruncate table test;insert into test values(1);insert into test values(‘a‘);insert into test values(3);[root@localhost ~]# mysql -uroot -p test < test.sqlEnter 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.sqlEnter 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.sqlEnter 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 |+----+
•••展开全文