步骤六:连接DRDS实例逻辑库 步骤二:连接实例 方式一:MySQL命令行连接DRDS逻辑库 数据访问层支持 MySQL 命令行方式访问,登录弹性云主机,打开命令行工具,输入以下命令。 mysql h {DRDSHOST} P {DRDSPORT} u {DRDSUSER} [D DRDSDATABASE] p [ commentstrue ] 参数说明 参数 说明 DRDSHOST DRDS实例的IP地址。 DRDSPORT DRDS实例的监听端口。 DRDSUSER DRDS实例设置的用户。 DRDSDATABASE 计划要连接的逻辑库。 commentstrue 使用HINT语法情况下,命令行方式访问要加上参数 commentstrue 。 下面为Windows服务器命令行窗口中使用表中举例参数后,MySQL命令连接服务器的回显情况。 [root@9afe645c6f7a ~] mysql h127.0.0.1 P8888 uxxxx p Enter password: Welcome to the MariaDB monitor. Commands end with ; or g. Your MySQL connection id is 208 Server version: 5.7.21UDALDBPROXY2.8.2P320230606T06:10:23Z DBProxy Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MySQL [(none)]> 方式二:JDBC驱动连接DRDS逻辑库 本节以SQL查询语句连接DRDS实例为例。 public static void main(String[] args) throws ClassNotFoundException { //加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); String userName "xxxx"; String password "xxxx"; String url "xx.xx.xx.xx:xxxx"; String schema "xxxx"; try(Connection conn DriverManager.getConnection(String.format("jdbc:mysql://%s/%s?user%s&password%s&useUnicodetrue&characterEncodingutf8",url, schema, userName, password));) { //查询条件带上分片键或切片索引键,否则语句将广播执行 Statement stmt conn.createStatement(); ResultSet rs stmt.executeQuery("select now() as Systemtime"); rs.next(); String space ""; for (int i 1; i < rs.getMetaData().getColumnCount(); i++) { System.out.print(space + rs.getMetaData().getColumnName(i) + " " + rs.getString(i)); space ", "; } } catch (Exception e) { e.printStackTrace(); } }