Creating a Data Source with MariaDB Connector/ODBC
Contents
Creating a Data Source with MariaDB Connector/ODBC on Windows
To create a data source on Windows, you would use the ODBC Data Source Administrator.
Creating a Data Source with MariaDB Connector/ODBC on Linux
To create a data source on Linux, there are two steps:
- First, configure UnixODBC to recognize MariaDB Connector/ODBC as a
Driver
. - Second, configure UnixODBC with a Data Source Name (DSN) for your MariaDB Server.
Configuring MariaDB Connector/ODBC as a UnixODBC Driver on Linux
You might also want to configure UnixODBC to recognize MariaDB Connector/ODBC as a Driver
. To configure the Driver
, you can use the odbcinst
tool, which can add a configuration entry for MariaDB Connector/ODBC to the system's global /etc/odbcinst.ini
file.
For example, create a template file similar to the following, with a name like MariaDB_odbc_driver_template.ini
:
[MariaDB ODBC 3.0 Driver] Description = MariaDB Connector/ODBC v.3.0 Driver = /usr/lib64/libmaodbc.so
And then install it to the system's global /etc/odbcinst.ini
file with the following command:
sudo odbcinst -i -d -f MariaDB_odbc_driver_template.ini
At this point, you should be able to connect to MariaDB by using the Driver
with the SQLDriverConnect
function. To connect with SQLDriverConnect
, you would need to specify Driver={MariaDB ODBC 3.0 Driver}
in your connection string along with your other connection parameters.
See Parameters for connection string options.
Configuring a DSN with UnixODBC on Linux
You might also want to configure UnixODBC with a Data Source Name (DSN) for your MariaDB Server. A DSN
allows you to centrally configure all of your server's connection parameters, so that you can easily configure how to connect to your server in your environment. To configure the DSN
, you can use the odbcinst
tool, which can add a configuration entry for the given data source to the system's global /etc/odbc.ini
file or your user's local
file.~
/.odbc.ini
For example, create a template file similar to the following, with a name like MariaDB_odbc_data_source_template.ini
:
[MariaDB-server] Description=MariaDB server Driver=MariaDB ODBC 3.0 Driver SERVER=<your server> USER=<your user> PASSWORD=<your password> DATABASE=<your database> PORT=<your port>
And then you can install it to the system's global /etc/odbc.ini
file with the following command:
sudo odbcinst -i -s -l -f MariaDB_odbc_data_source_template.ini
Or you can install it to your user's local
file with the following command:~
/.odbc.ini
odbcinst -i -s -h -f MariaDB_odbc_data_source_template.ini
At this point, you should be able to connect to MariaDB by using the DSN
with either SQLConnect
or the SQLDriverConnect
functions. To connect with SQLConnect
, you would have to provide MariaDB-server
as the ServerName
parameter. To connect with SQLDriverConnect
, you would have to provide DSN={MariaDB-server}
in the connection string along with your other connection parameters.
See Parameters for connection string options.
UnixODBC also provides a GUI to add DSNs
. However, MariaDB Connector/ODBC doesn't yet support this GUI interface for adding DSNs
.
Verifying a DSN Configuration with UnixODBC on Linux
You can verify that a DSN
is properly configured with UnixODBC on Linux by using the isql
utility.
For example, if the DSN
is called MariaDB-server
, then we can verify that it works properly by executing the following:
$ isql MariaDB-server +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> SELECT @@global.hostname; +-------------------------------------------+ | @@global.hostname | +-------------------------------------------+ | ip-172-30-0-249.us-west-2.compute.internal| +-------------------------------------------+ SQLRowCount returns 1 1 rows fetched SQL> quit
Changing UnixODBC's Configuration File Paths
You can also change the paths that unixODBC uses for its configuration files by changing some environment variables. For example:
ODBCSYSINI
- Overloads path to unixODBC configuration files. By default equals to/etc
.ODBCINSTINI
- Overloads the name of the drivers configuration file. It is relative toODBCSYSINI
and by default set toodbcinst.ini
.ODBCINI
- Overloads the path to user configuration file. By default it is set to
.~
/.odbc.ini