Skip to content

Instantly share code, notes, and snippets.

@dveeden
Created September 17, 2021 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dveeden/176a8023f301f25783e8a6f5e29c4aa5 to your computer and use it in GitHub Desktop.
Save dveeden/176a8023f301f25783e8a6f5e29c4aa5 to your computer and use it in GitHub Desktop.
MariaDB Connector/J testing with TiDB v5.2.0 and Jython
#!/bin/jython
import sys
import java.util.Properties
import java.sql.SQLException
import org.mariadb.jdbc.Driver
# Test user setup:
#
# mysql-sql> create user 'test_native'@'%' identified with 'mysql_native_password' by 'test';
# Query OK, 0 rows affected (0.0778 sec)
# mysql-sql> create user 'test_sha2'@'%' identified with 'caching_sha2_password' by 'test';
# Query OK, 0 rows affected (0.0346 sec)
# mysql-sql> grant select on test.* to 'test_native'@'%';
# Query OK, 0 rows affected (0.0457 sec)
# mysql-sql> grant select on test.* to 'test_sha2'@'%';
# Query OK, 0 rows affected (0.0358 sec)
# export CLASSPATH=/path/to/mariadb-java-client-X.Y.Z.jar
for user in ["test_native", "test_sha2"]:
for ssl in ["false", "true"]:
drv = org.mariadb.jdbc.Driver()
p = java.util.Properties()
driverName = "{} v{}.{}".format(
drv.getClass().canonicalName, drv.majorVersion, drv.minorVersion
)
jdbc_url = "jdbc:mariadb://127.0.0.1:4000/test?user={}&password=test&useSSL={}&trustServerCertificate=true".format(
user, ssl
)
print("======================================================")
try:
conn = drv.connect(jdbc_url, p)
stmt = conn.prepareStatement(
'SELECT CONNECTION_ID() AS "connid", VERSION() AS "version"'
)
res = stmt.executeQuery()
while res.next():
print(
"connection {} to TiDB {}".format(
res.getString("connid"), res.getString("version")
)
)
print("%s %s\nSuccess: %s" % (driverName, jdbc_url, conn))
except java.sql.SQLException as e:
print("%s %s\nFailed: %s" % (driverName, jdbc_url, e))
print("======================================================")
@dveeden
Copy link
Author

dveeden commented Sep 17, 2021

So looks like mysql_native_password works in all situations, but caching_sha2_password only works with MariaDB Connector/J 2.7.4 on a TLS connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment