Access MySQL database in R on Mac OS X - mysql

I am having problems accessing my MySQL database from R on mac (Mac OS X 10.11.2). The following code:
library("RODBC")
mycom <- odbcConnect("wsn_mysql_5", uid="root", pwd="*****")
Gives this error:
1: In RODBC::odbcDriverConnect("DSN=wsn_mysql_5;UID=root;PWD=****") :
[RODBC] ERROR: state 00000, code 15509574, message
[iODBC][DriverManager]dlopen(/usr/local/lib/libmyodbc5a.so,
6): no suitable image found. Did find:
/usr/local/lib/libmyodbc5a.so: mach-o, but wrong architecture
2: In RODBC::odbcDriverConnect("DSN=wsn_mysql_5;UID=root;PWD=****") :
[RODBC] ERROR: state IM003, code 15509574, message
[iODBC][Driver Manager]Specified driver could not be loaded
What I have tried so far:
I installed iODBC driver manager 3.52.12.
I installed MySQL connector odbc-5.3.6-osx10.11-x86-64bit and created a DSN (server=localhost, user=root, password=*****,database=wsn_db).
I was getting this error when testing it in iODBC Administrator:
[iODBC][Driver manager]dlopen(/usr/local/lib/libmyodbc5w.so, 6): no suitable image found.
Did find: /usr/local/lib/libmyodbc5w.so: mach -o, but wrong architecture.
How to add ODBC to MAMP on OSX led me to think that is was a 32-bit vs 64-bit problem.
So I installed MySQL connector odbc-5.2.7-osx10.7-x86-32bit which is the most recent 32-bit version I found. When I test this with iODBC Administrator, it seems to work, but I get the error mentioned at the beginning when I try the code in R.
The FAQ of iODBC says the following regarding this type of error:
[iODBC] [Driver Manager]Specified driver could not be loaded
There are a few reasons why this could occur, and thinking through the architecture helps. Your application has loaded libiodbc successfully, and it has found an odbc.ini file (or equivalent through the ODBCINI environment variable), and it has found a DSN within that odbc.ini that matches the name requested in your connection.
However, the driver manager has had problems loading the library specified in the `Driver=' line of that DSN definition. Either it doesn't exist, or its permissions are insufficient to allow your application to load it (it must be readable and executable, and the directories leading down to it must be executable), or maybe the file is not a dynamic library - it could be a static library (a *.a file except on AIX) or is otherwise corrupted. These are all things to check, or you may be best off reinstalling the driver if all the permissions check out.
So I checked the permission, but they were correct.
I also reinstalled the driver, but it didn't change anything.
After reading this, I checked odbc.ini and obdcinst.ini.
I changed odbc.ini to:
[ODBC Data Sources]
wsn_mysql_7 = wsn_mysql_7
[ODBC]
TraceLibrary=
[wsn_mysql_7]
Driver = /usr/local/lib/libmyodbc5w.so
DATABASE = wsn_db
DESCRIPTION = DSN for wsn_db in R
SERVER = localhost
UID = root
PASSWORD = *****
and odbcinst.ini to
[ODBC Drivers]
MySQL ODBC 5.2 ANSI Driver=Installed
MySQL ODBC 5.2 Unicode Driver=Installed
[ODBC Connection Pooling]
PerfMon=0
Retry Wait=
[MySQL ODBC 5.2 ANSI Driver]
Driver = /usr/local/lib/libmyodbc5a.so
Description = mySQL driver
Threading = 0
[MySQL ODBC 5.2 Unicode Driver]
Driver = /usr/local/lib/libmyodbc5w.so
Description = mySQL driver
Threading = 0
But it didn't seem to change anything.
I also tried an alternative RODBC code based on this question.
mycom <- odbcDriverConnect("Provider=MSDASQL;Driver={MySQL ODBC 5.2 UNICODE Driver};
Server=localhost;database=wsn_db;Uid=root;Pwd=*****;Option=3")
I am not sure what to do next? Even a completely different way to access the mySQL data in R would help.

I finally manage to have a working configuration by using unixODBC and using a 64-bit ANSI connector. If you are trying to connect to a MySQL database with RODBC I recommend:
To follow this tutorial to install unixODBC and RODBC
Install the latest MySQL ODBC connector. I installed mysql-connector-odbc-5.3.6-osx10.11-x86-64bit. I had to install from tar since the installer didn't work. The instructions are here.
I used ODBC Administrator to configure the DSN, but I guess the instruction from [Hiltmon's tutorial] might work as well.
I first used the Unicode driver, but got the following error:
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=wsn_db_dsn;UID=root;PWD=****") :
[RODBC] ERROR: state H, code 0, message [unixODBC][
2: In RODBC::odbcDriverConnect("DSN=wsn_db_dsn;UID=root;PWD=****") :
ODBC connection failed
I corrected this by selecting the ANSI driver as suggested in RODBC error - ODBC connection failed - can't connect to MySQL with my mac (mavericks).
I hope this can save time to someone.

Related

odbcConnect with Access database difference between R and RStudio

I can use the following code successfully in the R GUI to connect to a Microsoft Access database:
library(RODBC)
#ODBC connection
scallopchannel<-odbcDriverConnect("scallop_2016")
"scallop_2016" is a DSN for the Access database I connect to.
In RStudio I have to specify to path to the Access database like this:
ODBC connection
scallopchannel<-odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=S://Adv/Scallop Central/2015 RSA/2015 RSA Database/2015 RSA Database_9-4-2015.accdb")
Otherwise I get an error message:
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=db2017") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In RODBC::odbcDriverConnect("DSN=db2017") : ODBC connection failed
When I type to troubleshoot the problem
odbcDataSources ()
RStudio indicates that the Access datasource is there.
This is a pain since I sometimes connect to multiple databases within a session and I prefer to be able to code for just the DSN.
I am using RStudio 1.0.153 and R GUI 3.3.2.
I am also using 32 bit R and 32 bit RStudio.

pyodbc + MySQL + Windows: Data source name not found and no default driver specified

I am trying to connect to MySQL 5.6 on a Windows Server 2008 R2 localhost with pyodbc. I used the full installation for the MySQL instance on the localhost, including the ODBC connector. I have it connecting to a remote SQL Server instance beautifully, but for the life of me I can't get it to connect to the local MySQL instance. I am using this guide from connectionstrings.com as reference.
Here's some code:
import pyodbc
def create_mssql_conn():
return pyodbc.connect(r'Driver={SQL Server};Server=MSSQLSRV;Database=ecomm;Trusted_Connection=yes;')
def create_mysql_conn():
return pyodbc.connect(r'Provider=MSDASQL;Driver={MySQL ODBC 5.6 UNICODE Driver};Server=127.0.0.1;Database=ecomm;User=root;Password=myP#$$w0rd;Option=3;')
# conn = create_mssql_conn() # This one works
conn = create_mysql_conn() # This one breaks
cursor = conn.cursor()
cursor.execute('SELECT * FROM inventory')
while 1:
row = cursor.fetchone()
if not row:
break
print row
Here is the error:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I tried everything from adding Provider=MSDASQL; to changing ANSI to UNICODE in the connection string. Any suggestions?
I was having a similar issue. I am using windows 8, and mysql.
The way I solved the problem was by going into my
control panel>Systems and Security>Administrative Tools.>ODBC Data Sources
Either the 32 bit or 64 bit version depending on your computer.
Then you click on the System DNS file. If you do not see any MySQL driver you have to click ADD. It brings up a list, from that list select the MySQL driver.
For me it was MySQL ODBC 5.3 ANSI(they have a unicode driver also). Click finish. Once you do that then you have to change your connection line in your code to the corresponding Driver that you just filled out.
Ex:
def create_mysql_conn():
return pyodbc.connect(r'Driver={MySQL ODBC 5.3 ANSI Driver};Server=MSSQLSRV;Database=ecomm;Trusted_Connection=yes;')
This should work, or at least it solved my connection issue because I was getting all sorts of different errors with everything I tried. This was what solved the issue for me.
In Win64 there are two ODBC environments: 32 and 64 bit and you can work with Python 32 bit or Python 64 bit. Check what version of Python you use and then use proper version of odbcad32.exe (32 bit version is located in the SysWoW64 directory) to create SystemDSN. With SystemDSN you can check if you can connect to the database. Then try to connect to DB from Python code.
You can list ODBC datasources available to pyodbc via my code recipe: https://code.activestate.com/recipes/578815-printing-list-of-odbc-data-sources-with-pyodbc-mod/
Head to Administrative Tools and the ODBC Data Sources. You should click on the DNS file. Most likely you'd see Microsoft Access and excel as the only data sources available, so you need to add your SQL Server. Mine was SQL Server 2008 R2, and so far, it has worked.
For the error mentioned
"pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')"
I installed the 'Microsoft Access Database Engine 2010 Redistributable 64-bit' as I have 64-bit system and then added the DSN in the directory of MS Access database and it's working now.
Here is the link of the video for how to create DSN and connect to python.
https://www.youtube.com/watch?v=zw9P2wSnoIo

Error Trying to Connect R to MySQL using RODBC

I am attempting to connect R to a MySQL database via RODBC (in Win 7 environment), but cannot get past the following error:
[RODBC] ERROR: state IM014, code 0, message [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
After entering the following command:
odbcConnect(dsn,uid="My_UID",pwd="My_PWD")
I have successfully created a driver for the database by following the odbc wizard, and have installed the RODBC package into RStudio (ver 0.98.490). The MySQL is running on 32bit, I have the 32bit driver installed and working, and my RStudio is also 32bit. I know that the driver is working since both the test connection in config. works, as do queries run on excel using the driver.
What can I do to get the connection to work in R?
(Previous responses to this question seem to have been removed.)
Thanks in advance!
I faced a similar RODBC connectivity issue using DSN, where R was crashing abruptly without any logs/error trace. I tried with DSN-less ODBC connection API, it works fine.
odbcDriverConnect("SERVERNAME=;DRIVER={};DATABASE=;UID=;PWD=;")
Steps:
1. Install ODBC driver
2. Define VNode (host, listen address, protocol)
3. Use the odbcDriverConnect API
Docs:
http://cran.r-project.org/web/packages/RODBC/RODBC.pdf
http://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf
Hope this helps.

MySQL ODBC Issue: Data source name not found and no default driver specified

I'm currently trying to run a classic ASP application which I've been given source code for. I want to set up on my 64bit Windows 7 dev machine and am having trouble with an ODBC based data connection to a MySQL instance.
I'm seeing the error:
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC Driver Manager] Data source name not found
and no default driver specified
/includes/<File Name>.asp, line 100
What I've tried:
The connection is DSN-less.
The application is running under IIS app pool with local system permissions. w3wp.exe can be seen running under NT AUTHORITY/SYSTEM in process monitor.
The application is running under IIS app pool with 32 bit applications allowed to run.
Have tried with Connector/ODBC 5.1.10 64 bit version only installed from http://dev.mysql.com (At this point no driver was listed under C:\Windows\SysWOW64\odbcad32.exe but was under C:\Windows\system32\odbcad32.exe)
Have tried with Connector/ODBC 5.1.10 32 bit version only installed from http://dev.mysql.com (At this point no driver was listed under C:\Windows\system32\odbcad32.exe but was under C:\Windows\SysWOW64\odbcad32.exe)
Have tried with Connector/ODBC 5.1.10 32 bit and 64 bit versions installed.
Verified driver name is not misspelled. Along with other checks from here http://support.microsoft.com/kb/306345.
Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=DBName;User=root;Password=Password;Option=3
Additional Information:
I'm monitoring in process monitor, and the two results are:
PATH NOT FOUND (Looking for .asp/web.config which seems odd.
BUFFER OVERFLOW
Both entries show:
User: NT AUTHORITY\SYSTEM
Process:
C:\Windows\SysWOW64\inetsrv\w3wp.exe
C:\Windows\SysWOW64\odbcint.dll
I'm stumped can any one make a suggestion on how I get this running in the context I have described?
Solved the problem now. Recording here in case this of use to others.
The issue was much simpler than it first appeared. The problem was that the application used a mixture of named and unnamed datasources (DSN / DSN-Less).
It was not apparent to me that any named connections were used until I set up the application for debugging in Visual studio. Here is a rough guide to debugging the application in visual studio (Except I used HTTP based website, rather than file system):
http://www.codeproject.com/Articles/28792/Debugging-Classic-ASP-VBScript-in-Visual-Studio-20
Following creating the required DSN, there were some further exceptions being thrown regarding default values in database columns. This was due to a MySQL setting that can be changed in the my.ini file.
http://bugs.mysql.com/bug.php?id=14306
C:\Program Files (x86)\MySQL\MySQL Server 5.5\my.ini
# Set the SQL mode to strict
# sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
sql-mode=""
I found this to work from Windows to MySQL as a DSN-less connection. The trick was to ELIMINATE the port spec at the end of the server address.
"DRIVER={MySQL ODBC 5.3 UNICODE Driver}; Server=**;Database=**;User=**;Password=**; OPTION=3"
Note: Server string is the internet address of the server, BUT NO PORT
SPECIFIED - ie, NO ":3306" on the end

odbc under osx 10.7

I'm trying to install an ODBC driver under MacOSX 10.7 (Lion) since the last few hours... But I can't get it to work. I need one for the RODBC package in R to work with a DB.
Under 10.6 everything worked fine :-\
The old drivers are installed, but when I test it with /usr/bin/iodbctest I get the following message:
iODBC Demonstration program
This program shows an interactive SQL processor
Driver Manager: 03.52.0607.1008
Enter ODBC connect string (? shows list): ?
DSN | Driver
------------------------------------------------------------------------------
lugv | MySQL ODBC 5.1 Driver
Enter ODBC connect string (? shows list): lugv
1: SQLDriverConnect = [iODBC][Driver Manager]dlopen(/usr/local/lib/libmyodbc5.so, 6): no suitable image found. Did find:
/usr/local/lib/libmyodbc5.so: mach-o, but wrong architecture (0) SQLSTATE=00000
2: SQLDriverConnect = [iODBC][Driver Manager]Specified driver could not be loaded (0) SQLSTATE=IM003
When I download the newest MYSQL ODBC Connector, it doesn't compile anymore and also doesn't have a bin or lib folder as mentioned in the manual...
Does someone have an idea?
Thanks!