MySQL Shell连接
MySQL Shell可以配置为在启动应用程序时使用命令选项连接到MySQL服务器,或者在MySQL Shell内部使用\connect命令连接到MySQL服务器。您 想要连接的MySQL服务器的地址可以使用单独的参数来指定,例如用户,主机名和端口,或者使用统一资源标识符(URI),格式为 user@host:port/schema,例如mike@myserver:33060/testDB。以下部分描述这些连接方法。
无论选择哪种方法连接,了解MySQL Shell如何处理密码都是很重要的。默认情况下,假定连接需要密码。在登录提示时要求输入密码。要指定 无密码帐户,请使用——password选项而不指定密码,或者在URI中的用户后面使用:而不指定密码。
如果您没有为连接指定参数,则使用以下默认值:
.user默认为当前系统用户名
.host默认为localhost
.port在使用X Session时默认为X Plugin端口33060,在使用Classic Session时默认为端口3306
使用X协议的MySQL Shell连接总是使用TCP,不支持使用Unix套接字。MySQL当满足以下条件时,Shell连接使用MySQL协议默认使用Unix套接字:
.–port未指定
.–host未指定或者为localhost
.–socket为到套接字提供了路径
.–classic被指定
如果指定了–host,但它不是localhost,则建立TCP连接。在这种情况下,如果没有指定–port,则使用默认值3306。如果满足套接字连接的条 件,但没有指定–socket,则使用默认套接字
使用URI字符串进行连接
通过使用–uri命令选项以字符串格式传递连接数据,可以配置MySQL Shell连接到的MySQL服务器。
使用以下格式:
[dbuser[:[dbpassword]]@]host[:port][/schema]
这些选项的说明:
.dbuser:指定认证过程中使用的MySQL用户帐号
.dbpassword:指定要用于身份验证过程的用户密码,不建议将密码存储在URI中。
.host:指定会话对象所连接的主机。如果未指定,则默认使用localhost。
.port:指定目标MySQL服务器正在监听连接的端口。如果没有指定,33060默认用于启用X协议的会话,而3306是传统MySQL协议会话的默认值。
.schema:指定会话建立时要设置为默认的数据库
如果没有使用URI指定密码(建议这样做),则会提示输入密码。下面的示例展示了如何使用这些命令选项:
.连接到端口33065的 Node 会话
[root@localhost ~]# mysqlsh --uri root@10.13.13.25:33065 --node WARNING: The --node option was deprecated, please use --mysqlx instead. (Option has been processed as --mysqlx). Please provide the password for 'root@localhost:33065': ****** MySQL Shell 8.0.41 Copyright (c) 2016, 2025, 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 '\?' for help; '\quit' to exit. Creating an X protocol session to 'root@localhost:33065' Fetching schema names for auto-completion... Press ^C to stop. Your MySQL connection id is 31 Server version: 5.7.26-log Source distribution No default schema selected; type \useto set one. MySQL 10.13.13.25:33065 JS >
.连接一个经典会话。
[root@localhost ~]# mysqlsh --uri root@10.13.13.25 --classic WARNING: The --classic option was deprecated, please use --mysql instead. (Option has been processed as --mysql). Please provide the password for 'root@10.13.13.25': ****** Save password for 'root@10.13.13.25'? [Y]es/[N]o/Ne[v]er (default No): N MySQL Shell 8.0.41 Copyright (c) 2016, 2025, 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 '\?' for help; '\quit' to exit. Creating a Classic session to 'root@10.13.13.25' Fetching schema names for auto-completion... Press ^C to stop. Your MySQL connection id is 32 Server version: 5.7.26-log Source distribution No default schema selected; type \useto set one. MySQL 10.13.13.25:3306 JS >
虽然不建议使用无密码帐户,但也可以在用户名后使用“:”指定不需要密码的用户,例如:
[root@localhost ~]#mysqlsh --uri user:@localhost
使用单个参数进行连接
除了使用URI指定连接参数之外,还可以为每个值使用单独的参数定义连接数据
使用以下参数:
.–dbuser (-u) value
.–dbpassword value
.–host (-h) value
.–port (-P) value
.–schema (-D) value
.–password (-p)
.–socket (-S)
前5个参数匹配URI格式中使用的令牌,参数–password表示用户连接时不需要密码,为保持一致性,部分参数支持以下别名:
.–user相当于–dbuser
.–password 相当于 –dbpassword
.–database 相当于 –schema
当以多种方式指定参数时,适用以下规则:
.如果多次指定参数,则使用最后一次出现的值
.如果同时指定了各个连接参数和–uri,那么–uri的值将被视为基础,而各个参数的值会覆盖基础URI中的特定组件。
尝试在端口33065上与指定用户建立XSession。
[root@localhost ~]# mysqlsh -u root -h 10.13.13.25 -P 33065 Please provide the password for 'root@10.13.13.25:33065': ****** MySQL Shell 8.0.41 Copyright (c) 2016, 2025, 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 '\?' for help; '\quit' to exit. Creating a session to 'root@10.13.13.25:33065' Fetching schema names for auto-completion... Press ^C to stop. Your MySQL connection id is 34 Server version: 5.7.26-log Source distribution No default schema selected; type \useto set one. MySQL 10.13.13.25:33065 JS >
尝试与指定用户建立经典会话
[root@localhost ~]# mysqlsh -u root -h 10.13.13.25 --classic WARNING: The --classic option was deprecated, please use --mysql instead. (Option has been processed as --mysql). Please provide the password for 'root@10.13.13.25': ****** Save password for 'root@10.13.13.25'? [Y]es/[N]o/Ne[v]er (default No): N MySQL Shell 8.0.41 Copyright (c) 2016, 2025, 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 '\?' for help; '\quit' to exit. Creating a Classic session to 'root@10.13.13.25' Fetching schema names for auto-completion... Press ^C to stop. Your MySQL connection id is 35 Server version: 5.7.26-log Source distribution No default schema selected; type \useto set one. MySQL 10.13.13.25:3306 JS >
尝试与指定用户建立节点会话
[root@localhost ~]# mysqlsh --node -u root -h 10.13.13.25 WARNING: The --node option was deprecated, please use --mysqlx instead. (Option has been processed as --mysqlx). Please provide the password for 'root@10.13.13.25': ****** MySQL Shell 8.0.41 Copyright (c) 2016, 2025, 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 '\?' for help; '\quit' to exit. Creating an X protocol session to 'root@10.13.13.25:33060' Fetching schema names for auto-completion... Press ^C to stop. Your MySQL connection id is 36 Server version: 5.7.26-log Source distribution No default schema selected; type \useto set one. MySQL 10.13.13.25:33060 JS >