connect to external sql server using pyodbc and FreeTDS drivers on rhel 7 linux - pyodbc

I have an AWS EC2 instance, installed FreeTDS, I run osql and it tells me Configuration looks ok but then does an isql. That fails. I tried the tsql -LH but nothing returned. tsql -S returns:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20009 (severity 9):
Unable to connect: Adaptive Server is unavailable or does not exist
There was a problem connecting to the server
Any suggestions?

Related

mysql window function working on production server, not on Raspberry Pi

I have just setup my Raspberry Pi (RPI) with OSMC (debian 9 stretch), nginx, PHP 7, Mysql and phpmyadmin
All seems to be working except the mysql windows functions. For example the following query produces a syntax error:
SELECT sum(propertyId) over() FROM `properties`;
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '() FROM `properties` LIMIT 0, 25' at line 1
However, the exact same query and database tables works fine on my production server.
Here are the setups (from the respective phpmyadmins) for the two servers:
RASPBERRY PI (not working)
Database server
Server: Localhost via UNIX socket
Server type: MariaDB
Server version: 10.1.26-MariaDB-0+deb9u1 - Debian 9.1
Protocol version: 10
User: phpmyadmin#localhost
Server charset: UTF-8 Unicode (utf8)
Web server
nginx/1.10.3
Database client version: libmysql - mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $
PHP extension: mysqliDocumentation mbstringDocumentation
PHP version: 7.0.30-0+deb9u1
PRODUCTION SERVER (WORKING FINE)
Database server
Server: Localhost via UNIX socket
Server type: MariaDB
Server version: 10.2.12-MariaDB-log - MariaDB Server
Protocol version: 10
User: montegoventures#localhost
Server charset: UTF-8 Unicode (utf8)
Web server
cpsrvd 11.70.0.53
Database client version: libmysql - 5.1.73
PHP extension: mysqliDocumentation curlDocumentation mbstringDocumentation
PHP version: 5.6.30
I have tried countless times to upgrade the mariadb on the RPI to version 10.2 but no matter what I do, it still installs version 10.1.26
So, any suggestions on how I can get my simple query to work on my RPI server? Yes, I know it is a trivial query, but if I can't get that simple one to work on the RPI, how can I get my complex queries to work?
thanks

"[unixODBC][Driver Manager]Data source name not found, and no default driver specified" despite /etc/odbc.ini having the DSN

