My problem: My team is writing a program in python and, locally, I have a db (let's call it test.db) on SQL SERVER. So I have the following code:
connection = pypyodbc.connect( "DRIVER={SQL Server};SERVER=localhost;trusted_connection='yes';DATABASE=test")
My issue now, is my teammates need to use my Database. All I've found online is how to allow access to my DB through Management Studio ( so allow TCP/IP and allow port 1434)
I would like to have something like this
connection = pypyodbc.connect( "DRIVER={SQL Server};SERVER="My ip address" ;trusted_connection='yes';DATABASE=test")
Is this possible? ( where my ip address = some kind of number)
I was thinking of installing a server that listens for incoming connections but I've never really done this so I was wondering if there was another way to go about this.
Related
I'm trying to connect to Amazon RDS Postgresql database with this python code
import psycopg2
engine = psycopg2.connect(
database="vietop2database",
user="postgres",
password="07041999",
host="vietop2.cf4afg8yq42c.us-east-1.rds.amazonaws.com",
port='5433'
)
cursor = engine.cursor()
print('opened database successfully')
I encountered an error:
could not connect to server: Connection timed out
Is the server running on host "vietop2.cf4afg8yq42c.us-east-1.rds.amazonaws.com" (54.161.159.194) and accepting
TCP/IP connections on port 5433?
I consulted this trouble shooting on amazon and I already make sure the DB instance's public accessibility is set to Yes to allow external connections. I also changed port to 5433 and set VPC security to default. Yet, I fail to connect to the database. What might be the reasons? Please help me. Thank you very much
Below are the database connectivity and configuration information
I found the answer. I need to add new inbound rule allowing all traffic of IPv4 type
When I execute the code
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="user",
passwd="password"
)
I get:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)
I've tried using an example with "pymysql' and the error stays.
I looked around the internet and a lot of people say it could be a firewall inbound problem. Yet there is no fire wall stopping 3306. The security group on Amazon RDS allows all connections. I connected the RDS instance to my local mySql Workbench (so I can make tables and stuff from there). Interestingly enough when I run the code and the error persists there is additional client connections that pop up. Anyone else deal with this? Thank you very much I'm trying to learn this part of AWS well.
I figured it out. My host name should NOT be "localhost" it should be my endpoint on Amazon RDS. It seems clear now considering when I connected my RDS database to mySql workbench I put in "databasename.xxxxxxx.us-east-1.rds.amazonaws.com" for my host name. Now I can connect and read/write to the database.
I have a python application designed to pull data from a remote database server using pyodbc, then organize and display the data in a spreadsheet. I've had it working fine for several months now, with multiple coworkers in my department using it through a shared network folder.
My connection:
pyodbc.connect('DRIVER={SQL Server};
SERVER=<myServer_name>;
DATABASE=<myDB_name>;
UID=personsUser;
PWD=personsPassword')
A different employee within our same network recently tried to use the program and got this error:
pyodbc.Error: ('08001','[08001][Microsoft][ODBC SQL Server Driver]
[DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect)')
It looked like a simple permissions issue to me, so to confirm I replaced the userID and password with my own hardcoded in, but it gave the same error. Furthermore the same employee can log in and execute queries through SQL Server Management Studio without issue.
Since everyone else in our department can still use the application fine, I know it must be a client-side issue, but I just can't pinpoint the problem. Any input would be greatly appreciated, Thanks!
Updates:
Per flipperPA's answer below, I updated my connection string to include the port:
con = pyodbc.connect('''DRIVER={SQL Server};
SERVER=<myServer_name>;
PORT=1433;
DATABASE=<myDB_name>;
UID=personsUser;
PWD=personsPassword;''')
Unfortunately we still got the same error.
He is running 32-bit Windows 7 on an HP machine, the same setup as the rest of the group so it shouldn't to be an os-level issue.
He does operate SSMS on the same machine, but I ran through the telnet check just be sure - no issue there.
I've taught myself the pyodbc API and basic SQL, but I'm still relatively new to the underlying concepts of databases and remote connections. Could you explain the TDS driver a little more?
When including SERVER, I've found you often need to include the PORT as well; this is the most likely problem:
pyodbc.connect('DRIVER={SQL Server};
SERVER=<myServer_name>;
PORT=1433;
DATABASE=<myDB_name>;
UID=personsUser;
PWD=personsPassword')
I connect mostly from Linux, however. Could it be the other person is connecting from Mac OS/X or Linux? If so, they'll need to use the FreeTDS driver (MS provides one as well, but it is flaky, at best). If you continue to have problems, from the coworkers machine, make sure you can connect from the machine you're having issues with (unless its the same machine they can connect SSMS from):
telnet <myServer_name> 1433
If it connects, you're good, if it hangs on connecting, you're most likely looking at a firewall issue. Good luck!
After talking with a knowledgeable friend I was finally able to figure out my issue!
For some reason, the user's system was configured to connect using named pipes, but the server I was connecting to only had TCP/IP protocol enabled. The solution was to force the application to use TCP/IP by adding "tcp:" to the front of the server name.
The fixed connection string:
pyodbc.connect('''DRIVER={SQL Server};
SERVER=tcp:<myServer_name>;
PORT=1433;
DATABASE=<myDB_name>;
UID=personsUser;
PWD=personsPassword
''')
If for any of you still doesn't work you can try to refer the Localdb (if that's the case) by its pipe address.
If the DB name is LocalDBA, in cmd type
SqlLocalDB LocalDBA v
Get the instance pipe name and then put it on the server name:
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=np:\\.\pipe\LOCALDB#ECE5B7EE\tsql\query;'
r'PORT=1433;'
r'DATABASE=VT-DE;'
r'trusted_connection=yes;'
)
I need to use python to connect database in phpMyAdmin.
import MySQLdb
db = MySQLdb.connect(host="10.0.0.140",port=80,user="root",passwd="password")
cursor=db.cursor()
cursor.execute("SHOW DATABASES")
results=cursor.fetchall()
for result in results:
print row
and I get this error
2013,"Lost connection to MySQL server at'waiting for initial communication packet',system error:0"
I can access the database in chrome, so I don`t think it is a problem of remote access.
=======================update=======================
The real reason is that I am in a limited net segment.(maybe)
There is something wrong with SQL connection out of my code.
If changed code like this:(port is not needed)
MySQLdb.connect(host="10.0.0.140",user="root",passwd="password")
People in public net segment can connect the phpMyAdmin but I can`t.
I can not change my net segment so I can`t confirm it is the real reason.
But it is the only difference between me and others.
Check the connection, are you sure you run your mysql at port 80?
As I wrote in the comments, I doubt it runs port 80, this is the url to your phpMyAdmin and not the core mysql database server. Normally the web and database run on different machines, different IP addresses etc. Is this a hosted environment? Or do you run this in your local machine.
If it is your local machine then changing to port 3306 should work.
If it is a hosted environment by a hosting partner then you need to check the JDBC url from them. As you wrote it is XXXX hosted partner. Then check your XXX and it will provide you a XXX jdbc url for your mysql, and be sure to grant access to the user so you can logon to the mysql from your workstation remotly. The mysql url/host could be something like this NNNN-aaaa-bbbb-cccc-dddd.xxx.domain.xx
Also did you try the PyMYSQL
import pymysql
db = pymysql.connect(host='10.0.0.140',user='root',passwd='password')
cursor = db.cursor()
query = ("SHOW DATABASES")
cursor.execute(query)
for r in cursor:
print r
This is not the way you should do it. phpMyAdmin serves HTML to the user and should be used in a Web browser. You should connect directly to the MySQL host which is usually listening on port :3306. Also, you should keep in mind that, in production, MySQL servers are ordinarily not listening on public interfaces.
I previously wrote my app using local development servers, and now that I have moved it onto an openshift small gear almost all works except for mysql connections.
In my code I have the line:
self.db = MySQLdb.connect(host, username, password, dbname)
When I review the openshift error log, the following error is reported:
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
I think that python is trying to connect using a UNIX socket as opposed to an INET one, but I'm not sure how to change this behavior. Any help is much appreciated.
Not specific to MySQLdb: if you use localhost as hostname, a MySQL client using the MySQL C libraries will try to connect using UNIX socket (or named pipe on Windows). There are 2 ways around this, but you'll need to grant extra permissions to make it work for both:
Use IP address 127.0.0.1
Use IP address 127.0.0.1 instead of the localhost hostname. This will make MySQL client connect using TCP/IP.
Use option files
The other way is to force the protocol using using option files. For example, in your ~/.my.cnf (or any file you want), add the following:
[python]
protocol=tcp
Now use the connection arguments to read the option file and group:
import MySQLdb
cnx = MySQLdb.connect(host='localhost', user='scott', passwd='tiger',
read_default_file='~/.my.cnf',
read_default_group='python')
The group name does not need to be python, but it is good not to use mysql or client as it might interfere with other MySQL tools (unless you want that of course).
For setting up permissions, you'll need to use the IP address of localhost, something like:
mysql> GRANT SELECT TO yourdb.* TO 'scott'#'127.0.0.1' IDENTIFIED BY ...;
(Site note: MySQL database drivers such as MySQL Connector/Python do not consider localhost to be special and connect through TCP/IP right away and you have to explicitly use the unix_socket.)
As I later discovered, while the database server runs on localhost, it runs on a very specific localhost bind address. In my case it was an address that I would never have though to try if I hadn't noticed how phpmyadmin was connecting.