Set up a native connection from Matlab to MySQL with SSL

12 views (last 30 days)
I'm trying to use the native mysql interface to connect to a MySql 8.0.37 database on Azure. This database was previously deployed to a test server which didn't require SSL and I was able to connect to it without issue. I'm also able to connect to the new Azure MySql database using DBeaver and the MySql Workbench from the same client computer, but I get the following error when I try to connect with Matlab's mysql connector:
Error using mysql (line 49)
Connections using insecure transport are prohibited while --require_secure_transport=ON..
My connection code is as follows (where aosDbSettings is a struct containing the connection details):
conn = mysql( ...
aosDbSettings.UserName, aosDbSettings.UserPassword, ...
'Server', aosDbSettings.Server, ...
'DatabaseName', aosDbSettings.DatabaseName, ...
'PortNumber', aosDbSettings.PortNumber ...
);
Having reviewed the documentation for the Native MySql Interface (specifically the mysql function), I don't see any options related to SSL. Are there any undocumented options available to make this work? I find it surprising that there's no mention of security options in the documentation. Am I missing something?

Accepted Answer

Umar
Umar on 13 Aug 2024

Hi @Brian,

While the native mysql function in MATLAB does not directly support SSL options, using the JDBC driver provides a viable alternative for connecting to a MySQL database that requires secure transport. By specifying the appropriate connection properties, you can successfully establish a secure connection to your Azure MySQL database. Here’s how you can do it:

% Load the JDBC driver
jdbcDriver = 'com.mysql.cj.jdbc.Driver';
% Update with the path to your MySQL JDBC driver
javaaddpath('path_to_mysql_connector_java.jar');
% Define connection properties
    dbURL = sprintf('jdbc:mysql://%s:%d/%s?useSSL=true&requireSSL=true', ...
    aosDbSettings.Server, aosDbSettings.PortNumber, 
    aosDbSettings.DatabaseName);
    connProps = java.util.Properties();
    connProps.setProperty('user', aosDbSettings.UserName);
    connProps.setProperty('password', aosDbSettings.UserPassword);
    % Establish the connection
    conn = database('', '', '', jdbcDriver, dbURL, connProps);

In this code, replace path_to_mysql_connector_java.jar with the actual path to your MySQL JDBC driver. The connection URL includes parameters useSSL=true and requireSSL=true, which enforce SSL usage. For more guidance, I will suggest the following link to help you out as well.

https://www.mathworks.com/support/search.html/answers/32618-connecting-to-mysql-using-jdbc.html?fq%5B%5D=asset_type_name:answer&fq%5B%5D=category:database/index&page=1

Also, make sure that the SSL certificates are correctly set up. You may need to provide the paths to the CA certificate, client certificate, and client key in the connection string if required. Hope this should help resolve your problem. If you encounter further issues, consider exploring the MATLAB Database Toolbox

https://www.mathworks.com/help/database/ug/choose-between-odbc-and-jdbc-drivers.html

or consulting with MathWorks support for additional assistance.

  3 Comments
Umar
Umar on 15 Aug 2024
Hi @Brian,
Thank you for your update regarding your exploration of the ODBC driver with SSL. I'm glad to hear that you found a helpful article that clarified its functionality for your application. It's great that you're considering delving into the JDBC driver as well, especially given its support for the `runstoredprocedure` function with parameters. I believe that exploring both options will provide you with a comprehensive understanding of how to best implement your requirements. Should you have any further questions or need additional assistance as you continue this exploration, please feel free to reach out. I'm here to help.

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!