Getting the "[unixODBC][Driver Manager]Data source name not found, and no default driver specified" despite /etc/odbc.ini having the DSN referenced in the connection string. Why would this be happening and what can be done?
Trying to set up pyodbc for connecting to MSSQLServer using the docs (centos 7). Yet, when trying to actually connect to the database,
import pyodbc
# setup db connection
server = 'myserver'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn_str = 'DSN=MyMSSQLServer;DATABASE='+database+'UID='+username+'PWD='+password+'MultipleActiveResultSets=True;'
cnxn = pyodbc.connect(cnxn_str)
getting error "[unixODBC][Driver Manager]Data source name not found, and no default driver specified", even though when running:
[mapr#mnode01 ~]$ cat /etc/odbc.ini
[MyMSSQLServer] Driver=ODBC Driver 13 for SQL Server
Description=My MS SQL Server
Trace=No
Server=<now using my sql server ip>
I can see that the DSN that I referenced in the python code is recorded in the /etc/odbc.ini file. Does anyone know what could be going on here? Thanks.
Note: I initially ran the pyodbc setup exactly as specified in the docs, but later re-did this step to use the ip address of the sql server I wanted to connect to:
vi /home/user/odbcadd.txt
[MyMSSQLServer] Driver = ODBC Driver 13 for SQL Server
Description = My MS SQL Server
Trace = No
Server = <my sql server ip>
and just re-ran the write to odbc.ini:
sudo odbcinst -i -s -f /home/user/odbcadd.txt -l
The solution commented by user Gord Thompson solved my problem. The ~/odbcadd.txt file that I was using to fill the /etc/odbc.ini file needed to have the 'Driver=' line on a separate line from the DSN to look like:
[mapr#mnode01 ~]$ cat /etc/odbc.ini
[MyMSSQLServer]
Driver=ODBC Driver 13 for SQL Server
Description=My MS SQL Server
Trace=No
Server=172.18.4.38

RStudio on Windows PostgreSQL SSL Connection with RPostgreSQL

I am having trouble creating an SSL connection using RPostgreSQL to an AWS hosted PostgreSQL database.
Here is what I've tried so far:
Created the PostgreSQL database on AWS.
Set the database parameter "rds.force_ssl" to 1.
Downloaded the AWS public key from https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
Test the connection from a windows command prompt with psql (it works).
Executed the following in R:
library(RPostgreSQL)
cert <- paste0("C:/Users/johnr/Downloads/", "rds-combined-ca-bundle.pem")
dbname <- paste0("dbname=", "flargnog", " ", "sslrootcert=", cert, " ", "sslmode=verify-full")
host <- "xxxxxx.xxxxx.us-region-2.rds.amazonaws.com"
con <- dbConnect(dbDriver("PostgreSQL"), user="username", host=host, port=5432, dbname=dbname, password="abcd1234!")
I receive an error message after executing the last statement:
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect username#xxxxxx.xxxxx.us-region-2.rds.amazonaws.com on dbname "flargnog"
If I change the rds.force_ssl setting to 0 (and remove the ssl stuff from dbname) the connection works just fine.
I have looked at other posts on Stackoverflow related to this issue. This and this seem to indicate an SSL connection is not possible due to issues with RPostgreSQL. However, this post indicates that you can.
Any guidance would be appreciated!
You can try to ssh to the rds instance using e.g. putty and port-forward your local port 5432 to the remote port 5432. Once the ssh connection is open in R just connect to localhost:5432...
Here is how to port-forward using putty:
http://www.akadia.com/services/ssh_putty.html
Here is how this works via command-line:
https://gist.github.com/magnetikonline/3d239b82265398568f31
P.S.: Make sure your instance is in a security-group that accepts ssh connections - port 22

RSelenium Connecting to a running server

I am trying to use Rselenium to automate some of my more tedious reporting tasks
I have downloaded the Java virtual machine as per the instructions
I have gotten it running by using the code below
# Run the Command at the command line
cd selenium
java -jar selenium-server-standalone-3.0.1.jar
In R i then add the following code
require(RSelenium)
remDr <- remoteDriver(remoteServerAddr = "localhost"
, port = 4445L
, browserName = "firefox"
)
remDr <- remoteDriver(port = 4445L)
remDr$open()
When i run the last line i get the error
[1] "Connecting to remote server"
Error in checkError(res) :
Couldnt connect to host on http://localhost:4445/wd/hub.
Please ensure a Selenium server is running.
I can see in the command line window that the server is running as i am getting the message Selenium Server is up and Running
Can anyone see what I'm doing incorrectly?
Update
I have tried switching the port to 4444 as based on the advice below but i get the error
From the Cmd Prompt
Selenium message:The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
From R
Error: Summary: UnknownError
Detail: An unknown server-side error occurred while processing the command.
class: java.lang.IllegalStateException
Further Details: run errorDetails method
From Firefox version 48 the gecko driver is also required to drive a Firefox browser with Selenium Server. The geckodriver can be downloaded at https://github.com/mozilla/geckodriver/releases. If you wish to run the Selenium server manually you should then either
Add the geckodriver path to PATH
Or set the webdriver.gecko.driver system property on the JVM
The second method would be done as:
java -Dwebdriver.gecko.driver="path-to-geckodriver" -jar selenium-server-standalone-3.0.1.jar
If you are running windows and have downloaded the Selenium standalone to C:\Selenium and the geckodriver to the same location then this would look like:
C:\Users\john>cd C:\Selenium
C:\Selenium>java -Dwebdriver.gecko.driver="C:\Selenium\geckodriver.exe" -jar selenium-server-standalone-3.0.1.jar
NOTE: on a 32bit windows machine you will need the 32bit geckodriver and on a 64bit machine the corresponding 64bit geckodriver.
Alternatively the recommended way to run a Selenium server with RSelenium is to run a Docker container which includes the Selenium Server, geckodriver and appropriate Firefox browser:
docker run -d -p 5901:5900 -p 127.0.0.1:4444:4444 --link http-server selenium/standalone-firefox-debug:3.0.1-barium
see the vignette at http://rpubs.com/johndharrison/RSelenium-Docker

pyodbc.connect to FreeTDS connection requires explicit PORT=1433;

I have a python script running on python 2.7 in CentOS 2.6 that connects to a Sql erver database:
pyodbc.connect("DRIVER=FreeTDS;SERVER=someServer;DATABASE=someDb;UID=myUser;PWD=superSecret;CHARSET=UTF8;TDS_Version=7.2")
That call will fail with the following: pyodbc.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
The freetds trace will say login.c:436:invalid port number
Adding PORT=1433; will cause the connection to succeed even though that is the default port number, and I added the following to my freetds.conf:
[global]
# TDS protocol version
tds version = 7.0
port = 1433
How do I make FreeTDS try port 1433 as the default port so I don't have to set it in the query string?
Instead of this, as you noted:
pyodbc.connect("DRIVER=FreeTDS;SERVER=someServer;DATABASE=someDb;UID=myUser;PWD=superSecret;CHARSET=UTF8;TDS_Version=7.2")
...you could put these values in your connection string, like this:
pyodbc.connect("DRIVER={FreeTDS};SERVER=someServer;PORT=1433;DATABASE=someDb;UID=myUser;PWD=superSecret;TDS_Version=7.2;CHARSET=UTF8")
For your freetds.conf configuration:
[global]
client charset = UTF-8
tds version = 7.2
[someServer]
host = someServer
port = 1433
tds version = 7.2
Both ways have always worked for me.
On a side note, I've started using this driver, made by Microsoft specifically for RedHat and CentOS (although I don't know if it will work on a version as old as yours):
https://msdn.microsoft.com/en-us/library/hh568451%28v=sql.110%29.aspx
Best of luck!

Resources