1. Setup ODBC on your Mac (including Apple Silicon)
Install brew
unixodbc
package from brew
works for Apple Silicon
See https://formulae.brew.sh/formula/unixodbc#default
Install unixodbc
with brew:
brew install unixodbc
2. Setup a connector for mysql
The MariaDB connector
is a drop in replacement for the mysql-connector and works for Apple Silicon.
Install MariaDB connector
with brew:
brew install mariadb-connector-odbc
3. Configure ODBC Drivers and User DSN
You can inspect the ODBC config files locations:
odbcinst -j
You can edit the files manually but it's easier to use ODBC Manager
. Download and install ODBC Manager
from:
Using ODBC Manager
Drivers
- Under 'Drivers', choose 'Add...' to add a new DB driver
- Enter at a minimum:
Driver Name: MariaDB ODBC 3.1 Driver Driver File (Apple Silicon): /opt/homebrew/Cellar/mariadb-connector-odbc/3.1.15/lib/mariadb/libmaodbc.dylib or (Intel-based): /usr/local/Cellar/mariadb-connector-odbc/3.1.15/lib/mariadb/libmaodbc.dylib
User DSN
- Under 'User DSN', choose 'Add...' to add a new data source
- Select the 'MariaDB ODBC 3.1 Driver'
- Enter a Data Source Name (DSN) (recommend to use same as remote DB name)
- Configure the DSN, with the following keyword/values:
SERVER: localhost PORT: 3307 CHARSET: utf8 DATABASE: <database> USER: <user>
4. SSH tunnel to remote mysql server
ssh -fn -L 3307:localhost:3306 `<remote server>`
if you are using 3307 for something else, use any of the higher port numbers.
5. Test remote ODBC connection
You can use isql
to test your ODBC connection.
isql -v <dsn_name> <user> <passwd>
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> show tables;
+-----------------------------------------------------------------+
| Tables_in_<database> |
+-----------------------------------------------------------------+
| table_one |
| table_two |
| .... |
+-----------------------------------------------------------------+
6. Opening STATA
macos
will not set the env variable LD_LIBRARY_PATH
even if you export to path in your profile (.zshrc
) (See also about SIP https://support.apple.com/en-us/HT204899).
To get past this load STATA from the command line and set the LD_LIBRARY_PATH at the same time:
# On Apple Silicon Macs
export LD_LIBRARY_PATH=/opt/homebrew/lib/:/opt/homebrew/mariadb-connector-odbc/ && \
/Applications/Stata/StataIC.app/Contents/MacOS/StataIC &
# On Intel-based Macs
export LD_LIBRARY_PATH=/usr/local/Cellar/:/usr/local/Cellar/mariadb-connector-odbc/ && \
/Applications/Stata/StataIC.app/Contents/MacOS/StataIC &
# Note, possible alternative Stata install locations:
# - /Applications/Stata/StataSE.app/Contents/MacOS/StataSE
# - /Applications/Stata/StataMP.app/Contents/MacOS/StataMP
7. ODBC in STATA
Specify ODBC driver manager
# For this session only
set odbcmgr unixodbc
# Permanantly
set odbcmgr unixodbc, permanently
List your ODBC connections
. odbc list
Data Source Name Driver
-------------------------------------------------------------------------------
ODBC
<dsn_name> /opt/homebrew/Cellar/mariadb-connector-odbc (Apple Silicon)
<dsn_name> /usr/local/Cellar/mariadb-connector-odbc/ (Intel-based)
-------------------------------------------------------------------------------
Authenticate ODBC connection:
- Go to File | Import | ODBC data sources
- Click on Authentication at the top right of the ODBC data sources dialog
- enter user and password
- select the data source
- close the dialog
Run your query using an ODBC connection:
odbc load, exec("select * from mytable") clear dsn("<dsn_name>")
I tried opening Staata using step 6:
ODBS is for postgresql and Stata location is MP instead of ic. Everything else is exactly the same. I get the following errors:
For reference I am using Stata13. Am I going wrong somewhere?