SQLConnectionOptions - Define MySQL native interface database connection options - MATLAB (original) (raw)
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.
Properties
DataSourceName
— Data source name
string scalar
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
Vendor
— Database vendor
string scalar
This property is read-only.
Database vendor, specified as a string scalar. Specify this property using thevendor
input argument in the databaseConnectionOptions function. After theSQLConnectionOptions
object exists, you cannot set this property to another value.
Example: "MySQL"
Data Types: string
DatabaseName
— Database name
string scalar
Database name, specified as a string scalar. Set this property using the setoptions function.
Example: "toystore_doc"
Data Types: string
Server
— Database server name or address
"localhost"
(default) | string scalar
Database server name or address, specified as a string scalar. Set this property using the setoptions function.
Data Types: string
PortNumber
— Server port number where the server is listening
numeric scalar
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
rmoptions | Remove MySQL native interface connection options |
---|---|
saveAsDataSource | Save MySQL native interface data source |
setoptions | Set MySQL native interface connection options |
reset | Reset MySQL native interface connection options to defaults |
testConnection | Test MySQL native interface database connection |
Examples
Create MySQL Native Interface Data Source
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 sourceVendor
— Database vendor nameDatabaseName
— Name of the databaseServer
— Name of the database serverPortNumber
— 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)
Save the configured data source.
You can connect to the new data source using the mysql function or the Database Explorer app.
Version History
Introduced in R2020b