testConnection
Test MySQL native interface database connection
Syntax
Description
tests the MySQL® native interface database connection specified by the status
= testConnection(opts
,username
,password
)SQLConnectionOptions
object opts
, a user name, and a
password.
Examples
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)
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.
Create and configure a MySQL native interface data source to a MySQL database. Test the database connection to the MySQL native interface data source and retrieve the error message.
Create a MySQL native interface data source for a MySQL 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 using an incorrect user name and password. The
testConnection
function returns the logical 0
,
which indicates the database connection fails. Retrieve and display the error message
for the failed connection.
username = "wronguser"; password = "wrongpassword"; [status,message] = testConnection(opts,username,password)
status = logical
0
message =
'1045 : Access denied for user 'wronguser'@'SERVERNAME' (using password: YES).'
Input Arguments
Database connection options, specified as an SQLConnectionOptions
object.
Username required to access the database, specified as a character
vector or string scalar. If no username is required, specify an
empty value ""
. Provide this username if you
do not store it as part of the data source when using the
Database Explorer app to make a connection.
Password required to access the database, specified as a character vector or string scalar. If
no password is required, specify an empty value
""
. Provide this password if you
do not store it as part of the data source when using the
Database Explorer app to make a connection.
Data Types: char
| string
Output Arguments
Connection status, returned as a logical true
if the connection
test passes or false
if the connection test fails.
Error message, returned as a character vector. If the connection test passes, then the error message is an empty character vector. Otherwise, the error message contains text describing the failed database connection.
Version History
Introduced in R2020b
See Also
Objects
Functions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)