Main Content

SQLConnectionOptions

Define MySQL native interface database connection options

Since R2020b

Description

Create connection options for a MySQL® native interface connection.

First, create an SQLConnectionOptions object, set the connection options, test the connection, and save the data source. Then, create a MySQL native interface connection using the saved data source. The connection options include the options required to make a database connection. You can also define additional connection options for a specific database driver.

Creation

Create an SQLConnectionOptions object using the databaseConnectionOptions function.

Properties

expand all

Data source name, specified as a string scalar. You can use the data source name in the mysql function to create a database connection for the MySQL native interface.

Example: "MySQLDataSource"

Data Types: string

This property is read-only.

Database vendor, specified as a string scalar. Specify this property using the vendor input argument in the databaseConnectionOptions function. After the SQLConnectionOptions object exists, you cannot set this property to another value.

Example: "MySQL"

Data Types: string

Database name, specified as a string scalar. Set this property using the setoptions function.

Example: "toystore_doc"

Data Types: string

Database server name or address, specified as a string scalar. Set this property using the setoptions function.

Data Types: string

Server port number where the server is listening, specified as a numeric scalar. The default value is 3306 for a MySQL database. Set this property using the setoptions function.

Data Types: double

Object Functions

rmoptionsRemove MySQL native interface connection options
saveAsDataSourceSave MySQL native interface data source
setoptionsSet MySQL native interface connection options
resetReset MySQL native interface connection options to defaults
testConnectionTest MySQL native interface database connection

Examples

collapse all

Create, configure, test, and save a MySQL® native interface data source for a MySQL database.

Create a MySQL native interface data source for a MySQL native interface database connection.

vendor = "MySQL";
opts = databaseConnectionOptions("native",vendor)
opts = 
  SQLConnectionOptions with properties:

              DataSourceName: ""
                      Vendor: "MySQL"

                DatabaseName: ""
                      Server: "localhost"
                  PortNumber: 3306

opts is an SQLConnectionOptions object with these properties:

  • DataSourceName — Name of the data source

  • Vendor — Database vendor name

  • DatabaseName — Name of the database

  • Server — Name of the database server

  • PortNumber — Port number

Configure the data source by setting the database connection options for the data source MySQLDataSource, database name toystore_doc, database server dbtb01, and port number 3306.

opts = setoptions(opts, ...
    'DataSourceName',"MySQLDataSource", ...
    'DatabaseName',"toystore_doc",'Server',"dbtb01", ...
    'PortNumber',3306)
opts = 
  SQLConnectionOptions with properties:

              DataSourceName: "MySQLDataSource"
                      Vendor: "MySQL"

                DatabaseName: "toystore_doc"
                      Server: "dbtb01"
                  PortNumber: 3306

The setoptions function sets the DataSourceName, DatabaseName, Server, and PortNumber properties in the SQLConnectionOptions object.

Test the database connection with a user name and password. The testConnection function returns the logical 1, which indicates the database connection is successful.

username = "root";
password = "matlab";
status = testConnection(opts,username,password)
status = logical
   1

Save the configured data source.

saveAsDataSource(opts)

You can connect to the new data source using the mysql function or the Database Explorer app.

Version History

Introduced in R2020b