I need to create a linked server in MS SQL Server for 2 AWS RDS servers,
from Python 2.7. For this I use the pymssql library. The SQL commands are executed with a SQL Client without problems, the linked servers are created, they can be consulted that exist and can be used.
The problem occurs when you execute a SQL creation in Python (from an instance AWS Linux, in the same AZ), then you get the following error message.
Simple SQL queries from python (such as a Select) work without problems using pymssql.
The same happens when a stored procedure is created in the database and it called from python, if the instructions to create the linked servers are included then the same error is received. If you create the procedure in the sql in python the result is similar.
Does anyone know how to do it? Thank you very much for the help.
Linkedserver Commands:
EXEC master.dbo.sp_addlinkedserver #server = N'linkserver1', #srvproduct=N'', #provider=N'SQLNCLI', #datasrc=N'aa.bb.cc.dd';
EXEC master.dbo.sp_dropserver #server = N'linkserver1';
EXEC sys.sp_linkedservers;
Python Code:
**import pymssql
def main():
conn = pymssql.connect(server, user, clave, base)
cursor = conn.cursor()
cursor.execute("EXEC master.dbo.sp_addlinkedserver #server = N'RDSPrivate01', #srvproduct=N'', #provider=N'SQLNCLI', #datasrc=N'win-01.nnnnnn.us-west-1.rds.amazonaws.com'")
if __name__== "__main__":
main()**
Message Error:
cursor.execute("EXEC master.dbo.sp_addlinkedserver #server =
N'RDSPrivate01', #srvproduct=N'', #provider=N'SQLNCLI',
#datasrc=N'win-01.nnnnnnnnnnn.us-west-1.rds.amazonaws.com'") File
"src/pymssql.pyx", line 468, in pymssql.Cursor.execute
pymssql.OperationalError: (15002, "The procedure
'sys.sp_addlinkedserver' cannot be executed within a transaction.
DB-Lib error message 20018, severity 16:\nGeneral SQL Server error:
Check messages from the SQL Server\n")
Links used:
https://aws.amazon.com/blogs/database/implement-linked-servers-with-amazon-rds-for-microsoft-sql-server/
http://www.pymssql.org/en/stable/pymssql_examples.html#important-note-about-cursors
Related
I need to connect the Python script with google sql.
I have created the database in google sql and now want to connect it with python script.
I am trying to use this so far:
conn = pymysql.connect(host='ip.ip.ipi.ip',user='user', passwd = "passwd", db = 'mysql',
charset = 'utf8')
cur = conn.cursor()
cur.execute("USE Example_Database")
but getting this error:
OperationalError: (2003, "Can't connect to MySQL server on '35.246.235.61' (timed out)")
Also tried to use this but not working:
from google.cloud.sql.connector import connector
connector.connect(
"instance:instancename",
"mysql-connector",
host='ip.ip.ipi.ip',
user="user",
password="passwd",
db="Database"
)
It seems there is connectivity issue to Google SQL from where you run the code.
Where are you running your python code?
Can you check if it can connec to you Google SQL endpoint on port 3306 (or custom port you are using)?
telnet <Google_SQL_IP> 3306
Depending on where your python code is hosted you need to allow necessary access for your app server to cloud sql.
You can connect to a Cloud SQL instance using the following methods:
By using the proxy (Second Generation instances only)
By configuring access for one or more public IP addresses
By using the JDBC Socket Factory (for the Java programming language, Second Generation instances only)
By using the Cloud SQL Proxy library (for the Go programming language, Second Generation instances only)
Please review the following link which describes options for you to connect:
https://cloud.google.com/sql/docs/mysql/connect-external-app
I'm trying to connect to a SQL Server named instance from python 3.4 on a remote server, and get an error.
File "C:\Scripts\Backups Integrity Report\Backup Integrity Reports.py", line 269, in
conn = pymssql.connect(host=r'hwcvcs01\HDPS', user='My-office\romano', password='PASS', database='CommServ')
File "pymssql.pyx", line 636, in pymssql.connect (pymssql.c:10178)
pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')
Other SQLs are connected without a problem. Also I manage to connect to the SQL using the Management Studio, from the same remote server.
Tried different ports, tried to connect to the host itself rather than the instance, and also tried pypyodbc.
What might be the problem?
For a named instance that looks like this:
myhost\myinstance,1435
You can connect via pymssql with the following:
pymssql.connect(host='myhost', server='myinstance', port='1435', user='user', password='pw')
According to the pymssql documentation on the pymssql Connection class, for a named instance containing database theDatabase, looking like this:
myhost\myinstance
You could connect as follows:
pymssql.connect(host=r'myhost\myinstance', database='theDatabase', user='user', password='pw')
The r-string is a so-called raw string that does not treat the '' as an escape.
I'm using pyodbc library from here and I'm connecting this way:
conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};Server=(localdb)\MSSQLLocalDB;Integrated Security=true; database = online_banking; autocommit = True')
I use MSSQLLocalDBbecause it's the default instance name for SQL Server 2014. And this last version of Python 2.7.
However I cant run any simple query, every if them raise the error, saying that there is no such object or in that particular case database:
cursor.execute('use online_banking;')
The full error:
pyodbc.Error: ('08004', "[08004] [Microsoft][SQL Server Native Client 11.0][SQL Server]Database 'online_banking' does not exist. Make sure that the name is entered correctly. (911) (SQLExecDirectW)")
So what is wrong here?
There is only 1 instance installed and such databases(.mdf)
As you can see only 1 engine:
Selecting that engine will allow me to see online_banking db
upd1 Database've been created this way:
CREATE DATABASE [online_banking]
ON PRIMARY
( NAME = N'online_banking', FILENAME = N'C:\...\online_banking.mdf' ,
SIZE = 512000KB , MAXSIZE = UNLIMITED, FILEGROWTH = 30%)
LOG ON
( NAME = N'online_banking_log', FILENAME = N'C:\...\online_banking_log.ldf' ,
SIZE = 1024KB , MAXSIZE = 20GB , FILEGROWTH = 10%)
GO
upd2 I've used built-in tool sqlcmd.
So this sqlcmd -S (LocalDB)\MSSQLLocalDB -i C:\Users\1.sql -E have shown, that
MSSQLLocalDB doesn't have my database.
However sqlcmd -S localhost -i C:\Users\1.sql -E performed successfully.
I'm totally confused, I' ve installed only one server, moreover SQL Management studio sees only one local server with my online_banking DB. This is look really weird to me.
Trying to use this connection string in Python
conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};Server=localhost;Integrated Security=true; database = online_banking; autocommit = True')
causes the error below:
pyodbc.Error: ('28000', '[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]\x... "". (18456) (SQLDriverConnect); [01S00] [Microsoft][SQL Server Native Client 11.0]\xcd\xe5\xe....xe8\xff (0); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]\xce...ff "". (18456); [01S00] [Microsoft][SQL Server Native Client 11.0]\xcd\xe.... (0)'
upd3: Specified mdf should be attached, got it:
Tried several ways, always errors (with database specified or not in connection string):
conn = pyodbc.connect(
r'Driver={SQL Server Native Client 11.0};Server=(localdb)\MSSQLLocalDB; database =online_banking; AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL12.SQLSERVERINSAF\MSSQL\DATA\online_banking.mdf;Trusted_Connection=Yes; Integrated Security=true; database = online_banking;')
error: A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I found out, that may be related with parent server which already have attached this db, but failed to solve this.
upd4
I tried simple code from here to see if "online_banking" shows up in the list of databases for that instance. But faced another error:
pyodbc.Error: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]\ - unreadable error
In addition that database according to SSMS seems have already attached by online_banking DB
As it turns out, the database in question was already attached to the default instance of SQL Server on the local machine, so all that was needed to connect was
import pyodbc
conn_str = (
r"Driver={SQL Server Native Client 11.0};"
r"Server=(local);"
r"Database=online_banking;"
r"Trusted_Connection=yes;"
)
conn = pyodbc.connect(conn_str)
There were two main points of confusion:
Q: What is the name of a SQL Server "default instance"?
A: It doesn't have one.
When referring to a SQL Server instance by name, a default instance simply goes by the name of the machine, while a named instance is identified by MachineName\InstanceName. So, on a server named PANORAMA
If we install a "default instance" of SQL Server we refer to it as PANORAMA.
If we install a "named instance" called "SQLEXPRESS" we refer to it as PANORAMA\SQLEXPRESS.
If we are referring to a SQL server instance on the local machine we can use (local) instead of PANORAMA.
Q: Do (local) and (localdb) mean the same thing?
A: NO.
(local) and (local)\InstanceName refer to "real" server-based instances of SQL Server. These are the instances that have been around since SQL Server was first released. They run as a service and are able to accept network connections and do all of the the things we expect a database server to do.
(localdb) and (localdb)\InstanceName references – with (localdb) usually capitalized as (LocalDB) for clarity – are used to connect to "SQL Server LocalDB" instances. These are temporary local SQL Server instances primarily intended for developers. For details see the following MSDN blog post:
SQL Express v LocalDB v SQL Compact Edition
It could possibly be a security issue. You are using integrated security so it will use the security credentials of the windows login that the client program is running. If that user or a group that the user belongs to does not have at least public access to the database, it will appear as if the database does not exist. Either ensure that the user or a group that the user is a member of is set up with a login and that it has at least public access to your database, or use SQL server authentication and send a username and password in your connection string.
I'm attempting to connect to a local instance of SQL Server running on my machine. I am able to connect to a local instance with this code from our server, but it fails on my local machine.
I've enabled named pipes and all the ips in the SQL Server configuration.
The code I'm using is as follows:
from pymssql import connect
server = r'.\SQLEXPRESS2014' # I've also tried MORGANT-PC\SQLEXPRESS and SQLEXPRESS2014
username = 'MyUserName'
password = 'MyPassword'
master_database_name = 'SuperSecretDatabase'
port = 5000
server_args = {'host': server, 'user': username, 'password': password,
'database': master_database_name, 'port': port} # I've tried having the first key be both host and server, because pymssql's docs are unclear on the difference.
master_database = connect(**server_args)
If I use the instance name, I get this error:
pymssql.InterfaceError: Connection to the database failed for an unknown reason.
I set the port to 5000 so that I could try connecting to it with
server = 127.0.0.1
port = 5000
which fails with the slightly different error message:
pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist\nNet-Lib error during Unknown error (10035)\n')
I've read a bunch of answers here on SO, and most of them seem to indicate it's an issue with FreeTDS, but I'm on Windows 8.1, so I don't have FreeTDS.
I've tried connecting with sqlcmd with the host\instance name and that works fine. It also works in SSMS.
I've tried passing .\SQLEXPRESS2014 to both the host and server parameter in pymssql.connect() and they both fail with the same aforementioned error.
I briefly tried using adodbapi, but I'm getting exactly the same error messages.
The solution ended up being a combination of things.
I needed to disable all IPs other than 127.0.0.1.
I needed to create C:\freetds.conf with the following text:
[global]
port = 1433
tds version = 7.0
I needed to change the account my SQL instance logs in with to LocalSystem.
Yes, 1433 s the default.
This works fine for me:
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=server_name; Database=db_name;Uid=; Pwd=; trusted_connection=yes")
initdata <- sqlQuery(dbconnection,paste("select * from MyTable;"))
odbcClose(channel)
Try connecting on the default SQL server port, which is 1433, not 5000.
And check that you can connect to the correct instance using SQL mgmt studio.
I had a similar issue, with the following error:
_mssql.MSSQLDatabaseException: (18456, b"Login failed for user
'script_svc'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server
error: Check messages from the SQL Server\nDB-Lib error message 20002,
severity 9:\nAdaptive Server connection failed\n")
The user I had established was a local user on the machine.
The solution for me was putting ".\" in front of the username and it then recognized it as a local user and allowed the query to work.
Your results may vary.. but I thought I would mention it.
Have you tried using pyodbc instead?
import pyodbc
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=SERVERNAME;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()
cursor.execute("select user_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
print row.user_id, row.user_name
Don't forget to add the ODBC driver to your Windows. Go to: 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 ODBC Driver 13 for SQL Server. 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.
Source: pyodbc + MySQL + Windows: Data source name not found and no default driver specified
I am working with MySQL-python package. I am able to execute MySQL dependent scripts from command line, however doing the same through browser (Apache CGI) yields the following error:
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/var/www/html/temp.py in ()
9
10 # Establich a connection
11 db = MySQLdb.connection(host="127.0.0.1", user="root", passwd="", db="inserv")
12
13 # Run a MySQL query from Python and get the result set
db undefined, MySQLdb = <module 'MySQLdb' from
'/usr/lib64/python2.6/site-packages
/MySQLdb/__init__.pyc'>, MySQLdb.connection = <type '_mysql.connection'>,
host undefined, user undefined, passwd undefined
<class '_mysql_exceptions.OperationalError'>:
(2003, "Can't connect to MySQL server on '127.0.0.1' (13)")
args = (2003, "Can't connect to MySQL server on '127.0.0.1' (13)")
message = ''
I have been stuck in this situation for past few days. MySQL commands issued through php based sites execute appropriately and I can, also, login to MySQL from command line. The problem seem to be with Python CGI only.
I have also tried the same with oursql package and there seems to be a similar problem. How can I address this situation?
Edit
As per #Real's answer I have edited my code to use MySQLdb.connect() but the problem still persist and traceback ends with:
2003, "Can't connect to MySQL server on '127.0.0.1' (13)"
You should be using connect, not connection. Mysqldb.connect() returns a connection object but you appear to be calling mysqldb.connection(). See The docs for an example of how to do it.