Using pymssql to connect to sql server in Ubuntu - python

I am trying to use pymssql library to connect to the sql server in ubuntu but got the following error. But I tried the same script in the mac and it works.
I think there is a FreeTDS configuration problem in ubuntu but almost tried all of the recommanded methods in the web still no luck so far. Anyone can point me to the right direction?
import pymssql
conn = pymssql.connect(server='blah.database.windows.net',
user='blah',
password='!',
database='database')
cursor = conn.cursor()
cursor.execute('SELECT * FROM table')
data = cursor.fetchall()
print(data)
error:
Traceback (most recent call last):
File "test.py", line 13, in <module>
database='my data base')
File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
pymssql.OperationalError: (20002, 'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')

Related

Stuck with a MySQLdb SSL connection error in Python2.7

I have to maintain an old website built using Python2.7 that needs to continue working until we've finished creating a completely new version with more modern tools. Now this old website needs access to a remote MySQL database (connection is set up and working correctly), which so far has worked using the following:
import MySQLdb
db = MySQLdb.connect(host=Host,user=User,passwd=Pass,db=DBse)
Now the server has been upgraded from Ubuntu 18.04. to Ubuntu 20.04., and while I managed to install pip and MySQLdb for Python2.7, I now get the following error for the lines above:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2026, 'SSL connection error: unknown error number')
The SSL connection works fine in Python3 or directly from the command line.
Is there anything I can do to make this work?
Locate and add the line skip_ssl in /etc/mysql/my.cnf ( have tried this on MySQL 8.0.29 , Ubuntu 18)
This was the only thing that worked for me.
[mysqld]
skip_ssl

module 'mysql' has no attribute 'connector', trying to connect mysql to python (spyder 3.8)

I've been trying to connect Python and MySql but I keep getting errors. This is my code:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="kdvis",
password="root")
print (mydb)
And this is the error I'm getting:
Traceback (most recent call last):
File "C:\Users\kdvis\Desktop\flies\11\python\connection1.py", line 9, in <module>
mydb = mysql.connector.connect(
AttributeError: module 'mysql' has no attribute 'connector'
I've reconfigured MySql, gotten a caching_sha2_password error, a sha256 password requires SSL error, an access denied for user#localhost error, tried to change my root password using alter keyword on MySql, and read almost every solution but none worked.
Help would be appreciated very much.
Have you installed mysql as from pip.
If from other source remove it and install it from pip using coomand.
pip install mysql

Not able to connect python with hive on windows machine

I have installed anaconda navigator and working in the spyder workbook.
I am getting an error like TTransport exception.
I have installed all the packages like pyhive, sasl, thrift, thrift-sasl. while connecting python to hive, I was getting this error,"Could not start SASL: b'Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2'".
Is there any packages do I want to install?? As well I am working in windows, so kindly help me with that..
from pyhive import hive
import sasl
import thrift
import thrift_sasl
conn1 = hive.Connection(host="xxxxx", port=00000, username="yyyy")
cur2 = conn1.cursor()
Errors:
conn1 = hive.Connection(host="xxxx", port=00000, username="yyyy")
Traceback (most recent call last):
File "", line 1, in
conn1 = hive.Connection(host="xxxx", port=00000, username="yyyy")
File "C:\Users\sgpbtp02\AppData\Local\Continuum\anaconda3\lib\site-packages\pyhive\hive.py", line 192, in init
self._transport.open()
File "C:\Users\sgpbtp02\AppData\Local\Continuum\anaconda3\lib\site-packages\thrift_sasl__init__.py", line 79, in open
message=("Could not start SASL: %s" % self.sasl.getError()))
TTransportException: Could not start SASL: b'Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2'

Use mysqldb connect ipv6 server in Python

I use mysqldb connect my db server, server'ip is ::1. Code like this:
MySQLdb.connect(host='::1',user='admin',passwd='123456',db='test')
but, I get an error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-i686/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.linux-i686/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host '::1' (1)")
I want to know what should I do to allow mysqldb to support IPv6.
Instead of using ::1 for the host, try use localhost.
If that doesn't work, your system likely does not support IPv6. You can find more information about IPv6 support here.

Troubleshooting pymssql connection to Azure mssql on linux cloud at pythonanywhere

I have not been able to connect to an Azure hosted MSSQL server through pymssql on pythonanywhere.
I have zero previous linux experience, so that is making it harder than it should.
I am trying this sample script provided with the instructions:
host = "123.456.789.012"
username = "yourusername"
password = "yourpassword"
database = "yourdatabasename"
conn = pymssql.connect(host, username, password, database)
cursor = conn.cursor()
But I get this error:
Traceback (most recent call last):
File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
File "_mssql.pyx", line 637, in _mssql.MSSQLConnection.__init__ (_mssql.c:6581)
File "_mssql.pyx", line 1630, in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:17524)
_mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (my_server_name.database.windows.net:1433)\n')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (my_server_name.database.windows.net:1433)\n')
I figured it had to do with the TDS Version I am running, which I get to be 4.2 by running tsql -C on bash. Here is the output:
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
I have tried placing a .freetds.conf file both at home/myuser/ and at home/myuser/my_virtual_env/, but that did not change any behavior. Should I have restarted the bash console or the python console? I did not think so. Here are the file contents:
[global]
tds version = 7.1
[azure]
tds version = 7.4
The other alternative would be to use pyodbc, but that would also involve configuring freetds and then some more, so it looks like there is nothing to be had there.
I should point out that I have pymssql connecting just fine to azure on my win10 machine with the following connection string:
arguments = dict(server=db_server_add, user=db_usn+"#my_server",
password=db_pswd, database=db_name, charset="utf8")
AZURE_ENGINE = create_engine('mssql+pymssql:///', connect_args=arguments)
In Python Anywhere on a free account, it does not allow access to all web addresses. Only addresses from a specific list.
"If you have a paid Python Anywhere plan"
https://help.pythonanywhere.com/pages/MSSQLServer/

Categories